Skip to content

Commit 619888b

Browse files
committed
Optional hour:minute, second and timezone in TimeValue
1 parent e8093d0 commit 619888b

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

src/values/TimeValue.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ var SELF = dv.TimeValue = util.inherit( 'DvTimeValue', PARENT, function( timesta
2828
this._time = {};
2929

3030
try {
31-
var matches = /^([+-]?\d+)-(\d+)-(\d+)T(\d{2}):(\d{2}):(\d{2})Z$/.exec( timestamp );
31+
var matches = /^([+-]?\d+)-(\d+)-(\d+)T(?:(\d+):(\d+)(?::(\d+))?Z?)?$/.exec( timestamp );
3232
this._time.year = parseInt( matches[1], 10 );
3333
this._time.month = parseInt( matches[2], 10 );
3434
this._time.day = parseInt( matches[3], 10 );
35-
this._time.hour = parseInt( matches[4], 10 );
36-
this._time.minute = parseInt( matches[5], 10 );
37-
this._time.second = parseInt( matches[6], 10 );
35+
this._time.hour = parseInt( matches[4] || 0, 10 );
36+
this._time.minute = parseInt( matches[5] || 0, 10 );
37+
this._time.second = parseInt( matches[6] || 0, 10 );
3838
} catch( e ) {
39-
throw new Error( 'Unable to process supposed timestamp string' );
39+
throw new Error( 'Unable to process timestamp "' + timestamp + '"' );
4040
}
4141

4242
this._options = {

tests/src/values/TimeValue.tests.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ define( [
3636
getConstructorArguments: function() {
3737
return [
3838
['+0000000000001942-04-01T00:00:00Z'],
39+
40+
// Optional parts
41+
['+0000000000001942-04-01T00:00:00'],
42+
['+0000000000001942-04-01T00:00'],
43+
['+0000000000001942-04-01T'],
44+
['0000000000001942-04-01T'],
45+
['1942-04-01T'],
46+
47+
// Minimal and maximal length
48+
['+1-1-1T1:1:1Z'],
49+
['+9999999999999999-12-31T23:59:59Z'],
50+
51+
// Options
3952
['+0000000000001400-01-01T00:00:00Z', {
4053
calendarModel: 'http://www.wikidata.org/entity/Q1985786'
4154
} ],
@@ -45,6 +58,44 @@ define( [
4558
];
4659
},
4760

61+
/**
62+
* Tests if the constructor fails as expected for invalid and unsupported timestamp values.
63+
*
64+
* @since 0.7
65+
*
66+
* @param {QUnit} assert
67+
*/
68+
testGivenInvalidTimestamp_ConstructorThrowsException: function( assert ) {
69+
var invalidTimestamps = [
70+
// Non-strings
71+
undefined,
72+
null,
73+
1,
74+
0.1,
75+
76+
// The "T" is required
77+
'',
78+
'1942-04-01',
79+
'+0000000000002015-01-01 01:01:01Z',
80+
81+
// Unsupported time zones
82+
'+0000000000002015-01-01T01:01:01A',
83+
'+0000000000002015-01-01T01:01:01+0000',
84+
'+0000000000002015-01-01T01:01:01+00:00'
85+
];
86+
87+
for ( var i = 0; i < invalidTimestamps.length; i++ ) {
88+
var invalidTimestamp = invalidTimestamps[i];
89+
90+
assert.throws(
91+
function() {
92+
new dv.TimeValue( invalidTimestamp );
93+
},
94+
'"' + invalidTimestamp + '" is not a valid TimeValue timestamp'
95+
);
96+
}
97+
},
98+
4899
/**
49100
* Tests if the equals method is able to return false.
50101
*

0 commit comments

Comments
 (0)