Skip to content

Commit

Permalink
Fixes #37: Incorrect timezone conversion on edge of months
Browse files Browse the repository at this point in the history
  • Loading branch information
blax committed Mar 19, 2013
1 parent edadb1a commit 9eef069
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
12 changes: 9 additions & 3 deletions index.js
Expand Up @@ -310,6 +310,14 @@ function setTimezone (timezone, relative) {
this.getTimezoneAbbr = function getTimezoneAbbr() {
return tz.tzname[zoneInfo.isDaylightSavings ? 1 : 0];
}

// Sets day, month and year at once
this.setAllDateFields = function setAllDateFields(y,mo,d) {
zoneInfo.year = y - 1900;
zoneInfo.month = mo;
zoneInfo.dayOfMonth = d;
return mktime.call(this);
}
// Sets the day of the month (from 1-31) in the current timezone
this.setDate = function setDate(v) {
zoneInfo.dayOfMonth = v;
Expand Down Expand Up @@ -422,9 +430,7 @@ function setTimezone (timezone, relative) {
this.toLocaleString = this.toString;

if (relative) {
this.setFullYear(y)
this.setMonth(mo)
this.setDate(d)
this.setAllDateFields(y,mo,d)
this.setHours(h)
this.setMinutes(m)
this.setSeconds(s)
Expand Down
23 changes: 17 additions & 6 deletions test/date.js
Expand Up @@ -113,7 +113,6 @@ describe('Date', function () {
, date = d.getDate()
, month = d.getMonth()
, year = d.getFullYear()

d.setTimezone('US/Pacific', true)

d.getMilliseconds().should.equal(millis)
Expand All @@ -132,7 +131,7 @@ describe('Date', function () {
d.getTime().should.not.equal(old)
})

it('should calculate correctly even between months', function () {
it('should calculate correctly when UTC date is day after timezone date', function () {
var forwards = {
timezone: 'US/Pacific', hour: 22, minute: 47,
year: 2013, month: 1, date: 31
Expand All @@ -144,19 +143,31 @@ describe('Date', function () {
d.toString().should.equal('Thu Jan 31 2013 22:47:01 GMT-0800 (PST)');
d.setTimezone('UTC');
d.toString().should.equal('Fri Feb 01 2013 06:47:01 GMT+0000 (UTC)');
})

it('should calculate correctly when UTC date is day before timezone date', function () {
var d = new time.Date(2010, 0, 31, 19, 0, 0, 0, 'UTC');
d.toString().should.equal('Sun Jan 31 2010 19:00:00 GMT+0000 (UTC)')

var backwards = {
timezone: 'Australia/Sydney', hour: 2, minute: 47,
year: 2013, month: 2, date: 1
};
var d = new time.Date(

var d2 = new time.Date(
backwards.year, backwards.month - 1, backwards.date,
backwards.hour, backwards.minute, 1, 1, backwards.timezone
);
d.toString().should.equal('Fri Feb 01 2013 02:47:01 GMT+1100 (EST)');
d.setTimezone('UTC');
d.toString().should.equal('Thu Jan 31 2013 15:47:01 GMT+0000 (UTC)');
d2.toString().should.equal('Fri Feb 01 2013 02:47:01 GMT+1100 (EST)');
d2.setTimezone('UTC');
d2.toString().should.equal('Thu Jan 31 2013 15:47:01 GMT+0000 (UTC)');
})

it('should calculate correctly on edge of months', function() {
var d = new time.Date("2013-02-01 01:02:03", 'US/Pacific');
d.toString().should.equal('Fri Feb 01 2013 01:02:03 GMT-0800 (PST)');
d.setTimezone('UTC');
d.toString().should.equal('Fri Feb 01 2013 09:02:03 GMT+0000 (UTC)');
})

})
Expand Down

0 comments on commit 9eef069

Please sign in to comment.