Skip to content

Commit 4837385

Browse files
committed
Merge pull request #73 from wmde/optionalTime
Optional hour:minute, second and timezone in TimeValue
2 parents cfdb5f5 + a6b13f9 commit 4837385

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

.jshintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"unused": "vars",
1717
"laxbreak": true,
1818
"laxcomma": false,
19+
"loopfunc": true,
1920
"onevar": false,
2021
"bitwise": false,
2122
"forin": false,

src/values/TimeValue.js

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

3131
try {
32-
var matches = /^([-+]?\d+)-(\d+)-(\d+)T(\d{2}):(\d{2}):(\d{2})Z$/.exec( timestamp );
32+
var matches = /^([-+]?\d+)-(\d+)-(\d+)T(?:(\d+):(\d+)(?::(\d+))?Z?)?$/.exec( timestamp );
3333

3434
// Strip additional leading zeros from the year, but keep 4 digits.
3535
this._time.year = matches[1].replace( /\b0+(?=\d{4})/, '' );
3636
this._time.month = parseInt( matches[2], 10 );
3737
this._time.day = parseInt( matches[3], 10 );
38-
this._time.hour = parseInt( matches[4], 10 );
39-
this._time.minute = parseInt( matches[5], 10 );
40-
this._time.second = parseInt( matches[6], 10 );
38+
this._time.hour = parseInt( matches[4] || 0, 10 );
39+
this._time.minute = parseInt( matches[5] || 0, 10 );
40+
this._time.second = parseInt( matches[6] || 0, 10 );
4141
} catch( e ) {
42-
throw new Error( 'Unable to process supposed timestamp string' );
42+
throw new Error( 'Unable to process timestamp "' + timestamp + '"' );
4343
}
4444

4545
this._options = {

tests/src/values/TimeValue.tests.js

Lines changed: 53 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,46 @@ 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+
testConstructorThrowsException: 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+
'1',
79+
'1942-04-01',
80+
'+0000000000002015-01-01 01:01:01Z',
81+
82+
// Unsupported time zones
83+
'+0000000000002015-01-01T01:01:01A',
84+
'+0000000000002015-01-01T01:01:01+0000',
85+
'+0000000000002015-01-01T01:01:01+00:00'
86+
];
87+
var i, invalidTimestamp;
88+
89+
for ( i = 0; i < invalidTimestamps.length; i++ ) {
90+
invalidTimestamp = invalidTimestamps[i];
91+
92+
assert.throws(
93+
function() {
94+
dv.TimeValue( invalidTimestamp );
95+
},
96+
'"' + invalidTimestamp + '" is not a valid TimeValue timestamp'
97+
);
98+
}
99+
},
100+
48101
/**
49102
* Tests if the equals method is able to return false.
50103
*

0 commit comments

Comments
 (0)