3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing date and time functions.
14 # $Id: date.test,v 1.34 2009/04/16 12:58:03 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Do not use a codec for tests in this file, as the database file is
20 # manipulated directly using tcl scripts (using the [hexio_write] command).
24 # Skip this whole file if date and time functions are omitted
27 ifcapable {!datetime} {
32 proc datetest {tnum expr result} {
33 do_test date-$tnum [subst {
34 execsql "SELECT coalesce($expr,'NULL')"
38 datetest 1.1 julianday('2000-01-01') 2451544.5
39 datetest 1.2 julianday('1970-01-01') 2440587.5
40 datetest 1.3 julianday('1910-04-20') 2418781.5
41 datetest 1.4 julianday('1986-02-09') 2446470.5
42 datetest 1.5 julianday('12:00:00') 2451545.0
43 datetest 1.6 {julianday('2000-01-01 12:00:00')} 2451545.0
44 datetest 1.7 {julianday('2000-01-01 12:00')} 2451545.0
45 datetest 1.8 julianday('bogus') NULL
46 datetest 1.9 julianday('1999-12-31') 2451543.5
47 datetest 1.10 julianday('1999-12-32') NULL
48 datetest 1.11 julianday('1999-13-01') NULL
49 datetest 1.12 julianday('2003-02-31') 2452701.5
50 datetest 1.13 julianday('2003-03-03') 2452701.5
51 datetest 1.14 julianday('+2000-01-01') NULL
52 datetest 1.15 julianday('200-01-01') NULL
53 datetest 1.16 julianday('2000-1-01') NULL
54 datetest 1.17 julianday('2000-01-1') NULL
55 datetest 1.18.1 {julianday('2000-01-01 12:00:00')} 2451545.0
56 datetest 1.18.2 {julianday('2000-01-01T12:00:00')} 2451545.0
57 datetest 1.18.3 {julianday('2000-01-01 T12:00:00')} 2451545.0
58 datetest 1.18.4 {julianday('2000-01-01T 12:00:00')} 2451545.0
59 datetest 1.18.4 {julianday('2000-01-01 T 12:00:00')} 2451545.0
60 datetest 1.19 {julianday('2000-01-01 12:00:00.1')} 2451545.00000116
61 datetest 1.20 {julianday('2000-01-01 12:00:00.01')} 2451545.00000012
62 datetest 1.21 {julianday('2000-01-01 12:00:00.001')} 2451545.00000001
63 datetest 1.22 {julianday('2000-01-01 12:00:00.')} NULL
64 datetest 1.23 julianday(12345.6) 12345.6
65 datetest 1.23b julianday(1721059.5) 1721059.5
66 datetest 1.24 {julianday('2001-01-01 12:00:00 bogus')} NULL
67 datetest 1.25 {julianday('2001-01-01 bogus')} NULL
68 datetest 1.26 {julianday('2001-01-01 12:60:00')} NULL
69 datetest 1.27 {julianday('2001-01-01 12:59:60')} NULL
70 datetest 1.28 {julianday('2001-00-01')} NULL
71 datetest 1.29 {julianday('2001-01-00')} NULL
73 datetest 2.1 datetime(0,'unixepoch') {1970-01-01 00:00:00}
74 datetest 2.1b datetime(0,'unixepoc') NULL
75 datetest 2.1c datetime(0,'unixepochx') NULL
76 datetest 2.1d datetime('2003-10-22','unixepoch') NULL
77 datetest 2.2 datetime(946684800,'unixepoch') {2000-01-01 00:00:00}
78 datetest 2.2b datetime('946684800','unixepoch') {2000-01-01 00:00:00}
79 for {set i 0} {$i<1000} {incr i} {
80 set sql [format {strftime('%%H:%%M:%%f',1237962480.%03d,'unixepoch')} $i]
81 set res [format {06:28:00.%03d} $i]
82 datetest 2.2c-$i $sql $res
84 datetest 2.3 {date('2003-10-22','weekday 0')} 2003-10-26
85 datetest 2.4 {date('2003-10-22','weekday 1')} 2003-10-27
86 datetest 2.4a {date('2003-10-22','weekday 1')} 2003-10-27
87 datetest 2.4b {date('2003-10-22','weekday 1x')} NULL
88 datetest 2.4c {date('2003-10-22','weekday -1')} NULL
89 datetest 2.4d {date('2003-10-22','weakday 1x')} NULL
90 datetest 2.4e {date('2003-10-22','weekday ')} NULL
91 datetest 2.5 {date('2003-10-22','weekday 2')} 2003-10-28
92 datetest 2.6 {date('2003-10-22','weekday 3')} 2003-10-22
93 datetest 2.7 {date('2003-10-22','weekday 4')} 2003-10-23
94 datetest 2.8 {date('2003-10-22','weekday 5')} 2003-10-24
95 datetest 2.9 {date('2003-10-22','weekday 6')} 2003-10-25
96 datetest 2.10 {date('2003-10-22','weekday 7')} NULL
97 datetest 2.11 {date('2003-10-22','weekday 5.5')} NULL
98 datetest 2.12 {datetime('2003-10-22 12:34','weekday 0')} {2003-10-26 12:34:00}
99 datetest 2.13 {datetime('2003-10-22 12:34','start of month')} \
100 {2003-10-01 00:00:00}
101 datetest 2.14 {datetime('2003-10-22 12:34','start of year')} \
102 {2003-01-01 00:00:00}
103 datetest 2.15 {datetime('2003-10-22 12:34','start of day')} \
104 {2003-10-22 00:00:00}
105 datetest 2.15a {datetime('2003-10-22 12:34','start of')} NULL
106 datetest 2.15b {datetime('2003-10-22 12:34','start of bogus')} NULL
107 datetest 2.16 time('12:34:56.43') 12:34:56
108 datetest 2.17 {datetime('2003-10-22 12:34','1 day')} {2003-10-23 12:34:00}
109 datetest 2.18 {datetime('2003-10-22 12:34','+1 day')} {2003-10-23 12:34:00}
110 datetest 2.19 {datetime('2003-10-22 12:34','+1.25 day')} {2003-10-23 18:34:00}
111 datetest 2.20 {datetime('2003-10-22 12:34','-1.0 day')} {2003-10-21 12:34:00}
112 datetest 2.21 {datetime('2003-10-22 12:34','1 month')} {2003-11-22 12:34:00}
113 datetest 2.22 {datetime('2003-10-22 12:34','11 month')} {2004-09-22 12:34:00}
114 datetest 2.23 {datetime('2003-10-22 12:34','-13 month')} {2002-09-22 12:34:00}
115 datetest 2.24 {datetime('2003-10-22 12:34','1.5 months')} {2003-12-07 12:34:00}
116 datetest 2.25 {datetime('2003-10-22 12:34','-5 years')} {1998-10-22 12:34:00}
117 datetest 2.26 {datetime('2003-10-22 12:34','+10.5 minutes')} \
118 {2003-10-22 12:44:30}
119 datetest 2.27 {datetime('2003-10-22 12:34','-1.25 hours')} \
120 {2003-10-22 11:19:00}
121 datetest 2.28 {datetime('2003-10-22 12:34','11.25 seconds')} \
122 {2003-10-22 12:34:11}
123 datetest 2.29 {datetime('2003-10-22 12:24','+5 bogus')} NULL
124 datetest 2.30 {datetime('2003-10-22 12:24','+++')} NULL
125 datetest 2.31 {datetime('2003-10-22 12:24','+12.3e4 femtoseconds')} NULL
126 datetest 2.32 {datetime('2003-10-22 12:24','+12.3e4 uS')} NULL
127 datetest 2.33 {datetime('2003-10-22 12:24','+1 abc')} NULL
128 datetest 2.34 {datetime('2003-10-22 12:24','+1 abcd')} NULL
129 datetest 2.35 {datetime('2003-10-22 12:24','+1 abcde')} NULL
130 datetest 2.36 {datetime('2003-10-22 12:24','+1 abcdef')} NULL
131 datetest 2.37 {datetime('2003-10-22 12:24','+1 abcdefg')} NULL
132 datetest 2.38 {datetime('2003-10-22 12:24','+1 abcdefgh')} NULL
133 datetest 2.39 {datetime('2003-10-22 12:24','+1 abcdefghi')} NULL
134 set sqlite_current_time 1199243045
135 datetest 2.40 {datetime()} {2008-01-02 03:04:05}
136 set sqlite_current_time 0
137 datetest 2.41 {datetime('2003-10-22 12:24','23 seconds')} {2003-10-22 12:24:23}
138 datetest 2.42 {datetime('2003-10-22 12:24','345 second')} {2003-10-22 12:29:45}
139 datetest 2.43 {datetime('2003-10-22 12:24','4 second')} {2003-10-22 12:24:04}
140 datetest 2.44 {datetime('2003-10-22 12:24','56 second')} {2003-10-22 12:24:56}
141 datetest 2.45 {datetime('2003-10-22 12:24','60 second')} {2003-10-22 12:25:00}
142 datetest 2.46 {datetime('2003-10-22 12:24','70 second')} {2003-10-22 12:25:10}
143 datetest 2.47 {datetime('2003-10-22 12:24','8.6 seconds')} {2003-10-22 12:24:08}
144 datetest 2.48 {datetime('2003-10-22 12:24','9.4 second')} {2003-10-22 12:24:09}
145 datetest 2.49 {datetime('2003-10-22 12:24','0000 second')} {2003-10-22 12:24:00}
146 datetest 2.50 {datetime('2003-10-22 12:24','0001 second')} {2003-10-22 12:24:01}
147 datetest 2.51 {datetime('2003-10-22 12:24','nonsense')} NULL
149 datetest 2.60 {datetime('2023-02-31')} {2023-03-03 00:00:00}
151 datetest 3.1 {strftime('%d','2003-10-31 12:34:56.432')} 31
152 datetest 3.2.1 {strftime('pre%fpost','2003-10-31 12:34:56.432')} pre56.432post
153 datetest 3.2.2 {strftime('%f','2003-10-31 12:34:59.9999999')} 59.999
154 datetest 3.3 {strftime('%H','2003-10-31 12:34:56.432')} 12
155 datetest 3.4 {strftime('%j','2003-10-31 12:34:56.432')} 304
156 datetest 3.5 {strftime('%J','2003-10-31 12:34:56.432')} 2452944.024264259
157 datetest 3.6 {strftime('%m','2003-10-31 12:34:56.432')} 10
158 datetest 3.7 {strftime('%M','2003-10-31 12:34:56.432')} 34
159 datetest 3.8.1 {strftime('%s','2003-10-31 12:34:56.432')} 1067603696
160 datetest 3.8.2 {strftime('%s','2038-01-19 03:14:07')} 2147483647
161 datetest 3.8.3 {strftime('%s','2038-01-19 03:14:08')} 2147483648
162 datetest 3.8.4 {strftime('%s','2201-04-09 12:00:00')} 7298164800
163 datetest 3.8.5 {strftime('%s','9999-12-31 23:59:59')} 253402300799
164 datetest 3.8.6 {strftime('%s','1969-12-31 23:59:59')} -1
165 datetest 3.8.7 {strftime('%s','1901-12-13 20:45:52')} -2147483648
166 datetest 3.8.8 {strftime('%s','1901-12-13 20:45:51')} -2147483649
167 datetest 3.8.9 {strftime('%s','1776-07-04 00:00:00')} -6106060800
168 datetest 3.9 {strftime('%S','2003-10-31 12:34:56.432')} 56
169 datetest 3.10 {strftime('%w','2003-10-31 12:34:56.432')} 5
170 datetest 3.11.1 {strftime('%W','2003-10-31 12:34:56.432')} 43
171 datetest 3.11.2 {strftime('%W','2004-01-01')} 00
172 datetest 3.11.3 {strftime('%W','2004-01-02')} 00
173 datetest 3.11.4 {strftime('%W','2004-01-03')} 00
174 datetest 3.11.5 {strftime('abc%Wxyz','2004-01-04')} abc00xyz
175 datetest 3.11.6 {strftime('%W','2004-01-05')} 01
176 datetest 3.11.7 {strftime('%W','2004-01-06')} 01
177 datetest 3.11.8 {strftime('%W','2004-01-07')} 01
178 datetest 3.11.9 {strftime('%W','2004-01-08')} 01
179 datetest 3.11.10 {strftime('%W','2004-01-09')} 01
180 datetest 3.11.11 {strftime('%W','2004-07-18')} 28
181 datetest 3.11.12 {strftime('%W','2004-12-31')} 52
182 datetest 3.11.13 {strftime('%W','2007-12-31')} 53
183 datetest 3.11.14 {strftime('%W','2007-01-01')} 01
184 datetest 3.11.15 {strftime('%W %j',2454109.04140970)} {02 008}
185 datetest 3.11.16 {strftime('%W %j',2454109.04140971)} {02 008}
186 datetest 3.11.17 {strftime('%W %j',2454109.04140972)} {02 008}
187 datetest 3.11.18 {strftime('%W %j',2454109.04140973)} {02 008}
188 datetest 3.11.19 {strftime('%W %j',2454109.04140974)} {02 008}
189 datetest 3.11.20 {strftime('%W %j',2454109.04140975)} {02 008}
190 datetest 3.11.21 {strftime('%W %j',2454109.04140976)} {02 008}
191 datetest 3.11.22 {strftime('%W %j',2454109.04140977)} {02 008}
192 datetest 3.11.23 {strftime('%W %j',2454109.04140978)} {02 008}
193 datetest 3.11.24 {strftime('%W %j',2454109.04140979)} {02 008}
194 datetest 3.11.25 {strftime('%W %j',2454109.04140980)} {02 008}
195 datetest 3.11.99 {strftime('%W %j','2454109.04140970')} {02 008}
196 datetest 3.12 {strftime('%Y','2003-10-31 12:34:56.432')} 2003
197 datetest 3.13 {strftime('%%','2003-10-31 12:34:56.432')} %
198 datetest 3.14 {strftime('%_','2003-10-31 12:34:56.432')} NULL
199 datetest 3.15 {strftime('%Y-%m-%d','2003-10-31')} 2003-10-31
200 proc repeat {n txt} {
208 datetest 3.16 "strftime('[repeat 200 %Y]','2003-10-31')" [repeat 200 2003]
209 datetest 3.17 "strftime('[repeat 200 abc%m123]','2003-10-31')" \
210 [repeat 200 abc10123]
212 foreach c {a b c h i n o q r t v x y z
213 A B C D E K L N O Q Z
214 0 1 2 3 4 5 6 6 7 9 _} {
215 datetest 3.18.$c "strftime('%$c','2003-10-31')" NULL
217 datetest 3.20 {strftime('%e','2023-08-09')} { 9}
218 datetest 3.21 {strftime('%F %T','2023-08-09 01:23')} {2023-08-09 01:23:00}
219 datetest 3.22 {strftime('%k','2023-08-09 04:59:59')} { 4}
220 datetest 3.23 {strftime('%I%P','2023-08-09 11:59:59')} {11am}
221 datetest 3.24 {strftime('%I%p','2023-08-09 12:00:00')} {12PM}
222 datetest 3.25 {strftime('%I%P','2023-08-09 12:59:59.9')} {12pm}
223 datetest 3.26 {strftime('%I%p','2023-08-09 13:00:00')} {01PM}
224 datetest 3.27 {strftime('%I%P','2023-08-09 23:59:59')} {11pm}
225 datetest 3.28 {strftime('%I%p','2023-08-09 00:00:00')} {12AM}
226 datetest 3.29 {strftime('%l:%M%P','2023-08-09 13:00:00')} { 1:00pm}
227 datetest 3.30 {strftime('%F %R','2023-08-09 12:34:56')} {2023-08-09 12:34}
228 datetest 3.31 {strftime('%w %u','2023-01-01')} {0 7}
229 datetest 3.32 {strftime('%w %u','2023-01-02')} {1 1}
230 datetest 3.33 {strftime('%w %u','2023-01-03')} {2 2}
231 datetest 3.34 {strftime('%w %u','2023-01-04')} {3 3}
232 datetest 3.35 {strftime('%w %u','2023-01-05')} {4 4}
233 datetest 3.36 {strftime('%w %u','2023-01-06')} {5 5}
234 datetest 3.37 {strftime('%w %u','2023-01-07')} {6 6}
236 # Ticket #2276. Make sure leading zeros are inserted where appropriate.
239 {strftime('%d/%f/%H/%W/%j/%m/%M/%S/%Y','0421-01-02 03:04:05.006')} \
240 02/05.006/03/00/002/01/04/05/0421
242 set sqlite_current_time 1157124367
243 datetest 4.1 {date('now')} {2006-09-01}
244 set sqlite_current_time 0
246 datetest 5.1 {datetime('1994-04-16 14:00:00 +05:00')} {1994-04-16 09:00:00}
247 datetest 5.2 {datetime('1994-04-16 14:00:00 -05:15')} {1994-04-16 19:15:00}
248 datetest 5.3 {datetime('1994-04-16 05:00:00 +08:30')} {1994-04-15 20:30:00}
249 datetest 5.4 {datetime('1994-04-16 14:00:00 -11:55')} {1994-04-17 01:55:00}
250 datetest 5.5 {datetime('1994-04-16 14:00:00 -11:60')} NULL
251 datetest 5.6 {datetime('1994-04-16 14:00:00 -11:55 ')} {1994-04-17 01:55:00}
252 datetest 5.7 {datetime('1994-04-16 14:00:00 -11:55 x')} NULL
253 datetest 5.8 {datetime('1994-04-16T14:00:00Z')} {1994-04-16 14:00:00}
254 datetest 5.9 {datetime('1994-04-16 14:00:00z')} {1994-04-16 14:00:00}
255 datetest 5.10 {datetime('1994-04-16 14:00:00 Z')} {1994-04-16 14:00:00}
256 datetest 5.11 {datetime('1994-04-16 14:00:00z ')} {1994-04-16 14:00:00}
257 datetest 5.12 {datetime('1994-04-16 14:00:00 z ')} {1994-04-16 14:00:00}
258 datetest 5.13 {datetime('1994-04-16 14:00:00Zulu')} NULL
259 datetest 5.14 {datetime('1994-04-16 14:00:00Z +05:00')} NULL
260 datetest 5.15 {datetime('1994-04-16 14:00:00 +05:00 Z')} NULL
262 # localtime->utc and utc->localtime conversions.
264 # Use SQLITE_TESTCTRL_LOCALTIME_FAULT=2 to set an alternative localtime_r()
265 # implementation that is not locale-dependent. The testing localtime_r()
266 # operates as follows:
268 # (1) Localtime is 30 minutes earlier than (west of) UTC on
269 # even days (counting from 1970-01-01)
271 # (2) Localtime is 30 minutes later than (east of) UTC on odd days.
273 # (3) The function fails for the specific date/time value
274 # of 2000-05-29 14:16:00 in order to test the ability of
275 # SQLite to deal with localtime_r() failures.
277 proc local_to_utc {tn utc local} {
278 do_execsql_test date-$tn "SELECT datetime('$utc','localtime')" [list $local]
280 proc utc_to_local {tn local utc} {
281 do_execsql_test date-$tn "SELECT datetime('$local','utc')" [list $utc]
284 sqlite3_test_control SQLITE_TESTCTRL_LOCALTIME_FAULT 2
285 local_to_utc 6.1 {2000-10-29 12:00:00} {2000-10-29 12:30:00}
286 utc_to_local 6.2 {2000-10-29 12:30:00} {2000-10-29 12:00:00}
287 local_to_utc 6.3 {2000-10-30 12:00:00} {2000-10-30 11:30:00}
288 utc_to_local 6.4 {2000-10-30 11:30:00} {2000-10-30 12:00:00}
290 local_to_utc 6.5 {2000-10-28 23:59:59} {2000-10-28 23:29:59}
291 local_to_utc 6.6 {2000-10-29 00:00:00} {2000-10-29 00:30:00}
293 # The previous two cases establish that no such localtime as
294 # 2000-10-29 00:10:00 exists. Verify that we get a reasonable
295 # answer if we try to convert this non-existant localtime to utc?
297 utc_to_local 6.7 {2000-10-29 00:10:00} {2000-10-28 23:40:00}
299 local_to_utc 6.8 {2022-02-10 23:59:59} {2022-02-11 00:29:59}
300 local_to_utc 6.9 {2022-02-11 00:00:00} {2022-02-10 23:30:00}
301 local_to_utc 6.10 {2022-02-10 23:45:00} {2022-02-11 00:15:00}
302 local_to_utc 6.11 {2022-02-11 00:45:00} {2022-02-11 00:15:00}
304 # The previous two cases show that two different UTC values give
305 # the same localtime of 2022-02-11 00:15:00. When converting from
306 # that localtime back to UTC, we should get one or the other of
307 # the two UTC values.
309 utc_to_local 6.12 {2022-02-11 00:15:00} {2022-02-11 00:45:00}
311 # If localtime_r() fails, the datetime() SQL function should raise an error
313 do_catchsql_test date-6.20 {
314 SELECT datetime('2000-05-29 14:16:00','localtime');
315 } {1 {local time unavailable}}
317 # Modifiers work for dates that are way out of band for localtime_r()
319 local_to_utc 6.21 {1800-10-29 12:00:00} {1800-10-29 12:30:00}
320 utc_to_local 6.22 {1800-10-29 12:30:00} {1800-10-29 12:00:00}
321 local_to_utc 6.23 {3000-10-30 12:00:00} {3000-10-30 11:30:00}
322 utc_to_local 6.24 {3000-10-30 11:30:00} {3000-10-30 12:00:00}
324 # If the time is specified to be ZULU, or if it has an explicit
325 # timezone extension, then the time will already be UTC and subsequent
326 # 'utc' modifiers are no-ops.
328 do_execsql_test date-6.25 {
329 SELECT datetime('2000-10-29 12:00Z','utc','utc');
330 } {{2000-10-29 12:00:00}}
331 do_execsql_test date-6.26 {
332 SELECT datetime('2000-10-29 12:00:00+05:00');
333 } {{2000-10-29 07:00:00}}
334 do_execsql_test date-6.27 {
335 SELECT datetime('2000-10-29 12:00:00+05:00', 'utc');
336 } {{2000-10-29 07:00:00}}
338 # Multiple back-and-forth UTC to LOCAL to UTC...
339 do_execsql_test date-6.28 {
340 SELECT datetime('2000-10-29 12:00:00Z', 'localtime');
341 } {{2000-10-29 12:30:00}}
342 do_execsql_test date-6.29 {
343 SELECT datetime('2000-10-29 12:00:00Z', 'utc', 'localtime');
344 } {{2000-10-29 12:30:00}}
345 do_execsql_test date-6.30 {
346 SELECT datetime('2000-10-29 12:00:00Z', 'utc', 'localtime', 'utc');
347 } {{2000-10-29 12:00:00}}
348 do_execsql_test date-6.31 {
349 SELECT datetime('2000-10-29 12:00:00Z', 'utc','localtime','utc','localtime');
350 } {{2000-10-29 12:30:00}}
351 do_execsql_test date-6.32 {
352 SELECT datetime('2000-10-29 12:00:00Z', 'localtime','localtime');
353 } {{2000-10-29 12:30:00}}
356 # Restore the use of the OS localtime_r() before going on...
357 sqlite3_test_control SQLITE_TESTCTRL_LOCALTIME_FAULT 0
359 # Date-time functions that contain NULL arguments return a NULL
362 datetest 7.1 {datetime(null)} NULL
363 datetest 7.2 {datetime('now',null)} NULL
364 datetest 7.3 {datetime('now','localtime',null)} NULL
365 datetest 7.4 {time(null)} NULL
366 datetest 7.5 {time('now',null)} NULL
367 datetest 7.6 {time('now','localtime',null)} NULL
368 datetest 7.7 {date(null)} NULL
369 datetest 7.8 {date('now',null)} NULL
370 datetest 7.9 {date('now','localtime',null)} NULL
371 datetest 7.10 {julianday(null)} NULL
372 datetest 7.11 {julianday('now',null)} NULL
373 datetest 7.12 {julianday('now','localtime',null)} NULL
374 datetest 7.13 {strftime(null,'now')} NULL
375 datetest 7.14 {strftime('%s',null)} NULL
376 datetest 7.15 {strftime('%s','now',null)} NULL
377 datetest 7.16 {strftime('%s','now','localtime',null)} NULL
379 # Test modifiers when the date begins as a julian day number - to
380 # make sure the HH:MM:SS is preserved. Ticket #551.
382 set sqlite_current_time [db eval {SELECT strftime('%s','2003-10-22 12:34:00')}]
383 datetest 8.1 {datetime('now','weekday 0')} {2003-10-26 12:34:00}
384 datetest 8.2 {datetime('now','weekday 1')} {2003-10-27 12:34:00}
385 datetest 8.3 {datetime('now','weekday 2')} {2003-10-28 12:34:00}
386 datetest 8.4 {datetime('now','weekday 3')} {2003-10-22 12:34:00}
387 datetest 8.5 {datetime('now','start of month')} {2003-10-01 00:00:00}
388 datetest 8.6 {datetime('now','start of year')} {2003-01-01 00:00:00}
389 datetest 8.7 {datetime('now','start of day')} {2003-10-22 00:00:00}
390 datetest 8.8 {datetime('now','1 day')} {2003-10-23 12:34:00}
391 datetest 8.9 {datetime('now','+1 day')} {2003-10-23 12:34:00}
392 datetest 8.10 {datetime('now','+1.25 day')} {2003-10-23 18:34:00}
393 datetest 8.11 {datetime('now','-1.0 day')} {2003-10-21 12:34:00}
394 datetest 8.12 {datetime('now','1 month')} {2003-11-22 12:34:00}
395 datetest 8.13 {datetime('now','11 month')} {2004-09-22 12:34:00}
396 datetest 8.14 {datetime('now','-13 month')} {2002-09-22 12:34:00}
397 datetest 8.15 {datetime('now','1.5 months')} {2003-12-07 12:34:00}
398 datetest 8.16 {datetime('now','-5 years')} {1998-10-22 12:34:00}
399 datetest 8.17 {datetime('now','+10.5 minutes')} {2003-10-22 12:44:30}
400 datetest 8.18 {datetime('now','-1.25 hours')} {2003-10-22 11:19:00}
401 datetest 8.19 {datetime('now','11.25 seconds')} {2003-10-22 12:34:11}
402 datetest 8.90 {datetime('now','abcdefghijklmnopqrstuvwyxzABCDEFGHIJLMNOP')} NULL
403 set sqlite_current_time 0
405 # Negative years work. Example: '-4713-11-26' is JD 1.5.
407 datetest 9.1 {julianday('-4713-11-24 12:00:00')} {0.0}
408 datetest 9.2 {julianday(datetime(5))} {5.0}
409 datetest 9.3 {julianday(datetime(10))} {10.0}
410 datetest 9.4 {julianday(datetime(100))} {100.0}
411 datetest 9.5 {julianday(datetime(1000))} {1000.0}
412 datetest 9.6 {julianday(datetime(10000))} {10000.0}
413 datetest 9.7 {julianday(datetime(100000))} {100000.0}
415 # datetime() with just an HH:MM:SS correctly inserts the date 2000-01-01.
417 datetest 10.1 {datetime('01:02:03')} {2000-01-01 01:02:03}
418 datetest 10.2 {date('01:02:03')} {2000-01-01}
419 datetest 10.3 {strftime('%Y-%m-%d %H:%M','01:02:03')} {2000-01-01 01:02}
421 # Test the new HH:MM:SS modifier
423 datetest 11.1 {datetime('2004-02-28 20:00:00', '-01:20:30')} \
424 {2004-02-28 18:39:30}
425 datetest 11.2 {datetime('2004-02-28 20:00:00', '+12:30:00')} \
426 {2004-02-29 08:30:00}
427 datetest 11.3 {datetime('2004-02-28 20:00:00', '+12:30')} \
428 {2004-02-29 08:30:00}
429 datetest 11.4 {datetime('2004-02-28 20:00:00', '12:30')} \
430 {2004-02-29 08:30:00}
431 datetest 11.5 {datetime('2004-02-28 20:00:00', '-12:00')} \
432 {2004-02-28 08:00:00}
433 datetest 11.6 {datetime('2004-02-28 20:00:00', '-12:01')} \
434 {2004-02-28 07:59:00}
435 datetest 11.7 {datetime('2004-02-28 20:00:00', '-11:59')} \
436 {2004-02-28 08:01:00}
437 datetest 11.8 {datetime('2004-02-28 20:00:00', '11:59')} \
438 {2004-02-29 07:59:00}
439 datetest 11.9 {datetime('2004-02-28 20:00:00', '12:01')} \
440 {2004-02-29 08:01:00}
441 datetest 11.10 {datetime('2004-02-28 20:00:00', '12:60')} NULL
444 datetest 12.1 {datetime('2005-09-01')} {2005-09-01 00:00:00}
445 datetest 12.2 {datetime('2005-09-01','+0 hours')} {2005-09-01 00:00:00}
450 SELECT strftime('%Y-%m-%d %H:%M:%f', julianday('2006-09-24T10:50:26.047'))
452 } {{2006-09-24 10:50:26.047}}
455 datetest 13.2 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 12:34:59.6')} \
456 {2007-01-01 12:34:59}
457 datetest 13.3 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 12:34:59.6')} \
458 {2007-01-01 12:34:59.600}
459 datetest 13.4 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 12:59:59.6')} \
460 {2007-01-01 12:59:59}
461 datetest 13.5 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 12:59:59.6')} \
462 {2007-01-01 12:59:59.600}
463 datetest 13.6 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 23:59:59.6')} \
464 {2007-01-01 23:59:59}
465 datetest 13.7 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 23:59:59.6')} \
466 {2007-01-01 23:59:59.600}
469 datetest 13.11 {julianday(2454832.5,'-1 day')} {2454831.5}
470 datetest 13.12 {julianday(2454832.5,'+1 day')} {2454833.5}
471 datetest 13.13 {julianday(2454832.5,'-1.5 day')} {2454831.0}
472 datetest 13.14 {julianday(2454832.5,'+1.5 day')} {2454834.0}
473 datetest 13.15 {julianday(2454832.5,'-3 hours')} {2454832.375}
474 datetest 13.16 {julianday(2454832.5,'+3 hours')} {2454832.625}
475 datetest 13.17 {julianday(2454832.5,'-45 minutes')} {2454832.46875}
476 datetest 13.18 {julianday(2454832.5,'+45 minutes')} {2454832.53125}
477 datetest 13.19 {julianday(2454832.5,'-675 seconds')} {2454832.4921875}
478 datetest 13.20 {julianday(2454832.5,'+675 seconds')} {2454832.5078125}
479 datetest 13.21 {julianday(2454832.5,'-1.5 months')} {2454786.5}
480 datetest 13.22 {julianday(2454832.5,'+1.5 months')} {2454878.5}
481 datetest 13.23 {julianday(2454832.5,'-1.5 years')} {2454284.0}
482 datetest 13.24 {julianday(2454832.5,'+1.5 years')} {2455380.0}
484 datetest 13.30 {date('2000-01-01','+1.5 years')} {2001-07-02}
485 datetest 13.31 {date('2001-01-01','+1.5 years')} {2002-07-02}
486 datetest 13.32 {date('2002-01-01','+1.5 years')} {2003-07-02}
487 datetest 13.33 {date('2002-01-01','-1.5 years')} {2000-07-02}
488 datetest 13.34 {date('2001-01-01','-1.5 years')} {1999-07-02}
489 datetest 13.35 {date('2023-02-28')} {2023-02-28}
490 datetest 13.36 {date('2023-02-29')} {2023-03-01}
491 datetest 13.37 {date('2023-04-31')} {2023-05-01}
493 # Test for issues reported by BareFeet (list.sql at tandb.com.au)
494 # on mailing list on 2008-06-12.
496 # Put a floating point number in the database so that we can manipulate
497 # raw bits using the hexio interface.
499 if {0==[sqlite3 -has-codec]} {
502 PRAGMA auto_vacuum=OFF;
503 PRAGMA page_size = 1024;
505 INSERT INTO t1 VALUES(1.1);
508 hexio_write test.db 2040 4142ba32bffffff9
510 db eval {SELECT * FROM t1}
513 # Changing the least significant byte of the floating point value between
514 # 00 and FF should always generate a time of either 23:59:59 or 00:00:00,
517 for {set i 0} {$i<=255} {incr i} {
519 hexio_write test.db 2047 [format %02x $i]
521 do_test date-14.2.$i {
522 set date [db one {SELECT datetime(x) FROM t1}]
523 expr {$date eq "2008-06-12 00:00:00" || $date eq "2008-06-11 23:59:59"}
528 # Verify that multiple calls to date functions with 'now' return the
531 # EVIDENCE-OF: R-34818-13664 The 'now' argument to date and time
532 # functions always returns exactly the same value for multiple
533 # invocations within the same sqlite3_step() call.
535 proc sleeper {} {after 100}
537 db func sleeper sleeper
539 SELECT c - a FROM (SELECT julianday('now') AS a,
540 sleeper(), julianday('now') AS c);
545 SELECT a==b FROM (SELECT current_timestamp AS a,
546 sleeper(), current_timestamp AS b);
550 # Tests of extreme values in date/time functions. Run with UBSan or the
551 # equivalent to verify no signed interger overflow warnings.
553 datetest 16.1 {date(147483649)} NULL
554 datetest 16.2 {datetime(0)} {-4713-11-24 12:00:00}
555 datetest 16.3 {datetime(5373484.49999999)} {9999-12-31 23:59:59}
556 datetest 16.4 {julianday('-4713-11-24 12:00:00')} 0.0
557 datetest 16.5 {julianday('9999-12-31 23:59:59.999')} 5373484.49999999
558 datetest 16.6 {datetime(0,'+464269060799 seconds')} {9999-12-31 23:59:59}
559 datetest 16.7 {datetime(0,'+464269060800 seconds')} NULL
560 datetest 16.8 {datetime(0,'+7737817679 minutes')} {9999-12-31 23:59:00}
561 datetest 16.9 {datetime(0,'+7737817680 minutes')} NULL
562 datetest 16.10 {datetime(0,'+128963627 hours')} {9999-12-31 23:00:00}
563 datetest 16.11 {datetime(0,'+128963628 hours')} NULL
564 datetest 16.12 {datetime(0,'+5373484 days')} {9999-12-31 12:00:00}
565 datetest 16.13 {datetime(0,'+5373485 days')} NULL
566 datetest 16.14 {datetime(0,'+176545 months')} {9999-12-24 12:00:00}
567 datetest 16.15 {datetime(0,'+176546 months')} NULL
568 datetest 16.16 {datetime(0,'+14712 years')} {9999-11-24 12:00:00}
569 datetest 16.17 {datetime(0,'+14713 years')} NULL
570 datetest 16.20 {datetime(5373484.4999999,'-464269060799 seconds')} \
571 {-4713-11-24 12:00:00}
572 datetest 16.21 {datetime(5373484,'-464269060800 seconds')} NULL
573 datetest 16.22 {datetime(5373484.4999999,'-7737817679 minutes')} \
574 {-4713-11-24 12:00:59}
575 datetest 16.23 {datetime(5373484,'-7737817680 minutes')} NULL
576 datetest 16.24 {datetime(5373484.4999999,'-128963627 hours')} \
577 {-4713-11-24 12:59:59}
578 datetest 16.25 {datetime(5373484,'-128963628 hours')} NULL
579 datetest 16.26 {datetime(5373484,'-5373484 days')} {-4713-11-24 12:00:00}
580 datetest 16.27 {datetime(5373484,'-5373485 days')} NULL
581 datetest 16.28 {datetime(5373484,'-176545 months')} {-4713-12-01 12:00:00}
582 datetest 16.29 {datetime(5373484,'-176546 months')} NULL
583 datetest 16.30 {datetime(5373484,'-14712 years')} {-4713-12-31 12:00:00}
584 datetest 16.31 {datetime(5373484,'-14713 years')} NULL
586 # 2017-03-02: Wrong 'start of day' computation.
587 # https://www.sqlite.org/src/info/6097cb92745327a1
589 datetest 17.1 {datetime(2457754, 'start of day')} {2016-12-31 00:00:00}
590 datetest 17.2 {datetime(2457828)} {2017-03-15 12:00:00}
591 datetest 17.3 {datetime(2457828,'start of day')} {2017-03-15 00:00:00}
592 datetest 17.4 {datetime(2457828,'start of month')} {2017-03-01 00:00:00}
593 datetest 17.5 {datetime(2457828,'start of year')} {2017-01-01 00:00:00}
594 datetest 17.6 {datetime(37,'start of year')} NULL
595 datetest 17.7 {datetime(38,'start of year')} {-4712-01-01 00:00:00}
597 # 2022-03-04 https://sqlite.org/forum/forumpost/2ffbaa2c3fd7fb82
598 # The 'localtime' modifier should preserve fractional seconds.
600 datetest 18.1 {strftime('%f',1.234,'unixepoch','localtime')} {01.234}
602 # 2023-04 The 'subsecond' (or 'subsec') modifier alters resolutions
603 # to at least milliseconds. Added for release 3.42.0 .
604 datetest 18.2 {unixepoch('1970-01-01T00:00:00.1', 'subsec')} {0.1}
605 datetest 18.3 {unixepoch('1970-01-01T00:00:00.2', 'subsecond')} {0.2}
606 datetest 18.4 {julianday('-4713-11-24 13:40:48.864', 'subsec')} {0.07001}
607 datetest 18.5 {typeof(unixepoch('now', 'subsecond'))} {real}
609 # 2024-03-03 the 'ceiling' and 'floor' operators.
611 datetest 19.1 {date('2000-01-31','floor')} {2000-01-31}
612 datetest 19.2a {date('2000-02-31','floor')} {2000-02-29}
613 datetest 19.2b {date('1999-02-31','floor')} {1999-02-28}
614 datetest 19.2c {date('1900-02-31','floor')} {1900-02-28}
615 datetest 19.3 {date('2000-03-31','floor')} {2000-03-31}
616 datetest 19.4 {date('2000-04-31','floor')} {2000-04-30}
617 datetest 19.5 {date('2000-05-31','floor')} {2000-05-31}
618 datetest 19.6 {date('2000-06-31','floor')} {2000-06-30}
619 datetest 19.7 {date('2000-07-31','floor')} {2000-07-31}
620 datetest 19.8 {date('2000-08-31','floor')} {2000-08-31}
621 datetest 19.9 {date('2000-09-31','floor')} {2000-09-30}
622 datetest 19.10 {date('2000-10-31','floor')} {2000-10-31}
623 datetest 19.11 {date('2000-11-31','floor')} {2000-11-30}
624 datetest 19.12 {date('2000-12-31','floor')} {2000-12-31}
625 datetest 19.21 {date('2000-01-31','ceiling')} {2000-01-31}
626 datetest 19.22a {date('2000-02-31','ceiling')} {2000-03-02}
627 datetest 19.22b {date('1999-02-31','ceiling')} {1999-03-03}
628 datetest 19.22c {date('1900-02-31','ceiling')} {1900-03-03}
629 datetest 19.23 {date('2000-03-31','ceiling')} {2000-03-31}
630 datetest 19.24 {date('2000-04-31','ceiling')} {2000-05-01}
631 datetest 19.25 {date('2000-05-31','ceiling')} {2000-05-31}
632 datetest 19.26 {date('2000-06-31','ceiling')} {2000-07-01}
633 datetest 19.27 {date('2000-07-31','ceiling')} {2000-07-31}
634 datetest 19.28 {date('2000-08-31','ceiling')} {2000-08-31}
635 datetest 19.29 {date('2000-09-31','ceiling')} {2000-10-01}
636 datetest 19.30 {date('2000-10-31','ceiling')} {2000-10-31}
637 datetest 19.31 {date('2000-11-31','ceiling')} {2000-12-01}
638 datetest 19.32 {date('2000-12-31','ceiling')} {2000-12-31}
639 datetest 19.40 {date('2024-01-31','+1 month','ceiling')} {2024-03-02}
640 datetest 19.41 {date('2024-01-31','+1 month','floor')} {2024-02-29}
641 datetest 19.42 {date('2023-01-31','+1 month','ceiling')} {2023-03-03}
642 datetest 19.43 {date('2023-01-31','+1 month','floor')} {2023-02-28}
643 datetest 19.44 {date('2024-02-29','+1 year','ceiling')} {2025-03-01}
644 datetest 19.45 {date('2024-02-29','+1 year','floor')} {2025-02-28}
645 datetest 19.46 {date('2024-02-29','-110 years','ceiling')} {1914-03-01}
646 datetest 19.47 {date('2024-02-29','-110 years','floor')} {1914-02-28}
647 datetest 19.48 {date('2024-02-29','-0110-00-00','floor')} {1914-02-28}
648 datetest 19.49 {date('2024-02-29','-0110-00-00','ceiling')} {1914-03-01}
649 datetest 19.50 {date('2000-08-31','+0023-06-00','floor')} {2024-02-29}
650 datetest 19.51 {date('2000-08-31','+0022-06-00','floor')} {2023-02-28}
651 datetest 19.52 {date('2000-08-31','+0023-06-00','ceiling')} {2024-03-02}
652 datetest 19.53 {date('2000-08-31','+0022-06-00','ceiling')} {2023-03-03}