xref: /onnv-gate/usr/src/lib/libsqlite/test/date.test (revision 4520:7dbeadedd7fe)
1*4520Snw141292
2*4520Snw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*4520Snw141292
4*4520Snw141292# 2003 October 31
5*4520Snw141292#
6*4520Snw141292# The author disclaims copyright to this source code.  In place of
7*4520Snw141292# a legal notice, here is a blessing:
8*4520Snw141292#
9*4520Snw141292#    May you do good and not evil.
10*4520Snw141292#    May you find forgiveness for yourself and forgive others.
11*4520Snw141292#    May you share freely, never taking more than you give.
12*4520Snw141292#
13*4520Snw141292#***********************************************************************
14*4520Snw141292# This file implements regression tests for SQLite library.  The
15*4520Snw141292# focus of this file is testing date and time functions.
16*4520Snw141292#
17*4520Snw141292# $Id: date.test,v 1.7.2.1 2004/07/18 22:25:16 drh Exp $
18*4520Snw141292
19*4520Snw141292set testdir [file dirname $argv0]
20*4520Snw141292source $testdir/tester.tcl
21*4520Snw141292
22*4520Snw141292proc datetest {tnum expr result} {
23*4520Snw141292  do_test date-$tnum [subst {
24*4520Snw141292    execsql "SELECT coalesce($expr,'NULL')"
25*4520Snw141292  }] [list $result]
26*4520Snw141292}
27*4520Snw141292
28*4520Snw141292datetest 1.1 julianday('2000-01-01') 2451544.5
29*4520Snw141292datetest 1.2 julianday('1970-01-01') 2440587.5
30*4520Snw141292datetest 1.3 julianday('1910-04-20') 2418781.5
31*4520Snw141292datetest 1.4 julianday('1986-02-09') 2446470.5
32*4520Snw141292datetest 1.5 julianday('12:00:00') 2451545
33*4520Snw141292datetest 1.6 {julianday('2000-01-01 12:00:00')} 2451545
34*4520Snw141292datetest 1.7 {julianday('2000-01-01 12:00')} 2451545
35*4520Snw141292datetest 1.8 julianday('bogus') NULL
36*4520Snw141292datetest 1.9 julianday('1999-12-31') 2451543.5
37*4520Snw141292datetest 1.10 julianday('1999-12-32') NULL
38*4520Snw141292datetest 1.11 julianday('1999-13-01') NULL
39*4520Snw141292datetest 1.12 julianday('2003-02-31') 2452701.5
40*4520Snw141292datetest 1.13 julianday('2003-03-03') 2452701.5
41*4520Snw141292datetest 1.14 julianday('+2000-01-01') NULL
42*4520Snw141292datetest 1.15 julianday('200-01-01') NULL
43*4520Snw141292datetest 1.16 julianday('2000-1-01') NULL
44*4520Snw141292datetest 1.17 julianday('2000-01-1') NULL
45*4520Snw141292datetest 1.18 {julianday('2000-01-01     12:00:00')} 2451545
46*4520Snw141292datetest 1.19 {julianday('2000-01-01 12:00:00.1')}   2451545.00000116
47*4520Snw141292datetest 1.20 {julianday('2000-01-01 12:00:00.01')}  2451545.00000012
48*4520Snw141292datetest 1.21 {julianday('2000-01-01 12:00:00.001')} 2451545.00000001
49*4520Snw141292datetest 1.22 {julianday('2000-01-01 12:00:00.')} NULL
50*4520Snw141292datetest 1.23 julianday(12345.6) 12345.6
51*4520Snw141292datetest 1.24 {julianday('2001-01-01 12:00:00 bogus')} NULL
52*4520Snw141292datetest 1.25 {julianday('2001-01-01 bogus')} NULL
53*4520Snw141292
54*4520Snw141292datetest 2.1 datetime(0,'unixepoch') {1970-01-01 00:00:00}
55*4520Snw141292datetest 2.2 datetime(946684800,'unixepoch') {2000-01-01 00:00:00}
56*4520Snw141292datetest 2.3 {date('2003-10-22','weekday 0')} 2003-10-26
57*4520Snw141292datetest 2.4 {date('2003-10-22','weekday 1')} 2003-10-27
58*4520Snw141292datetest 2.5 {date('2003-10-22','weekday 2')} 2003-10-28
59*4520Snw141292datetest 2.6 {date('2003-10-22','weekday 3')} 2003-10-22
60*4520Snw141292datetest 2.7 {date('2003-10-22','weekday 4')} 2003-10-23
61*4520Snw141292datetest 2.8 {date('2003-10-22','weekday 5')} 2003-10-24
62*4520Snw141292datetest 2.9 {date('2003-10-22','weekday 6')} 2003-10-25
63*4520Snw141292datetest 2.10 {date('2003-10-22','weekday 7')} NULL
64*4520Snw141292datetest 2.11 {date('2003-10-22','weekday 5.5')} NULL
65*4520Snw141292datetest 2.12 {datetime('2003-10-22 12:34','weekday 0')} {2003-10-26 12:34:00}
66*4520Snw141292datetest 2.13 {datetime('2003-10-22 12:34','start of month')} \
67*4520Snw141292   {2003-10-01 00:00:00}
68*4520Snw141292datetest 2.14 {datetime('2003-10-22 12:34','start of year')} \
69*4520Snw141292   {2003-01-01 00:00:00}
70*4520Snw141292datetest 2.15 {datetime('2003-10-22 12:34','start of day')} \
71*4520Snw141292   {2003-10-22 00:00:00}
72*4520Snw141292datetest 2.16 time('12:34:56.43') 12:34:56
73*4520Snw141292datetest 2.17 {datetime('2003-10-22 12:34','1 day')} {2003-10-23 12:34:00}
74*4520Snw141292datetest 2.18 {datetime('2003-10-22 12:34','+1 day')} {2003-10-23 12:34:00}
75*4520Snw141292datetest 2.19 {datetime('2003-10-22 12:34','+1.25 day')} {2003-10-23 18:34:00}
76*4520Snw141292datetest 2.20 {datetime('2003-10-22 12:34','-1.0 day')} {2003-10-21 12:34:00}
77*4520Snw141292datetest 2.21 {datetime('2003-10-22 12:34','1 month')} {2003-11-22 12:34:00}
78*4520Snw141292datetest 2.22 {datetime('2003-10-22 12:34','11 month')} {2004-09-22 12:34:00}
79*4520Snw141292datetest 2.23 {datetime('2003-10-22 12:34','-13 month')} {2002-09-22 12:34:00}
80*4520Snw141292datetest 2.24 {datetime('2003-10-22 12:34','1.5 months')} {2003-12-07 12:34:00}
81*4520Snw141292datetest 2.25 {datetime('2003-10-22 12:34','-5 years')} {1998-10-22 12:34:00}
82*4520Snw141292datetest 2.26 {datetime('2003-10-22 12:34','+10.5 minutes')} \
83*4520Snw141292  {2003-10-22 12:44:30}
84*4520Snw141292datetest 2.27 {datetime('2003-10-22 12:34','-1.25 hours')} \
85*4520Snw141292  {2003-10-22 11:19:00}
86*4520Snw141292datetest 2.28 {datetime('2003-10-22 12:34','11.25 seconds')} \
87*4520Snw141292  {2003-10-22 12:34:11}
88*4520Snw141292datetest 2.29 {datetime('2003-10-22 12:24','+5 bogus')} NULL
89*4520Snw141292
90*4520Snw141292
91*4520Snw141292datetest 3.1 {strftime('%d','2003-10-31 12:34:56.432')} 31
92*4520Snw141292datetest 3.2 {strftime('%f','2003-10-31 12:34:56.432')} 56.432
93*4520Snw141292datetest 3.3 {strftime('%H','2003-10-31 12:34:56.432')} 12
94*4520Snw141292datetest 3.4 {strftime('%j','2003-10-31 12:34:56.432')} 304
95*4520Snw141292datetest 3.5 {strftime('%J','2003-10-31 12:34:56.432')} 2452944.024264259
96*4520Snw141292datetest 3.6 {strftime('%m','2003-10-31 12:34:56.432')} 10
97*4520Snw141292datetest 3.7 {strftime('%M','2003-10-31 12:34:56.432')} 34
98*4520Snw141292datetest 3.8 {strftime('%s','2003-10-31 12:34:56.432')} 1067603696
99*4520Snw141292datetest 3.9 {strftime('%S','2003-10-31 12:34:56.432')} 56
100*4520Snw141292datetest 3.10 {strftime('%w','2003-10-31 12:34:56.432')} 5
101*4520Snw141292datetest 3.11.1 {strftime('%W','2003-10-31 12:34:56.432')} 43
102*4520Snw141292datetest 3.11.2 {strftime('%W','2004-01-01')} 00
103*4520Snw141292datetest 3.11.3 {strftime('%W','2004-01-02')} 00
104*4520Snw141292datetest 3.11.4 {strftime('%W','2004-01-03')} 00
105*4520Snw141292datetest 3.11.5 {strftime('%W','2004-01-04')} 00
106*4520Snw141292datetest 3.11.6 {strftime('%W','2004-01-05')} 01
107*4520Snw141292datetest 3.11.7 {strftime('%W','2004-01-06')} 01
108*4520Snw141292datetest 3.11.8 {strftime('%W','2004-01-07')} 01
109*4520Snw141292datetest 3.11.9 {strftime('%W','2004-01-08')} 01
110*4520Snw141292datetest 3.11.10 {strftime('%W','2004-01-09')} 01
111*4520Snw141292datetest 3.11.11 {strftime('%W','2004-07-18')} 28
112*4520Snw141292datetest 3.11.12 {strftime('%W','2004-12-31')} 52
113*4520Snw141292datetest 3.11.13 {strftime('%W','2007-12-31')} 53
114*4520Snw141292datetest 3.11.14 {strftime('%W','2007-01-01')} 01
115*4520Snw141292datetest 3.12 {strftime('%Y','2003-10-31 12:34:56.432')} 2003
116*4520Snw141292datetest 3.13 {strftime('%%','2003-10-31 12:34:56.432')} %
117*4520Snw141292datetest 3.14 {strftime('%_','2003-10-31 12:34:56.432')} NULL
118*4520Snw141292datetest 3.15 {strftime('%Y-%m-%d','2003-10-31')} 2003-10-31
119*4520Snw141292proc repeat {n txt} {
120*4520Snw141292  set x {}
121*4520Snw141292  while {$n>0} {
122*4520Snw141292    append x $txt
123*4520Snw141292    incr n -1
124*4520Snw141292  }
125*4520Snw141292  return $x
126*4520Snw141292}
127*4520Snw141292datetest 3.16 "strftime('[repeat 200 %Y]','2003-10-31')" [repeat 200 2003]
128*4520Snw141292datetest 3.17 "strftime('[repeat 200 abc%m123]','2003-10-31')" \
129*4520Snw141292    [repeat 200 abc10123]
130*4520Snw141292
131*4520Snw141292set now [clock format [clock seconds] -format "%Y-%m-%d" -gmt 1]
132*4520Snw141292datetest 4.1 {date('now')} $now
133*4520Snw141292
134*4520Snw141292datetest 5.1 {datetime('1994-04-16 14:00:00 -05:00')} {1994-04-16 09:00:00}
135*4520Snw141292datetest 5.2 {datetime('1994-04-16 14:00:00 +05:15')} {1994-04-16 19:15:00}
136*4520Snw141292datetest 5.3 {datetime('1994-04-16 05:00:00 -08:30')} {1994-04-15 20:30:00}
137*4520Snw141292datetest 5.4 {datetime('1994-04-16 14:00:00 +11:55')} {1994-04-17 01:55:00}
138*4520Snw141292
139*4520Snw141292# localtime->utc and utc->localtime conversions.  These tests only work
140*4520Snw141292# if the localtime is in the US Eastern Time (the time in Charlotte, NC
141*4520Snw141292# and in New York.)
142*4520Snw141292#
143*4520Snw141292if {[clock scan [clock format 0 -format {%b %d, %Y %H:%M:%S}] -gmt 1]==-18000} {
144*4520Snw141292  datetest 6.1 {datetime('2000-10-29 05:59:00','localtime')}\
145*4520Snw141292      {2000-10-29 01:59:00}
146*4520Snw141292  datetest 6.2 {datetime('2000-10-29 06:00:00','localtime')}\
147*4520Snw141292      {2000-10-29 01:00:00}
148*4520Snw141292  datetest 6.3 {datetime('2000-04-02 06:59:00','localtime')}\
149*4520Snw141292      {2000-04-02 01:59:00}
150*4520Snw141292  datetest 6.4 {datetime('2000-04-02 07:00:00','localtime')}\
151*4520Snw141292      {2000-04-02 03:00:00}
152*4520Snw141292  datetest 6.5 {datetime('2000-10-29 01:59:00','utc')} {2000-10-29 05:59:00}
153*4520Snw141292  datetest 6.6 {datetime('2000-10-29 02:00:00','utc')} {2000-10-29 07:00:00}
154*4520Snw141292  datetest 6.7 {datetime('2000-04-02 01:59:00','utc')} {2000-04-02 06:59:00}
155*4520Snw141292  datetest 6.8 {datetime('2000-04-02 02:00:00','utc')} {2000-04-02 06:00:00}
156*4520Snw141292
157*4520Snw141292  datetest 6.10 {datetime('2000-01-01 12:00:00','localtime')} \
158*4520Snw141292      {2000-01-01 07:00:00}
159*4520Snw141292  datetest 6.11 {datetime('1969-01-01 12:00:00','localtime')} \
160*4520Snw141292      {1969-01-01 07:00:00}
161*4520Snw141292  datetest 6.12 {datetime('2039-01-01 12:00:00','localtime')} \
162*4520Snw141292      {2039-01-01 07:00:00}
163*4520Snw141292  datetest 6.13 {datetime('2000-07-01 12:00:00','localtime')} \
164*4520Snw141292      {2000-07-01 08:00:00}
165*4520Snw141292  datetest 6.14 {datetime('1969-07-01 12:00:00','localtime')} \
166*4520Snw141292      {1969-07-01 07:00:00}
167*4520Snw141292  datetest 6.15 {datetime('2039-07-01 12:00:00','localtime')} \
168*4520Snw141292      {2039-07-01 07:00:00}
169*4520Snw141292  set sqlite_current_time \
170*4520Snw141292     [db eval {SELECT strftime('%s','2000-07-01 12:34:56')}]
171*4520Snw141292  datetest 6.16 {datetime('now','localtime')} {2000-07-01 08:34:56}
172*4520Snw141292  set sqlite_current_time 0
173*4520Snw141292}
174*4520Snw141292
175*4520Snw141292# Date-time functions that contain NULL arguments return a NULL
176*4520Snw141292# result.
177*4520Snw141292#
178*4520Snw141292datetest 7.1 {datetime(null)} NULL
179*4520Snw141292datetest 7.2 {datetime('now',null)} NULL
180*4520Snw141292datetest 7.3 {datetime('now','localtime',null)} NULL
181*4520Snw141292datetest 7.4 {time(null)} NULL
182*4520Snw141292datetest 7.5 {time('now',null)} NULL
183*4520Snw141292datetest 7.6 {time('now','localtime',null)} NULL
184*4520Snw141292datetest 7.7 {date(null)} NULL
185*4520Snw141292datetest 7.8 {date('now',null)} NULL
186*4520Snw141292datetest 7.9 {date('now','localtime',null)} NULL
187*4520Snw141292datetest 7.10 {julianday(null)} NULL
188*4520Snw141292datetest 7.11 {julianday('now',null)} NULL
189*4520Snw141292datetest 7.12 {julianday('now','localtime',null)} NULL
190*4520Snw141292datetest 7.13 {strftime(null,'now')} NULL
191*4520Snw141292datetest 7.14 {strftime('%s',null)} NULL
192*4520Snw141292datetest 7.15 {strftime('%s','now',null)} NULL
193*4520Snw141292datetest 7.16 {strftime('%s','now','localtime',null)} NULL
194*4520Snw141292
195*4520Snw141292# Test modifiers when the date begins as a julian day number - to
196*4520Snw141292# make sure the HH:MM:SS is preserved.  Ticket #551.
197*4520Snw141292#
198*4520Snw141292set sqlite_current_time [db eval {SELECT strftime('%s','2003-10-22 12:34:00')}]
199*4520Snw141292datetest 8.1 {datetime('now','weekday 0')} {2003-10-26 12:34:00}
200*4520Snw141292datetest 8.2 {datetime('now','weekday 1')} {2003-10-27 12:34:00}
201*4520Snw141292datetest 8.3 {datetime('now','weekday 2')} {2003-10-28 12:34:00}
202*4520Snw141292datetest 8.4 {datetime('now','weekday 3')} {2003-10-22 12:34:00}
203*4520Snw141292datetest 8.5 {datetime('now','start of month')} {2003-10-01 00:00:00}
204*4520Snw141292datetest 8.6 {datetime('now','start of year')} {2003-01-01 00:00:00}
205*4520Snw141292datetest 8.7 {datetime('now','start of day')} {2003-10-22 00:00:00}
206*4520Snw141292datetest 8.8 {datetime('now','1 day')} {2003-10-23 12:34:00}
207*4520Snw141292datetest 8.9 {datetime('now','+1 day')} {2003-10-23 12:34:00}
208*4520Snw141292datetest 8.10 {datetime('now','+1.25 day')} {2003-10-23 18:34:00}
209*4520Snw141292datetest 8.11 {datetime('now','-1.0 day')} {2003-10-21 12:34:00}
210*4520Snw141292datetest 8.12 {datetime('now','1 month')} {2003-11-22 12:34:00}
211*4520Snw141292datetest 8.13 {datetime('now','11 month')} {2004-09-22 12:34:00}
212*4520Snw141292datetest 8.14 {datetime('now','-13 month')} {2002-09-22 12:34:00}
213*4520Snw141292datetest 8.15 {datetime('now','1.5 months')} {2003-12-07 12:34:00}
214*4520Snw141292datetest 8.16 {datetime('now','-5 years')} {1998-10-22 12:34:00}
215*4520Snw141292datetest 8.17 {datetime('now','+10.5 minutes')} {2003-10-22 12:44:30}
216*4520Snw141292datetest 8.18 {datetime('now','-1.25 hours')} {2003-10-22 11:19:00}
217*4520Snw141292datetest 8.19 {datetime('now','11.25 seconds')} {2003-10-22 12:34:11}
218*4520Snw141292set sqlite_current_time 0
219*4520Snw141292
220*4520Snw141292# Negative years work.  Example:  '-4713-11-26' is JD 1.5.
221*4520Snw141292#
222*4520Snw141292datetest 9.1 {julianday('-4713-11-24 12:00:00')} {0}
223*4520Snw141292datetest 9.2 {julianday(datetime(5))} {5}
224*4520Snw141292datetest 9.3 {julianday(datetime(10))} {10}
225*4520Snw141292datetest 9.4 {julianday(datetime(100))} {100}
226*4520Snw141292datetest 9.5 {julianday(datetime(1000))} {1000}
227*4520Snw141292datetest 9.6 {julianday(datetime(10000))} {10000}
228*4520Snw141292datetest 9.7 {julianday(datetime(100000))} {100000}
229*4520Snw141292
230*4520Snw141292# datetime() with just an HH:MM:SS correctly inserts the date 2000-01-01.
231*4520Snw141292#
232*4520Snw141292datetest 10.1 {datetime('01:02:03')}  {2000-01-01 01:02:03}
233*4520Snw141292datetest 10.2 {date('01:02:03')}  {2000-01-01}
234*4520Snw141292datetest 10.3 {strftime('%Y-%m-%d %H:%M','01:02:03')} {2000-01-01 01:02}
235*4520Snw141292
236*4520Snw141292# Test the new HH:MM:SS modifier
237*4520Snw141292#
238*4520Snw141292datetest 11.1 {datetime('2004-02-28 20:00:00', '-01:20:30')} \
239*4520Snw141292   {2004-02-28 18:39:30}
240*4520Snw141292datetest 11.2 {datetime('2004-02-28 20:00:00', '+12:30:00')} \
241*4520Snw141292   {2004-02-29 08:30:00}
242*4520Snw141292datetest 11.3 {datetime('2004-02-28 20:00:00', '+12:30')} \
243*4520Snw141292   {2004-02-29 08:30:00}
244*4520Snw141292datetest 11.4 {datetime('2004-02-28 20:00:00', '12:30')} \
245*4520Snw141292   {2004-02-29 08:30:00}
246*4520Snw141292datetest 11.5 {datetime('2004-02-28 20:00:00', '-12:00')} \
247*4520Snw141292   {2004-02-28 08:00:00}
248*4520Snw141292datetest 11.6 {datetime('2004-02-28 20:00:00', '-12:01')} \
249*4520Snw141292   {2004-02-28 07:59:00}
250*4520Snw141292datetest 11.7 {datetime('2004-02-28 20:00:00', '-11:59')} \
251*4520Snw141292   {2004-02-28 08:01:00}
252*4520Snw141292datetest 11.8 {datetime('2004-02-28 20:00:00', '11:59')} \
253*4520Snw141292   {2004-02-29 07:59:00}
254*4520Snw141292datetest 11.9 {datetime('2004-02-28 20:00:00', '12:01')} \
255*4520Snw141292   {2004-02-29 08:01:00}
256*4520Snw141292
257*4520Snw141292
258*4520Snw141292
259*4520Snw141292
260*4520Snw141292finish_test
261