Skip to content

Pad the year to 4 digits #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/values/TimeValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var PARENT = dv.DataValue;
* @since 0.1
* @licence GNU GPL v2+
* @author H. Snater < mediawiki@snater.com >
* @author Thiemo Mättig
*
* @constructor
*
Expand All @@ -28,8 +29,10 @@ var SELF = dv.TimeValue = util.inherit( 'DvTimeValue', PARENT, function( timesta
this._time = {};

try {
var matches = /^([+-]?\d+)-(\d+)-(\d+)T(\d{2}):(\d{2}):(\d{2})Z$/.exec( timestamp );
this._time.year = parseInt( matches[1], 10 );
var matches = /^([-+]?\d+)-(\d+)-(\d+)T(\d{2}):(\d{2}):(\d{2})Z$/.exec( timestamp );

// Strip additional leading zeros from the year, but keep 4 digits.
this._time.year = matches[1].replace( /\b0+(?=\d{4})/, '' );
this._time.month = parseInt( matches[2], 10 );
this._time.day = parseInt( matches[3], 10 );
this._time.hour = parseInt( matches[4], 10 );
Expand Down Expand Up @@ -105,7 +108,7 @@ var SELF = dv.TimeValue = util.inherit( 'DvTimeValue', PARENT, function( timesta
* @return {string}
*/
getSortKey: function() {
return this._getTimestamp();
return this._getTimestamp( true );
},

/**
Expand All @@ -120,7 +123,7 @@ var SELF = dv.TimeValue = util.inherit( 'DvTimeValue', PARENT, function( timesta
/**
* @since 0.7
*
* @return {number}
* @return {string}
*/
getYear: function() {
return this._time.year;
Expand Down Expand Up @@ -193,14 +196,16 @@ var SELF = dv.TimeValue = util.inherit( 'DvTimeValue', PARENT, function( timesta
},

/**
* Returns a YMD-ordered timestamp string resembling ISO 8601.
* @private
*
* @return {string}
* @param {bool} [padYear=false] True if the year should be padded to the maximum length of 16
* digits, false for the default padding to 4 digits.
*
* @return {string} A YMD-ordered timestamp string resembling ISO 8601.
*/
_getTimestamp: function() {
return ( ( this._time.year < 0 ) ? '-' : '+' )
+ pad( this._time.year, 11 ) + '-'
_getTimestamp: function( padYear ) {
return ( this._time.year.charAt( 0 ) === '-' ? '-' : '+' )
+ pad( this._time.year, padYear ? 16 : 4 ) + '-'
+ pad( this._time.month, 2 ) + '-'
+ pad( this._time.day, 2 ) + 'T'
+ pad( this._time.hour, 2 ) + ':'
Expand Down Expand Up @@ -328,12 +333,18 @@ SELF.getPrecisionById = function( id ) {
/**
* @ignore
*
* @param {number} number
* @param {number|string} number
* @param {number} digits
* @return {string}
*/
function pad( number, digits ) {
number = String( Math.abs( number ) );
if( typeof number !== 'string' ) {
number = String( number );
}

// Strip sign characters.
number = number.replace( /^[-+]/, '' );

if ( number.length >= digits ) {
return number;
}
Expand Down
130 changes: 114 additions & 16 deletions tests/src/values/TimeValue.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,132 @@ define( [
},

/**
* Tests the effect of the private pad() function, relevant in getSortKey() and toJSON().
* Tests the effect of the private pad() function, relevant in toJSON() and getSortKey().
*
* @since 0.7
*
* @param {QUnit} assert
*/
testPad: function( assert ) {
var testCases = {
'-123456789012-00-00T00:00:00Z': '-123456789012-00-00T00:00:00Z',
'-12345678901-00-00T00:00:00Z': '-12345678901-00-00T00:00:00Z',
'-1-1-1T01:01:01Z': '-00000000001-01-01T01:01:01Z',
'1-1-1T01:01:01Z': '+00000000001-01-01T01:01:01Z',
'12-00-00T00:00:00Z': '+00000000012-00-00T00:00:00Z',
'1234567890-00-00T00:00:00Z': '+01234567890-00-00T00:00:00Z',
'12345678901-00-00T00:00:00Z': '+12345678901-00-00T00:00:00Z',
'123456789012-00-00T00:00:00Z': '+123456789012-00-00T00:00:00Z',
'1234567890123456-00-00T00:00:00Z': '+1234567890123456-00-00T00:00:00Z'
// Without leading zeros
'-9000000000000000-12-31T23:59:59Z': [
'-9000000000000000-12-31T23:59:59Z',
'-9000000000000000-12-31T23:59:59Z'
],
'-123456789012-00-00T00:00:00Z': [
'-123456789012-00-00T00:00:00Z',
'-0000123456789012-00-00T00:00:00Z'
],
'-12345678901-00-00T00:00:00Z': [
'-12345678901-00-00T00:00:00Z',
'-0000012345678901-00-00T00:00:00Z'
],
'-1234-1-1T01:01:01Z': [
'-1234-01-01T01:01:01Z',
'-0000000000001234-01-01T01:01:01Z'
],
'-123-1-1T01:01:01Z': [
'-0123-01-01T01:01:01Z',
'-0000000000000123-01-01T01:01:01Z'
],
'-12-1-1T01:01:01Z': [
'-0012-01-01T01:01:01Z',
'-0000000000000012-01-01T01:01:01Z'
],
'-1-1-1T01:01:01Z': [
'-0001-01-01T01:01:01Z',
'-0000000000000001-01-01T01:01:01Z'
],
'0-1-1T01:01:01Z': [
'+0000-01-01T01:01:01Z',
'+0000000000000000-01-01T01:01:01Z'
],
'1-1-1T01:01:01Z': [
'+0001-01-01T01:01:01Z',
'+0000000000000001-01-01T01:01:01Z'
],
'12-00-00T00:00:00Z': [
'+0012-00-00T00:00:00Z',
'+0000000000000012-00-00T00:00:00Z'
],
'123-00-00T00:00:00Z': [
'+0123-00-00T00:00:00Z',
'+0000000000000123-00-00T00:00:00Z'
],
'1234-00-00T00:00:00Z': [
'+1234-00-00T00:00:00Z',
'+0000000000001234-00-00T00:00:00Z'
],
'1234567890-00-00T00:00:00Z': [
'+1234567890-00-00T00:00:00Z',
'+0000001234567890-00-00T00:00:00Z'
],
'12345678901-00-00T00:00:00Z': [
'+12345678901-00-00T00:00:00Z',
'+0000012345678901-00-00T00:00:00Z'
],
'123456789012-00-00T00:00:00Z': [
'+123456789012-00-00T00:00:00Z',
'+0000123456789012-00-00T00:00:00Z'
],
'1234567890123456-00-00T00:00:00Z': [
'+1234567890123456-00-00T00:00:00Z',
'+1234567890123456-00-00T00:00:00Z'
],
'9000000000000000-12-31T23:59:59Z': [
'+9000000000000000-12-31T23:59:59Z',
'+9000000000000000-12-31T23:59:59Z'
],

// With leading zeros
'-0900000000000000-12-31T23:59:59Z': [
'-900000000000000-12-31T23:59:59Z',
'-0900000000000000-12-31T23:59:59Z'
],
'-0000000000000123-01-01T01:01:01Z': [
'-0123-01-01T01:01:01Z',
'-0000000000000123-01-01T01:01:01Z'
],
'+0000000000000000-01-01T01:01:01Z': [
'+0000-01-01T01:01:01Z',
'+0000000000000000-01-01T01:01:01Z'
],
'+0000000000000001-01-01T01:01:01Z': [
'+0001-01-01T01:01:01Z',
'+0000000000000001-01-01T01:01:01Z'
],
'+0900000000000000-12-31T23:59:59Z': [
'+900000000000000-12-31T23:59:59Z',
'+0900000000000000-12-31T23:59:59Z'
],

// Year would become 10000000000000000 when parsed as a number
'-9999999999999999-12-31T23:59:59Z': [
'-9999999999999999-12-31T23:59:59Z',
'-9999999999999999-12-31T23:59:59Z'
],
'9999999999999999-12-31T23:59:59Z': [
'+9999999999999999-12-31T23:59:59Z',
'+9999999999999999-12-31T23:59:59Z'
]
};

for( var iso8601 in testCases ) {
var expected = testCases[iso8601],
actual = new dv.TimeValue( iso8601 ).getSortKey();
for( var timestamp in testCases ) {
var timeValue = new dv.TimeValue( timestamp ),
json = timeValue.toJSON().time,
sortKey = timeValue.getSortKey(),
expectedJSON = testCases[timestamp][0],
expectedSortKey = testCases[timestamp][1];

assert.ok(
expected === actual,
'Expected getSortKey() to return "' + expected + '", got "' + actual + '"'
json === expectedJSON,
'Expected toJSON().time to return "' + expectedJSON + '", got "' + json + '"'
);
assert.ok(
sortKey === expectedSortKey,
'Expected getSortKey() to return "' + expectedSortKey + '", got "' + sortKey + '"'
);

}
}

Expand Down