2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library. The
2N/A# focus of this file is testing date and time functions.
2N/Aset testdir [file dirname $argv0]
2N/Aproc datetest {tnum expr result} {
2N/A do_test date-$tnum [subst {
2N/A execsql "SELECT coalesce($expr,'NULL')"
2N/Adatetest 1.1 julianday('2000-01-01') 2451544.5
2N/Adatetest 1.2 julianday('1970-01-01') 2440587.5
2N/Adatetest 1.3 julianday('1910-04-20') 2418781.5
2N/Adatetest 1.4 julianday('1986-02-09') 2446470.5
2N/Adatetest 1.5 julianday('12:00:00') 2451545
2N/Adatetest 1.6 {julianday('2000-01-01 12:00:00')} 2451545
2N/Adatetest 1.7 {julianday('2000-01-01 12:00')} 2451545
2N/Adatetest 1.8 julianday('bogus') NULL
2N/Adatetest 1.9 julianday('1999-12-31') 2451543.5
2N/Adatetest 1.10 julianday('1999-12-32') NULL
2N/Adatetest 1.11 julianday('1999-13-01') NULL
2N/Adatetest 1.12 julianday('2003-02-31') 2452701.5
2N/Adatetest 1.13 julianday('2003-03-03') 2452701.5
2N/Adatetest 1.14 julianday('+2000-01-01') NULL
2N/Adatetest 1.15 julianday('200-01-01') NULL
2N/Adatetest 1.16 julianday('2000-1-01') NULL
2N/Adatetest 1.17 julianday('2000-01-1') NULL
2N/Adatetest 1.18 {julianday('2000-01-01 12:00:00')} 2451545
2N/Adatetest 1.19 {julianday('2000-01-01 12:00:00.1')} 2451545.00000116
2N/Adatetest 1.20 {julianday('2000-01-01 12:00:00.01')} 2451545.00000012
2N/Adatetest 1.21 {julianday('2000-01-01 12:00:00.001')} 2451545.00000001
2N/Adatetest 1.22 {julianday('2000-01-01 12:00:00.')} NULL
2N/Adatetest 1.23 julianday(12345.6) 12345.6
2N/Adatetest 1.24 {julianday('2001-01-01 12:00:00 bogus')} NULL
2N/Adatetest 1.25 {julianday('2001-01-01 bogus')} NULL
2N/Adatetest 2.1 datetime(0,'unixepoch') {1970-01-01 00:00:00}
2N/Adatetest 2.2 datetime(946684800,'unixepoch') {2000-01-01 00:00:00}
2N/Adatetest 2.3 {date('2003-10-22','weekday 0')} 2003-10-26
2N/Adatetest 2.4 {date('2003-10-22','weekday 1')} 2003-10-27
2N/Adatetest 2.5 {date('2003-10-22','weekday 2')} 2003-10-28
2N/Adatetest 2.6 {date('2003-10-22','weekday 3')} 2003-10-22
2N/Adatetest 2.7 {date('2003-10-22','weekday 4')} 2003-10-23
2N/Adatetest 2.8 {date('2003-10-22','weekday 5')} 2003-10-24
2N/Adatetest 2.9 {date('2003-10-22','weekday 6')} 2003-10-25
2N/Adatetest 2.10 {date('2003-10-22','weekday 7')} NULL
2N/Adatetest 2.11 {date('2003-10-22','weekday 5.5')} NULL
2N/Adatetest 2.12 {datetime('2003-10-22 12:34','weekday 0')} {2003-10-26 12:34:00}
2N/Adatetest 2.13 {datetime('2003-10-22 12:34','start of month')} \
2N/A {2003-10-01 00:00:00}
2N/Adatetest 2.14 {datetime('2003-10-22 12:34','start of year')} \
2N/A {2003-01-01 00:00:00}
2N/Adatetest 2.15 {datetime('2003-10-22 12:34','start of day')} \
2N/A {2003-10-22 00:00:00}
2N/Adatetest 2.16 time('12:34:56.43') 12:34:56
2N/Adatetest 2.17 {datetime('2003-10-22 12:34','1 day')} {2003-10-23 12:34:00}
2N/Adatetest 2.18 {datetime('2003-10-22 12:34','+1 day')} {2003-10-23 12:34:00}
2N/Adatetest 2.19 {datetime('2003-10-22 12:34','+1.25 day')} {2003-10-23 18:34:00}
2N/Adatetest 2.20 {datetime('2003-10-22 12:34','-1.0 day')} {2003-10-21 12:34:00}
2N/Adatetest 2.21 {datetime('2003-10-22 12:34','1 month')} {2003-11-22 12:34:00}
2N/Adatetest 2.22 {datetime('2003-10-22 12:34','11 month')} {2004-09-22 12:34:00}
2N/Adatetest 2.23 {datetime('2003-10-22 12:34','-13 month')} {2002-09-22 12:34:00}
2N/Adatetest 2.24 {datetime('2003-10-22 12:34','1.5 months')} {2003-12-07 12:34:00}
2N/Adatetest 2.25 {datetime('2003-10-22 12:34','-5 years')} {1998-10-22 12:34:00}
2N/Adatetest 2.26 {datetime('2003-10-22 12:34','+10.5 minutes')} \
2N/A {2003-10-22 12:44:30}
2N/Adatetest 2.27 {datetime('2003-10-22 12:34','-1.25 hours')} \
2N/A {2003-10-22 11:19:00}
2N/Adatetest 2.28 {datetime('2003-10-22 12:34','11.25 seconds')} \
2N/A {2003-10-22 12:34:11}
2N/Adatetest 2.29 {datetime('2003-10-22 12:24','+5 bogus')} NULL
2N/Adatetest 3.1 {strftime('%d','2003-10-31 12:34:56.432')} 31
2N/Adatetest 3.2 {strftime('%f','2003-10-31 12:34:56.432')} 56.432
2N/Adatetest 3.3 {strftime('%H','2003-10-31 12:34:56.432')} 12
2N/Adatetest 3.4 {strftime('%j','2003-10-31 12:34:56.432')} 304
2N/Adatetest 3.5 {strftime('%J','2003-10-31 12:34:56.432')} 2452944.024264259
2N/Adatetest 3.6 {strftime('%m','2003-10-31 12:34:56.432')} 10
2N/Adatetest 3.7 {strftime('%M','2003-10-31 12:34:56.432')} 34
2N/Adatetest 3.8 {strftime('%s','2003-10-31 12:34:56.432')} 1067603696
2N/Adatetest 3.9 {strftime('%S','2003-10-31 12:34:56.432')} 56
2N/Adatetest 3.10 {strftime('%w','2003-10-31 12:34:56.432')} 5
2N/Adatetest 3.11.1 {strftime('%W','2003-10-31 12:34:56.432')} 43
2N/Adatetest 3.11.2 {strftime('%W','2004-01-01')} 00
2N/Adatetest 3.11.3 {strftime('%W','2004-01-02')} 00
2N/Adatetest 3.11.4 {strftime('%W','2004-01-03')} 00
2N/Adatetest 3.11.5 {strftime('%W','2004-01-04')} 00
2N/Adatetest 3.11.6 {strftime('%W','2004-01-05')} 01
2N/Adatetest 3.11.7 {strftime('%W','2004-01-06')} 01
2N/Adatetest 3.11.8 {strftime('%W','2004-01-07')} 01
2N/Adatetest 3.11.9 {strftime('%W','2004-01-08')} 01
2N/Adatetest 3.11.10 {strftime('%W','2004-01-09')} 01
2N/Adatetest 3.11.11 {strftime('%W','2004-07-18')} 28
2N/Adatetest 3.11.12 {strftime('%W','2004-12-31')} 52
2N/Adatetest 3.11.13 {strftime('%W','2007-12-31')} 53
2N/Adatetest 3.11.14 {strftime('%W','2007-01-01')} 01
2N/Adatetest 3.12 {strftime('%Y','2003-10-31 12:34:56.432')} 2003
2N/Adatetest 3.13 {strftime('%%','2003-10-31 12:34:56.432')} %
2N/Adatetest 3.14 {strftime('%_','2003-10-31 12:34:56.432')} NULL
2N/Adatetest 3.15 {strftime('%Y-%m-%d','2003-10-31')} 2003-10-31
2N/Aproc repeat {n txt} {
2N/Adatetest 3.16 "strftime('[repeat 200 %Y]','2003-10-31')" [repeat 200 2003]
2N/Adatetest 3.17 "strftime('[repeat 200 abc%m123]','2003-10-31')" \
2N/A [repeat 200 abc10123]
2N/Aset now [clock format [clock seconds] -format "%Y-%m-%d" -gmt 1]
2N/Adatetest 4.1 {date('now')} $now
2N/Adatetest 5.1 {datetime('1994-04-16 14:00:00 -05:00')} {1994-04-16 09:00:00}
2N/Adatetest 5.2 {datetime('1994-04-16 14:00:00 +05:15')} {1994-04-16 19:15:00}
2N/Adatetest 5.3 {datetime('1994-04-16 05:00:00 -08:30')} {1994-04-15 20:30:00}
2N/Adatetest 5.4 {datetime('1994-04-16 14:00:00 +11:55')} {1994-04-17 01:55:00}
2N/A# localtime->utc and utc->localtime conversions. These tests only work
2N/A# if the localtime is in the US Eastern Time (the time in Charlotte, NC
2N/Aif {[clock scan [clock format 0 -format {%b %d, %Y %H:%M:%S}] -gmt 1]==-18000} {
2N/A datetest 6.1 {datetime('2000-10-29 05:59:00','localtime')}\
2N/A {2000-10-29 01:59:00}
2N/A datetest 6.2 {datetime('2000-10-29 06:00:00','localtime')}\
2N/A {2000-10-29 01:00:00}
2N/A datetest 6.3 {datetime('2000-04-02 06:59:00','localtime')}\
2N/A {2000-04-02 01:59:00}
2N/A datetest 6.4 {datetime('2000-04-02 07:00:00','localtime')}\
2N/A {2000-04-02 03:00:00}
2N/A datetest 6.5 {datetime('2000-10-29 01:59:00','utc')} {2000-10-29 05:59:00}
2N/A datetest 6.6 {datetime('2000-10-29 02:00:00','utc')} {2000-10-29 07:00:00}
2N/A datetest 6.7 {datetime('2000-04-02 01:59:00','utc')} {2000-04-02 06:59:00}
2N/A datetest 6.8 {datetime('2000-04-02 02:00:00','utc')} {2000-04-02 06:00:00}
2N/A datetest 6.10 {datetime('2000-01-01 12:00:00','localtime')} \
2N/A {2000-01-01 07:00:00}
2N/A datetest 6.11 {datetime('1969-01-01 12:00:00','localtime')} \
2N/A {1969-01-01 07:00:00}
2N/A datetest 6.12 {datetime('2039-01-01 12:00:00','localtime')} \
2N/A {2039-01-01 07:00:00}
2N/A datetest 6.13 {datetime('2000-07-01 12:00:00','localtime')} \
2N/A {2000-07-01 08:00:00}
2N/A datetest 6.14 {datetime('1969-07-01 12:00:00','localtime')} \
2N/A {1969-07-01 07:00:00}
2N/A datetest 6.15 {datetime('2039-07-01 12:00:00','localtime')} \
2N/A {2039-07-01 07:00:00}
2N/A set sqlite_current_time \
2N/A [db eval {SELECT strftime('%s','2000-07-01 12:34:56')}]
2N/A datetest 6.16 {datetime('now','localtime')} {2000-07-01 08:34:56}
2N/A set sqlite_current_time 0
2N/A# Date-time functions that contain NULL arguments return a NULL
2N/Adatetest 7.1 {datetime(null)} NULL
2N/Adatetest 7.2 {datetime('now',null)} NULL
2N/Adatetest 7.3 {datetime('now','localtime',null)} NULL
2N/Adatetest 7.4 {time(null)} NULL
2N/Adatetest 7.5 {time('now',null)} NULL
2N/Adatetest 7.6 {time('now','localtime',null)} NULL
2N/Adatetest 7.7 {date(null)} NULL
2N/Adatetest 7.8 {date('now',null)} NULL
2N/Adatetest 7.9 {date('now','localtime',null)} NULL
2N/Adatetest 7.10 {julianday(null)} NULL
2N/Adatetest 7.11 {julianday('now',null)} NULL
2N/Adatetest 7.12 {julianday('now','localtime',null)} NULL
2N/Adatetest 7.13 {strftime(null,'now')} NULL
2N/Adatetest 7.14 {strftime('%s',null)} NULL
2N/Adatetest 7.15 {strftime('%s','now',null)} NULL
2N/Adatetest 7.16 {strftime('%s','now','localtime',null)} NULL
2N/A# Test modifiers when the date begins as a julian day number - to
2N/A# make sure the HH:MM:SS is preserved. Ticket #551.
2N/Aset sqlite_current_time [db eval {SELECT strftime('%s','2003-10-22 12:34:00')}]
2N/Adatetest 8.1 {datetime('now','weekday 0')} {2003-10-26 12:34:00}
2N/Adatetest 8.2 {datetime('now','weekday 1')} {2003-10-27 12:34:00}
2N/Adatetest 8.3 {datetime('now','weekday 2')} {2003-10-28 12:34:00}
2N/Adatetest 8.4 {datetime('now','weekday 3')} {2003-10-22 12:34:00}
2N/Adatetest 8.5 {datetime('now','start of month')} {2003-10-01 00:00:00}
2N/Adatetest 8.6 {datetime('now','start of year')} {2003-01-01 00:00:00}
2N/Adatetest 8.7 {datetime('now','start of day')} {2003-10-22 00:00:00}
2N/Adatetest 8.8 {datetime('now','1 day')} {2003-10-23 12:34:00}
2N/Adatetest 8.9 {datetime('now','+1 day')} {2003-10-23 12:34:00}
2N/Adatetest 8.10 {datetime('now','+1.25 day')} {2003-10-23 18:34:00}
2N/Adatetest 8.11 {datetime('now','-1.0 day')} {2003-10-21 12:34:00}
2N/Adatetest 8.12 {datetime('now','1 month')} {2003-11-22 12:34:00}
2N/Adatetest 8.13 {datetime('now','11 month')} {2004-09-22 12:34:00}
2N/Adatetest 8.14 {datetime('now','-13 month')} {2002-09-22 12:34:00}
2N/Adatetest 8.15 {datetime('now','1.5 months')} {2003-12-07 12:34:00}
2N/Adatetest 8.16 {datetime('now','-5 years')} {1998-10-22 12:34:00}
2N/Adatetest 8.17 {datetime('now','+10.5 minutes')} {2003-10-22 12:44:30}
2N/Adatetest 8.18 {datetime('now','-1.25 hours')} {2003-10-22 11:19:00}
2N/Adatetest 8.19 {datetime('now','11.25 seconds')} {2003-10-22 12:34:11}
2N/Aset sqlite_current_time 0
2N/A# Negative years work. Example: '-4713-11-26' is JD 1.5.
2N/Adatetest 9.1 {julianday('-4713-11-24 12:00:00')} {0}
2N/Adatetest 9.2 {julianday(datetime(5))} {5}
2N/Adatetest 9.3 {julianday(datetime(10))} {10}
2N/Adatetest 9.4 {julianday(datetime(100))} {100}
2N/Adatetest 9.5 {julianday(datetime(1000))} {1000}
2N/Adatetest 9.6 {julianday(datetime(10000))} {10000}
2N/Adatetest 9.7 {julianday(datetime(100000))} {100000}
2N/A# datetime() with just an HH:MM:SS correctly inserts the date 2000-01-01.
2N/Adatetest 10.1 {datetime('01:02:03')} {2000-01-01 01:02:03}
2N/Adatetest 10.2 {date('01:02:03')} {2000-01-01}
2N/Adatetest 10.3 {strftime('%Y-%m-%d %H:%M','01:02:03')} {2000-01-01 01:02}
2N/A# Test the new HH:MM:SS modifier
2N/Adatetest 11.1 {datetime('2004-02-28 20:00:00', '-01:20:30')} \
2N/A {2004-02-28 18:39:30}
2N/Adatetest 11.2 {datetime('2004-02-28 20:00:00', '+12:30:00')} \
2N/A {2004-02-29 08:30:00}
2N/Adatetest 11.3 {datetime('2004-02-28 20:00:00', '+12:30')} \
2N/A {2004-02-29 08:30:00}
2N/Adatetest 11.4 {datetime('2004-02-28 20:00:00', '12:30')} \
2N/A {2004-02-29 08:30:00}
2N/Adatetest 11.5 {datetime('2004-02-28 20:00:00', '-12:00')} \
2N/A {2004-02-28 08:00:00}
2N/Adatetest 11.6 {datetime('2004-02-28 20:00:00', '-12:01')} \
2N/A {2004-02-28 07:59:00}
2N/Adatetest 11.7 {datetime('2004-02-28 20:00:00', '-11:59')} \
2N/A {2004-02-28 08:01:00}
2N/Adatetest 11.8 {datetime('2004-02-28 20:00:00', '11:59')} \
2N/A {2004-02-29 07:59:00}
2N/Adatetest 11.9 {datetime('2004-02-28 20:00:00', '12:01')} \
2N/A {2004-02-29 08:01:00}