@@ -18,6 +18,7 @@ describe('dates', function() {
18
18
19
19
describe ( 'dateTime2ms' , function ( ) {
20
20
it ( 'should accept valid date strings' , function ( ) {
21
+ var tzOffset ;
21
22
22
23
[
23
24
[ '2016' , new Date ( 2016 , 0 , 1 ) ] ,
@@ -34,10 +35,11 @@ describe('dates', function() {
34
35
// first century, also allow month, day, and hour to be 1-digit, and not all
35
36
// three digits of milliseconds
36
37
[ '0013-1-1 1:00:00.6' , d1c ] ,
37
- // we support more than 4 digits too, though Date objects don't. More than that
38
+ // we support tenths of msec too, though Date objects don't. Smaller than that
38
39
// and we hit the precision limit of js numbers unless we're close to the epoch.
39
40
// It won't break though.
40
41
[ '0013-1-1 1:00:00.6001' , + d1c + 0.1 ] ,
42
+ [ '0013-1-1 1:00:00.60011111111' , + d1c + 0.11111111 ] ,
41
43
42
44
// 2-digit years get mapped to now-70 -> now+29
43
45
[ thisYear_2 + '-05' , new Date ( thisYear , 4 , 1 ) ] ,
@@ -50,11 +52,16 @@ describe('dates', function() {
50
52
[ '2014-03-04 08:15:34+1200' , new Date ( 2014 , 2 , 4 , 8 , 15 , 34 ) ] ,
51
53
[ '2014-03-04 08:15:34.567-05:45' , new Date ( 2014 , 2 , 4 , 8 , 15 , 34 , 567 ) ] ,
52
54
] . forEach ( function ( v ) {
53
- expect ( Lib . dateTime2ms ( v [ 0 ] ) ) . toBe ( + v [ 1 ] , v [ 0 ] ) ;
55
+ // just for sub-millisecond precision tests, use timezoneoffset
56
+ // from the previous date object
57
+ if ( v [ 1 ] . getTimezoneOffset ) tzOffset = v [ 1 ] . getTimezoneOffset ( ) ;
58
+
59
+ var expected = + v [ 1 ] - ( tzOffset * 60000 ) ;
60
+ expect ( Lib . dateTime2ms ( v [ 0 ] ) ) . toBe ( expected , v [ 0 ] ) ;
54
61
55
62
// ISO-8601: all the same stuff with t or T as the separator
56
- expect ( Lib . dateTime2ms ( v [ 0 ] . trim ( ) . replace ( ' ' , 't' ) ) ) . toBe ( + v [ 1 ] , v [ 0 ] . trim ( ) . replace ( ' ' , 't' ) ) ;
57
- expect ( Lib . dateTime2ms ( '\r\n\t ' + v [ 0 ] . trim ( ) . replace ( ' ' , 'T' ) + '\r\n\t ' ) ) . toBe ( + v [ 1 ] , v [ 0 ] . trim ( ) . replace ( ' ' , 'T' ) ) ;
63
+ expect ( Lib . dateTime2ms ( v [ 0 ] . trim ( ) . replace ( ' ' , 't' ) ) ) . toBe ( expected , v [ 0 ] . trim ( ) . replace ( ' ' , 't' ) ) ;
64
+ expect ( Lib . dateTime2ms ( '\r\n\t ' + v [ 0 ] . trim ( ) . replace ( ' ' , 'T' ) + '\r\n\t ' ) ) . toBe ( expected , v [ 0 ] . trim ( ) . replace ( ' ' , 'T' ) ) ;
58
65
} ) ;
59
66
} ) ;
60
67
@@ -69,7 +76,7 @@ describe('dates', function() {
69
76
[
70
77
1000 , 9999 , - 1000 , - 9999
71
78
] . forEach ( function ( v ) {
72
- expect ( Lib . dateTime2ms ( v ) ) . toBe ( + ( new Date ( v , 0 , 1 ) ) , v ) ;
79
+ expect ( Lib . dateTime2ms ( v ) ) . toBe ( Date . UTC ( v , 0 , 1 ) , v ) ;
73
80
} ) ;
74
81
75
82
[
@@ -78,7 +85,7 @@ describe('dates', function() {
78
85
[ nowMinus70_2 , nowMinus70 ] ,
79
86
[ 99 , 1999 ]
80
87
] . forEach ( function ( v ) {
81
- expect ( Lib . dateTime2ms ( v [ 0 ] ) ) . toBe ( + ( new Date ( v [ 1 ] , 0 , 1 ) ) , v [ 0 ] ) ;
88
+ expect ( Lib . dateTime2ms ( v [ 0 ] ) ) . toBe ( Date . UTC ( v [ 1 ] , 0 , 1 ) , v [ 0 ] ) ;
82
89
} ) ;
83
90
} ) ;
84
91
@@ -93,7 +100,7 @@ describe('dates', function() {
93
100
d1c ,
94
101
new Date ( 2015 , 8 , 7 , 23 , 34 , 45 , 567 )
95
102
] . forEach ( function ( v ) {
96
- expect ( Lib . dateTime2ms ( v ) ) . toBe ( + v ) ;
103
+ expect ( Lib . dateTime2ms ( v ) ) . toBe ( + v - v . getTimezoneOffset ( ) * 60000 ) ;
97
104
} ) ;
98
105
} ) ;
99
106
@@ -124,6 +131,30 @@ describe('dates', function() {
124
131
expect ( Lib . dateTime2ms ( v ) ) . toBeUndefined ( v ) ;
125
132
} ) ;
126
133
} ) ;
134
+
135
+ var JULY1MS = 181 * 24 * 3600 * 1000 ;
136
+
137
+ it ( 'should use UTC with no timezone offset or daylight saving time' , function ( ) {
138
+ expect ( Lib . dateTime2ms ( '1970-01-01' ) ) . toBe ( 0 ) ;
139
+
140
+ // 181 days (and no DST hours) between jan 1 and july 1 in a non-leap-year
141
+ // 31 + 28 + 31 + 30 + 31 + 30
142
+ expect ( Lib . dateTime2ms ( '1970-07-01' ) ) . toBe ( JULY1MS ) ;
143
+ } ) ;
144
+
145
+ it ( 'should interpret JS dates by local time, not by its getTime()' , function ( ) {
146
+ // not really part of the test, just to make sure the test is meaningful
147
+ // the test should NOT be run in a UTC environment
148
+ expect ( [
149
+ Number ( new Date ( 1970 , 0 , 1 ) ) ,
150
+ Number ( new Date ( 1970 , 6 , 1 ) )
151
+ ] ) . not . toEqual ( [ 0 , JULY1MS ] ) ;
152
+
153
+ // now repeat the previous test and show that we throw away
154
+ // timezone info from js dates
155
+ expect ( Lib . dateTime2ms ( new Date ( 1970 , 0 , 1 ) ) ) . toBe ( 0 ) ;
156
+ expect ( Lib . dateTime2ms ( new Date ( 1970 , 6 , 1 ) ) ) . toBe ( JULY1MS ) ;
157
+ } ) ;
127
158
} ) ;
128
159
129
160
describe ( 'ms2DateTime' , function ( ) {
@@ -159,8 +190,8 @@ describe('dates', function() {
159
190
160
191
it ( 'should not accept Date objects beyond our limits or other objects' , function ( ) {
161
192
[
162
- + ( new Date ( 10000 , 0 , 1 ) ) ,
163
- + ( new Date ( - 10000 , 11 , 31 , 23 , 59 , 59 , 999 ) ) ,
193
+ Date . UTC ( 10000 , 0 , 1 ) ,
194
+ Date . UTC ( - 10000 , 11 , 31 , 23 , 59 , 59 , 999 ) ,
164
195
'' ,
165
196
'2016-01-01' ,
166
197
'0' ,
@@ -191,19 +222,20 @@ describe('dates', function() {
191
222
} ) ;
192
223
193
224
describe ( 'cleanDate' , function ( ) {
194
- it ( 'should convert any number or js Date within range to a date string ' , function ( ) {
225
+ it ( 'should convert numbers or js Dates to strings based on local TZ ' , function ( ) {
195
226
[
196
227
new Date ( 0 ) ,
197
228
new Date ( 2000 ) ,
198
229
new Date ( 2000 , 0 , 1 ) ,
199
230
new Date ( ) ,
200
- new Date ( - 9999 , 0 , 1 ) ,
201
- new Date ( 9999 , 11 , 31 , 23 , 59 , 59 , 999 )
231
+ new Date ( - 9999 , 0 , 3 ) , // we lose one day of range +/- tzoffset this way
232
+ new Date ( 9999 , 11 , 29 , 23 , 59 , 59 , 999 )
202
233
] . forEach ( function ( v ) {
203
- expect ( typeof Lib . ms2DateTime ( + v ) ) . toBe ( 'string' ) ;
204
- expect ( Lib . cleanDate ( v ) ) . toBe ( Lib . ms2DateTime ( + v ) ) ;
205
- expect ( Lib . cleanDate ( + v ) ) . toBe ( Lib . ms2DateTime ( + v ) ) ;
206
- expect ( Lib . cleanDate ( v , '2000-01-01' ) ) . toBe ( Lib . ms2DateTime ( + v ) ) ;
234
+ var expected = Lib . ms2DateTime ( Lib . dateTime2ms ( v ) ) ;
235
+ expect ( typeof expected ) . toBe ( 'string' ) ;
236
+ expect ( Lib . cleanDate ( v ) ) . toBe ( expected ) ;
237
+ expect ( Lib . cleanDate ( + v ) ) . toBe ( expected ) ;
238
+ expect ( Lib . cleanDate ( v , '2000-01-01' ) ) . toBe ( expected ) ;
207
239
} ) ;
208
240
} ) ;
209
241
0 commit comments