Skip to content

Optional hour:minute, second and timezone in TimeValue #73

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
Jun 1, 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
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"unused": "vars",
"laxbreak": true,
"laxcomma": false,
"loopfunc": true,
"onevar": false,
"bitwise": false,
"forin": false,
Expand Down
10 changes: 5 additions & 5 deletions src/values/TimeValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ 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 );
var matches = /^([-+]?\d+)-(\d+)-(\d+)T(?:(\d+):(\d+)(?::(\d+))?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 );
this._time.minute = parseInt( matches[5], 10 );
this._time.second = parseInt( matches[6], 10 );
this._time.hour = parseInt( matches[4] || 0, 10 );
this._time.minute = parseInt( matches[5] || 0, 10 );
this._time.second = parseInt( matches[6] || 0, 10 );
} catch( e ) {
throw new Error( 'Unable to process supposed timestamp string' );
throw new Error( 'Unable to process timestamp "' + timestamp + '"' );
}

this._options = {
Expand Down
53 changes: 53 additions & 0 deletions tests/src/values/TimeValue.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ define( [
getConstructorArguments: function() {
return [
['+0000000000001942-04-01T00:00:00Z'],

// Optional parts
['+0000000000001942-04-01T00:00:00'],
['+0000000000001942-04-01T00:00'],
['+0000000000001942-04-01T'],
['0000000000001942-04-01T'],
['1942-04-01T'],

// Minimal and maximal length
['+1-1-1T1:1:1Z'],
['+9999999999999999-12-31T23:59:59Z'],

// Options
['+0000000000001400-01-01T00:00:00Z', {
calendarModel: 'http://www.wikidata.org/entity/Q1985786'
} ],
Expand All @@ -45,6 +58,46 @@ define( [
];
},

/**
* Tests if the constructor fails as expected for invalid and unsupported timestamp values.
*
* @since 0.7
*
* @param {QUnit} assert
*/
testConstructorThrowsException: function( assert ) {
var invalidTimestamps = [
// Non-strings
undefined,
null,
1,
0.1,

// The "T" is required
'',
'1',
'1942-04-01',
'+0000000000002015-01-01 01:01:01Z',

// Unsupported time zones
'+0000000000002015-01-01T01:01:01A',
'+0000000000002015-01-01T01:01:01+0000',
'+0000000000002015-01-01T01:01:01+00:00'
];
var i, invalidTimestamp;

for ( i = 0; i < invalidTimestamps.length; i++ ) {
invalidTimestamp = invalidTimestamps[i];

assert.throws(
function() {
dv.TimeValue( invalidTimestamp );
},
'"' + invalidTimestamp + '" is not a valid TimeValue timestamp'
);
}
},

/**
* Tests if the equals method is able to return false.
*
Expand Down