Skip to content

Commit

Permalink
Date: Olson-timezone-support (real 'z')
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramalingam Kandaswamy,Manikandan committed Feb 10, 2017
1 parent 26f6b01 commit 8e074bf
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function validateRequiredCldr( path, value ) {
*/
Globalize.dateFormatter =
Globalize.prototype.dateFormatter = function( options ) {
var args, cldr, numberFormatters, pad, pattern, properties, returnFn;
var args, cldr, numberFormatters, pad, pattern, properties, returnFn, tzId;

validateParameterTypePlainObject( options, "options" );

Expand All @@ -66,6 +66,7 @@ Globalize.prototype.dateFormatter = function( options ) {
pattern = dateExpandPattern( options, cldr );
properties = dateFormatProperties( pattern, cldr );
cldr.off( "get", validateRequiredCldr );
properties.tzId = tzId;//TODO validate

// Create needed number formatters.
numberFormatters = properties.numberFormatters;
Expand Down
55 changes: 53 additions & 2 deletions src/date/format-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ define([
*
* TODO Support other calendar types.
*/
return function( pattern, cldr ) {
return function( pattern, cldr, tzId ) {
var properties = {
numberFormatters: {},
pattern: pattern,
timeSeparator: numberSymbol( "timeSeparator", cldr )
timeSeparator: numberSymbol( "timeSeparator", cldr ),
tzId: tzId
},
widths = [ "abbreviated", "wide", "narrow" ];

Expand Down Expand Up @@ -208,6 +209,56 @@ return function( pattern, cldr ) {

// Zone
case "z":

// z...zzz: "{shortRegion}", eg. "PST" or "PDT".
// zzzz: "{regionName} {Standard Time}" or "{regionName} {Daylight Time}",
// eg. "Pacific Standard Time" or "Pacific Daylight Time".
if ( properties.tzId ) {

// The latest metazone data of the metazone array.
//TODO expand to support the historic metazones based on the given date.
var metaZone = cldr.supplemental(
[ "metaZones/metazoneInfo/timezone/" + properties.tzId, 0 ]
);
if ( metaZone ) {
properties.exemplarCity = cldr.main(
"dates/timeZoneNames/zone/" +
properties.tzId +
"/exemplarCity"
);
if ( length < 4 ) {
properties.standardTzName = cldr.main(
"dates/timeZoneNames/metazone/" +
metaZone.usesMetazone._mzone +
"/short/standard"
);
properties.daylightTzName = cldr.main(
"dates/timeZoneNames/metazone/" +
metaZone.usesMetazone._mzone +
"/short/daylight"
);
} else {
properties.standardTzName = cldr.main(
"dates/timeZoneNames/metazone/" +
metaZone.usesMetazone._mzone +
"/long/standard"
);
properties.daylightTzName = cldr.main(
"dates/timeZoneNames/metazone/" +
metaZone.usesMetazone._mzone +
"/long/daylight"
);
}
properties.regionFormatStandard = cldr.main(
"dates/timeZoneNames/regionFormat-type-standard"
);
properties.regionFormatDaylight = cldr.main(
"dates/timeZoneNames/regionFormat-type-daylight"
);
}
}

/* falls through */
case "O":

// O: "{gmtFormat}+H;{gmtFormat}-H" or "{gmtZeroFormat}", eg. "GMT-8" or "GMT".
Expand Down
22 changes: 21 additions & 1 deletion src/date/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ define([
"./pattern-re",
"./start-of",
"./timezone-hour-format",
"./timezone-region-format",
"./week-days",
"../util/remove-literal-quotes"
], function( dateDayOfWeek, dateDayOfYear, dateMillisecondsInDay, datePatternRe, dateStartOf,
dateTimezoneHourFormat, dateWeekDays, removeLiteralQuotes ) {
dateTimezoneHourFormat, dateTimezoneRegionFormat, dateWeekDays, removeLiteralQuotes ) {

/**
* format( date, properties )
Expand Down Expand Up @@ -207,6 +208,25 @@ return function( date, numberFormatters, properties ) {

// Zone
case "z":

// z...zzz: "{shortRegion}", eg. "PST" or "PDT".
// zzzz: "{regionName} {Standard Time}" or "{regionName} {Daylight Time}",
// eg. "Pacific Standard Time" or "Pacific Daylight Time".
if ( properties.tzId ) {
ret = dateTimezoneRegionFormat(
date,
properties.regionFormatStandard,
properties.regionFormatDaylight,
properties.standardTzName,
properties.daylightTzName,
properties.exemplarCity
);
if ( ret ) {
break;
}
}

/* falls through */
case "O":

// O: "{gmtFormat}+H;{gmtFormat}-H" or "{gmtZeroFormat}", eg. "GMT-8" or "GMT".
Expand Down
30 changes: 30 additions & 0 deletions src/date/timezone-region-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
define([], function() {

/**
* regionFormat( date, format, timeZoneId, formatNumber )
*
* Return date's timezone names according to the format passed.
* Eg for format when timezone offset is 180:
* "regionFormat": "{0} Time",
* "regionFormat-type-standard": "{0} Standard Time",
* "regionFormat-type-daylight": "{0} Daylight Time",
* "fallbackFormat": "{1} ({0})",
*/
return function( date, regionFormatStandard, regionFormatDaylight, standardTzName, daylightTzName,
exemplarCity ) {

// var isDST = date.isDST(); FIXME
var isDST = false,
format;
if ( isDST ) {
format = ( daylightTzName ) ? daylightTzName :
( regionFormatStandard ) ? regionFormatDaylight.replace( /\{0\}/, exemplarCity ) :
undefined;
} else {
format = ( standardTzName ) ? standardTzName :
( regionFormatStandard ) ? regionFormatStandard.replace( /\{0\}/, exemplarCity ) :
undefined;
}
return format;
};
});
10 changes: 7 additions & 3 deletions test/functional/date/date-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ QUnit.test( "should allow for runtime compilation", function( assert ) {
"pm-alt-variant": "pm"
},
"pattern": "h:mm:ss a",
"timeSeparator": ":"
"timeSeparator": ":",
"tzId": undefined
});
}
);
Expand All @@ -120,6 +121,7 @@ QUnit.test( "should allow for runtime compilation", function( assert ) {
assert.deepEqual( runtimeArgs[ 1 ], {
"pattern": "EEEE, MMMM d, y",
"timeSeparator": ":",
"tzId": undefined,
"days": {
"E": {
"4": {
Expand Down Expand Up @@ -214,7 +216,8 @@ QUnit.test( "should allow for runtime compilation", function( assert ) {
}
},
"pattern": "E, MMM d, y G, h:mm:ss a",
"timeSeparator": ":"
"timeSeparator": ":",
"tzId": undefined
});
}
);
Expand Down Expand Up @@ -246,7 +249,8 @@ QUnit.test( "should allow for runtime compilation", function( assert ) {
}
},
"pattern": "LLL",
"timeSeparator": ":"
"timeSeparator": ":",
"tzId": undefined
});
}
);
Expand Down
44 changes: 40 additions & 4 deletions test/unit/date/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ define([
"json!cldr-data/supplemental/likelySubtags.json",
"json!cldr-data/supplemental/timeData.json",
"json!cldr-data/supplemental/weekData.json",
"json!cldr-data/supplemental/metaZones.json",

"cldr/event",
"cldr/supplemental"
], function( Cldr, format, formatProperties, stringPad, deCaGregorian, enCaGregorian,
enTimeZoneNames, enGbCaGregorian, enInCaGregorian, ptCaGregorian, ruCaGregorian, likelySubtags,
timeData, weekData ) {
timeData, weekData, metaZones ) {

var cldr,
year0 = new Date( -62167190400000 ),
Expand Down Expand Up @@ -52,15 +53,16 @@ Cldr.load(
ptCaGregorian,
ruCaGregorian,
timeData,
weekData
weekData,
metaZones
);

cldr = new Cldr( "en" );

QUnit.assert.dateFormat = function( date, pattern, cldr, expected ) {
QUnit.assert.dateFormat = function( date, pattern, cldr, expected, tzId ) {
var pad,
numberFormatters = [],
properties = formatProperties( pattern, cldr );
properties = formatProperties( pattern, cldr, tzId );

// Create simple number formatters for this test purposes.
for ( pad in properties.numberFormatters ) {
Expand Down Expand Up @@ -443,6 +445,20 @@ QUnit.test( "should format various milliseconds (A+)", function( assert ) {
/**
* Zone
*/
QUnit.test( "should format timezone (z)", function( assert ) {
var date = new Date();
assert.dateFormat(date, "z", cldr, "PST", "America/Los_Angeles");
assert.dateFormat(date, "zz", cldr, "PST", "America/Los_Angeles");
assert.dateFormat(date, "zzz", cldr, "PST", "America/Los_Angeles");
assert.dateFormat(date, "zzzz", cldr, "Pacific Standard Time", "America/Los_Angeles");

//fall through to 'O' format
date = new FakeDate( 0 );
assert.dateFormat(date, "z", cldr, "GMT", "Etc/GMT");
assert.dateFormat(date, "zz", cldr, "GMT", "Etc/GMT");
assert.dateFormat(date, "zzz", cldr, "GMT", "Etc/GMT");
assert.dateFormat(date, "zzzz", cldr, "GMT", "Etc/GMT");
});

QUnit.test( "should format timezone (z)", function( assert ) {
var date = new FakeDate( 0 );
Expand All @@ -464,6 +480,26 @@ QUnit.test( "should format timezone (z)", function( assert ) {
assert.dateFormat( date, "zzzz", cldr, "GMT+11:00" );
});

QUnit.test( "should format timezone (O)", function( assert ) {
var date = new FakeDate( 0 );
assert.dateFormat( date, "O", cldr, "GMT" );
assert.dateFormat( date, "OO", cldr, "GMT" );
assert.dateFormat( date, "OOO", cldr, "GMT" );
assert.dateFormat( date, "OOOO", cldr, "GMT" );

date = new FakeDate( -3 );
assert.dateFormat( date, "O", cldr, "GMT-3" );
assert.dateFormat( date, "OO", cldr, "GMT-3" );
assert.dateFormat( date, "OOO", cldr, "GMT-3" );
assert.dateFormat( date, "OOOO", cldr, "GMT-03:00" );

date = new FakeDate( 11 );
assert.dateFormat( date, "O", cldr, "GMT+11" );
assert.dateFormat( date, "OO", cldr, "GMT+11" );
assert.dateFormat( date, "OOO", cldr, "GMT+11" );
assert.dateFormat( date, "OOOO", cldr, "GMT+11:00" );
});

QUnit.test( "should format timezone (Z)", function( assert ) {
var date = new FakeDate( 0 );
assert.dateFormat( date, "Z", cldr, "+0000" );
Expand Down

0 comments on commit 8e074bf

Please sign in to comment.