diff --git a/json2dbf.js b/json2dbf.js index 758d2a5..a0ed2e4 100644 --- a/json2dbf.js +++ b/json2dbf.js @@ -2,7 +2,7 @@ var dbf = require('./'), fs = require('fs'); var buf = dbf.structure([ - {foo:'bar',noo:10}, + {foo:'bar',noo:new Date()}, {foo:'louie'} ]); 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..5b60ed0 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(date.getTime())) return " "; + return ("0000"+date.getFullYear()).slice(-4) + ("00"+(date.getMonth()+1)).slice(-2) + ("00"+date.getDate()).slice(-2); +}; diff --git a/src/structure.js b/src/structure.js index 4b02783..9bc286d 100644 --- a/src/structure.js +++ b/src/structure.js @@ -29,7 +29,7 @@ module.exports = function structure(data, meta) { view.setUint8(0, 0x03); // date of last update view.setUint8(1, now.getFullYear() - 1900); - view.setUint8(2, now.getMonth()); + view.setUint8(2, now.getMonth()+1); view.setUint8(3, now.getDate()); // number of records view.setUint32(4, data.length, true); @@ -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