From aedab316dea3d42657bb3fd1f1217594ef77287e Mon Sep 17 00:00:00 2001 From: Rafael Xavier de Souza Date: Mon, 22 May 2017 11:00:33 -0300 Subject: [PATCH] Date: Show optional fields (min & sec) of x and X timezone patterns Fixes #339 --- src/date/format.js | 32 ++++++++++++--- src/date/timezone-hour-format.js | 8 +++- test/unit/date/format.js | 68 +++++++++++++++++++++++--------- 3 files changed, 84 insertions(+), 24 deletions(-) diff --git a/src/date/format.js b/src/date/format.js index 88cc7836e..3d32bb50c 100644 --- a/src/date/format.js +++ b/src/date/format.js @@ -34,7 +34,7 @@ return function( date, numberFormatters, properties ) { } properties.pattern.replace( datePatternRe, function( current ) { - var dateField, type, value, + var aux, dateField, type, value, chr = current.charAt( 0 ), length = current.length; @@ -283,10 +283,32 @@ return function( date, numberFormatters, properties ) { /* falls through */ case "x": - // x: hourFormat("+HH;-HH") - // xx or xxxx: hourFormat("+HHmm;-HHmm") - // xxx or xxxxx: hourFormat("+HH:mm;-HH:mm") - value = length === 1 ? "+HH;-HH" : ( length % 2 ? "+HH:mm;-HH:mm" : "+HHmm;-HHmm" ); + // x: hourFormat("+HH[mm];-HH[mm]") + // xx: hourFormat("+HHmm;-HHmm") + // xxx: hourFormat("+HH:mm;-HH:mm") + // xxxx: hourFormat("+HHmm[ss];-HHmm[ss]") + // xxxxx: hourFormat("+HH:mm[:ss];-HH:mm[:ss]") + aux = date.getTimezoneOffset(); + + // If x and timezone offset has non-zero minutes, use xx (i.e., show minutes). + if ( length === 1 && aux % 60 - aux % 1 !== 0 ) { + length += 1; + } + + // If (xxxx or xxxxx) and timezone offset has zero seconds, use xx or xxx + // respectively (i.e., don't show optional seconds). + if ( ( length === 4 || length === 5 ) && aux % 1 === 0 ) { + length -= 2; + } + + value = [ + "+HH;-HH", + "+HHmm;-HHmm", + "+HH:mm;-HH:mm", + "+HHmmss;-HHmmss", + "+HH:mm:ss;-HH:mm:ss" + ][ length - 1 ]; + value = dateTimezoneHourFormat( date, value, ":" ); break; diff --git a/src/date/timezone-hour-format.js b/src/date/timezone-hour-format.js index d47390b78..d2b42b207 100644 --- a/src/date/timezone-hour-format.js +++ b/src/date/timezone-hour-format.js @@ -10,6 +10,7 @@ define([ * - "+H;-H": -3 * - "+HHmm;-HHmm": -0300 * - "+HH:mm;-HH:mm": -03:00 + * - "+HH:mm:ss;-HH:mm:ss": -03:00:00 */ return function( date, format, timeSeparator, formatNumber ) { var absOffset, @@ -40,7 +41,12 @@ return function( date, format, timeSeparator, formatNumber ) { // Update minutes offset and return. .replace( /mm/, function() { - return formatNumber[ 2 ]( absOffset % 60 ); + return formatNumber[ 2 ]( Math.floor( absOffset % 60 ) ); + }) + + // Update minutes offset and return. + .replace( /ss/, function() { + return formatNumber[ 2 ]( Math.floor( absOffset % 1 * 60 ) ); }); }; diff --git a/test/unit/date/format.js b/test/unit/date/format.js index 97661183f..fb6fbd057 100644 --- a/test/unit/date/format.js +++ b/test/unit/date/format.js @@ -1420,18 +1420,34 @@ QUnit.test( "should format timezone (X)", function( assert ) { value: "-03:00" }]); - // TODO (see https://github.com/jquery/globalize/issues/339) - // date = new FakeDate( -7.883 ); - // assert.dateFormat( date, "X", cldr, "-0752" ); - // assert.dateFormat( date, "XX", cldr, "-0752" ); - // assert.dateFormat( date, "XXX", cldr, "-07:52" ); - // assert.dateFormat( date, "XXXX", cldr, "-075258" ); - // assert.dateFormat( date, "XXXXX", cldr, "-07:52:58" ); + date = new FakeDate( -7.883 ); + assert.dateFormat( date, "X", cldr, [{ + type: "zone", + value: "-0752" + }]); + assert.dateFormat( date, "XX", cldr, [{ + type: "zone", + value: "-0752" + }]); + assert.dateFormat( date, "XXX", cldr, [{ + type: "zone", + value: "-07:52" + }]); + assert.dateFormat( date, "XXXX", cldr, [{ + type: "zone", + value: "-075258" + }]); + assert.dateFormat( date, "XXXXX", cldr, [{ + type: "zone", + value: "-07:52:58" + }]); date = new FakeDate( 5.5 ); - // TODO (see https://github.com/jquery/globalize/issues/339) - // assert.dateFormat( date, "X", cldr, "+0530" ); + assert.dateFormat( date, "X", cldr, [{ + type: "zone", + value: "+0530" + }]); assert.dateFormat( date, "XX", cldr, [{ type: "zone", value: "+0530" @@ -1517,18 +1533,34 @@ QUnit.test( "should format timezone (x)", function( assert ) { value: "-03:00" }]); - // TODO (see https://github.com/jquery/globalize/issues/339) - // date = new FakeDate( -7.883 ); - // assert.dateFormat( date, "x", cldr, "-0752" ); - // assert.dateFormat( date, "xx", cldr, "-0752" ); - // assert.dateFormat( date, "xxx", cldr, "-07:52" ); - // assert.dateFormat( date, "xxxx", cldr, "-075258" ); - // assert.dateFormat( date, "xxxxx", cldr, "-07:52:58" ); + date = new FakeDate( -7.883 ); + assert.dateFormat( date, "x", cldr, [{ + type: "zone", + value: "-0752" + }]); + assert.dateFormat( date, "xx", cldr, [{ + type: "zone", + value: "-0752" + }]); + assert.dateFormat( date, "xxx", cldr, [{ + type: "zone", + value: "-07:52" + }]); + assert.dateFormat( date, "xxxx", cldr, [{ + type: "zone", + value: "-075258" + }]); + assert.dateFormat( date, "xxxxx", cldr, [{ + type: "zone", + value: "-07:52:58" + }]); date = new FakeDate( 5.5 ); - // TODO (see https://github.com/jquery/globalize/issues/339) - // assert.dateFormat( date, "x", cldr, "+0530" ); + assert.dateFormat( date, "x", cldr, [{ + type: "zone", + value: "+0530" + }]); assert.dateFormat( date, "xx", cldr, [{ type: "zone", value: "+0530"