Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date Support Working #46

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dbf.js
dbf.js
4 changes: 2 additions & 2 deletions json2dbf.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ var dbf = require('./'),
fs = require('fs');

var buf = dbf.structure([
{foo:'bar',noo:10},
{foo:'bar',noo:new Date()},
SeanR104 marked this conversation as resolved.
Show resolved Hide resolved
{foo:'louie'}
]);

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];
Expand Down
3 changes: 2 additions & 1 deletion src/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};
Expand Down Expand Up @@ -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){
Expand Down
5 changes: 5 additions & 0 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
4 changes: 2 additions & 2 deletions src/structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down