@@ -18,6 +18,7 @@ describe('dates', function() {
1818
1919 describe ( 'dateTime2ms' , function ( ) {
2020 it ( 'should accept valid date strings' , function ( ) {
21+ var tzOffset ;
2122
2223 [
2324 [ '2016' , new Date ( 2016 , 0 , 1 ) ] ,
@@ -34,10 +35,11 @@ describe('dates', function() {
3435 // first century, also allow month, day, and hour to be 1-digit, and not all
3536 // three digits of milliseconds
3637 [ '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
3839 // and we hit the precision limit of js numbers unless we're close to the epoch.
3940 // It won't break though.
4041 [ '0013-1-1 1:00:00.6001' , + d1c + 0.1 ] ,
42+ [ '0013-1-1 1:00:00.60011111111' , + d1c + 0.11111111 ] ,
4143
4244 // 2-digit years get mapped to now-70 -> now+29
4345 [ thisYear_2 + '-05' , new Date ( thisYear , 4 , 1 ) ] ,
@@ -50,11 +52,16 @@ describe('dates', function() {
5052 [ '2014-03-04 08:15:34+1200' , new Date ( 2014 , 2 , 4 , 8 , 15 , 34 ) ] ,
5153 [ '2014-03-04 08:15:34.567-05:45' , new Date ( 2014 , 2 , 4 , 8 , 15 , 34 , 567 ) ] ,
5254 ] . 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 ] ) ;
5461
5562 // 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' ) ) ;
5865 } ) ;
5966 } ) ;
6067
@@ -69,7 +76,7 @@ describe('dates', function() {
6976 [
7077 1000 , 9999 , - 1000 , - 9999
7178 ] . 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 ) ;
7380 } ) ;
7481
7582 [
@@ -78,7 +85,7 @@ describe('dates', function() {
7885 [ nowMinus70_2 , nowMinus70 ] ,
7986 [ 99 , 1999 ]
8087 ] . 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 ] ) ;
8289 } ) ;
8390 } ) ;
8491
@@ -93,7 +100,7 @@ describe('dates', function() {
93100 d1c ,
94101 new Date ( 2015 , 8 , 7 , 23 , 34 , 45 , 567 )
95102 ] . forEach ( function ( v ) {
96- expect ( Lib . dateTime2ms ( v ) ) . toBe ( + v ) ;
103+ expect ( Lib . dateTime2ms ( v ) ) . toBe ( + v - v . getTimezoneOffset ( ) * 60000 ) ;
97104 } ) ;
98105 } ) ;
99106
@@ -124,6 +131,30 @@ describe('dates', function() {
124131 expect ( Lib . dateTime2ms ( v ) ) . toBeUndefined ( v ) ;
125132 } ) ;
126133 } ) ;
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+ } ) ;
127158 } ) ;
128159
129160 describe ( 'ms2DateTime' , function ( ) {
@@ -159,8 +190,8 @@ describe('dates', function() {
159190
160191 it ( 'should not accept Date objects beyond our limits or other objects' , function ( ) {
161192 [
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 ) ,
164195 '' ,
165196 '2016-01-01' ,
166197 '0' ,
@@ -191,19 +222,20 @@ describe('dates', function() {
191222 } ) ;
192223
193224 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 ( ) {
195226 [
196227 new Date ( 0 ) ,
197228 new Date ( 2000 ) ,
198229 new Date ( 2000 , 0 , 1 ) ,
199230 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 )
202233 ] . 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 ) ;
207239 } ) ;
208240 } ) ;
209241
0 commit comments