@@ -8,21 +8,19 @@ import timeFormat, { QlikTimeToDate } from '../timeFormat';
8
8
* which means if you're specifying a date 2013-12-31 59:59:59 and the last millisecond,
9
9
* you *might* end up in 2014.
10
10
*/
11
+ const QT = QlikTimeToDate ( 0 ) . getTime ( ) ;
11
12
function DateToQlikTime ( value ) {
12
- let offset = QlikTimeToDate ( 0 ) ;
13
-
14
- const off2 = ( ( offset . getTimezoneOffset ( ) * 60 * 1000 ) - ( value . getTimezoneOffset ( ) * 60 * 1000 ) ) + ( offset . getTimezoneOffset ( ) * 60 * 1000 ) ;
15
-
16
- offset = offset . setTime ( offset . getTime ( ) + ( value . getTimezoneOffset ( ) * 60 * 1000 ) ) ;
17
-
18
- return (
19
- ( ( value . getTime ( ) - offset ) + off2 + 0.5 ) / ( 60 * 60 * 24 )
20
- ) / 1000 ;
13
+ return ( value . getTime ( ) - QT ) / ( 60 * 60 * 24 ) / 1000 ;
21
14
}
22
15
23
16
describe ( 'qlik timeFormat' , ( ) => {
24
17
let n ;
25
18
19
+ it ( 'should create valid qlik date -> JS Date' , ( ) => {
20
+ let d = QlikTimeToDate ( 0 ) ;
21
+ expect ( d . toISOString ( ) ) . to . equal ( '1899-12-30T00:00:00.000Z' ) ;
22
+ } ) ;
23
+
26
24
describe ( 'Basic' , ( ) => {
27
25
it ( 'should format dates correctly according to constructor pattern' , ( ) => {
28
26
const formatter = timeFormat ( 'MMMM DD, YYYY' ) ;
@@ -89,7 +87,7 @@ describe('qlik timeFormat', () => {
89
87
describe ( 'Years' , ( ) => {
90
88
it ( 'should format year correctly' , ( ) => {
91
89
n = timeFormat ( ) ;
92
- let d = DateToQlikTime ( new Date ( 2014 , 3 , 24 , 13 , 55 , 40 , 100 ) ) ; // thursday 24th april 2014 @ 13:55:40:100
90
+ let d = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 3 , 24 , 13 , 55 , 40 , 100 ) ) ) ; // thursday 24th april 2014 @ 13:55:40:100
93
91
94
92
expect ( n . format ( 'Y' , d ) ) . to . equal ( '14' ) ;
95
93
expect ( n . format ( 'y' , d ) ) . to . equal ( '14' ) ;
@@ -102,7 +100,7 @@ describe('qlik timeFormat', () => {
102
100
expect ( n . format ( 'YYYYY' , d ) ) . to . equal ( '02014' ) ;
103
101
expect ( n . format ( 'YYYYYY YY' , d ) ) . to . equal ( '002014 14' ) ;
104
102
105
- d = DateToQlikTime ( new Date ( 123401 , 11 , 1 , 13 , 55 , 40 , 100 ) ) ;
103
+ d = DateToQlikTime ( new Date ( Date . UTC ( 123401 , 11 , 1 , 13 , 55 , 40 , 100 ) ) ) ;
106
104
expect ( n . format ( 'Y' , d ) ) . to . equal ( '1' ) ;
107
105
expect ( n . format ( 'YY' , d ) ) . to . equal ( '01' ) ;
108
106
expect ( n . format ( 'YYY' , d ) ) . to . equal ( '401' ) ;
@@ -125,18 +123,18 @@ describe('qlik timeFormat', () => {
125
123
_11 ;
126
124
beforeEach ( ( ) => {
127
125
n = timeFormat ( ) ;
128
- _0 = DateToQlikTime ( new Date ( 2014 , 0 , 1 ) ) ;
129
- _1 = DateToQlikTime ( new Date ( 2014 , 1 , 1 ) ) ;
130
- _2 = DateToQlikTime ( new Date ( 2014 , 2 , 1 ) ) ;
131
- _3 = DateToQlikTime ( new Date ( 2014 , 3 , 1 ) ) ;
132
- _4 = DateToQlikTime ( new Date ( 2014 , 4 , 1 ) ) ;
133
- _5 = DateToQlikTime ( new Date ( 2014 , 5 , 1 ) ) ;
134
- _6 = DateToQlikTime ( new Date ( 2014 , 6 , 1 ) ) ;
135
- _7 = DateToQlikTime ( new Date ( 2014 , 7 , 1 ) ) ;
136
- _8 = DateToQlikTime ( new Date ( 2014 , 8 , 1 ) ) ;
137
- _9 = DateToQlikTime ( new Date ( 2014 , 9 , 1 ) ) ;
138
- _10 = DateToQlikTime ( new Date ( 2014 , 10 , 1 ) ) ;
139
- _11 = DateToQlikTime ( new Date ( 2014 , 11 , 1 ) ) ;
126
+ _0 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 1 ) ) ) ;
127
+ _1 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 1 , 1 ) ) ) ;
128
+ _2 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 2 , 1 ) ) ) ;
129
+ _3 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 3 , 1 ) ) ) ;
130
+ _4 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 4 , 1 ) ) ) ;
131
+ _5 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 5 , 1 ) ) ) ;
132
+ _6 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 6 , 1 ) ) ) ;
133
+ _7 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 7 , 1 ) ) ) ;
134
+ _8 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 8 , 1 ) ) ) ;
135
+ _9 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 9 , 1 ) ) ) ;
136
+ _10 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 10 , 1 ) ) ) ;
137
+ _11 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 11 , 1 ) ) ) ;
140
138
} ) ;
141
139
142
140
it ( 'as number' , ( ) => {
@@ -214,13 +212,13 @@ describe('qlik timeFormat', () => {
214
212
sun ;
215
213
beforeEach ( ( ) => {
216
214
n = timeFormat ( null ) ;
217
- mon = DateToQlikTime ( new Date ( 2014 , 0 , 6 ) ) ;
218
- tue = DateToQlikTime ( new Date ( 2014 , 0 , 7 ) ) ;
219
- wed = DateToQlikTime ( new Date ( 2014 , 0 , 8 ) ) ;
220
- thu = DateToQlikTime ( new Date ( 2014 , 0 , 9 ) ) ;
221
- fri = DateToQlikTime ( new Date ( 2014 , 0 , 10 ) ) ;
222
- sat = DateToQlikTime ( new Date ( 2014 , 0 , 11 ) ) ;
223
- sun = DateToQlikTime ( new Date ( 2014 , 0 , 12 ) ) ;
215
+ mon = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 6 ) ) ) ;
216
+ tue = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 7 ) ) ) ;
217
+ wed = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 8 ) ) ) ;
218
+ thu = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 9 ) ) ) ;
219
+ fri = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 10 ) ) ) ;
220
+ sat = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 11 ) ) ) ;
221
+ sun = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 12 ) ) ) ;
224
222
} ) ;
225
223
226
224
it ( 'as number' , ( ) => {
@@ -318,9 +316,9 @@ describe('qlik timeFormat', () => {
318
316
_31 ;
319
317
beforeEach ( ( ) => {
320
318
n = timeFormat ( null ) ;
321
- _1 = DateToQlikTime ( new Date ( 2014 , 0 , 1 ) ) ;
322
- _15 = DateToQlikTime ( new Date ( 2014 , 0 , 15 ) ) ;
323
- _31 = DateToQlikTime ( new Date ( 2014 , 0 , 31 ) ) ;
319
+ _1 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 1 ) ) ) ;
320
+ _15 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 15 ) ) ) ;
321
+ _31 = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 31 ) ) ) ;
324
322
} ) ;
325
323
326
324
it ( 'as number' , ( ) => {
@@ -353,7 +351,7 @@ describe('qlik timeFormat', () => {
353
351
describe ( 'Combinations' , ( ) => {
354
352
it ( 'should support combinations of year, month, weekday and day' , ( ) => {
355
353
n = timeFormat ( null ) ;
356
- const d = DateToQlikTime ( new Date ( 2014 , 3 , 24 , 13 , 55 , 40 , 100 ) ) ; // thursday 24th april 2014 @ 13:55:40:100
354
+ const d = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 3 , 24 , 13 , 55 , 40 , 100 ) ) ) ; // thursday 24th april 2014 @ 13:55:40:100
357
355
358
356
expect ( n . format ( 'YYYY-MM-DD' , d ) ) . to . equal ( '2014-04-24' ) ;
359
357
expect ( n . format ( 'DD/MM -YY' , d ) ) . to . equal ( '24/04 -14' ) ;
@@ -448,19 +446,19 @@ describe('qlik timeFormat', () => {
448
446
449
447
describe ( 'Fractions' , ( ) => {
450
448
it ( ' ' , ( ) => {
451
- const d = DateToQlikTime ( new Date ( 2014 , 0 , 1 , 0 , 0 , 0 , 456 ) ) ;
449
+ const d = DateToQlikTime ( new Date ( Date . UTC ( 2014 , 0 , 1 , 0 , 0 , 0 , 452 ) ) ) ;
452
450
expect ( n . format ( 'f F' , d ) ) . to . equal ( '4 4' ) ; // tenths of a second
453
451
expect ( n . format ( 'ff' , d ) ) . to . equal ( '45' ) ; // hundreths of a second
454
- expect ( n . format ( 'fff FFF' , d ) ) . to . equal ( '456 456 ' ) ; // tousandths of a second
455
- expect ( n . format ( 'ffff' , d ) ) . to . equal ( '4560 ' ) ; // ten thousandths of a second
456
- expect ( n . format ( 'fffff' , d ) ) . to . equal ( '45600 ' ) ; // hundred thousandths of a second
457
- expect ( n . format ( 'ffffff' , d ) ) . to . equal ( '456000 ' ) ; // millionths of a second
452
+ expect ( n . format ( 'fff FFF' , d ) ) . to . equal ( '452 452 ' ) ; // tousandths of a second
453
+ expect ( n . format ( 'ffff' , d ) ) . to . equal ( '4520 ' ) ; // ten thousandths of a second
454
+ expect ( n . format ( 'fffff' , d ) ) . to . equal ( '45200 ' ) ; // hundred thousandths of a second
455
+ expect ( n . format ( 'ffffff' , d ) ) . to . equal ( '452000 ' ) ; // millionths of a second
458
456
} ) ;
459
457
} ) ;
460
458
461
459
it ( 'should support combinations of hours, minutes, seconds and fractions' , ( ) => {
462
460
const d = 41753.58033550347 ;
463
- // let d = DateToQlikTime(new Date(2014, 3, 24, 13, 55, 40, 987));
461
+ // let d = DateToQlikTime(new Date(Date.UTC( 2014, 3, 24, 13, 55, 40, 987));
464
462
465
463
expect ( n . format ( 'h:m:s' , midnight ) ) . to . equal ( '0:0:0' ) ;
466
464
expect ( n . format ( 'hh:mm:ss' , midnight ) ) . to . equal ( '00:00:00' ) ;
@@ -471,7 +469,7 @@ describe('qlik timeFormat', () => {
471
469
expect ( n . format ( 'h:m:s tt' , noon59 ) ) . to . equal ( '12:59:0 pm' ) ;
472
470
expect ( n . format ( 'h:m:s[.fff]][' , noon59 ) ) . to . equal ( '12:59:0' ) ;
473
471
474
- // expect( n.format( new Date( 2014, 0, 1, 13, 55, 30, 123 ), 'h:m:s[.ffff]' ) ).to.equal( '13:55.30.1230' ); // TODO?
472
+ // expect( n.format( new Date(Date.UTC( 2014, 0, 1, 13, 55, 30, 123 ), 'h:m:s[.ffff]' ) ).to.equal( '13:55.30.1230' ); // TODO?
475
473
476
474
expect ( n . format ( 'YYYY-MM-DD hh:mm:ss.ffff' , d ) ) . to . equal ( '2014-04-24 13:55:40.9870' ) ;
477
475
expect ( n . format ( 'DD/MM -YY h:m:s.fff TT' , d ) ) . to . equal ( '24/04 -14 1:55:40.987 PM' ) ;
0 commit comments