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 a0f3c96
Show file tree
Hide file tree
Showing 6 changed files with 410 additions and 274 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, timeZone;

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.timeZone = timeZone;//TODO validate

// Create needed number formatters.
numberFormatters = properties.numberFormatters;
Expand Down
56 changes: 54 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, timeZone ) {
var properties = {
numberFormatters: {},
pattern: pattern,
timeSeparator: numberSymbol( "timeSeparator", cldr )
timeSeparator: numberSymbol( "timeSeparator", cldr ),
timeZone: timeZone
},
widths = [ "abbreviated", "wide", "narrow" ];

Expand Down Expand Up @@ -208,6 +209,57 @@ 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.timeZone ) {

// The latest metazone data of the metazone array.
//TODO expand to support the historic metazones based on the given date.
var standardTzName,
daylightTzName,
metaZone = cldr.supplemental([
"metaZones/metazoneInfo/timezone", properties.timeZone, 0
]);

if ( metaZone ) {
standardTzName = cldr.main([
"dates/timeZoneNames/metazone",
metaZone.usesMetazone._mzone,
length < 4 ? "short" : "long",
"standard"
]);
daylightTzName = cldr.main([
"dates/timeZoneNames/metazone",
metaZone.usesMetazone._mzone,
length < 4 ? "short" : "long",
"daylight"
]);
if ( !standardTzName && !daylightTzName ) {
var exemplarCity = cldr.main(
"dates/timeZoneNames/zone/",
properties.timeZone,
"/exemplarCity"
);
standardTzName = cldr.main(
"dates/timeZoneNames/regionFormat-type-standard"
)
.replace( /\{0\}/, exemplarCity );

daylightTzName = cldr.main(
"dates/timeZoneNames/regionFormat-type-daylight"
)
.replace( /\{0\}/, exemplarCity );
}
if ( standardTzName && daylightTzName ) {
properties.standardTzName = standardTzName;
properties.daylightTzName = daylightTzName;
break;
}
}
}
/* falls through */
case "O":

// O: "{gmtFormat}+H;{gmtFormat}-H" or "{gmtZeroFormat}", eg. "GMT-8" or "GMT".
Expand Down
19 changes: 18 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,22 @@ 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.timeZone ) {
ret = dateTimezoneRegionFormat(
date,
properties.standardTzName,
properties.daylightTzName
);
if ( ret ) {
break;
}
}

/* falls through */
case "O":

// O: "{gmtFormat}+H;{gmtFormat}-H" or "{gmtZeroFormat}", eg. "GMT-8" or "GMT".
Expand Down
25 changes: 25 additions & 0 deletions src/date/timezone-region-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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, standardTzName, daylightTzName ) {

// var isDST = date.isDST(); FIXME
var isDST = false,
format;
if ( isDST ) {
format = daylightTzName;
} else {
format = standardTzName;
}
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": ":",
"timeZone": 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": ":",
"timeZone": 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": ":",
"timeZone": undefined
});
}
);
Expand Down Expand Up @@ -246,7 +249,8 @@ QUnit.test( "should allow for runtime compilation", function( assert ) {
}
},
"pattern": "LLL",
"timeSeparator": ":"
"timeSeparator": ":",
"timeZone": undefined
});
}
);
Expand Down
Loading

0 comments on commit a0f3c96

Please sign in to comment.