Skip to content

Commit

Permalink
Merge pull request #7 from algesten/master
Browse files Browse the repository at this point in the history
fix for incorrect time zone. close #5
  • Loading branch information
ryanseys committed Nov 1, 2014
2 parents 69f8a17 + 8ad08d0 commit 5c659bd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/lune.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Converts a Number in Julian date form to a Date object.
* Source: http://blog.bahrenburgs.com/2011/01/javascript-julian-day-conversions.html
*/
Number.prototype.Julian2Date = function() {
Number.prototype.Julian2Date = function(inUTC) {
var X = parseFloat(this)+0.5;
var Z = Math.floor(X); //Get day without time
var F = X - Z; //Get time
Expand Down Expand Up @@ -59,7 +59,11 @@
UT *= 60;
var second = Math.round(UT);

return new Date(year, month, day, hour, minute, second);
if (inUTC) {
return new Date(Date.UTC(year, month, day, hour, minute, second));
} else {
return new Date(year, month, day, hour, minute, second);
}
};

/**
Expand Down Expand Up @@ -409,7 +413,7 @@
console.log("TRUEPHASE called with invalid phase selector ", tphase);
}

return pt.Julian2Date();
return pt.Julian2Date(true);
}

/**
Expand Down Expand Up @@ -447,4 +451,3 @@
'phase': phase
};
})();

17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,21 @@ describe('lune', function() {
assert.equal(JSON.stringify(feb17), JSON.stringify(lunedata));
});
});

describe('#phase_hunt', function() {
it('should handle timezones correctly', function() {
process.env.TZ = 'America/New_York';
var d = new Date(2014, 10, 1, 16, 56, 00);
var t = d.getTime();
if (1414837560000 != t) {
console.log('Unable to run timezone test because process.env.TZ quirk. '+
'See https://github.com/joyent/node/issues/3286');
return;
}
var hunt = lune.phase_hunt(d);
// 1415292777000 incorrect EST time
// 1415312577000 correct UTC time
assert(1415312577000, hunt.full_date.getTime());
});
});
});

0 comments on commit 5c659bd

Please sign in to comment.