Skip to content

Commit

Permalink
Date Parser: Improve timezone handling
Browse files Browse the repository at this point in the history
- Handle special case when target date and today date are in different
  DST rules;

Fixes globalizejs#689
  • Loading branch information
rxaviers committed Feb 23, 2017
1 parent 439d9b9 commit 315054c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/date/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ return function( value, tokens, properties ) {
case "O":
case "X":
case "x":
timezoneOffset = token.value - date.getTimezoneOffset();
timezoneOffset = token.value;
break;
}

Expand Down Expand Up @@ -271,8 +271,8 @@ return function( value, tokens, properties ) {
date.setHours( date.getHours() + 12 );
}

if ( timezoneOffset ) {
date.setMinutes( date.getMinutes() + timezoneOffset );
if ( timezoneOffset !== undefined ) {
date.setMinutes( date.getMinutes() + timezoneOffset - date.getTimezoneOffset() );
}

// Truncate date at the most precise unit defined. Eg.
Expand Down
14 changes: 14 additions & 0 deletions test/functional/date/parse-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ QUnit.test( "should parse raw pattern", function( assert ) {
});

QUnit.test( "should parse a formatted date (reverse operation test)", function( assert ) {
var OrigDate;

extraSetup();

ar = Globalize( "ar" );
Expand All @@ -168,6 +170,18 @@ QUnit.test( "should parse a formatted date (reverse operation test)", function(
date = startOf( date, "minute" );
assert.deepEqual( Globalize.parseDate( Globalize.formatDate( date, { datetime: "full" } ), { datetime: "full" } ), date );
assert.deepEqual( ar.parseDate( ar.formatDate( date, { datetime: "full" } ), { datetime: "full" } ), date );

// Test #689 - special test when target date and today are in different DST rules.
// Note it was arbitrarily chosen O, other timezone patterns are supposed to pass too.
// date1 = a DST date (or vice-versa depending on the running environment).
// FakeDate.today = a standard time date (or vice-versa depending on the running environment).
/* globals Date:true */
OrigDate = Date;
Date = util.FakeDate;
date = new Date( 2017, 6, 1, 12, 0 );
util.FakeDate.today = new Date( 2017, 0, 1 );
assert.deepEqual( Globalize.parseDate( Globalize.formatDate( date, { datetime: "full" } ), { datetime: "full" } ), date );
Date = OrigDate;
});

});

0 comments on commit 315054c

Please sign in to comment.