Skip to content

Commit

Permalink
fixup! Date: Olson-timezone-support (real 'z', 'v', 'V') and options.…
Browse files Browse the repository at this point in the history
…timeZone (2/2)
  • Loading branch information
rxaviers committed Mar 14, 2017
1 parent 0c94e0a commit 0f46b44
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 89 deletions.
1 change: 1 addition & 0 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function validateRequiredCldr( path, value ) {
skip: [
/dates\/calendars\/gregorian\/dateTimeFormats\/availableFormats/,
/dates\/calendars\/gregorian\/days\/.*\/short/,
/dates\/timeZoneNames\/zone/,
/dates\/timeZoneNames\/metazone/,
/globalize-iana/,
/supplemental\/metaZones/,
Expand Down
112 changes: 54 additions & 58 deletions src/date/format-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,57 @@ return function( pattern, cldr, timeZone ) {
},
widths = [ "abbreviated", "wide", "narrow" ];

function getTimeZoneName( length, type ) {
var metaZone, result;

result = cldr.main([
"dates/timeZoneNames/zone",
timeZone,
length < 4 ? "short" : "long",
type
]);

if ( result ) {
return result;
}

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

// return cldr.main([
result = cldr.main([
"dates/timeZoneNames/metazone",
metaZone,
length < 4 ? "short" : "long",
type
]);

return result;
}

function setNumberFormatterPattern( pad ) {
properties.numberFormatters[ pad ] = stringPad( "", pad );
}

if ( timeZone ) {
properties.timeZoneData = {
offsets: cldr.get([ "globalize-iana/zoneData", timeZone, "offsets" ]),
untils: cldr.get([ "globalize-iana/zoneData", timeZone, "untils" ])
};
}

function setNumberFormatterPattern( pad ) {
properties.numberFormatters[ pad ] = stringPad( "", pad );
}

pattern.replace( datePatternRe, function( current ) {
var formatNumber,
chr = current.charAt( 0 ),
length = current.length,
metaZone,
standardTzName,
daylightTzName,
genericTzName;

if ( timeZone && ( chr === "v" || chr === "z" )) {

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

if ( chr === "j" ) {

// Locale preferred hHKk.
Expand All @@ -71,57 +92,38 @@ return function( pattern, cldr, timeZone ) {
length = 4;
}

// z...zzz: fallback to "O"
// zzzz: fallback to "OOOO"
// z...zzz: "{shortRegion}", eg. "PST" or "PDT".
// zzzz: "{regionName} {Standard Time}" or "{regionName} {Daylight Time}",
// e.g., "Pacific Standard Time" or "Pacific Daylight Time".
// http://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
if ( chr === "z" ) {
if ( metaZone ) {

//z...zzz: "{shortRegion}", eg. "PST" or "PDT".
//zzzz: "{regionName} {Standard Time}" or "{regionName} {Daylight Time}",
//eg. "Pacific Standard Time" or "Pacific Daylight Time".
standardTzName = cldr.main([
"dates/timeZoneNames/metazone",
metaZone,
length < 4 ? "short" : "long",
"standard"
]);
daylightTzName = cldr.main([
"dates/timeZoneNames/metazone",
metaZone,
length < 4 ? "short" : "long",
"daylight"
]);
standardTzName = getTimeZoneName( length, "standard" );
daylightTzName = getTimeZoneName( length, "daylight" );
if ( standardTzName ) {
properties.standardTzName = standardTzName;
}
if ( daylightTzName ) {
properties.daylightTzName = daylightTzName;
}

//fall through "O" format
if ( !metaZone || !standardTzName ) {
// Fall through the "O" format in case one name is missing.
if ( !standardTzName || !daylightTzName ) {
chr = "O";
if ( length < 4 ) {
length = 1;
}
}
}

// v: fallback to "VVVV"
// vvvv: fallback to "VVVV"
// v...vvv: "{shortRegion}", eg. "PT".
// vvvv: "{regionName} {Time}" or "{regionName} {Time}",
// e.g., "Pacific Time"
// http://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
if ( chr === "v" ) {
if ( metaZone ) {

//v...vvv: "{shortRegion}", eg. "PT".
//vvvv: "{regionName} {Time}" or "{regionName} {Time}",
//eg. "Pacific Time"
genericTzName = cldr.main([
"dates/timeZoneNames/metazone",
metaZone,
length === 1 ? "short" : "long",
"generic"
]);
}
genericTzName = getTimeZoneName( length, "generic" );

//fall through "V" format
if ( !metaZone || !genericTzName ) {
// Fall back to "V" format.
if ( !genericTzName ) {
chr = "V";
length = 4;
}
Expand Down Expand Up @@ -290,11 +292,6 @@ return function( pattern, cldr, timeZone ) {
break;

// Zone
case "z":
properties.standardTzName = standardTzName;
properties.daylightTzName = daylightTzName;
break;

case "v":
if ( length !== 1 && length !== 4 ) {
throw createErrorUnsupportedFeature({
Expand Down Expand Up @@ -355,7 +352,6 @@ return function( pattern, cldr, timeZone ) {
properties.gmtZeroFormat = cldr.main( "dates/timeZoneNames/gmtZeroFormat" );
properties.tzLongHourFormat = cldr.main( "dates/timeZoneNames/hourFormat" );

// FIXME do we need this Z?
/* falls through */
case "Z":
case "X":
Expand Down
37 changes: 22 additions & 15 deletions src/date/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ return function( date, numberFormatters, properties ) {
}
}

// z...zzz: "{shortRegion}", e.g., "PST" or "PDT".
// zzzz: "{regionName} {Standard Time}" or "{regionName} {Daylight Time}",
// e.g., "Pacific Standard Time" or "Pacific Daylight Time".
if ( chr === "z" ) {
if ( date.isDST ) {
value = date.isDST() ? properties.daylightTzName : properties.standardTzName;
}

// Fall back to "O" format.
if ( !value ) {
chr = "O";
if ( length < 4 ) {
length = 1;
}
}
}

switch ( chr ) {

// Era
Expand Down Expand Up @@ -216,22 +233,13 @@ return function( date, numberFormatters, properties ) {

// Zone
case "z":
break;

// z...zzz: "{shortRegion}", eg. "PST" or "PDT".
// zzzz: "{regionName} {Standard Time}" or "{regionName} {Daylight Time}",
// eg. "Pacific Standard Time" or "Pacific Daylight Time".
if ( date.isDST && properties.standardTzName ) {
value = date.isDST() && properties.daylightTzName ?
properties.daylightTzName : properties.standardTzName;
break;
}

/* falls through */
case "v":

//v...vvv: "{shortRegion}", eg. "PT".
//vvvv: "{regionName} {Time}",
//eg. "Pacific Time"
// v...vvv: "{shortRegion}", eg. "PT".
// vvvv: "{regionName} {Time}",
// e.g., "Pacific Time".
if ( properties.genericTzName ) {
value = properties.genericTzName;
break;
Expand All @@ -240,8 +248,7 @@ return function( date, numberFormatters, properties ) {
/* falls through */
case "V":

//VVVV: "{explarCity} {Time}",
//eg. "Los Angeles Time"
//VVVV: "{explarCity} {Time}", e.g., "Los Angeles Time"
if ( properties.timeZoneName ) {
value = properties.timeZoneName;
break;
Expand Down
42 changes: 35 additions & 7 deletions test/unit/date/format-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ define([
"cldr",
"src/date/format-properties",
"json!cldr-data/main/en/ca-gregorian.json",
"json!cldr-data/main/en/timeZoneNames.json",
"json!cldr-data/main/en-GB/ca-gregorian.json",
"json!cldr-data/main/en-GB/timeZoneNames.json",
"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, properties, enCaGregorian, likelySubtags, timeData, weekData, metaZones ) {
], function( Cldr, properties, enCaGregorian, enTimeZoneNames, enGbCaGregorian, enGbTimeZoneNames,
likelySubtags, timeData, weekData, metaZones ) {

var cldr;

Cldr.load(
enCaGregorian,
enTimeZoneNames,
enGbCaGregorian,
enGbTimeZoneNames,
likelySubtags,
timeData,
weekData,
Expand Down Expand Up @@ -126,20 +133,42 @@ QUnit.test( "should return dayPeriods property for period (a)", function( assert

QUnit.test( "should return standardTzName and daylightTzName properties for zone (z|zz|zzz|zzzz|zzzzz)",
function( assert ) {
var timeZone = "America/Los_Angeles";
var timeZone,
enGb = new Cldr( "en-GB" );

timeZone = "America/Los_Angeles";
[ "z", "zz", "zzz" ].forEach(function( pattern ) {
assert.equal( properties( pattern, cldr, timeZone ).standardTzName, "PST" );
assert.equal( properties( pattern, cldr, timeZone ).daylightTzName, "PDT" );
});

[ "zzzz", "zzzzz" ].forEach(function( pattern ) {
assert.equal( properties( pattern, cldr, timeZone ).standardTzName, "Pacific Standard Time" );
assert.equal( properties( pattern, cldr, timeZone ).daylightTzName, "Pacific Daylight Time" );
});

// Test for two things:
// - daylightTzName using the zone data (primary), not the metazone (secondary try);
// - standardTzName being undefined, therefore requiring the O fallback properties;
timeZone = "Europe/London";
[ "z", "zz", "zzz" ].forEach(function( pattern ) {
var formatProperties = properties( pattern, enGb, timeZone );
assert.ok( !( "standardTzName" in formatProperties ) );
assert.equal( formatProperties.daylightTzName, "BST" );
assert.ok( "gmtFormat" in formatProperties );
assert.ok( "gmtZeroFormat" in formatProperties );
assert.ok( "tzLongHourFormat" in formatProperties );
});
[ "zzzz", "zzzzz" ].forEach(function( pattern ) {
var formatProperties = properties( pattern, enGb, timeZone );
assert.ok( !( "standardTzName" in formatProperties ) );
assert.equal( formatProperties.daylightTzName, "British Summer Time" );
assert.ok( "gmtFormat" in formatProperties );
assert.ok( "gmtZeroFormat" in formatProperties );
assert.ok( "tzLongHourFormat" in formatProperties );
});
});

QUnit.test( "should return standardTzName and daylightTzName properties for zone (v|vvvv)",
function( assert ) {
QUnit.test( "should return genericTzName property for zone (v|vvvv)", function( assert ) {
var pattern,
timeZone = "America/Los_Angeles";

Expand All @@ -150,8 +179,7 @@ QUnit.test( "should return standardTzName and daylightTzName properties for zone
assert.equal( properties( pattern, cldr, timeZone ).genericTzName, "Pacific Time" );
});

QUnit.test( "should return standardTzName and daylightTzName properties for zone (VV|VVV|VVVV)",
function( assert ) {
QUnit.test( "should return timeZoneName properties for zone (VV|VVV|VVVV)", function( assert ) {
var pattern,
timeZone = "America/Los_Angeles";

Expand Down
Loading

0 comments on commit 0f46b44

Please sign in to comment.