xref: /sqlite-3.40.0/test/date.test (revision ea840117)
17014aff3Sdrh# 2003 October 31
27014aff3Sdrh#
37014aff3Sdrh# The author disclaims copyright to this source code.  In place of
47014aff3Sdrh# a legal notice, here is a blessing:
57014aff3Sdrh#
67014aff3Sdrh#    May you do good and not evil.
77014aff3Sdrh#    May you find forgiveness for yourself and forgive others.
87014aff3Sdrh#    May you share freely, never taking more than you give.
97014aff3Sdrh#
107014aff3Sdrh#***********************************************************************
117014aff3Sdrh# This file implements regression tests for SQLite library.  The
127014aff3Sdrh# focus of this file is testing date and time functions.
137014aff3Sdrh#
147fee360dSdrh# $Id: date.test,v 1.34 2009/04/16 12:58:03 drh Exp $
157014aff3Sdrh
167014aff3Sdrhset testdir [file dirname $argv0]
177014aff3Sdrhsource $testdir/tester.tcl
187014aff3Sdrh
1968928b6cSdan# Do not use a codec for tests in this file, as the database file is
2068928b6cSdan# manipulated directly using tcl scripts (using the [hexio_write] command).
2168928b6cSdan#
2268928b6cSdando_not_use_codec
2368928b6cSdan
2427d258a3Sdrh# Skip this whole file if date and time functions are omitted
2527d258a3Sdrh# at compile-time
2627d258a3Sdrh#
27b7f9164eSdrhifcapable {!datetime} {
2827d258a3Sdrh  finish_test
2927d258a3Sdrh  return
3027d258a3Sdrh}
3127d258a3Sdrh
327014aff3Sdrhproc datetest {tnum expr result} {
337014aff3Sdrh  do_test date-$tnum [subst {
347014aff3Sdrh    execsql "SELECT coalesce($expr,'NULL')"
357014aff3Sdrh  }] [list $result]
367014aff3Sdrh}
3792febd92Sdrhset tcl_precision 15
387014aff3Sdrhdatetest 1.1 julianday('2000-01-01') 2451544.5
397014aff3Sdrhdatetest 1.2 julianday('1970-01-01') 2440587.5
407014aff3Sdrhdatetest 1.3 julianday('1910-04-20') 2418781.5
417014aff3Sdrhdatetest 1.4 julianday('1986-02-09') 2446470.5
4292febd92Sdrhdatetest 1.5 julianday('12:00:00') 2451545.0
4392febd92Sdrhdatetest 1.6 {julianday('2000-01-01 12:00:00')} 2451545.0
4492febd92Sdrhdatetest 1.7 {julianday('2000-01-01 12:00')} 2451545.0
457014aff3Sdrhdatetest 1.8 julianday('bogus') NULL
467014aff3Sdrhdatetest 1.9 julianday('1999-12-31') 2451543.5
477014aff3Sdrhdatetest 1.10 julianday('1999-12-32') NULL
487014aff3Sdrhdatetest 1.11 julianday('1999-13-01') NULL
497014aff3Sdrhdatetest 1.12 julianday('2003-02-31') 2452701.5
507014aff3Sdrhdatetest 1.13 julianday('2003-03-03') 2452701.5
517014aff3Sdrhdatetest 1.14 julianday('+2000-01-01') NULL
527014aff3Sdrhdatetest 1.15 julianday('200-01-01') NULL
537014aff3Sdrhdatetest 1.16 julianday('2000-1-01') NULL
547014aff3Sdrhdatetest 1.17 julianday('2000-01-1') NULL
554cb29b4cSdrhdatetest 1.18.1 {julianday('2000-01-01     12:00:00')} 2451545.0
564cb29b4cSdrhdatetest 1.18.2 {julianday('2000-01-01T12:00:00')} 2451545.0
574cb29b4cSdrhdatetest 1.18.3 {julianday('2000-01-01 T12:00:00')} 2451545.0
584cb29b4cSdrhdatetest 1.18.4 {julianday('2000-01-01T 12:00:00')} 2451545.0
594cb29b4cSdrhdatetest 1.18.4 {julianday('2000-01-01 T 12:00:00')} 2451545.0
607014aff3Sdrhdatetest 1.19 {julianday('2000-01-01 12:00:00.1')}   2451545.00000116
617014aff3Sdrhdatetest 1.20 {julianday('2000-01-01 12:00:00.01')}  2451545.00000012
627014aff3Sdrhdatetest 1.21 {julianday('2000-01-01 12:00:00.001')} 2451545.00000001
637014aff3Sdrhdatetest 1.22 {julianday('2000-01-01 12:00:00.')} NULL
64d76a902cSdrhdatetest 1.23 julianday(12345.6) 12345.6
653edb157eSdrhdatetest 1.23b julianday(1721059.5) 1721059.5
667014aff3Sdrhdatetest 1.24 {julianday('2001-01-01 12:00:00 bogus')} NULL
677014aff3Sdrhdatetest 1.25 {julianday('2001-01-01 bogus')} NULL
68018d1a49Sdrhdatetest 1.26 {julianday('2001-01-01 12:60:00')} NULL
69018d1a49Sdrhdatetest 1.27 {julianday('2001-01-01 12:59:60')} NULL
7005f7c19aSdrhdatetest 1.28 {julianday('2001-00-01')} NULL
7105f7c19aSdrhdatetest 1.29 {julianday('2001-01-00')} NULL
727014aff3Sdrh
737014aff3Sdrhdatetest 2.1 datetime(0,'unixepoch') {1970-01-01 00:00:00}
7405f7c19aSdrhdatetest 2.1b datetime(0,'unixepoc') NULL
7505f7c19aSdrhdatetest 2.1c datetime(0,'unixepochx') NULL
7605f7c19aSdrhdatetest 2.1d datetime('2003-10-22','unixepoch') NULL
777014aff3Sdrhdatetest 2.2 datetime(946684800,'unixepoch') {2000-01-01 00:00:00}
78c8d75674Sdrhdatetest 2.2b datetime('946684800','unixepoch') {2000-01-01 00:00:00}
797fee360dSdrhfor {set i 0} {$i<1000} {incr i} {
807fee360dSdrh  set sql [format {strftime('%%H:%%M:%%f',1237962480.%03d,'unixepoch')} $i]
817fee360dSdrh  set res [format {06:28:00.%03d} $i]
827fee360dSdrh  datetest 2.2c-$i $sql $res
837fee360dSdrh}
847014aff3Sdrhdatetest 2.3 {date('2003-10-22','weekday 0')} 2003-10-26
857014aff3Sdrhdatetest 2.4 {date('2003-10-22','weekday 1')} 2003-10-27
8605f7c19aSdrhdatetest 2.4a {date('2003-10-22','weekday  1')} 2003-10-27
87025586a2Sdrhdatetest 2.4b {date('2003-10-22','weekday  1x')} NULL
8805f7c19aSdrhdatetest 2.4c {date('2003-10-22','weekday  -1')} NULL
8905f7c19aSdrhdatetest 2.4d {date('2003-10-22','weakday  1x')} NULL
9005f7c19aSdrhdatetest 2.4e {date('2003-10-22','weekday ')} NULL
917014aff3Sdrhdatetest 2.5 {date('2003-10-22','weekday 2')} 2003-10-28
927014aff3Sdrhdatetest 2.6 {date('2003-10-22','weekday 3')} 2003-10-22
937014aff3Sdrhdatetest 2.7 {date('2003-10-22','weekday 4')} 2003-10-23
947014aff3Sdrhdatetest 2.8 {date('2003-10-22','weekday 5')} 2003-10-24
957014aff3Sdrhdatetest 2.9 {date('2003-10-22','weekday 6')} 2003-10-25
967014aff3Sdrhdatetest 2.10 {date('2003-10-22','weekday 7')} NULL
977014aff3Sdrhdatetest 2.11 {date('2003-10-22','weekday 5.5')} NULL
98c5dd9fa8Sdrhdatetest 2.12 {datetime('2003-10-22 12:34','weekday 0')} {2003-10-26 12:34:00}
997014aff3Sdrhdatetest 2.13 {datetime('2003-10-22 12:34','start of month')} \
1007014aff3Sdrh   {2003-10-01 00:00:00}
1017014aff3Sdrhdatetest 2.14 {datetime('2003-10-22 12:34','start of year')} \
1027014aff3Sdrh   {2003-01-01 00:00:00}
1037014aff3Sdrhdatetest 2.15 {datetime('2003-10-22 12:34','start of day')} \
1047014aff3Sdrh   {2003-10-22 00:00:00}
10505f7c19aSdrhdatetest 2.15a {datetime('2003-10-22 12:34','start of')} NULL
10605f7c19aSdrhdatetest 2.15b {datetime('2003-10-22 12:34','start of bogus')} NULL
1077014aff3Sdrhdatetest 2.16 time('12:34:56.43') 12:34:56
1087014aff3Sdrhdatetest 2.17 {datetime('2003-10-22 12:34','1 day')} {2003-10-23 12:34:00}
1097014aff3Sdrhdatetest 2.18 {datetime('2003-10-22 12:34','+1 day')} {2003-10-23 12:34:00}
1107014aff3Sdrhdatetest 2.19 {datetime('2003-10-22 12:34','+1.25 day')} {2003-10-23 18:34:00}
1117014aff3Sdrhdatetest 2.20 {datetime('2003-10-22 12:34','-1.0 day')} {2003-10-21 12:34:00}
1127014aff3Sdrhdatetest 2.21 {datetime('2003-10-22 12:34','1 month')} {2003-11-22 12:34:00}
1137014aff3Sdrhdatetest 2.22 {datetime('2003-10-22 12:34','11 month')} {2004-09-22 12:34:00}
1147014aff3Sdrhdatetest 2.23 {datetime('2003-10-22 12:34','-13 month')} {2002-09-22 12:34:00}
1157014aff3Sdrhdatetest 2.24 {datetime('2003-10-22 12:34','1.5 months')} {2003-12-07 12:34:00}
1167014aff3Sdrhdatetest 2.25 {datetime('2003-10-22 12:34','-5 years')} {1998-10-22 12:34:00}
1177014aff3Sdrhdatetest 2.26 {datetime('2003-10-22 12:34','+10.5 minutes')} \
1187014aff3Sdrh  {2003-10-22 12:44:30}
1197014aff3Sdrhdatetest 2.27 {datetime('2003-10-22 12:34','-1.25 hours')} \
1207014aff3Sdrh  {2003-10-22 11:19:00}
1217014aff3Sdrhdatetest 2.28 {datetime('2003-10-22 12:34','11.25 seconds')} \
1227014aff3Sdrh  {2003-10-22 12:34:11}
1237014aff3Sdrhdatetest 2.29 {datetime('2003-10-22 12:24','+5 bogus')} NULL
12405f7c19aSdrhdatetest 2.30 {datetime('2003-10-22 12:24','+++')} NULL
12505f7c19aSdrhdatetest 2.31 {datetime('2003-10-22 12:24','+12.3e4 femtoseconds')} NULL
12605f7c19aSdrhdatetest 2.32 {datetime('2003-10-22 12:24','+12.3e4 uS')} NULL
12705f7c19aSdrhdatetest 2.33 {datetime('2003-10-22 12:24','+1 abc')} NULL
12805f7c19aSdrhdatetest 2.34 {datetime('2003-10-22 12:24','+1 abcd')} NULL
12905f7c19aSdrhdatetest 2.35 {datetime('2003-10-22 12:24','+1 abcde')} NULL
13005f7c19aSdrhdatetest 2.36 {datetime('2003-10-22 12:24','+1 abcdef')} NULL
13105f7c19aSdrhdatetest 2.37 {datetime('2003-10-22 12:24','+1 abcdefg')} NULL
13205f7c19aSdrhdatetest 2.38 {datetime('2003-10-22 12:24','+1 abcdefgh')} NULL
13305f7c19aSdrhdatetest 2.39 {datetime('2003-10-22 12:24','+1 abcdefghi')} NULL
134008e476cSdrhset sqlite_current_time 1199243045
135008e476cSdrhdatetest 2.40 {datetime()} {2008-01-02 03:04:05}
136008e476cSdrhset sqlite_current_time 0
1375f3a367bSdrhdatetest 2.41 {datetime('2003-10-22 12:24','23 seconds')} {2003-10-22 12:24:23}
1385f3a367bSdrhdatetest 2.42 {datetime('2003-10-22 12:24','345 second')} {2003-10-22 12:29:45}
1395f3a367bSdrhdatetest 2.43 {datetime('2003-10-22 12:24','4 second')} {2003-10-22 12:24:04}
1405f3a367bSdrhdatetest 2.44 {datetime('2003-10-22 12:24','56 second')} {2003-10-22 12:24:56}
1415f3a367bSdrhdatetest 2.45 {datetime('2003-10-22 12:24','60 second')} {2003-10-22 12:25:00}
1425f3a367bSdrhdatetest 2.46 {datetime('2003-10-22 12:24','70 second')} {2003-10-22 12:25:10}
1435f3a367bSdrhdatetest 2.47 {datetime('2003-10-22 12:24','8.6 seconds')} {2003-10-22 12:24:08}
1445f3a367bSdrhdatetest 2.48 {datetime('2003-10-22 12:24','9.4 second')} {2003-10-22 12:24:09}
1455f3a367bSdrhdatetest 2.49 {datetime('2003-10-22 12:24','0000 second')} {2003-10-22 12:24:00}
1465f3a367bSdrhdatetest 2.50 {datetime('2003-10-22 12:24','0001 second')} {2003-10-22 12:24:01}
1475f3a367bSdrhdatetest 2.51 {datetime('2003-10-22 12:24','nonsense')} NULL
1487014aff3Sdrh
1497014aff3Sdrhdatetest 3.1 {strftime('%d','2003-10-31 12:34:56.432')} 31
1505f3a367bSdrhdatetest 3.2.1 {strftime('pre%fpost','2003-10-31 12:34:56.432')} pre56.432post
151008e476cSdrhdatetest 3.2.2 {strftime('%f','2003-10-31 12:34:59.9999999')} 59.999
1527014aff3Sdrhdatetest 3.3 {strftime('%H','2003-10-31 12:34:56.432')} 12
1537014aff3Sdrhdatetest 3.4 {strftime('%j','2003-10-31 12:34:56.432')} 304
15472b3fbc7Sdrhdatetest 3.5 {strftime('%J','2003-10-31 12:34:56.432')} 2452944.024264259
1557014aff3Sdrhdatetest 3.6 {strftime('%m','2003-10-31 12:34:56.432')} 10
1567014aff3Sdrhdatetest 3.7 {strftime('%M','2003-10-31 12:34:56.432')} 34
1576eb41523Sdrhdatetest 3.8.1 {strftime('%s','2003-10-31 12:34:56.432')} 1067603696
1586eb41523Sdrhdatetest 3.8.2 {strftime('%s','2038-01-19 03:14:07')} 2147483647
1596eb41523Sdrhdatetest 3.8.3 {strftime('%s','2038-01-19 03:14:08')} 2147483648
1606eb41523Sdrhdatetest 3.8.4 {strftime('%s','2201-04-09 12:00:00')} 7298164800
1616eb41523Sdrhdatetest 3.8.5 {strftime('%s','9999-12-31 23:59:59')} 253402300799
1626eb41523Sdrhdatetest 3.8.6 {strftime('%s','1969-12-31 23:59:59')} -1
1636eb41523Sdrhdatetest 3.8.7 {strftime('%s','1901-12-13 20:45:52')} -2147483648
1646eb41523Sdrhdatetest 3.8.8 {strftime('%s','1901-12-13 20:45:51')} -2147483649
1656eb41523Sdrhdatetest 3.8.9 {strftime('%s','1776-07-04 00:00:00')} -6106060800
1667014aff3Sdrhdatetest 3.9 {strftime('%S','2003-10-31 12:34:56.432')} 56
1677014aff3Sdrhdatetest 3.10 {strftime('%w','2003-10-31 12:34:56.432')} 5
1681020d49aSdrhdatetest 3.11.1 {strftime('%W','2003-10-31 12:34:56.432')} 43
1691020d49aSdrhdatetest 3.11.2 {strftime('%W','2004-01-01')} 00
1701020d49aSdrhdatetest 3.11.3 {strftime('%W','2004-01-02')} 00
1711020d49aSdrhdatetest 3.11.4 {strftime('%W','2004-01-03')} 00
1725f3a367bSdrhdatetest 3.11.5 {strftime('abc%Wxyz','2004-01-04')} abc00xyz
1731020d49aSdrhdatetest 3.11.6 {strftime('%W','2004-01-05')} 01
1741020d49aSdrhdatetest 3.11.7 {strftime('%W','2004-01-06')} 01
1751020d49aSdrhdatetest 3.11.8 {strftime('%W','2004-01-07')} 01
1761020d49aSdrhdatetest 3.11.9 {strftime('%W','2004-01-08')} 01
1771020d49aSdrhdatetest 3.11.10 {strftime('%W','2004-01-09')} 01
1781020d49aSdrhdatetest 3.11.11 {strftime('%W','2004-07-18')} 28
1791020d49aSdrhdatetest 3.11.12 {strftime('%W','2004-12-31')} 52
1801020d49aSdrhdatetest 3.11.13 {strftime('%W','2007-12-31')} 53
1811020d49aSdrhdatetest 3.11.14 {strftime('%W','2007-01-01')} 01
182c2c9eef6Sdrhdatetest 3.11.15 {strftime('%W %j',2454109.04140970)} {02 008}
183c2c9eef6Sdrhdatetest 3.11.16 {strftime('%W %j',2454109.04140971)} {02 008}
184c2c9eef6Sdrhdatetest 3.11.17 {strftime('%W %j',2454109.04140972)} {02 008}
185c2c9eef6Sdrhdatetest 3.11.18 {strftime('%W %j',2454109.04140973)} {02 008}
186c2c9eef6Sdrhdatetest 3.11.19 {strftime('%W %j',2454109.04140974)} {02 008}
187c2c9eef6Sdrhdatetest 3.11.20 {strftime('%W %j',2454109.04140975)} {02 008}
188c2c9eef6Sdrhdatetest 3.11.21 {strftime('%W %j',2454109.04140976)} {02 008}
189c2c9eef6Sdrhdatetest 3.11.22 {strftime('%W %j',2454109.04140977)} {02 008}
190c8d75674Sdrhdatetest 3.11.23 {strftime('%W %j',2454109.04140978)} {02 008}
191c8d75674Sdrhdatetest 3.11.24 {strftime('%W %j',2454109.04140979)} {02 008}
192c8d75674Sdrhdatetest 3.11.25 {strftime('%W %j',2454109.04140980)} {02 008}
193c8d75674Sdrhdatetest 3.11.99 {strftime('%W %j','2454109.04140970')} {02 008}
1947014aff3Sdrhdatetest 3.12 {strftime('%Y','2003-10-31 12:34:56.432')} 2003
1957014aff3Sdrhdatetest 3.13 {strftime('%%','2003-10-31 12:34:56.432')} %
1967014aff3Sdrhdatetest 3.14 {strftime('%_','2003-10-31 12:34:56.432')} NULL
1977014aff3Sdrhdatetest 3.15 {strftime('%Y-%m-%d','2003-10-31')} 2003-10-31
1987014aff3Sdrhproc repeat {n txt} {
1997014aff3Sdrh  set x {}
2007014aff3Sdrh  while {$n>0} {
2017014aff3Sdrh    append x $txt
2027014aff3Sdrh    incr n -1
2037014aff3Sdrh  }
2047014aff3Sdrh  return $x
2057014aff3Sdrh}
2067014aff3Sdrhdatetest 3.16 "strftime('[repeat 200 %Y]','2003-10-31')" [repeat 200 2003]
2077014aff3Sdrhdatetest 3.17 "strftime('[repeat 200 abc%m123]','2003-10-31')" \
2087014aff3Sdrh    [repeat 200 abc10123]
2097014aff3Sdrh
2105f3a367bSdrhforeach c {a b c e g h i k l n o p q r t v x y z
2115f3a367bSdrh           A B C D E F G I K L N O P Q R T U V Z
2125f3a367bSdrh           0 1 2 3 4 5 6 6 7 9 _} {
2135f3a367bSdrh  datetest 3.18.$c "strftime('%$c','2003-10-31')" NULL
2145f3a367bSdrh}
2155f3a367bSdrh
2162ecad3b4Sdrh# Ticket #2276.  Make sure leading zeros are inserted where appropriate.
2172ecad3b4Sdrh#
2182ecad3b4Sdrhdatetest 3.20 \
2192ecad3b4Sdrh   {strftime('%d/%f/%H/%W/%j/%m/%M/%S/%Y','0421-01-02 03:04:05.006')} \
2202ecad3b4Sdrh   02/05.006/03/00/002/01/04/05/0421
2212ecad3b4Sdrh
2229645d8d4Sdrhset sqlite_current_time 1157124367
2239645d8d4Sdrhdatetest 4.1 {date('now')} {2006-09-01}
2249645d8d4Sdrhset sqlite_current_time 0
2257014aff3Sdrh
2265739103eSdrhdatetest 5.1 {datetime('1994-04-16 14:00:00 +05:00')} {1994-04-16 09:00:00}
2275739103eSdrhdatetest 5.2 {datetime('1994-04-16 14:00:00 -05:15')} {1994-04-16 19:15:00}
2285739103eSdrhdatetest 5.3 {datetime('1994-04-16 05:00:00 +08:30')} {1994-04-15 20:30:00}
2295739103eSdrhdatetest 5.4 {datetime('1994-04-16 14:00:00 -11:55')} {1994-04-17 01:55:00}
2305739103eSdrhdatetest 5.5 {datetime('1994-04-16 14:00:00 -11:60')} NULL
2311cfdc90bSdrhdatetest 5.6 {datetime('1994-04-16 14:00:00 -11:55  ')} {1994-04-17 01:55:00}
2321cfdc90bSdrhdatetest 5.7 {datetime('1994-04-16 14:00:00 -11:55 x')} NULL
2331cfdc90bSdrhdatetest 5.8 {datetime('1994-04-16T14:00:00Z')} {1994-04-16 14:00:00}
2341cfdc90bSdrhdatetest 5.9 {datetime('1994-04-16 14:00:00z')} {1994-04-16 14:00:00}
2351cfdc90bSdrhdatetest 5.10 {datetime('1994-04-16 14:00:00 Z')} {1994-04-16 14:00:00}
2361cfdc90bSdrhdatetest 5.11 {datetime('1994-04-16 14:00:00z    ')} {1994-04-16 14:00:00}
2371cfdc90bSdrhdatetest 5.12 {datetime('1994-04-16 14:00:00     z    ')} {1994-04-16 14:00:00}
2381cfdc90bSdrhdatetest 5.13 {datetime('1994-04-16 14:00:00Zulu')} NULL
2391cfdc90bSdrhdatetest 5.14 {datetime('1994-04-16 14:00:00Z +05:00')} NULL
2401cfdc90bSdrhdatetest 5.15 {datetime('1994-04-16 14:00:00 +05:00 Z')} NULL
2417014aff3Sdrh
24285548facSdrh# localtime->utc and utc->localtime conversions.
2437091cb05Sdrh#
24485548facSdrh# Use SQLITE_TESTCTRL_LOCALTIME_FAULT=2 to set an alternative localtime_r()
24585548facSdrh# implementation that is not locale-dependent.  This testing localtime_r()
24685548facSdrh# operates as follows:
24705eaf73cSshane#
24885548facSdrh#     (1)  Localtime is 30 minutes earlier than (west of) UTC on
24985548facSdrh#          even days (counting from 1970-01-01)
25005eaf73cSshane#
25185548facSdrh#     (2)  Localtime is 30 minutes later than (east of) UTC on odd days.
252e6a58a4eSdanielk1977#
25385548facSdrh#     (3)  The function fails for the specific date/time value
25485548facSdrh#          of 2000-05-29 14:16:00 in order to test the ability of
25585548facSdrh#          SQLite to deal with localtime_r() failures.
25685548facSdrh#
25785548facSdrhproc local_to_utc {tn utc local} {
25885548facSdrh  do_execsql_test date-$tn "SELECT datetime('$utc','localtime')" [list $local]
25985548facSdrh}
26085548facSdrhproc utc_to_local {tn local utc} {
26185548facSdrh  do_execsql_test date-$tn "SELECT datetime('$local','utc')" [list $utc]
26285548facSdrh}
26385548facSdrh
26485548facSdrhsqlite3_test_control SQLITE_TESTCTRL_LOCALTIME_FAULT 2
26585548facSdrhlocal_to_utc 6.1  {2000-10-29 12:00:00} {2000-10-29 12:30:00}
26685548facSdrhutc_to_local 6.2  {2000-10-29 12:30:00} {2000-10-29 12:00:00}
26785548facSdrhlocal_to_utc 6.3  {2000-10-30 12:00:00} {2000-10-30 11:30:00}
26885548facSdrhutc_to_local 6.4  {2000-10-30 11:30:00} {2000-10-30 12:00:00}
26985548facSdrh
27085548facSdrhlocal_to_utc 6.5  {2000-10-28 23:59:59} {2000-10-28 23:29:59}
27185548facSdrhlocal_to_utc 6.6  {2000-10-29 00:00:00} {2000-10-29 00:30:00}
27285548facSdrh
27385548facSdrh# The previous two cases establish that no such localtime as
27485548facSdrh# 2000-10-29 00:10:00 exists.  Verify that we get a reasonable
27585548facSdrh# answer if we try to convert this non-existant localtime to utc?
27685548facSdrh#
27785548facSdrhutc_to_local 6.7  {2000-10-29 00:10:00} {2000-10-28 23:40:00}
27885548facSdrh
27985548facSdrhlocal_to_utc 6.8  {2022-02-10 23:59:59} {2022-02-11 00:29:59}
28085548facSdrhlocal_to_utc 6.9  {2022-02-11 00:00:00} {2022-02-10 23:30:00}
28185548facSdrhlocal_to_utc 6.10 {2022-02-10 23:45:00} {2022-02-11 00:15:00}
28285548facSdrhlocal_to_utc 6.11 {2022-02-11 00:45:00} {2022-02-11 00:15:00}
28385548facSdrh
28485548facSdrh# The previous two cases show that two different UTC values give
28585548facSdrh# the same localtime of 2022-02-11 00:15:00.  When converting from
28685548facSdrh# that localtime back to UTC, we should get one or the other of
28785548facSdrh# the two UTC values.
28885548facSdrh#
28985548facSdrhutc_to_local 6.12 {2022-02-11 00:15:00} {2022-02-11 00:45:00}
29085548facSdrh
29185548facSdrh# If localtime_r() fails, the datetime() SQL function should raise an error
29285548facSdrh#
29385548facSdrhdo_catchsql_test date-6.20 {
29485548facSdrh  SELECT datetime('2000-05-29 14:16:00','localtime');
29585548facSdrh} {1 {local time unavailable}}
29685548facSdrh
29785548facSdrh# Modifiers work for dates that are way out of band for localtime_r()
29885548facSdrh#
29985548facSdrhlocal_to_utc 6.21 {1800-10-29 12:00:00} {1800-10-29 12:30:00}
30085548facSdrhutc_to_local 6.22 {1800-10-29 12:30:00} {1800-10-29 12:00:00}
30185548facSdrhlocal_to_utc 6.23 {3000-10-30 12:00:00} {3000-10-30 11:30:00}
30285548facSdrhutc_to_local 6.24 {3000-10-30 11:30:00} {3000-10-30 12:00:00}
30385548facSdrh
30485548facSdrh# Restore the use of the OS localtime_r() before going on...
30585548facSdrhsqlite3_test_control SQLITE_TESTCTRL_LOCALTIME_FAULT 0
306e6a58a4eSdanielk1977
307f586aa8bSdrh# Date-time functions that contain NULL arguments return a NULL
308f586aa8bSdrh# result.
309f586aa8bSdrh#
310f586aa8bSdrhdatetest 7.1 {datetime(null)} NULL
311f586aa8bSdrhdatetest 7.2 {datetime('now',null)} NULL
312f586aa8bSdrhdatetest 7.3 {datetime('now','localtime',null)} NULL
313f586aa8bSdrhdatetest 7.4 {time(null)} NULL
314f586aa8bSdrhdatetest 7.5 {time('now',null)} NULL
315f586aa8bSdrhdatetest 7.6 {time('now','localtime',null)} NULL
316f586aa8bSdrhdatetest 7.7 {date(null)} NULL
317f586aa8bSdrhdatetest 7.8 {date('now',null)} NULL
318f586aa8bSdrhdatetest 7.9 {date('now','localtime',null)} NULL
319f586aa8bSdrhdatetest 7.10 {julianday(null)} NULL
320f586aa8bSdrhdatetest 7.11 {julianday('now',null)} NULL
321f586aa8bSdrhdatetest 7.12 {julianday('now','localtime',null)} NULL
322f586aa8bSdrhdatetest 7.13 {strftime(null,'now')} NULL
323f586aa8bSdrhdatetest 7.14 {strftime('%s',null)} NULL
324f586aa8bSdrhdatetest 7.15 {strftime('%s','now',null)} NULL
325f586aa8bSdrhdatetest 7.16 {strftime('%s','now','localtime',null)} NULL
326f586aa8bSdrh
327c5dd9fa8Sdrh# Test modifiers when the date begins as a julian day number - to
328c5dd9fa8Sdrh# make sure the HH:MM:SS is preserved.  Ticket #551.
329c5dd9fa8Sdrh#
330c5dd9fa8Sdrhset sqlite_current_time [db eval {SELECT strftime('%s','2003-10-22 12:34:00')}]
331c5dd9fa8Sdrhdatetest 8.1 {datetime('now','weekday 0')} {2003-10-26 12:34:00}
332c5dd9fa8Sdrhdatetest 8.2 {datetime('now','weekday 1')} {2003-10-27 12:34:00}
333c5dd9fa8Sdrhdatetest 8.3 {datetime('now','weekday 2')} {2003-10-28 12:34:00}
334c5dd9fa8Sdrhdatetest 8.4 {datetime('now','weekday 3')} {2003-10-22 12:34:00}
335c5dd9fa8Sdrhdatetest 8.5 {datetime('now','start of month')} {2003-10-01 00:00:00}
336c5dd9fa8Sdrhdatetest 8.6 {datetime('now','start of year')} {2003-01-01 00:00:00}
337c5dd9fa8Sdrhdatetest 8.7 {datetime('now','start of day')} {2003-10-22 00:00:00}
338c5dd9fa8Sdrhdatetest 8.8 {datetime('now','1 day')} {2003-10-23 12:34:00}
339c5dd9fa8Sdrhdatetest 8.9 {datetime('now','+1 day')} {2003-10-23 12:34:00}
340c5dd9fa8Sdrhdatetest 8.10 {datetime('now','+1.25 day')} {2003-10-23 18:34:00}
341c5dd9fa8Sdrhdatetest 8.11 {datetime('now','-1.0 day')} {2003-10-21 12:34:00}
342c5dd9fa8Sdrhdatetest 8.12 {datetime('now','1 month')} {2003-11-22 12:34:00}
343c5dd9fa8Sdrhdatetest 8.13 {datetime('now','11 month')} {2004-09-22 12:34:00}
344c5dd9fa8Sdrhdatetest 8.14 {datetime('now','-13 month')} {2002-09-22 12:34:00}
345c5dd9fa8Sdrhdatetest 8.15 {datetime('now','1.5 months')} {2003-12-07 12:34:00}
346c5dd9fa8Sdrhdatetest 8.16 {datetime('now','-5 years')} {1998-10-22 12:34:00}
347c5dd9fa8Sdrhdatetest 8.17 {datetime('now','+10.5 minutes')} {2003-10-22 12:44:30}
348c5dd9fa8Sdrhdatetest 8.18 {datetime('now','-1.25 hours')} {2003-10-22 11:19:00}
349c5dd9fa8Sdrhdatetest 8.19 {datetime('now','11.25 seconds')} {2003-10-22 12:34:11}
35005f7c19aSdrhdatetest 8.90 {datetime('now','abcdefghijklmnopqrstuvwyxzABCDEFGHIJLMNOP')} NULL
351c5dd9fa8Sdrhset sqlite_current_time 0
352c5dd9fa8Sdrh
353d76a902cSdrh# Negative years work.  Example:  '-4713-11-26' is JD 1.5.
354d76a902cSdrh#
355d76a902cSdrhdatetest 9.1 {julianday('-4713-11-24 12:00:00')} {0.0}
356d76a902cSdrhdatetest 9.2 {julianday(datetime(5))} {5.0}
357d76a902cSdrhdatetest 9.3 {julianday(datetime(10))} {10.0}
358d76a902cSdrhdatetest 9.4 {julianday(datetime(100))} {100.0}
359d76a902cSdrhdatetest 9.5 {julianday(datetime(1000))} {1000.0}
360d76a902cSdrhdatetest 9.6 {julianday(datetime(10000))} {10000.0}
361d76a902cSdrhdatetest 9.7 {julianday(datetime(100000))} {100000.0}
362d76a902cSdrh
3630d131ab7Sdrh# datetime() with just an HH:MM:SS correctly inserts the date 2000-01-01.
3640d131ab7Sdrh#
3650d131ab7Sdrhdatetest 10.1 {datetime('01:02:03')}  {2000-01-01 01:02:03}
3660d131ab7Sdrhdatetest 10.2 {date('01:02:03')}  {2000-01-01}
3670d131ab7Sdrhdatetest 10.3 {strftime('%Y-%m-%d %H:%M','01:02:03')} {2000-01-01 01:02}
3680d131ab7Sdrh
3690d131ab7Sdrh# Test the new HH:MM:SS modifier
3700d131ab7Sdrh#
3710d131ab7Sdrhdatetest 11.1 {datetime('2004-02-28 20:00:00', '-01:20:30')} \
3720d131ab7Sdrh   {2004-02-28 18:39:30}
3730d131ab7Sdrhdatetest 11.2 {datetime('2004-02-28 20:00:00', '+12:30:00')} \
3740d131ab7Sdrh   {2004-02-29 08:30:00}
3750d131ab7Sdrhdatetest 11.3 {datetime('2004-02-28 20:00:00', '+12:30')} \
3760d131ab7Sdrh   {2004-02-29 08:30:00}
3770d131ab7Sdrhdatetest 11.4 {datetime('2004-02-28 20:00:00', '12:30')} \
3780d131ab7Sdrh   {2004-02-29 08:30:00}
3790d131ab7Sdrhdatetest 11.5 {datetime('2004-02-28 20:00:00', '-12:00')} \
3800d131ab7Sdrh   {2004-02-28 08:00:00}
3810d131ab7Sdrhdatetest 11.6 {datetime('2004-02-28 20:00:00', '-12:01')} \
3820d131ab7Sdrh   {2004-02-28 07:59:00}
3830d131ab7Sdrhdatetest 11.7 {datetime('2004-02-28 20:00:00', '-11:59')} \
3840d131ab7Sdrh   {2004-02-28 08:01:00}
3850d131ab7Sdrhdatetest 11.8 {datetime('2004-02-28 20:00:00', '11:59')} \
3860d131ab7Sdrh   {2004-02-29 07:59:00}
3870d131ab7Sdrhdatetest 11.9 {datetime('2004-02-28 20:00:00', '12:01')} \
3880d131ab7Sdrh   {2004-02-29 08:01:00}
389018d1a49Sdrhdatetest 11.10 {datetime('2004-02-28 20:00:00', '12:60')} NULL
3900d131ab7Sdrh
391f11c34dfSdrh# Ticket #1964
392f11c34dfSdrhdatetest 12.1 {datetime('2005-09-01')} {2005-09-01 00:00:00}
393f11c34dfSdrhdatetest 12.2 {datetime('2005-09-01','+0 hours')} {2005-09-01 00:00:00}
394c5dd9fa8Sdrh
3959df9492aSdrh# Ticket #1991
3969df9492aSdrhdo_test date-13.1 {
3979df9492aSdrh  execsql {
3989df9492aSdrh    SELECT strftime('%Y-%m-%d %H:%M:%f', julianday('2006-09-24T10:50:26.047'))
3999df9492aSdrh  }
4009df9492aSdrh} {{2006-09-24 10:50:26.047}}
4019df9492aSdrh
402622fa9a8Sdrh# Ticket #2153
403622fa9a8Sdrhdatetest 13.2 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 12:34:59.6')} \
404622fa9a8Sdrh  {2007-01-01 12:34:59}
405622fa9a8Sdrhdatetest 13.3 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 12:34:59.6')} \
406622fa9a8Sdrh  {2007-01-01 12:34:59.600}
407622fa9a8Sdrhdatetest 13.4 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 12:59:59.6')} \
408622fa9a8Sdrh  {2007-01-01 12:59:59}
409622fa9a8Sdrhdatetest 13.5 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 12:59:59.6')} \
410622fa9a8Sdrh  {2007-01-01 12:59:59.600}
411622fa9a8Sdrhdatetest 13.6 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 23:59:59.6')} \
412622fa9a8Sdrh  {2007-01-01 23:59:59}
413622fa9a8Sdrhdatetest 13.7 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 23:59:59.6')} \
414622fa9a8Sdrh  {2007-01-01 23:59:59.600}
415622fa9a8Sdrh
416c531a223Sdrh# Ticket #3618
417c531a223Sdrhdatetest 13.11 {julianday(2454832.5,'-1 day')} {2454831.5}
418c531a223Sdrhdatetest 13.12 {julianday(2454832.5,'+1 day')} {2454833.5}
419c531a223Sdrhdatetest 13.13 {julianday(2454832.5,'-1.5 day')} {2454831.0}
420c531a223Sdrhdatetest 13.14 {julianday(2454832.5,'+1.5 day')} {2454834.0}
421c531a223Sdrhdatetest 13.15 {julianday(2454832.5,'-3 hours')} {2454832.375}
422c531a223Sdrhdatetest 13.16 {julianday(2454832.5,'+3 hours')} {2454832.625}
423c531a223Sdrhdatetest 13.17 {julianday(2454832.5,'-45 minutes')} {2454832.46875}
424c531a223Sdrhdatetest 13.18 {julianday(2454832.5,'+45 minutes')} {2454832.53125}
425c531a223Sdrhdatetest 13.19 {julianday(2454832.5,'-675 seconds')} {2454832.4921875}
426c531a223Sdrhdatetest 13.20 {julianday(2454832.5,'+675 seconds')} {2454832.5078125}
427c531a223Sdrhdatetest 13.21 {julianday(2454832.5,'-1.5 months')} {2454786.5}
428c531a223Sdrhdatetest 13.22 {julianday(2454832.5,'+1.5 months')} {2454878.5}
429c531a223Sdrhdatetest 13.23 {julianday(2454832.5,'-1.5 years')} {2454284.0}
430c531a223Sdrhdatetest 13.24 {julianday(2454832.5,'+1.5 years')} {2455380.0}
431c531a223Sdrh
432c531a223Sdrhdatetest 13.30 {date('2000-01-01','+1.5 years')} {2001-07-02}
433c531a223Sdrhdatetest 13.31 {date('2001-01-01','+1.5 years')} {2002-07-02}
434c531a223Sdrhdatetest 13.32 {date('2002-01-01','+1.5 years')} {2003-07-02}
435c531a223Sdrhdatetest 13.33 {date('2002-01-01','-1.5 years')} {2000-07-02}
436c531a223Sdrhdatetest 13.34 {date('2001-01-01','-1.5 years')} {1999-07-02}
437c531a223Sdrh
43885f477a1Sdrh# Test for issues reported by BareFeet (list.sql at tandb.com.au)
43985f477a1Sdrh# on mailing list on 2008-06-12.
44085f477a1Sdrh#
44185f477a1Sdrh# Put a floating point number in the database so that we can manipulate
44285f477a1Sdrh# raw bits using the hexio interface.
44385f477a1Sdrh#
44410f5a50eSdanif {0==[sqlite3 -has-codec]} {
44585f477a1Sdrh  do_test date-14.1 {
446f1aaaa40Sshane    execsql {
44785f477a1Sdrh      PRAGMA auto_vacuum=OFF;
44885f477a1Sdrh      PRAGMA page_size = 1024;
44985f477a1Sdrh      CREATE TABLE t1(x);
45085f477a1Sdrh      INSERT INTO t1 VALUES(1.1);
451f1aaaa40Sshane    }
45285f477a1Sdrh    db close
45385f477a1Sdrh    hexio_write test.db 2040 4142ba32bffffff9
45485f477a1Sdrh    sqlite3 db test.db
45585f477a1Sdrh    db eval {SELECT * FROM t1}
45685f477a1Sdrh  } {2454629.5}
457f1aaaa40Sshane
45885f477a1Sdrh  # Changing the least significant byte of the floating point value between
45985f477a1Sdrh  # 00 and FF should always generate a time of either 23:59:59 or 00:00:00,
46085f477a1Sdrh  # never 24:00:00
46185f477a1Sdrh  #
46285f477a1Sdrh  for {set i 0} {$i<=255} {incr i} {
46385f477a1Sdrh    db close
464c91d86c9Sdrh    hexio_write test.db 2047 [format %02x $i]
46585f477a1Sdrh    sqlite3 db test.db
46685f477a1Sdrh    do_test date-14.2.$i {
46785f477a1Sdrh      set date [db one {SELECT datetime(x) FROM t1}]
46885f477a1Sdrh      expr {$date eq "2008-06-12 00:00:00" || $date eq "2008-06-11 23:59:59"}
46985f477a1Sdrh    } {1}
47085f477a1Sdrh  }
47110f5a50eSdan}
47295a7b3e3Sdrh
47395a7b3e3Sdrh# Verify that multiple calls to date functions with 'now' return the
47495a7b3e3Sdrh# same answer.
47595a7b3e3Sdrh#
476e4bf4f08Sdrh# EVIDENCE-OF: R-34818-13664 The 'now' argument to date and time
477e4bf4f08Sdrh# functions always returns exactly the same value for multiple
478e4bf4f08Sdrh# invocations within the same sqlite3_step() call.
479e4bf4f08Sdrh#
48095a7b3e3Sdrhproc sleeper {} {after 100}
48195a7b3e3Sdrhdo_test date-15.1 {
48295a7b3e3Sdrh  db func sleeper sleeper
48395a7b3e3Sdrh  db eval {
484e9b9f759Sdrh     SELECT c - a FROM (SELECT julianday('now') AS a,
48595a7b3e3Sdrh                               sleeper(), julianday('now') AS c);
48695a7b3e3Sdrh  }
48795a7b3e3Sdrh} {0.0}
48895a7b3e3Sdrhdo_test date-15.2 {
48995a7b3e3Sdrh  db eval {
49095a7b3e3Sdrh     SELECT a==b FROM (SELECT current_timestamp AS a,
49195a7b3e3Sdrh                               sleeper(), current_timestamp AS b);
49295a7b3e3Sdrh  }
49395a7b3e3Sdrh} {1}
49495a7b3e3Sdrh
495d76a902cSdrh# Tests of extreme values in date/time functions.  Run with UBSan or the
496d76a902cSdrh# equivalent to verify no signed interger overflow warnings.
497d76a902cSdrh#
498d76a902cSdrhdatetest 16.1 {date(147483649)} NULL
499d76a902cSdrhdatetest 16.2 {datetime(0)} {-4713-11-24 12:00:00}
500d76a902cSdrhdatetest 16.3 {datetime(5373484.49999999)} {9999-12-31 23:59:59}
501d76a902cSdrhdatetest 16.4 {julianday('-4713-11-24 12:00:00')} 0.0
502d76a902cSdrhdatetest 16.5 {julianday('9999-12-31 23:59:59.999')} 5373484.49999999
503d76a902cSdrhdatetest 16.6 {datetime(0,'+464269060799 seconds')} {9999-12-31 23:59:59}
504d76a902cSdrhdatetest 16.7 {datetime(0,'+464269060800 seconds')} NULL
505d76a902cSdrhdatetest 16.8 {datetime(0,'+7737817679 minutes')} {9999-12-31 23:59:00}
506d76a902cSdrhdatetest 16.9 {datetime(0,'+7737817680 minutes')} NULL
507d76a902cSdrhdatetest 16.10 {datetime(0,'+128963627 hours')} {9999-12-31 23:00:00}
508d76a902cSdrhdatetest 16.11 {datetime(0,'+128963628 hours')} NULL
509d76a902cSdrhdatetest 16.12 {datetime(0,'+5373484 days')} {9999-12-31 12:00:00}
510d76a902cSdrhdatetest 16.13 {datetime(0,'+5373485 days')} NULL
511d76a902cSdrhdatetest 16.14 {datetime(0,'+176545 months')} {9999-12-24 12:00:00}
512d76a902cSdrhdatetest 16.15 {datetime(0,'+176546 months')} NULL
513d76a902cSdrhdatetest 16.16 {datetime(0,'+14712 years')} {9999-11-24 12:00:00}
514d76a902cSdrhdatetest 16.17 {datetime(0,'+14713 years')} NULL
515d76a902cSdrhdatetest 16.20 {datetime(5373484.4999999,'-464269060799 seconds')} \
516d76a902cSdrh                {-4713-11-24 12:00:00}
517d76a902cSdrhdatetest 16.21 {datetime(5373484,'-464269060800 seconds')} NULL
518d76a902cSdrhdatetest 16.22 {datetime(5373484.4999999,'-7737817679 minutes')} \
519d76a902cSdrh               {-4713-11-24 12:00:59}
520d76a902cSdrhdatetest 16.23 {datetime(5373484,'-7737817680 minutes')} NULL
521d76a902cSdrhdatetest 16.24 {datetime(5373484.4999999,'-128963627 hours')} \
522d76a902cSdrh               {-4713-11-24 12:59:59}
523d76a902cSdrhdatetest 16.25 {datetime(5373484,'-128963628 hours')} NULL
524d76a902cSdrhdatetest 16.26 {datetime(5373484,'-5373484 days')} {-4713-11-24 12:00:00}
525d76a902cSdrhdatetest 16.27 {datetime(5373484,'-5373485 days')} NULL
526d76a902cSdrhdatetest 16.28 {datetime(5373484,'-176545 months')} {-4713-12-01 12:00:00}
527d76a902cSdrhdatetest 16.29 {datetime(5373484,'-176546 months')} NULL
528d76a902cSdrhdatetest 16.30 {datetime(5373484,'-14712 years')} {-4713-12-31 12:00:00}
529d76a902cSdrhdatetest 16.31 {datetime(5373484,'-14713 years')} NULL
530d76a902cSdrh
5319715f7f0Sdrh# 2017-03-02: Wrong 'start of day' computation.
5329715f7f0Sdrh# https://www.sqlite.org/src/info/6097cb92745327a1
5339715f7f0Sdrh#
5349715f7f0Sdrhdatetest 17.1 {datetime(2457754, 'start of day')} {2016-12-31 00:00:00}
5359715f7f0Sdrhdatetest 17.2 {datetime(2457828)} {2017-03-15 12:00:00}
5369715f7f0Sdrhdatetest 17.3 {datetime(2457828,'start of day')} {2017-03-15 00:00:00}
5379715f7f0Sdrhdatetest 17.4 {datetime(2457828,'start of month')} {2017-03-01 00:00:00}
5389715f7f0Sdrhdatetest 17.5 {datetime(2457828,'start of year')} {2017-01-01 00:00:00}
53935a0925aSdrhdatetest 17.6 {datetime(37,'start of year')} NULL
54035a0925aSdrhdatetest 17.7 {datetime(38,'start of year')} {-4712-01-01 00:00:00}
5419715f7f0Sdrh
542*ea840117Sdrh# 2022-03-04 https://sqlite.org/forum/forumpost/2ffbaa2c3fd7fb82
543*ea840117Sdrh# The 'localtime' modifier should preserve fractional seconds.
544*ea840117Sdrh#
545*ea840117Sdrhdatetest 18.1 {strftime('%f',1.234,'unixepoch','localtime')} {01.234}
546d76a902cSdrh
5477014aff3Sdrhfinish_test
548