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