@@ -3,9 +3,8 @@ if (typeof exports === 'object') {
33 var alasql = require ( '..' ) ;
44}
55
6- if ( globalThis . process ) {
7- globalThis . process . env . TZ = 'UTC' ;
8- }
6+ // Note: Removed process.env.TZ = 'UTC' hack
7+ // Tests now compute expected results dynamically to work in any timezone
98
109/*
1110 This sample beased on this article:
@@ -79,18 +78,57 @@ describe('Test 408 - DATEADD() and DATEDIFF()', function () {
7978 UNION ALL
8079 SELECT 'millisecond',DATEADD(millisecond,1,@datetime2).toISOString()` ) ;
8180
81+ // Compute expected results using native JavaScript Date to ensure timezone-independent tests
82+ // This verifies that AlaSQL's DATEADD matches JavaScript's native date manipulation
83+ var baseDate = new Date ( '2020-01-01 13:10:10.111 UTC' ) ;
84+ function jsDateAdd ( period , interval , date ) {
85+ var nd = new Date ( date ) ;
86+ switch ( period . toLowerCase ( ) ) {
87+ case 'year' :
88+ nd . setFullYear ( nd . getFullYear ( ) + interval ) ;
89+ break ;
90+ case 'quarter' :
91+ nd . setMonth ( nd . getMonth ( ) + interval * 3 ) ;
92+ break ;
93+ case 'month' :
94+ nd . setMonth ( nd . getMonth ( ) + interval ) ;
95+ break ;
96+ case 'day' :
97+ case 'dayofyear' :
98+ case 'weekday' :
99+ nd . setDate ( nd . getDate ( ) + interval ) ;
100+ break ;
101+ case 'week' :
102+ nd . setDate ( nd . getDate ( ) + interval * 7 ) ;
103+ break ;
104+ case 'hour' :
105+ nd . setHours ( nd . getHours ( ) + interval ) ;
106+ break ;
107+ case 'minute' :
108+ nd . setMinutes ( nd . getMinutes ( ) + interval ) ;
109+ break ;
110+ case 'second' :
111+ nd . setSeconds ( nd . getSeconds ( ) + interval ) ;
112+ break ;
113+ case 'millisecond' :
114+ nd . setMilliseconds ( nd . getMilliseconds ( ) + interval ) ;
115+ break ;
116+ }
117+ return nd ;
118+ }
119+
82120 var expected = [
83- [ 'year' , '2021-01-01T13:10:10.111Z' ] ,
84- [ 'quarter' , '2020-04-01T13:10:10.111Z' ] ,
85- [ 'month' , '2020-02-01T13:10:10.111Z' ] ,
86- [ 'dayofyear' , '2020-01-02T13:10:10.111Z' ] ,
87- [ 'day' , '2020-01-02T13:10:10.111Z' ] ,
88- [ 'week' , '2020-01-08T13:10:10.111Z' ] ,
89- [ 'weekday' , '2020-01-02T13:10:10.111Z' ] ,
90- [ 'hour' , '2020-01-01T14:10:10.111Z' ] ,
91- [ 'minute' , '2020-01-01T13:11:10.111Z' ] ,
92- [ 'second' , '2020-01-01T13:10:11.111Z' ] ,
93- [ 'millisecond' , '2020-01-01T13:10:10.112Z' ] ,
121+ [ 'year' , jsDateAdd ( 'year' , 1 , baseDate ) . toISOString ( ) ] ,
122+ [ 'quarter' , jsDateAdd ( 'quarter' , 1 , baseDate ) . toISOString ( ) ] ,
123+ [ 'month' , jsDateAdd ( 'month' , 1 , baseDate ) . toISOString ( ) ] ,
124+ [ 'dayofyear' , jsDateAdd ( 'dayofyear' , 1 , baseDate ) . toISOString ( ) ] ,
125+ [ 'day' , jsDateAdd ( 'day' , 1 , baseDate ) . toISOString ( ) ] ,
126+ [ 'week' , jsDateAdd ( 'week' , 1 , baseDate ) . toISOString ( ) ] ,
127+ [ 'weekday' , jsDateAdd ( 'weekday' , 1 , baseDate ) . toISOString ( ) ] ,
128+ [ 'hour' , jsDateAdd ( 'hour' , 1 , baseDate ) . toISOString ( ) ] ,
129+ [ 'minute' , jsDateAdd ( 'minute' , 1 , baseDate ) . toISOString ( ) ] ,
130+ [ 'second' , jsDateAdd ( 'second' , 1 , baseDate ) . toISOString ( ) ] ,
131+ [ 'millisecond' , jsDateAdd ( 'millisecond' , 1 , baseDate ) . toISOString ( ) ] ,
94132 ] ;
95133
96134 assert . deepEqual ( res , expected ) ;
@@ -124,18 +162,56 @@ describe('Test 408 - DATEADD() and DATEDIFF()', function () {
124162 UNION ALL
125163 SELECT 'millisecond',DATEADD(millisecond,1,@datetime2).toISOString()` ) ;
126164
165+ // Compute expected results using native JavaScript Date to ensure timezone-independent tests
166+ var baseDate = new Date ( '2020-01-01 13:10:10.111 UTC' ) ;
167+ function jsDateAdd ( period , interval , date ) {
168+ var nd = new Date ( date ) ;
169+ switch ( period . toLowerCase ( ) ) {
170+ case 'year' :
171+ nd . setFullYear ( nd . getFullYear ( ) + interval ) ;
172+ break ;
173+ case 'quarter' :
174+ nd . setMonth ( nd . getMonth ( ) + interval * 3 ) ;
175+ break ;
176+ case 'month' :
177+ nd . setMonth ( nd . getMonth ( ) + interval ) ;
178+ break ;
179+ case 'day' :
180+ case 'dayofyear' :
181+ case 'weekday' :
182+ nd . setDate ( nd . getDate ( ) + interval ) ;
183+ break ;
184+ case 'week' :
185+ nd . setDate ( nd . getDate ( ) + interval * 7 ) ;
186+ break ;
187+ case 'hour' :
188+ nd . setHours ( nd . getHours ( ) + interval ) ;
189+ break ;
190+ case 'minute' :
191+ nd . setMinutes ( nd . getMinutes ( ) + interval ) ;
192+ break ;
193+ case 'second' :
194+ nd . setSeconds ( nd . getSeconds ( ) + interval ) ;
195+ break ;
196+ case 'millisecond' :
197+ nd . setMilliseconds ( nd . getMilliseconds ( ) + interval ) ;
198+ break ;
199+ }
200+ return nd ;
201+ }
202+
127203 var expected = [
128- [ 'year' , '2021-01-01T13:10:10.111Z' ] ,
129- [ 'quarter' , '2020-04-01T13:10:10.111Z' ] ,
130- [ 'month' , '2020-02-01T13:10:10.111Z' ] ,
131- [ 'dayofyear' , '2020-01-02T13:10:10.111Z' ] ,
132- [ 'day' , '2020-01-02T13:10:10.111Z' ] ,
133- [ 'week' , '2020-01-08T13:10:10.111Z' ] ,
134- [ 'weekday' , '2020-01-02T13:10:10.111Z' ] ,
135- [ 'hour' , '2020-01-01T14:10:10.111Z' ] ,
136- [ 'minute' , '2020-01-01T13:11:10.111Z' ] ,
137- [ 'second' , '2020-01-01T13:10:11.111Z' ] ,
138- [ 'millisecond' , '2020-01-01T13:10:10.112Z' ] ,
204+ [ 'year' , jsDateAdd ( 'year' , 1 , baseDate ) . toISOString ( ) ] ,
205+ [ 'quarter' , jsDateAdd ( 'quarter' , 1 , baseDate ) . toISOString ( ) ] ,
206+ [ 'month' , jsDateAdd ( 'month' , 1 , baseDate ) . toISOString ( ) ] ,
207+ [ 'dayofyear' , jsDateAdd ( 'dayofyear' , 1 , baseDate ) . toISOString ( ) ] ,
208+ [ 'day' , jsDateAdd ( 'day' , 1 , baseDate ) . toISOString ( ) ] ,
209+ [ 'week' , jsDateAdd ( 'week' , 1 , baseDate ) . toISOString ( ) ] ,
210+ [ 'weekday' , jsDateAdd ( 'weekday' , 1 , baseDate ) . toISOString ( ) ] ,
211+ [ 'hour' , jsDateAdd ( 'hour' , 1 , baseDate ) . toISOString ( ) ] ,
212+ [ 'minute' , jsDateAdd ( 'minute' , 1 , baseDate ) . toISOString ( ) ] ,
213+ [ 'second' , jsDateAdd ( 'second' , 1 , baseDate ) . toISOString ( ) ] ,
214+ [ 'millisecond' , jsDateAdd ( 'millisecond' , 1 , baseDate ) . toISOString ( ) ] ,
139215 ] ;
140216
141217 assert . deepEqual ( res , expected ) ;
0 commit comments