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