Skip to content

Commit

Permalink
Date: Show optional fields (min & sec) of x and X timezone patterns
Browse files Browse the repository at this point in the history
Fixes #339
  • Loading branch information
rxaviers committed May 23, 2017
1 parent 111d11d commit aedab31
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 24 deletions.
32 changes: 27 additions & 5 deletions src/date/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
8 changes: 7 additions & 1 deletion src/date/timezone-hour-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ) );
});
};

Expand Down
68 changes: 50 additions & 18 deletions test/unit/date/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit aedab31

Please sign in to comment.