Skip to content

Commit

Permalink
Date: Show optional minutes of O timezone pattern
Browse files Browse the repository at this point in the history
Ref #339
  • Loading branch information
rxaviers committed May 23, 2017
1 parent aedab31 commit 1ccd1a8
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 48 deletions.
19 changes: 15 additions & 4 deletions src/date/format-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ define([
"./first-day-of-week",
"./get-time-zone-name",
"./pattern-re",
"./timezone-hour-format/h",
"./timezone-hour-format/hm",
"../common/create-error/unsupported-feature",
"../common/format-message",
"../common/runtime-cache-data-bind",
"../number/symbol",
"../util/string/pad"
], function( dateFirstDayOfWeek, dateGetTimeZoneName, datePatternRe, createErrorUnsupportedFeature,
formatMessage, runtimeCacheDataBind, numberSymbol, stringPad ) {
], function( dateFirstDayOfWeek, dateGetTimeZoneName, datePatternRe, dateTimezoneHourFormatH,
dateTimezoneHourFormatHm, createErrorUnsupportedFeature, formatMessage, runtimeCacheDataBind,
numberSymbol, stringPad ) {

/**
* properties( pattern, cldr )
Expand Down Expand Up @@ -43,7 +46,7 @@ return function( pattern, cldr, timeZone ) {
}

pattern.replace( datePatternRe, function( current ) {
var chr, daylightTzName, formatNumber, genericTzName, length, standardTzName;
var aux, chr, daylightTzName, formatNumber, genericTzName, length, standardTzName;

chr = current.charAt( 0 );
length = current.length;
Expand Down Expand Up @@ -320,7 +323,15 @@ return function( pattern, cldr, timeZone ) {
// OOOO: "{gmtFormat}{hourFormat}" or "{gmtZeroFormat}", eg. "GMT-08:00" or "GMT".
properties.gmtFormat = cldr.main( "dates/timeZoneNames/gmtFormat" );
properties.gmtZeroFormat = cldr.main( "dates/timeZoneNames/gmtZeroFormat" );
properties.tzLongHourFormat = cldr.main( "dates/timeZoneNames/hourFormat" );

// Unofficial deduction of the hourFormat variations.
// Official spec is pending resolution: http://unicode.org/cldr/trac/ticket/8293
aux = cldr.main( "dates/timeZoneNames/hourFormat" );
properties.tzHourFormat = [
dateTimezoneHourFormatH( aux ),
dateTimezoneHourFormatHm( aux, "H" ),
dateTimezoneHourFormatHm( aux, "HH" )
];

/* falls through */
case "Z":
Expand Down
12 changes: 11 additions & 1 deletion src/date/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,19 @@ return function( date, numberFormatters, properties ) {
if ( date.getTimezoneOffset() === 0 ) {
value = properties.gmtZeroFormat;
} else {

// If O..OOO and timezone offset has non-zero minutes, use OOOO (i.e., show
// minutes).
aux = date.getTimezoneOffset();
if ( length < 4 && aux % 60 - aux % 1 !== 0 ) {
aux = 1;
} else {
aux = length < 4 ? 0 : 2;
}

value = dateTimezoneHourFormat(
date,
length < 4 ? "+H;-H" : properties.tzLongHourFormat,
properties.tzHourFormat[ aux ],
timeSeparator,
numberFormatters
);
Expand Down
26 changes: 26 additions & 0 deletions src/date/timezone-hour-format/h.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
define(function() {

/**
* timezoneHourFormatShortH( hourFormat )
*
* @hourFormat [String]
*
* Unofficial deduction of the short hourFormat given time zone `hourFormat` element.
* Official spec is pending resolution: http://unicode.org/cldr/trac/ticket/8293
*
* Example:
* - "+HH.mm;-HH.mm" => "+H;-H"
* - "+HH:mm;-HH:mm" => "+H;-H"
* - "+HH:mm;−HH:mm" => "+H;−H" (Note MINUS SIGN \u2212)
* - "+HHmm;-HHmm" => "+H:-H"
*/
return function( hourFormat ) {
return hourFormat
.split( ";" )
.map(function( format ) {
return format.slice( 0, format.indexOf( "H" ) + 1 );
})
.join( ";" );
};

});
35 changes: 35 additions & 0 deletions src/date/timezone-hour-format/hm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
define(function() {

/**
* timezoneHourFormatLongHm( hourFormat )
*
* @hourFormat [String]
*
* Unofficial deduction of the short hourFormat given time zone `hourFormat` element.
* Official spec is pending resolution: http://unicode.org/cldr/trac/ticket/8293
*
* Example (hFormat === "H"): (used for short Hm)
* - "+HH.mm;-HH.mm" => "+H.mm;-H.mm"
* - "+HH:mm;-HH:mm" => "+H:mm;-H:mm"
* - "+HH:mm;−HH:mm" => "+H:mm;−H:mm" (Note MINUS SIGN \u2212)
* - "+HHmm;-HHmm" => "+Hmm:-Hmm"
*
* Example (hFormat === "HH": (used for long Hm)
* - "+HH.mm;-HH.mm" => "+HH.mm;-HH.mm"
* - "+HH:mm;-HH:mm" => "+HH:mm;-HH:mm"
* - "+H:mm;-H:mm" => "+HH:mm;-HH:mm"
* - "+HH:mm;−HH:mm" => "+HH:mm;−HH:mm" (Note MINUS SIGN \u2212)
* - "+HHmm;-HHmm" => "+HHmm:-HHmm"
*/
return function( hourFormat, hFormat ) {
return hourFormat
.split( ";" )
.map(function( format ) {
var parts = format.split( /H+/ );
parts.splice( 1, 0, hFormat );
return parts.join( "" );
})
.join( ";" );
};

});
8 changes: 4 additions & 4 deletions test/unit/date/format-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ QUnit.test( "should return properties for timezone (z)", function( assert ) {
assert.ok( !( "daylightTzName" in formatProperties ) );
assert.ok( "gmtFormat" in formatProperties );
assert.ok( "gmtZeroFormat" in formatProperties );
assert.ok( "tzLongHourFormat" in formatProperties );
assert.ok( "tzHourFormat" in formatProperties );
});
[ "zzzz" ].forEach(function( pattern ) {
var formatProperties = properties( pattern, cldr, timeZone );
assert.equal( formatProperties.standardTzName, "Gulf Standard Time" );
assert.ok( !( "daylightTzName" in formatProperties ) );
assert.ok( "gmtFormat" in formatProperties );
assert.ok( "gmtZeroFormat" in formatProperties );
assert.ok( "tzLongHourFormat" in formatProperties );
assert.ok( "tzHourFormat" in formatProperties );
});

// Test for two things:
Expand All @@ -180,15 +180,15 @@ QUnit.test( "should return properties for timezone (z)", function( assert ) {
assert.equal( formatProperties.daylightTzName, "BST" );
assert.ok( "gmtFormat" in formatProperties );
assert.ok( "gmtZeroFormat" in formatProperties );
assert.ok( "tzLongHourFormat" in formatProperties );
assert.ok( "tzHourFormat" in formatProperties );
});
[ "zzzz" ].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 );
assert.ok( "tzHourFormat" in formatProperties );
});
});

