diff --git a/.gitignore b/.gitignore index 9917066..ba84763 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -dbf.js +dbf.js \ No newline at end of file diff --git a/json2dbf.js b/json2dbf.js old mode 100644 new mode 100755 index 758d2a5..10eaa52 --- a/json2dbf.js +++ b/json2dbf.js @@ -3,13 +3,14 @@ var dbf = require('./'), var buf = dbf.structure([ {foo:'bar',noo:10}, - {foo:'louie'} + {foo:'louie'}, + {baz:new Date()} ]); fs.writeFileSync('foo.dbf', toBuffer(buf.buffer)); function toBuffer(ab) { - var buffer = new Buffer(ab.byteLength); + var buffer = Buffer.alloc(ab.byteLength); var view = new Uint8Array(ab); for (var i = 0; i < buffer.length; ++i) { buffer[i] = view[i]; diff --git a/package.json b/package.json index 059c808..d1d282f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dbf", - "version": "0.2.0", + "version": "0.3.0", "description": "generate dbf files", "main": "index.js", "dependencies": { diff --git a/src/fields.js b/src/fields.js index 5961b76..27c9285 100644 --- a/src/fields.js +++ b/src/fields.js @@ -4,6 +4,7 @@ var types = { string: 'C', number: 'N', boolean: 'L', + date: 'D', // type to use if all values of a field are null null: 'C' }; @@ -36,7 +37,7 @@ function inherit(a, b) { function obj(_) { var fields = {}, o = []; - for (var p in _) fields[p] = _[p] === null ? 'null' : typeof _[p]; + for (var p in _) fields[p] = _[p] === null ? 'null' : _[p] instanceof Date ? 'date' : typeof _[p]; for (var n in fields) { var t = types[fields[n]]; if(t){ diff --git a/src/lib.js b/src/lib.js index 0c7ff80..fbeeaa7 100644 --- a/src/lib.js +++ b/src/lib.js @@ -31,3 +31,8 @@ module.exports.writeField = function writeField(view, fieldLength, str, offset) } return offset; }; + +module.exports.writeDate = function(date) { + if(!date || isNaN(new Date(date).getTime())) return " "; + return ("0000"+new Date(date).getFullYear()).slice(-4) + ("00"+(new Date(date).getMonth()+1)).slice(-2) + ("00"+new Date(date).getDate()).slice(-2); +}; diff --git a/src/structure.js b/src/structure.js index d1a2e9f..90d0c65 100644 --- a/src/structure.js +++ b/src/structure.js @@ -75,7 +75,7 @@ module.exports = function structure(data, meta) { // date case 'D': offset = lib.writeField(view, 8, - lib.lpad(val.toString(), 8, ' '), offset); + lib.writeDate(val), offset); break; // number