000001  # 2003 October 31
000002  #
000003  # The author disclaims copyright to this source code.  In place of
000004  # a legal notice, here is a blessing:
000005  #
000006  #    May you do good and not evil.
000007  #    May you find forgiveness for yourself and forgive others.
000008  #    May you share freely, never taking more than you give.
000009  #
000010  #***********************************************************************
000011  # This file implements regression tests for SQLite library.  The
000012  # focus of this file is testing date and time functions.
000013  #
000014  # $Id: date.test,v 1.34 2009/04/16 12:58:03 drh Exp $
000015  
000016  set testdir [file dirname $argv0]
000017  source $testdir/tester.tcl
000018  
000019  # Do not use a codec for tests in this file, as the database file is
000020  # manipulated directly using tcl scripts (using the [hexio_write] command).
000021  #
000022  do_not_use_codec
000023  
000024  # Skip this whole file if date and time functions are omitted
000025  # at compile-time
000026  #
000027  ifcapable {!datetime} {
000028    finish_test
000029    return
000030  }
000031  
000032  proc datetest {tnum expr result} {
000033    do_test date-$tnum [subst {
000034      execsql "SELECT coalesce($expr,'NULL')"
000035    }] [list $result]
000036  }
000037  set tcl_precision 15
000038  datetest 1.1 julianday('2000-01-01') 2451544.5
000039  datetest 1.2 julianday('1970-01-01') 2440587.5
000040  datetest 1.3 julianday('1910-04-20') 2418781.5
000041  datetest 1.4 julianday('1986-02-09') 2446470.5
000042  datetest 1.5 julianday('12:00:00') 2451545.0
000043  datetest 1.6 {julianday('2000-01-01 12:00:00')} 2451545.0
000044  datetest 1.7 {julianday('2000-01-01 12:00')} 2451545.0
000045  datetest 1.8 julianday('bogus') NULL
000046  datetest 1.9 julianday('1999-12-31') 2451543.5
000047  datetest 1.10 julianday('1999-12-32') NULL
000048  datetest 1.11 julianday('1999-13-01') NULL
000049  datetest 1.12 julianday('2003-02-31') 2452701.5
000050  datetest 1.13 julianday('2003-03-03') 2452701.5
000051  datetest 1.14 julianday('+2000-01-01') NULL
000052  datetest 1.15 julianday('200-01-01') NULL
000053  datetest 1.16 julianday('2000-1-01') NULL
000054  datetest 1.17 julianday('2000-01-1') NULL
000055  datetest 1.18.1 {julianday('2000-01-01     12:00:00')} 2451545.0
000056  datetest 1.18.2 {julianday('2000-01-01T12:00:00')} 2451545.0
000057  datetest 1.18.3 {julianday('2000-01-01 T12:00:00')} 2451545.0
000058  datetest 1.18.4 {julianday('2000-01-01T 12:00:00')} 2451545.0
000059  datetest 1.18.4 {julianday('2000-01-01 T 12:00:00')} 2451545.0
000060  datetest 1.19 {julianday('2000-01-01 12:00:00.1')}   2451545.00000116
000061  datetest 1.20 {julianday('2000-01-01 12:00:00.01')}  2451545.00000012
000062  datetest 1.21 {julianday('2000-01-01 12:00:00.001')} 2451545.00000001
000063  datetest 1.22 {julianday('2000-01-01 12:00:00.')} NULL
000064  datetest 1.23 julianday(12345.6) 12345.6
000065  datetest 1.23b julianday(1721059.5) 1721059.5
000066  datetest 1.24 {julianday('2001-01-01 12:00:00 bogus')} NULL
000067  datetest 1.25 {julianday('2001-01-01 bogus')} NULL
000068  datetest 1.26 {julianday('2001-01-01 12:60:00')} NULL
000069  datetest 1.27 {julianday('2001-01-01 12:59:60')} NULL
000070  datetest 1.28 {julianday('2001-00-01')} NULL
000071  datetest 1.29 {julianday('2001-01-00')} NULL
000072  
000073  datetest 2.1 datetime(0,'unixepoch') {1970-01-01 00:00:00}
000074  datetest 2.1b datetime(0,'unixepoc') NULL
000075  datetest 2.1c datetime(0,'unixepochx') NULL
000076  datetest 2.1d datetime('2003-10-22','unixepoch') NULL
000077  datetest 2.2 datetime(946684800,'unixepoch') {2000-01-01 00:00:00}
000078  datetest 2.2b datetime('946684800','unixepoch') {2000-01-01 00:00:00}
000079  for {set i 0} {$i<1000} {incr i} {
000080    set sql [format {strftime('%%H:%%M:%%f',1237962480.%03d,'unixepoch')} $i]
000081    set res [format {06:28:00.%03d} $i]
000082    datetest 2.2c-$i $sql $res
000083  }
000084  datetest 2.3 {date('2003-10-22','weekday 0')} 2003-10-26
000085  datetest 2.4 {date('2003-10-22','weekday 1')} 2003-10-27
000086  datetest 2.4a {date('2003-10-22','weekday  1')} 2003-10-27
000087  datetest 2.4b {date('2003-10-22','weekday  1x')} NULL
000088  datetest 2.4c {date('2003-10-22','weekday  -1')} NULL
000089  datetest 2.4d {date('2003-10-22','weakday  1x')} NULL
000090  datetest 2.4e {date('2003-10-22','weekday ')} NULL
000091  datetest 2.5 {date('2003-10-22','weekday 2')} 2003-10-28
000092  datetest 2.6 {date('2003-10-22','weekday 3')} 2003-10-22
000093  datetest 2.7 {date('2003-10-22','weekday 4')} 2003-10-23
000094  datetest 2.8 {date('2003-10-22','weekday 5')} 2003-10-24
000095  datetest 2.9 {date('2003-10-22','weekday 6')} 2003-10-25
000096  datetest 2.10 {date('2003-10-22','weekday 7')} NULL
000097  datetest 2.11 {date('2003-10-22','weekday 5.5')} NULL
000098  datetest 2.12 {datetime('2003-10-22 12:34','weekday 0')} {2003-10-26 12:34:00}
000099  datetest 2.13 {datetime('2003-10-22 12:34','start of month')} \
000100     {2003-10-01 00:00:00}
000101  datetest 2.14 {datetime('2003-10-22 12:34','start of year')} \
000102     {2003-01-01 00:00:00}
000103  datetest 2.15 {datetime('2003-10-22 12:34','start of day')} \
000104     {2003-10-22 00:00:00}
000105  datetest 2.15a {datetime('2003-10-22 12:34','start of')} NULL
000106  datetest 2.15b {datetime('2003-10-22 12:34','start of bogus')} NULL
000107  datetest 2.16 time('12:34:56.43') 12:34:56
000108  datetest 2.17 {datetime('2003-10-22 12:34','1 day')} {2003-10-23 12:34:00}
000109  datetest 2.18 {datetime('2003-10-22 12:34','+1 day')} {2003-10-23 12:34:00}
000110  datetest 2.19 {datetime('2003-10-22 12:34','+1.25 day')} {2003-10-23 18:34:00}
000111  datetest 2.20 {datetime('2003-10-22 12:34','-1.0 day')} {2003-10-21 12:34:00}
000112  datetest 2.21 {datetime('2003-10-22 12:34','1 month')} {2003-11-22 12:34:00}
000113  datetest 2.22 {datetime('2003-10-22 12:34','11 month')} {2004-09-22 12:34:00}
000114  datetest 2.23 {datetime('2003-10-22 12:34','-13 month')} {2002-09-22 12:34:00}
000115  datetest 2.24 {datetime('2003-10-22 12:34','1.5 months')} {2003-12-07 12:34:00}
000116  datetest 2.25 {datetime('2003-10-22 12:34','-5 years')} {1998-10-22 12:34:00}
000117  datetest 2.26 {datetime('2003-10-22 12:34','+10.5 minutes')} \
000118    {2003-10-22 12:44:30}
000119  datetest 2.27 {datetime('2003-10-22 12:34','-1.25 hours')} \
000120    {2003-10-22 11:19:00}
000121  datetest 2.28 {datetime('2003-10-22 12:34','11.25 seconds')} \
000122    {2003-10-22 12:34:11}
000123  datetest 2.29 {datetime('2003-10-22 12:24','+5 bogus')} NULL
000124  datetest 2.30 {datetime('2003-10-22 12:24','+++')} NULL
000125  datetest 2.31 {datetime('2003-10-22 12:24','+12.3e4 femtoseconds')} NULL
000126  datetest 2.32 {datetime('2003-10-22 12:24','+12.3e4 uS')} NULL
000127  datetest 2.33 {datetime('2003-10-22 12:24','+1 abc')} NULL
000128  datetest 2.34 {datetime('2003-10-22 12:24','+1 abcd')} NULL
000129  datetest 2.35 {datetime('2003-10-22 12:24','+1 abcde')} NULL
000130  datetest 2.36 {datetime('2003-10-22 12:24','+1 abcdef')} NULL
000131  datetest 2.37 {datetime('2003-10-22 12:24','+1 abcdefg')} NULL
000132  datetest 2.38 {datetime('2003-10-22 12:24','+1 abcdefgh')} NULL
000133  datetest 2.39 {datetime('2003-10-22 12:24','+1 abcdefghi')} NULL
000134  set sqlite_current_time 1199243045
000135  datetest 2.40 {datetime()} {2008-01-02 03:04:05}
000136  set sqlite_current_time 0
000137  datetest 2.41 {datetime('2003-10-22 12:24','23 seconds')} {2003-10-22 12:24:23}
000138  datetest 2.42 {datetime('2003-10-22 12:24','345 second')} {2003-10-22 12:29:45}
000139  datetest 2.43 {datetime('2003-10-22 12:24','4 second')} {2003-10-22 12:24:04}
000140  datetest 2.44 {datetime('2003-10-22 12:24','56 second')} {2003-10-22 12:24:56}
000141  datetest 2.45 {datetime('2003-10-22 12:24','60 second')} {2003-10-22 12:25:00}
000142  datetest 2.46 {datetime('2003-10-22 12:24','70 second')} {2003-10-22 12:25:10}
000143  datetest 2.47 {datetime('2003-10-22 12:24','8.6 seconds')} {2003-10-22 12:24:08}
000144  datetest 2.48 {datetime('2003-10-22 12:24','9.4 second')} {2003-10-22 12:24:09}
000145  datetest 2.49 {datetime('2003-10-22 12:24','0000 second')} {2003-10-22 12:24:00}
000146  datetest 2.50 {datetime('2003-10-22 12:24','0001 second')} {2003-10-22 12:24:01}
000147  datetest 2.51 {datetime('2003-10-22 12:24','nonsense')} NULL
000148  
000149  datetest 2.60 {datetime('2023-02-31')} {2023-03-03 00:00:00}
000150  
000151  datetest 3.1 {strftime('%d','2003-10-31 12:34:56.432')} 31
000152  datetest 3.2.1 {strftime('pre%fpost','2003-10-31 12:34:56.432')} pre56.432post
000153  datetest 3.2.2 {strftime('%f','2003-10-31 12:34:59.9999999')} 59.999
000154  datetest 3.3 {strftime('%H','2003-10-31 12:34:56.432')} 12
000155  datetest 3.4 {strftime('%j','2003-10-31 12:34:56.432')} 304
000156  datetest 3.5 {strftime('%J','2003-10-31 12:34:56.432')} 2452944.024264259
000157  datetest 3.6 {strftime('%m','2003-10-31 12:34:56.432')} 10
000158  datetest 3.7 {strftime('%M','2003-10-31 12:34:56.432')} 34
000159  datetest 3.8.1 {strftime('%s','2003-10-31 12:34:56.432')} 1067603696
000160  datetest 3.8.2 {strftime('%s','2038-01-19 03:14:07')} 2147483647
000161  datetest 3.8.3 {strftime('%s','2038-01-19 03:14:08')} 2147483648
000162  datetest 3.8.4 {strftime('%s','2201-04-09 12:00:00')} 7298164800
000163  datetest 3.8.5 {strftime('%s','9999-12-31 23:59:59')} 253402300799
000164  datetest 3.8.6 {strftime('%s','1969-12-31 23:59:59')} -1
000165  datetest 3.8.7 {strftime('%s','1901-12-13 20:45:52')} -2147483648
000166  datetest 3.8.8 {strftime('%s','1901-12-13 20:45:51')} -2147483649
000167  datetest 3.8.9 {strftime('%s','1776-07-04 00:00:00')} -6106060800
000168  datetest 3.9 {strftime('%S','2003-10-31 12:34:56.432')} 56
000169  datetest 3.10 {strftime('%w','2003-10-31 12:34:56.432')} 5
000170  datetest 3.11.1 {strftime('%W','2003-10-31 12:34:56.432')} 43
000171  datetest 3.11.2 {strftime('%W','2004-01-01')} 00
000172  datetest 3.11.3 {strftime('%W','2004-01-02')} 00
000173  datetest 3.11.4 {strftime('%W','2004-01-03')} 00
000174  datetest 3.11.5 {strftime('abc%Wxyz','2004-01-04')} abc00xyz
000175  datetest 3.11.6 {strftime('%W','2004-01-05')} 01
000176  datetest 3.11.7 {strftime('%W','2004-01-06')} 01
000177  datetest 3.11.8 {strftime('%W','2004-01-07')} 01
000178  datetest 3.11.9 {strftime('%W','2004-01-08')} 01
000179  datetest 3.11.10 {strftime('%W','2004-01-09')} 01
000180  datetest 3.11.11 {strftime('%W','2004-07-18')} 28
000181  datetest 3.11.12 {strftime('%W','2004-12-31')} 52
000182  datetest 3.11.13 {strftime('%W','2007-12-31')} 53
000183  datetest 3.11.14 {strftime('%W','2007-01-01')} 01
000184  datetest 3.11.15 {strftime('%W %j',2454109.04140970)} {02 008}
000185  datetest 3.11.16 {strftime('%W %j',2454109.04140971)} {02 008}
000186  datetest 3.11.17 {strftime('%W %j',2454109.04140972)} {02 008}
000187  datetest 3.11.18 {strftime('%W %j',2454109.04140973)} {02 008}
000188  datetest 3.11.19 {strftime('%W %j',2454109.04140974)} {02 008}
000189  datetest 3.11.20 {strftime('%W %j',2454109.04140975)} {02 008}
000190  datetest 3.11.21 {strftime('%W %j',2454109.04140976)} {02 008}
000191  datetest 3.11.22 {strftime('%W %j',2454109.04140977)} {02 008}
000192  datetest 3.11.23 {strftime('%W %j',2454109.04140978)} {02 008}
000193  datetest 3.11.24 {strftime('%W %j',2454109.04140979)} {02 008}
000194  datetest 3.11.25 {strftime('%W %j',2454109.04140980)} {02 008}
000195  datetest 3.11.99 {strftime('%W %j','2454109.04140970')} {02 008}
000196  datetest 3.12 {strftime('%Y','2003-10-31 12:34:56.432')} 2003
000197  datetest 3.13 {strftime('%%','2003-10-31 12:34:56.432')} %
000198  datetest 3.14 {strftime('%_','2003-10-31 12:34:56.432')} NULL
000199  datetest 3.15 {strftime('%Y-%m-%d','2003-10-31')} 2003-10-31
000200  proc repeat {n txt} {
000201    set x {} 
000202    while {$n>0} {
000203      append x $txt
000204      incr n -1
000205    }
000206    return $x
000207  }
000208  datetest 3.16 "strftime('[repeat 200 %Y]','2003-10-31')" [repeat 200 2003]
000209  datetest 3.17 "strftime('[repeat 200 abc%m123]','2003-10-31')" \
000210      [repeat 200 abc10123]
000211  
000212  foreach c {a b c h i n o q r t v x y z
000213             A B C D E K L N O Q Z
000214             0 1 2 3 4 5 6 6 7 9 _} {
000215    datetest 3.18.$c "strftime('%$c','2003-10-31')" NULL
000216  }
000217  datetest 3.20 {strftime('%e','2023-08-09')} { 9}
000218  datetest 3.21 {strftime('%F %T','2023-08-09 01:23')} {2023-08-09 01:23:00}
000219  datetest 3.22 {strftime('%k','2023-08-09 04:59:59')} { 4}
000220  datetest 3.23 {strftime('%I%P','2023-08-09 11:59:59')} {11am}
000221  datetest 3.24 {strftime('%I%p','2023-08-09 12:00:00')} {12PM}
000222  datetest 3.25 {strftime('%I%P','2023-08-09 12:59:59.9')} {12pm}
000223  datetest 3.26 {strftime('%I%p','2023-08-09 13:00:00')} {01PM}
000224  datetest 3.27 {strftime('%I%P','2023-08-09 23:59:59')} {11pm}
000225  datetest 3.28 {strftime('%I%p','2023-08-09 00:00:00')} {12AM}
000226  datetest 3.29 {strftime('%l:%M%P','2023-08-09 13:00:00')} { 1:00pm}
000227  datetest 3.30 {strftime('%F %R','2023-08-09 12:34:56')} {2023-08-09 12:34}
000228  datetest 3.31 {strftime('%w %u','2023-01-01')} {0 7}
000229  datetest 3.32 {strftime('%w %u','2023-01-02')} {1 1}
000230  datetest 3.33 {strftime('%w %u','2023-01-03')} {2 2}
000231  datetest 3.34 {strftime('%w %u','2023-01-04')} {3 3}
000232  datetest 3.35 {strftime('%w %u','2023-01-05')} {4 4}
000233  datetest 3.36 {strftime('%w %u','2023-01-06')} {5 5}
000234  datetest 3.37 {strftime('%w %u','2023-01-07')} {6 6}
000235  
000236  # Ticket #2276.  Make sure leading zeros are inserted where appropriate.
000237  #
000238  datetest 3.40 \
000239     {strftime('%d/%f/%H/%W/%j/%m/%M/%S/%Y','0421-01-02 03:04:05.006')} \
000240     02/05.006/03/00/002/01/04/05/0421
000241  
000242  set sqlite_current_time 1157124367
000243  datetest 4.1 {date('now')} {2006-09-01}
000244  set sqlite_current_time 0
000245  
000246  datetest 5.1 {datetime('1994-04-16 14:00:00 +05:00')} {1994-04-16 09:00:00}
000247  datetest 5.2 {datetime('1994-04-16 14:00:00 -05:15')} {1994-04-16 19:15:00}
000248  datetest 5.3 {datetime('1994-04-16 05:00:00 +08:30')} {1994-04-15 20:30:00}
000249  datetest 5.4 {datetime('1994-04-16 14:00:00 -11:55')} {1994-04-17 01:55:00}
000250  datetest 5.5 {datetime('1994-04-16 14:00:00 -11:60')} NULL
000251  datetest 5.6 {datetime('1994-04-16 14:00:00 -11:55  ')} {1994-04-17 01:55:00}
000252  datetest 5.7 {datetime('1994-04-16 14:00:00 -11:55 x')} NULL
000253  datetest 5.8 {datetime('1994-04-16T14:00:00Z')} {1994-04-16 14:00:00}
000254  datetest 5.9 {datetime('1994-04-16 14:00:00z')} {1994-04-16 14:00:00}
000255  datetest 5.10 {datetime('1994-04-16 14:00:00 Z')} {1994-04-16 14:00:00}
000256  datetest 5.11 {datetime('1994-04-16 14:00:00z    ')} {1994-04-16 14:00:00}
000257  datetest 5.12 {datetime('1994-04-16 14:00:00     z    ')} {1994-04-16 14:00:00}
000258  datetest 5.13 {datetime('1994-04-16 14:00:00Zulu')} NULL
000259  datetest 5.14 {datetime('1994-04-16 14:00:00Z +05:00')} NULL
000260  datetest 5.15 {datetime('1994-04-16 14:00:00 +05:00 Z')} NULL
000261  
000262  # localtime->utc and utc->localtime conversions.
000263  #
000264  # Use SQLITE_TESTCTRL_LOCALTIME_FAULT=2 to set an alternative localtime_r()
000265  # implementation that is not locale-dependent.  The testing localtime_r()
000266  # operates as follows:
000267  #
000268  #     (1)  Localtime is 30 minutes earlier than (west of) UTC on
000269  #          even days (counting from 1970-01-01)
000270  #
000271  #     (2)  Localtime is 30 minutes later than (east of) UTC on odd days.
000272  #
000273  #     (3)  The function fails for the specific date/time value
000274  #          of 2000-05-29 14:16:00 in order to test the ability of
000275  #          SQLite to deal with localtime_r() failures.
000276  #
000277  proc local_to_utc {tn utc local} {
000278    do_execsql_test date-$tn "SELECT datetime('$utc','localtime')" [list $local]
000279  }
000280  proc utc_to_local {tn local utc} {
000281    do_execsql_test date-$tn "SELECT datetime('$local','utc')" [list $utc]
000282  }
000283  
000284  sqlite3_test_control SQLITE_TESTCTRL_LOCALTIME_FAULT 2
000285  local_to_utc 6.1  {2000-10-29 12:00:00} {2000-10-29 12:30:00}
000286  utc_to_local 6.2  {2000-10-29 12:30:00} {2000-10-29 12:00:00}
000287  local_to_utc 6.3  {2000-10-30 12:00:00} {2000-10-30 11:30:00}
000288  utc_to_local 6.4  {2000-10-30 11:30:00} {2000-10-30 12:00:00}
000289  
000290  local_to_utc 6.5  {2000-10-28 23:59:59} {2000-10-28 23:29:59}
000291  local_to_utc 6.6  {2000-10-29 00:00:00} {2000-10-29 00:30:00}
000292  
000293  # The previous two cases establish that no such localtime as
000294  # 2000-10-29 00:10:00 exists.  Verify that we get a reasonable
000295  # answer if we try to convert this non-existant localtime to utc?
000296  #
000297  utc_to_local 6.7  {2000-10-29 00:10:00} {2000-10-28 23:40:00}
000298  
000299  local_to_utc 6.8  {2022-02-10 23:59:59} {2022-02-11 00:29:59}
000300  local_to_utc 6.9  {2022-02-11 00:00:00} {2022-02-10 23:30:00}
000301  local_to_utc 6.10 {2022-02-10 23:45:00} {2022-02-11 00:15:00}
000302  local_to_utc 6.11 {2022-02-11 00:45:00} {2022-02-11 00:15:00}
000303  
000304  # The previous two cases show that two different UTC values give
000305  # the same localtime of 2022-02-11 00:15:00.  When converting from
000306  # that localtime back to UTC, we should get one or the other of
000307  # the two UTC values.
000308  #
000309  utc_to_local 6.12 {2022-02-11 00:15:00} {2022-02-11 00:45:00}
000310  
000311  # If localtime_r() fails, the datetime() SQL function should raise an error
000312  #
000313  do_catchsql_test date-6.20 {
000314    SELECT datetime('2000-05-29 14:16:00','localtime');
000315  } {1 {local time unavailable}}
000316  
000317  # Modifiers work for dates that are way out of band for localtime_r()
000318  #
000319  local_to_utc 6.21 {1800-10-29 12:00:00} {1800-10-29 12:30:00}
000320  utc_to_local 6.22 {1800-10-29 12:30:00} {1800-10-29 12:00:00}
000321  local_to_utc 6.23 {3000-10-30 12:00:00} {3000-10-30 11:30:00}
000322  utc_to_local 6.24 {3000-10-30 11:30:00} {3000-10-30 12:00:00}
000323  
000324  # If the time is specified to be ZULU, or if it has an explicit
000325  # timezone extension, then the time will already be UTC and subsequent
000326  # 'utc' modifiers are no-ops.
000327  #
000328  do_execsql_test date-6.25 {
000329    SELECT datetime('2000-10-29 12:00Z','utc','utc');
000330  } {{2000-10-29 12:00:00}}
000331  do_execsql_test date-6.26 {
000332    SELECT datetime('2000-10-29 12:00:00+05:00');
000333  } {{2000-10-29 07:00:00}}
000334  do_execsql_test date-6.27 {
000335    SELECT datetime('2000-10-29 12:00:00+05:00', 'utc');
000336  } {{2000-10-29 07:00:00}}
000337  
000338  # Multiple back-and-forth UTC to LOCAL to UTC...
000339  do_execsql_test date-6.28 {
000340    SELECT datetime('2000-10-29 12:00:00Z', 'localtime');
000341  } {{2000-10-29 12:30:00}}
000342  do_execsql_test date-6.29 {
000343    SELECT datetime('2000-10-29 12:00:00Z', 'utc', 'localtime');
000344  } {{2000-10-29 12:30:00}}
000345  do_execsql_test date-6.30 {
000346    SELECT datetime('2000-10-29 12:00:00Z', 'utc', 'localtime', 'utc');
000347  } {{2000-10-29 12:00:00}}
000348  do_execsql_test date-6.31 {
000349    SELECT datetime('2000-10-29 12:00:00Z', 'utc','localtime','utc','localtime');
000350  } {{2000-10-29 12:30:00}}
000351  do_execsql_test date-6.32 {
000352    SELECT datetime('2000-10-29 12:00:00Z', 'localtime','localtime');
000353  } {{2000-10-29 12:30:00}}
000354  
000355  
000356  # Restore the use of the OS localtime_r() before going on...
000357  sqlite3_test_control SQLITE_TESTCTRL_LOCALTIME_FAULT 0
000358  
000359  # Date-time functions that contain NULL arguments return a NULL
000360  # result.
000361  #
000362  datetest 7.1 {datetime(null)} NULL
000363  datetest 7.2 {datetime('now',null)} NULL
000364  datetest 7.3 {datetime('now','localtime',null)} NULL
000365  datetest 7.4 {time(null)} NULL
000366  datetest 7.5 {time('now',null)} NULL
000367  datetest 7.6 {time('now','localtime',null)} NULL
000368  datetest 7.7 {date(null)} NULL
000369  datetest 7.8 {date('now',null)} NULL
000370  datetest 7.9 {date('now','localtime',null)} NULL
000371  datetest 7.10 {julianday(null)} NULL
000372  datetest 7.11 {julianday('now',null)} NULL
000373  datetest 7.12 {julianday('now','localtime',null)} NULL
000374  datetest 7.13 {strftime(null,'now')} NULL
000375  datetest 7.14 {strftime('%s',null)} NULL
000376  datetest 7.15 {strftime('%s','now',null)} NULL
000377  datetest 7.16 {strftime('%s','now','localtime',null)} NULL
000378  
000379  # Test modifiers when the date begins as a julian day number - to
000380  # make sure the HH:MM:SS is preserved.  Ticket #551.
000381  #
000382  set sqlite_current_time [db eval {SELECT strftime('%s','2003-10-22 12:34:00')}]
000383  datetest 8.1 {datetime('now','weekday 0')} {2003-10-26 12:34:00}
000384  datetest 8.2 {datetime('now','weekday 1')} {2003-10-27 12:34:00}
000385  datetest 8.3 {datetime('now','weekday 2')} {2003-10-28 12:34:00}
000386  datetest 8.4 {datetime('now','weekday 3')} {2003-10-22 12:34:00}
000387  datetest 8.5 {datetime('now','start of month')} {2003-10-01 00:00:00}
000388  datetest 8.6 {datetime('now','start of year')} {2003-01-01 00:00:00}
000389  datetest 8.7 {datetime('now','start of day')} {2003-10-22 00:00:00}
000390  datetest 8.8 {datetime('now','1 day')} {2003-10-23 12:34:00}
000391  datetest 8.9 {datetime('now','+1 day')} {2003-10-23 12:34:00}
000392  datetest 8.10 {datetime('now','+1.25 day')} {2003-10-23 18:34:00}
000393  datetest 8.11 {datetime('now','-1.0 day')} {2003-10-21 12:34:00}
000394  datetest 8.12 {datetime('now','1 month')} {2003-11-22 12:34:00}
000395  datetest 8.13 {datetime('now','11 month')} {2004-09-22 12:34:00}
000396  datetest 8.14 {datetime('now','-13 month')} {2002-09-22 12:34:00}
000397  datetest 8.15 {datetime('now','1.5 months')} {2003-12-07 12:34:00}
000398  datetest 8.16 {datetime('now','-5 years')} {1998-10-22 12:34:00}
000399  datetest 8.17 {datetime('now','+10.5 minutes')} {2003-10-22 12:44:30}
000400  datetest 8.18 {datetime('now','-1.25 hours')} {2003-10-22 11:19:00}
000401  datetest 8.19 {datetime('now','11.25 seconds')} {2003-10-22 12:34:11}
000402  datetest 8.90 {datetime('now','abcdefghijklmnopqrstuvwyxzABCDEFGHIJLMNOP')} NULL
000403  set sqlite_current_time 0
000404  
000405  # Negative years work.  Example:  '-4713-11-26' is JD 1.5.
000406  #
000407  datetest 9.1 {julianday('-4713-11-24 12:00:00')} {0.0}
000408  datetest 9.2 {julianday(datetime(5))} {5.0}
000409  datetest 9.3 {julianday(datetime(10))} {10.0}
000410  datetest 9.4 {julianday(datetime(100))} {100.0}
000411  datetest 9.5 {julianday(datetime(1000))} {1000.0}
000412  datetest 9.6 {julianday(datetime(10000))} {10000.0}
000413  datetest 9.7 {julianday(datetime(100000))} {100000.0}
000414  
000415  # datetime() with just an HH:MM:SS correctly inserts the date 2000-01-01.
000416  #
000417  datetest 10.1 {datetime('01:02:03')}  {2000-01-01 01:02:03}
000418  datetest 10.2 {date('01:02:03')}  {2000-01-01}
000419  datetest 10.3 {strftime('%Y-%m-%d %H:%M','01:02:03')} {2000-01-01 01:02}
000420  
000421  # Test the new HH:MM:SS modifier
000422  #
000423  datetest 11.1 {datetime('2004-02-28 20:00:00', '-01:20:30')} \
000424     {2004-02-28 18:39:30}
000425  datetest 11.2 {datetime('2004-02-28 20:00:00', '+12:30:00')} \
000426     {2004-02-29 08:30:00}
000427  datetest 11.3 {datetime('2004-02-28 20:00:00', '+12:30')} \
000428     {2004-02-29 08:30:00}
000429  datetest 11.4 {datetime('2004-02-28 20:00:00', '12:30')} \
000430     {2004-02-29 08:30:00}
000431  datetest 11.5 {datetime('2004-02-28 20:00:00', '-12:00')} \
000432     {2004-02-28 08:00:00}
000433  datetest 11.6 {datetime('2004-02-28 20:00:00', '-12:01')} \
000434     {2004-02-28 07:59:00}
000435  datetest 11.7 {datetime('2004-02-28 20:00:00', '-11:59')} \
000436     {2004-02-28 08:01:00}
000437  datetest 11.8 {datetime('2004-02-28 20:00:00', '11:59')} \
000438     {2004-02-29 07:59:00}
000439  datetest 11.9 {datetime('2004-02-28 20:00:00', '12:01')} \
000440     {2004-02-29 08:01:00}
000441  datetest 11.10 {datetime('2004-02-28 20:00:00', '12:60')} NULL
000442  
000443  # Ticket #1964
000444  datetest 12.1 {datetime('2005-09-01')} {2005-09-01 00:00:00}
000445  datetest 12.2 {datetime('2005-09-01','+0 hours')} {2005-09-01 00:00:00}
000446  
000447  # Ticket #1991
000448  do_test date-13.1 {
000449    execsql {
000450      SELECT strftime('%Y-%m-%d %H:%M:%f', julianday('2006-09-24T10:50:26.047'))
000451    }
000452  } {{2006-09-24 10:50:26.047}}
000453  
000454  # Ticket #2153
000455  datetest 13.2 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 12:34:59.6')} \
000456    {2007-01-01 12:34:59}
000457  datetest 13.3 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 12:34:59.6')} \
000458    {2007-01-01 12:34:59.600}
000459  datetest 13.4 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 12:59:59.6')} \
000460    {2007-01-01 12:59:59}
000461  datetest 13.5 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 12:59:59.6')} \
000462    {2007-01-01 12:59:59.600}
000463  datetest 13.6 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 23:59:59.6')} \
000464    {2007-01-01 23:59:59}
000465  datetest 13.7 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 23:59:59.6')} \
000466    {2007-01-01 23:59:59.600}
000467  
000468  # Ticket #3618
000469  datetest 13.11 {julianday(2454832.5,'-1 day')} {2454831.5}
000470  datetest 13.12 {julianday(2454832.5,'+1 day')} {2454833.5}
000471  datetest 13.13 {julianday(2454832.5,'-1.5 day')} {2454831.0}
000472  datetest 13.14 {julianday(2454832.5,'+1.5 day')} {2454834.0}
000473  datetest 13.15 {julianday(2454832.5,'-3 hours')} {2454832.375}
000474  datetest 13.16 {julianday(2454832.5,'+3 hours')} {2454832.625}
000475  datetest 13.17 {julianday(2454832.5,'-45 minutes')} {2454832.46875}
000476  datetest 13.18 {julianday(2454832.5,'+45 minutes')} {2454832.53125}
000477  datetest 13.19 {julianday(2454832.5,'-675 seconds')} {2454832.4921875}
000478  datetest 13.20 {julianday(2454832.5,'+675 seconds')} {2454832.5078125}
000479  datetest 13.21 {julianday(2454832.5,'-1.5 months')} {2454786.5}
000480  datetest 13.22 {julianday(2454832.5,'+1.5 months')} {2454878.5}
000481  datetest 13.23 {julianday(2454832.5,'-1.5 years')} {2454284.0}
000482  datetest 13.24 {julianday(2454832.5,'+1.5 years')} {2455380.0}
000483  
000484  datetest 13.30 {date('2000-01-01','+1.5 years')} {2001-07-02}
000485  datetest 13.31 {date('2001-01-01','+1.5 years')} {2002-07-02}
000486  datetest 13.32 {date('2002-01-01','+1.5 years')} {2003-07-02}
000487  datetest 13.33 {date('2002-01-01','-1.5 years')} {2000-07-02}
000488  datetest 13.34 {date('2001-01-01','-1.5 years')} {1999-07-02}
000489  datetest 13.35 {date('2023-02-28')} {2023-02-28}
000490  datetest 13.36 {date('2023-02-29')} {2023-03-01}
000491  datetest 13.37 {date('2023-04-31')} {2023-05-01}
000492  
000493  # Test for issues reported by BareFeet (list.sql at tandb.com.au)
000494  # on mailing list on 2008-06-12.
000495  #
000496  # Put a floating point number in the database so that we can manipulate
000497  # raw bits using the hexio interface.
000498  #
000499  if {0==[sqlite3 -has-codec]} {
000500    do_test date-14.1 {
000501      execsql {
000502        PRAGMA auto_vacuum=OFF;
000503        PRAGMA page_size = 1024;
000504        CREATE TABLE t1(x);
000505        INSERT INTO t1 VALUES(1.1);
000506      }
000507      db close
000508      hexio_write test.db 2040 4142ba32bffffff9
000509      sqlite3 db test.db
000510      db eval {SELECT * FROM t1}
000511    } {2454629.5}
000512    
000513    # Changing the least significant byte of the floating point value between
000514    # 00 and FF should always generate a time of either 23:59:59 or 00:00:00,
000515    # never 24:00:00
000516    #
000517    for {set i 0} {$i<=255} {incr i} {
000518      db close
000519      hexio_write test.db 2047 [format %02x $i]
000520      sqlite3 db test.db
000521      do_test date-14.2.$i {
000522        set date [db one {SELECT datetime(x) FROM t1}]
000523        expr {$date eq "2008-06-12 00:00:00" || $date eq "2008-06-11 23:59:59"}
000524      } {1}
000525    }
000526  }
000527  
000528  # Verify that multiple calls to date functions with 'now' return the
000529  # same answer.
000530  #
000531  # EVIDENCE-OF: R-34818-13664 The 'now' argument to date and time
000532  # functions always returns exactly the same value for multiple
000533  # invocations within the same sqlite3_step() call.
000534  #
000535  proc sleeper {} {after 100}
000536  do_test date-15.1 {
000537    db func sleeper sleeper
000538    db eval {
000539       SELECT c - a FROM (SELECT julianday('now') AS a,
000540                                 sleeper(), julianday('now') AS c);
000541    }
000542  } {0.0}
000543  do_test date-15.2 {
000544    db eval {
000545       SELECT a==b FROM (SELECT current_timestamp AS a,
000546                                 sleeper(), current_timestamp AS b);
000547    }
000548  } {1}
000549  
000550  # Tests of extreme values in date/time functions.  Run with UBSan or the
000551  # equivalent to verify no signed interger overflow warnings.
000552  #
000553  datetest 16.1 {date(147483649)} NULL
000554  datetest 16.2 {datetime(0)} {-4713-11-24 12:00:00}
000555  datetest 16.3 {datetime(5373484.49999999)} {9999-12-31 23:59:59}
000556  datetest 16.4 {julianday('-4713-11-24 12:00:00')} 0.0
000557  datetest 16.5 {julianday('9999-12-31 23:59:59.999')} 5373484.49999999
000558  datetest 16.6 {datetime(0,'+464269060799 seconds')} {9999-12-31 23:59:59}
000559  datetest 16.7 {datetime(0,'+464269060800 seconds')} NULL
000560  datetest 16.8 {datetime(0,'+7737817679 minutes')} {9999-12-31 23:59:00}
000561  datetest 16.9 {datetime(0,'+7737817680 minutes')} NULL
000562  datetest 16.10 {datetime(0,'+128963627 hours')} {9999-12-31 23:00:00}
000563  datetest 16.11 {datetime(0,'+128963628 hours')} NULL
000564  datetest 16.12 {datetime(0,'+5373484 days')} {9999-12-31 12:00:00}
000565  datetest 16.13 {datetime(0,'+5373485 days')} NULL
000566  datetest 16.14 {datetime(0,'+176545 months')} {9999-12-24 12:00:00}
000567  datetest 16.15 {datetime(0,'+176546 months')} NULL
000568  datetest 16.16 {datetime(0,'+14712 years')} {9999-11-24 12:00:00}
000569  datetest 16.17 {datetime(0,'+14713 years')} NULL
000570  datetest 16.20 {datetime(5373484.4999999,'-464269060799 seconds')} \
000571                  {-4713-11-24 12:00:00}
000572  datetest 16.21 {datetime(5373484,'-464269060800 seconds')} NULL
000573  datetest 16.22 {datetime(5373484.4999999,'-7737817679 minutes')} \
000574                 {-4713-11-24 12:00:59}
000575  datetest 16.23 {datetime(5373484,'-7737817680 minutes')} NULL
000576  datetest 16.24 {datetime(5373484.4999999,'-128963627 hours')} \
000577                 {-4713-11-24 12:59:59}
000578  datetest 16.25 {datetime(5373484,'-128963628 hours')} NULL
000579  datetest 16.26 {datetime(5373484,'-5373484 days')} {-4713-11-24 12:00:00}
000580  datetest 16.27 {datetime(5373484,'-5373485 days')} NULL
000581  datetest 16.28 {datetime(5373484,'-176545 months')} {-4713-12-01 12:00:00}
000582  datetest 16.29 {datetime(5373484,'-176546 months')} NULL
000583  datetest 16.30 {datetime(5373484,'-14712 years')} {-4713-12-31 12:00:00}
000584  datetest 16.31 {datetime(5373484,'-14713 years')} NULL
000585  
000586  # 2017-03-02: Wrong 'start of day' computation.
000587  # https://www.sqlite.org/src/info/6097cb92745327a1
000588  #
000589  datetest 17.1 {datetime(2457754, 'start of day')} {2016-12-31 00:00:00}
000590  datetest 17.2 {datetime(2457828)} {2017-03-15 12:00:00}
000591  datetest 17.3 {datetime(2457828,'start of day')} {2017-03-15 00:00:00}
000592  datetest 17.4 {datetime(2457828,'start of month')} {2017-03-01 00:00:00}
000593  datetest 17.5 {datetime(2457828,'start of year')} {2017-01-01 00:00:00}
000594  datetest 17.6 {datetime(37,'start of year')} NULL
000595  datetest 17.7 {datetime(38,'start of year')} {-4712-01-01 00:00:00}
000596  
000597  # 2022-03-04 https://sqlite.org/forum/forumpost/2ffbaa2c3fd7fb82
000598  # The 'localtime' modifier should preserve fractional seconds.
000599  #
000600  datetest 18.1 {strftime('%f',1.234,'unixepoch','localtime')} {01.234}
000601  
000602  # 2023-04 The 'subsecond' (or 'subsec') modifier alters resolutions
000603  # to at least milliseconds. Added for release 3.42.0 .
000604  datetest 18.2 {unixepoch('1970-01-01T00:00:00.1', 'subsec')} {0.1}
000605  datetest 18.3 {unixepoch('1970-01-01T00:00:00.2', 'subsecond')} {0.2}
000606  datetest 18.4 {julianday('-4713-11-24 13:40:48.864', 'subsec')} {0.07001}
000607  datetest 18.5 {typeof(unixepoch('now', 'subsecond'))} {real}
000608  
000609  # 2024-03-03 the 'ceiling' and 'floor' operators.
000610  #
000611  datetest 19.1 {date('2000-01-31','floor')} {2000-01-31}
000612  datetest 19.2a {date('2000-02-31','floor')} {2000-02-29}
000613  datetest 19.2b {date('1999-02-31','floor')} {1999-02-28}
000614  datetest 19.2c {date('1900-02-31','floor')} {1900-02-28}
000615  datetest 19.3 {date('2000-03-31','floor')} {2000-03-31}
000616  datetest 19.4 {date('2000-04-31','floor')} {2000-04-30}
000617  datetest 19.5 {date('2000-05-31','floor')} {2000-05-31}
000618  datetest 19.6 {date('2000-06-31','floor')} {2000-06-30}
000619  datetest 19.7 {date('2000-07-31','floor')} {2000-07-31}
000620  datetest 19.8 {date('2000-08-31','floor')} {2000-08-31}
000621  datetest 19.9 {date('2000-09-31','floor')} {2000-09-30}
000622  datetest 19.10 {date('2000-10-31','floor')} {2000-10-31}
000623  datetest 19.11 {date('2000-11-31','floor')} {2000-11-30}
000624  datetest 19.12 {date('2000-12-31','floor')} {2000-12-31}
000625  datetest 19.21 {date('2000-01-31','ceiling')} {2000-01-31}
000626  datetest 19.22a {date('2000-02-31','ceiling')} {2000-03-02}
000627  datetest 19.22b {date('1999-02-31','ceiling')} {1999-03-03}
000628  datetest 19.22c {date('1900-02-31','ceiling')} {1900-03-03}
000629  datetest 19.23 {date('2000-03-31','ceiling')} {2000-03-31}
000630  datetest 19.24 {date('2000-04-31','ceiling')} {2000-05-01}
000631  datetest 19.25 {date('2000-05-31','ceiling')} {2000-05-31}
000632  datetest 19.26 {date('2000-06-31','ceiling')} {2000-07-01}
000633  datetest 19.27 {date('2000-07-31','ceiling')} {2000-07-31}
000634  datetest 19.28 {date('2000-08-31','ceiling')} {2000-08-31}
000635  datetest 19.29 {date('2000-09-31','ceiling')} {2000-10-01}
000636  datetest 19.30 {date('2000-10-31','ceiling')} {2000-10-31}
000637  datetest 19.31 {date('2000-11-31','ceiling')} {2000-12-01}
000638  datetest 19.32 {date('2000-12-31','ceiling')} {2000-12-31}
000639  datetest 19.40 {date('2024-01-31','+1 month','ceiling')} {2024-03-02}
000640  datetest 19.41 {date('2024-01-31','+1 month','floor')} {2024-02-29}
000641  datetest 19.42 {date('2023-01-31','+1 month','ceiling')} {2023-03-03}
000642  datetest 19.43 {date('2023-01-31','+1 month','floor')} {2023-02-28}
000643  datetest 19.44 {date('2024-02-29','+1 year','ceiling')} {2025-03-01}
000644  datetest 19.45 {date('2024-02-29','+1 year','floor')} {2025-02-28}
000645  datetest 19.46 {date('2024-02-29','-110 years','ceiling')} {1914-03-01}
000646  datetest 19.47 {date('2024-02-29','-110 years','floor')} {1914-02-28}
000647  datetest 19.48 {date('2024-02-29','-0110-00-00','floor')} {1914-02-28}
000648  datetest 19.49 {date('2024-02-29','-0110-00-00','ceiling')} {1914-03-01}
000649  datetest 19.50 {date('2000-08-31','+0023-06-00','floor')} {2024-02-29}
000650  datetest 19.51 {date('2000-08-31','+0022-06-00','floor')} {2023-02-28}
000651  datetest 19.52 {date('2000-08-31','+0023-06-00','ceiling')} {2024-03-02}
000652  datetest 19.53 {date('2000-08-31','+0022-06-00','ceiling')} {2023-03-03}
000653  
000654  
000655  finish_test