Expand Down
73 changes: 34 additions & 39 deletions test/unit/date/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -1121,44 +1121,43 @@ QUnit.test( "should format timezone (z)", function( assert ) {
// Asia/Calcutta and should fall through 'O' format.
// isDST === false.
date = new Date( 2017, 0, 1 );
assert.dateFormatWithTimezone( date1, "z", "Asia/Calcutta", cldr, [{
assert.dateFormatWithTimezone( date, "z", "Asia/Calcutta", cldr, [{
type: "zone",
value: "GMT+5"
value: "GMT+5:30"
}]);
assert.dateFormatWithTimezone( date1, "zz", "Asia/Calcutta", cldr, [{
assert.dateFormatWithTimezone( date, "zz", "Asia/Calcutta", cldr, [{
type: "zone",
value: "GMT+5"
value: "GMT+5:30"
}]);
assert.dateFormatWithTimezone( date1, "zzz", "Asia/Calcutta", cldr, [{
assert.dateFormatWithTimezone( date, "zzz", "Asia/Calcutta", cldr, [{
type: "zone",
value: "GMT+5"
value: "GMT+5:30"
}]);
assert.dateFormatWithTimezone( date1, "zzzz", "Asia/Calcutta", cldr, [{
assert.dateFormatWithTimezone( date, "zzzz", "Asia/Calcutta", cldr, [{
type: "zone",
value: "India Standard Time"
}]);

// isDST === true:
// FIXME it should be GMT+6[:30]
date = new Date( 1943, 0, 1 );
assert.dateFormatWithTimezone( date1, "z", "Asia/Calcutta", cldr, [{
assert.dateFormatWithTimezone( date, "z", "Asia/Calcutta", cldr, [{
type: "zone",
value: "GMT+5"
value: "GMT+6:30"
}]);
assert.dateFormatWithTimezone( date1, "zz", "Asia/Calcutta", cldr, [{
assert.dateFormatWithTimezone( date, "zz", "Asia/Calcutta", cldr, [{
type: "zone",
value: "GMT+5"
value: "GMT+6:30"
}]);
assert.dateFormatWithTimezone( date1, "zzz", "Asia/Calcutta", cldr, [{
assert.dateFormatWithTimezone( date, "zzz", "Asia/Calcutta", cldr, [{
type: "zone",
value: "GMT+5"
}]);
assert.dateFormatWithTimezone( date1, "zzzz", "Asia/Calcutta", cldr, [{
type: "zone",
value: "India Standard Time"
value: "GMT+6:30"
}]);

// Fall through to 'O' format
assert.dateFormatWithTimezone( date, "zzzz", "Asia/Calcutta", cldr, [{
type: "zone",
value: "GMT+06:30"
}]);
assert.dateFormatWithTimezone( date1, "z", "Foo/Bar", cldr, [{
type: "zone",
value: "GMT"
Expand Down Expand Up @@ -1251,14 +1250,6 @@ QUnit.test( "should format timezone (O)", function( assert ) {
type: "zone",
value: "GMT"
}]);
assert.dateFormat( date, "OO", cldr, [{
type: "zone",
value: "GMT"
}]);
assert.dateFormat( date, "OOO", cldr, [{
type: "zone",
value: "GMT"
}]);
assert.dateFormat( date, "OOOO", cldr, [{
type: "zone",
value: "GMT"
Expand All @@ -1269,35 +1260,40 @@ QUnit.test( "should format timezone (O)", function( assert ) {
type: "zone",
value: "GMT-3"
}]);
assert.dateFormat( date, "OO", cldr, [{
assert.dateFormat( date, "OOOO", cldr, [{
type: "zone",
value: "GMT-3"
value: "GMT-03:00"
}]);
assert.dateFormat( date, "OOO", cldr, [{

date = new FakeDate( 11 );
assert.dateFormat( date, "O", cldr, [{
type: "zone",
value: "GMT-3"
value: "GMT+11"
}]);
assert.dateFormat( date, "OOOO", cldr, [{
type: "zone",
value: "GMT-03:00"
value: "GMT+11:00"
}]);

date = new FakeDate( 11 );
// TODO Support optional seconds.
date = new FakeDate( -7.883 );
assert.dateFormat( date, "O", cldr, [{
type: "zone",
value: "GMT+11"
value: "GMT-7:52"
}]);
assert.dateFormat( date, "OO", cldr, [{
assert.dateFormat( date, "OOOO", cldr, [{
type: "zone",
value: "GMT+11"
value: "GMT-07:52"
}]);
assert.dateFormat( date, "OOO", cldr, [{

date = new FakeDate( 5.5 );
assert.dateFormat( date, "O", cldr, [{
type: "zone",
value: "GMT+11"
value: "GMT+5:30"
}]);
assert.dateFormat( date, "OOOO", cldr, [{
type: "zone",
value: "GMT+11:00"
value: "GMT+05:30"
}]);
});

Expand Down Expand Up @@ -1443,7 +1439,6 @@ QUnit.test( "should format timezone (X)", function( assert ) {
}]);

date = new FakeDate( 5.5 );

assert.dateFormat( date, "X", cldr, [{
type: "zone",
value: "+0530"
Expand Down

0 comments on commit 1ccd1a8

Please sign in to comment.