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