os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/date.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2003 October 31
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this file is testing date and time functions.
sl@0
    13
#
sl@0
    14
# $Id: date.test,v 1.31 2008/07/08 02:12:37 drh Exp $
sl@0
    15
sl@0
    16
set testdir [file dirname $argv0]
sl@0
    17
source $testdir/tester.tcl
sl@0
    18
sl@0
    19
# Skip this whole file if date and time functions are omitted
sl@0
    20
# at compile-time
sl@0
    21
#
sl@0
    22
ifcapable {!datetime} {
sl@0
    23
  finish_test
sl@0
    24
  return
sl@0
    25
}
sl@0
    26
sl@0
    27
proc datetest {tnum expr result} {
sl@0
    28
  do_test date-$tnum [subst {
sl@0
    29
    execsql "SELECT coalesce($expr,'NULL')"
sl@0
    30
  }] [list $result]
sl@0
    31
}
sl@0
    32
set tcl_precision 15
sl@0
    33
datetest 1.1 julianday('2000-01-01') 2451544.5
sl@0
    34
datetest 1.2 julianday('1970-01-01') 2440587.5
sl@0
    35
datetest 1.3 julianday('1910-04-20') 2418781.5
sl@0
    36
datetest 1.4 julianday('1986-02-09') 2446470.5
sl@0
    37
datetest 1.5 julianday('12:00:00') 2451545.0
sl@0
    38
datetest 1.6 {julianday('2000-01-01 12:00:00')} 2451545.0
sl@0
    39
datetest 1.7 {julianday('2000-01-01 12:00')} 2451545.0
sl@0
    40
datetest 1.8 julianday('bogus') NULL
sl@0
    41
datetest 1.9 julianday('1999-12-31') 2451543.5
sl@0
    42
datetest 1.10 julianday('1999-12-32') NULL
sl@0
    43
datetest 1.11 julianday('1999-13-01') NULL
sl@0
    44
datetest 1.12 julianday('2003-02-31') 2452701.5
sl@0
    45
datetest 1.13 julianday('2003-03-03') 2452701.5
sl@0
    46
datetest 1.14 julianday('+2000-01-01') NULL
sl@0
    47
datetest 1.15 julianday('200-01-01') NULL
sl@0
    48
datetest 1.16 julianday('2000-1-01') NULL
sl@0
    49
datetest 1.17 julianday('2000-01-1') NULL
sl@0
    50
datetest 1.18.1 {julianday('2000-01-01     12:00:00')} 2451545.0
sl@0
    51
datetest 1.18.2 {julianday('2000-01-01T12:00:00')} 2451545.0
sl@0
    52
datetest 1.18.3 {julianday('2000-01-01 T12:00:00')} 2451545.0
sl@0
    53
datetest 1.18.4 {julianday('2000-01-01T 12:00:00')} 2451545.0
sl@0
    54
datetest 1.18.4 {julianday('2000-01-01 T 12:00:00')} 2451545.0
sl@0
    55
datetest 1.19 {julianday('2000-01-01 12:00:00.1')}   2451545.00000116
sl@0
    56
datetest 1.20 {julianday('2000-01-01 12:00:00.01')}  2451545.00000012
sl@0
    57
datetest 1.21 {julianday('2000-01-01 12:00:00.001')} 2451545.00000001
sl@0
    58
datetest 1.22 {julianday('2000-01-01 12:00:00.')} NULL
sl@0
    59
datetest 1.23 julianday(12345.6) 12345.6
sl@0
    60
datetest 1.23b julianday('12345.6') 12345.6
sl@0
    61
datetest 1.24 {julianday('2001-01-01 12:00:00 bogus')} NULL
sl@0
    62
datetest 1.25 {julianday('2001-01-01 bogus')} NULL
sl@0
    63
datetest 1.26 {julianday('2001-01-01 12:60:00')} NULL
sl@0
    64
datetest 1.27 {julianday('2001-01-01 12:59:60')} NULL
sl@0
    65
datetest 1.28 {julianday('2001-00-01')} NULL
sl@0
    66
datetest 1.29 {julianday('2001-01-00')} NULL
sl@0
    67
sl@0
    68
datetest 2.1 datetime(0,'unixepoch') {1970-01-01 00:00:00}
sl@0
    69
datetest 2.1b datetime(0,'unixepoc') NULL
sl@0
    70
datetest 2.1c datetime(0,'unixepochx') NULL
sl@0
    71
datetest 2.1d datetime('2003-10-22','unixepoch') NULL
sl@0
    72
datetest 2.2 datetime(946684800,'unixepoch') {2000-01-01 00:00:00}
sl@0
    73
datetest 2.2b datetime('946684800','unixepoch') {2000-01-01 00:00:00}
sl@0
    74
datetest 2.3 {date('2003-10-22','weekday 0')} 2003-10-26
sl@0
    75
datetest 2.4 {date('2003-10-22','weekday 1')} 2003-10-27
sl@0
    76
datetest 2.4a {date('2003-10-22','weekday  1')} 2003-10-27
sl@0
    77
datetest 2.4b {date('2003-10-22','weekday  1x')} 2003-10-27
sl@0
    78
datetest 2.4c {date('2003-10-22','weekday  -1')} NULL
sl@0
    79
datetest 2.4d {date('2003-10-22','weakday  1x')} NULL
sl@0
    80
datetest 2.4e {date('2003-10-22','weekday ')} NULL
sl@0
    81
datetest 2.5 {date('2003-10-22','weekday 2')} 2003-10-28
sl@0
    82
datetest 2.6 {date('2003-10-22','weekday 3')} 2003-10-22
sl@0
    83
datetest 2.7 {date('2003-10-22','weekday 4')} 2003-10-23
sl@0
    84
datetest 2.8 {date('2003-10-22','weekday 5')} 2003-10-24
sl@0
    85
datetest 2.9 {date('2003-10-22','weekday 6')} 2003-10-25
sl@0
    86
datetest 2.10 {date('2003-10-22','weekday 7')} NULL
sl@0
    87
datetest 2.11 {date('2003-10-22','weekday 5.5')} NULL
sl@0
    88
datetest 2.12 {datetime('2003-10-22 12:34','weekday 0')} {2003-10-26 12:34:00}
sl@0
    89
datetest 2.13 {datetime('2003-10-22 12:34','start of month')} \
sl@0
    90
   {2003-10-01 00:00:00}
sl@0
    91
datetest 2.14 {datetime('2003-10-22 12:34','start of year')} \
sl@0
    92
   {2003-01-01 00:00:00}
sl@0
    93
datetest 2.15 {datetime('2003-10-22 12:34','start of day')} \
sl@0
    94
   {2003-10-22 00:00:00}
sl@0
    95
datetest 2.15a {datetime('2003-10-22 12:34','start of')} NULL
sl@0
    96
datetest 2.15b {datetime('2003-10-22 12:34','start of bogus')} NULL
sl@0
    97
datetest 2.16 time('12:34:56.43') 12:34:56
sl@0
    98
datetest 2.17 {datetime('2003-10-22 12:34','1 day')} {2003-10-23 12:34:00}
sl@0
    99
datetest 2.18 {datetime('2003-10-22 12:34','+1 day')} {2003-10-23 12:34:00}
sl@0
   100
datetest 2.19 {datetime('2003-10-22 12:34','+1.25 day')} {2003-10-23 18:34:00}
sl@0
   101
datetest 2.20 {datetime('2003-10-22 12:34','-1.0 day')} {2003-10-21 12:34:00}
sl@0
   102
datetest 2.21 {datetime('2003-10-22 12:34','1 month')} {2003-11-22 12:34:00}
sl@0
   103
datetest 2.22 {datetime('2003-10-22 12:34','11 month')} {2004-09-22 12:34:00}
sl@0
   104
datetest 2.23 {datetime('2003-10-22 12:34','-13 month')} {2002-09-22 12:34:00}
sl@0
   105
datetest 2.24 {datetime('2003-10-22 12:34','1.5 months')} {2003-12-07 12:34:00}
sl@0
   106
datetest 2.25 {datetime('2003-10-22 12:34','-5 years')} {1998-10-22 12:34:00}
sl@0
   107
datetest 2.26 {datetime('2003-10-22 12:34','+10.5 minutes')} \
sl@0
   108
  {2003-10-22 12:44:30}
sl@0
   109
datetest 2.27 {datetime('2003-10-22 12:34','-1.25 hours')} \
sl@0
   110
  {2003-10-22 11:19:00}
sl@0
   111
datetest 2.28 {datetime('2003-10-22 12:34','11.25 seconds')} \
sl@0
   112
  {2003-10-22 12:34:11}
sl@0
   113
datetest 2.29 {datetime('2003-10-22 12:24','+5 bogus')} NULL
sl@0
   114
datetest 2.30 {datetime('2003-10-22 12:24','+++')} NULL
sl@0
   115
datetest 2.31 {datetime('2003-10-22 12:24','+12.3e4 femtoseconds')} NULL
sl@0
   116
datetest 2.32 {datetime('2003-10-22 12:24','+12.3e4 uS')} NULL
sl@0
   117
datetest 2.33 {datetime('2003-10-22 12:24','+1 abc')} NULL
sl@0
   118
datetest 2.34 {datetime('2003-10-22 12:24','+1 abcd')} NULL
sl@0
   119
datetest 2.35 {datetime('2003-10-22 12:24','+1 abcde')} NULL
sl@0
   120
datetest 2.36 {datetime('2003-10-22 12:24','+1 abcdef')} NULL
sl@0
   121
datetest 2.37 {datetime('2003-10-22 12:24','+1 abcdefg')} NULL
sl@0
   122
datetest 2.38 {datetime('2003-10-22 12:24','+1 abcdefgh')} NULL
sl@0
   123
datetest 2.39 {datetime('2003-10-22 12:24','+1 abcdefghi')} NULL
sl@0
   124
set sqlite_current_time 1199243045
sl@0
   125
datetest 2.40 {datetime()} {2008-01-02 03:04:05}
sl@0
   126
set sqlite_current_time 0
sl@0
   127
datetest 2.41 {datetime('2003-10-22 12:24','23 seconds')} {2003-10-22 12:24:23}
sl@0
   128
datetest 2.42 {datetime('2003-10-22 12:24','345 second')} {2003-10-22 12:29:45}
sl@0
   129
datetest 2.43 {datetime('2003-10-22 12:24','4 second')} {2003-10-22 12:24:04}
sl@0
   130
datetest 2.44 {datetime('2003-10-22 12:24','56 second')} {2003-10-22 12:24:56}
sl@0
   131
datetest 2.45 {datetime('2003-10-22 12:24','60 second')} {2003-10-22 12:25:00}
sl@0
   132
datetest 2.46 {datetime('2003-10-22 12:24','70 second')} {2003-10-22 12:25:10}
sl@0
   133
datetest 2.47 {datetime('2003-10-22 12:24','8.6 seconds')} {2003-10-22 12:24:08}
sl@0
   134
datetest 2.48 {datetime('2003-10-22 12:24','9.4 second')} {2003-10-22 12:24:09}
sl@0
   135
datetest 2.49 {datetime('2003-10-22 12:24','0000 second')} {2003-10-22 12:24:00}
sl@0
   136
datetest 2.50 {datetime('2003-10-22 12:24','0001 second')} {2003-10-22 12:24:01}
sl@0
   137
datetest 2.51 {datetime('2003-10-22 12:24','nonsense')} NULL
sl@0
   138
sl@0
   139
datetest 3.1 {strftime('%d','2003-10-31 12:34:56.432')} 31
sl@0
   140
datetest 3.2.1 {strftime('pre%fpost','2003-10-31 12:34:56.432')} pre56.432post
sl@0
   141
datetest 3.2.2 {strftime('%f','2003-10-31 12:34:59.9999999')} 59.999
sl@0
   142
datetest 3.3 {strftime('%H','2003-10-31 12:34:56.432')} 12
sl@0
   143
datetest 3.4 {strftime('%j','2003-10-31 12:34:56.432')} 304
sl@0
   144
datetest 3.5 {strftime('%J','2003-10-31 12:34:56.432')} 2452944.02426426
sl@0
   145
datetest 3.6 {strftime('%m','2003-10-31 12:34:56.432')} 10
sl@0
   146
datetest 3.7 {strftime('%M','2003-10-31 12:34:56.432')} 34
sl@0
   147
datetest 3.8 {strftime('%s','2003-10-31 12:34:56.432')} 1067603696
sl@0
   148
datetest 3.9 {strftime('%S','2003-10-31 12:34:56.432')} 56
sl@0
   149
datetest 3.10 {strftime('%w','2003-10-31 12:34:56.432')} 5
sl@0
   150
datetest 3.11.1 {strftime('%W','2003-10-31 12:34:56.432')} 43
sl@0
   151
datetest 3.11.2 {strftime('%W','2004-01-01')} 00
sl@0
   152
datetest 3.11.3 {strftime('%W','2004-01-02')} 00
sl@0
   153
datetest 3.11.4 {strftime('%W','2004-01-03')} 00
sl@0
   154
datetest 3.11.5 {strftime('abc%Wxyz','2004-01-04')} abc00xyz
sl@0
   155
datetest 3.11.6 {strftime('%W','2004-01-05')} 01
sl@0
   156
datetest 3.11.7 {strftime('%W','2004-01-06')} 01
sl@0
   157
datetest 3.11.8 {strftime('%W','2004-01-07')} 01
sl@0
   158
datetest 3.11.9 {strftime('%W','2004-01-08')} 01
sl@0
   159
datetest 3.11.10 {strftime('%W','2004-01-09')} 01
sl@0
   160
datetest 3.11.11 {strftime('%W','2004-07-18')} 28
sl@0
   161
datetest 3.11.12 {strftime('%W','2004-12-31')} 52
sl@0
   162
datetest 3.11.13 {strftime('%W','2007-12-31')} 53
sl@0
   163
datetest 3.11.14 {strftime('%W','2007-01-01')} 01
sl@0
   164
datetest 3.11.15 {strftime('%W %j',2454109.04140970)} {02 008}
sl@0
   165
datetest 3.11.16 {strftime('%W %j',2454109.04140971)} {02 008}
sl@0
   166
datetest 3.11.17 {strftime('%W %j',2454109.04140972)} {02 008}
sl@0
   167
datetest 3.11.18 {strftime('%W %j',2454109.04140973)} {02 008}
sl@0
   168
datetest 3.11.19 {strftime('%W %j',2454109.04140974)} {02 008}
sl@0
   169
datetest 3.11.20 {strftime('%W %j',2454109.04140975)} {02 008}
sl@0
   170
datetest 3.11.21 {strftime('%W %j',2454109.04140976)} {02 008}
sl@0
   171
datetest 3.11.22 {strftime('%W %j',2454109.04140977)} {02 008}
sl@0
   172
datetest 3.11.23 {strftime('%W %j',2454109.04140978)} {02 008}
sl@0
   173
datetest 3.11.24 {strftime('%W %j',2454109.04140979)} {02 008}
sl@0
   174
datetest 3.11.25 {strftime('%W %j',2454109.04140980)} {02 008}
sl@0
   175
datetest 3.11.99 {strftime('%W %j','2454109.04140970')} {02 008}
sl@0
   176
datetest 3.12 {strftime('%Y','2003-10-31 12:34:56.432')} 2003
sl@0
   177
datetest 3.13 {strftime('%%','2003-10-31 12:34:56.432')} %
sl@0
   178
datetest 3.14 {strftime('%_','2003-10-31 12:34:56.432')} NULL
sl@0
   179
datetest 3.15 {strftime('%Y-%m-%d','2003-10-31')} 2003-10-31
sl@0
   180
proc repeat {n txt} {
sl@0
   181
  set x {} 
sl@0
   182
  while {$n>0} {
sl@0
   183
    append x $txt
sl@0
   184
    incr n -1
sl@0
   185
  }
sl@0
   186
  return $x
sl@0
   187
}
sl@0
   188
datetest 3.16 "strftime('[repeat 200 %Y]','2003-10-31')" [repeat 200 2003]
sl@0
   189
datetest 3.17 "strftime('[repeat 200 abc%m123]','2003-10-31')" \
sl@0
   190
    [repeat 200 abc10123]
sl@0
   191
sl@0
   192
foreach c {a b c e g h i k l n o p q r t v x y z
sl@0
   193
           A B C D E F G I K L N O P Q R T U V Z
sl@0
   194
           0 1 2 3 4 5 6 6 7 9 _} {
sl@0
   195
  datetest 3.18.$c "strftime('%$c','2003-10-31')" NULL
sl@0
   196
}
sl@0
   197
sl@0
   198
# Ticket #2276.  Make sure leading zeros are inserted where appropriate.
sl@0
   199
#
sl@0
   200
datetest 3.20 \
sl@0
   201
   {strftime('%d/%f/%H/%W/%j/%m/%M/%S/%Y','0421-01-02 03:04:05.006')} \
sl@0
   202
   02/05.006/03/00/002/01/04/05/0421
sl@0
   203
sl@0
   204
set sqlite_current_time 1157124367
sl@0
   205
datetest 4.1 {date('now')} {2006-09-01}
sl@0
   206
set sqlite_current_time 0
sl@0
   207
sl@0
   208
datetest 5.1 {datetime('1994-04-16 14:00:00 +05:00')} {1994-04-16 09:00:00}
sl@0
   209
datetest 5.2 {datetime('1994-04-16 14:00:00 -05:15')} {1994-04-16 19:15:00}
sl@0
   210
datetest 5.3 {datetime('1994-04-16 05:00:00 +08:30')} {1994-04-15 20:30:00}
sl@0
   211
datetest 5.4 {datetime('1994-04-16 14:00:00 -11:55')} {1994-04-17 01:55:00}
sl@0
   212
datetest 5.5 {datetime('1994-04-16 14:00:00 -11:60')} NULL
sl@0
   213
datetest 5.6 {datetime('1994-04-16 14:00:00 -11:55  ')} {1994-04-17 01:55:00}
sl@0
   214
datetest 5.7 {datetime('1994-04-16 14:00:00 -11:55 x')} NULL
sl@0
   215
datetest 5.8 {datetime('1994-04-16T14:00:00Z')} {1994-04-16 14:00:00}
sl@0
   216
datetest 5.9 {datetime('1994-04-16 14:00:00z')} {1994-04-16 14:00:00}
sl@0
   217
datetest 5.10 {datetime('1994-04-16 14:00:00 Z')} {1994-04-16 14:00:00}
sl@0
   218
datetest 5.11 {datetime('1994-04-16 14:00:00z    ')} {1994-04-16 14:00:00}
sl@0
   219
datetest 5.12 {datetime('1994-04-16 14:00:00     z    ')} {1994-04-16 14:00:00}
sl@0
   220
datetest 5.13 {datetime('1994-04-16 14:00:00Zulu')} NULL
sl@0
   221
datetest 5.14 {datetime('1994-04-16 14:00:00Z +05:00')} NULL
sl@0
   222
datetest 5.15 {datetime('1994-04-16 14:00:00 +05:00 Z')} NULL
sl@0
   223
sl@0
   224
# localtime->utc and utc->localtime conversions.  These tests only work
sl@0
   225
# if the localtime is in the US Eastern Time (the time in Charlotte, NC
sl@0
   226
# and in New York.)
sl@0
   227
#
sl@0
   228
# On non-Vista Windows platform, '2006-03-31' is treated incorrectly as being
sl@0
   229
# in DST giving a 4 hour offset instead of 5.  In 2007, DST was extended to 
sl@0
   230
# start three weeks earlier (second Sunday in March) and end one week
sl@0
   231
# later (first Sunday in November).  Older Windows systems apply this
sl@0
   232
# new rule incorrectly to dates prior to 2007.
sl@0
   233
#
sl@0
   234
# It might be argued that this is masking a problem on non-Vista Windows
sl@0
   235
# platform.  A ticket has already been opened for this issue 
sl@0
   236
# (http://www.sqlite.org/cvstrac/tktview?tn=2322).  This is just to prevent
sl@0
   237
# more confusion/reports of the issue.
sl@0
   238
#
sl@0
   239
sl@0
   240
# $tzoffset_old should be 5 if DST is working correctly.
sl@0
   241
set tzoffset_old [db one {
sl@0
   242
  SELECT CAST(24*(julianday('2006-03-31') -
sl@0
   243
                  julianday('2006-03-31','localtime'))+0.5
sl@0
   244
              AS INT)
sl@0
   245
}]
sl@0
   246
sl@0
   247
# $tzoffset_new should be 4 if DST is working correctly.
sl@0
   248
set tzoffset_new [db one {
sl@0
   249
  SELECT CAST(24*(julianday('2007-03-31') -
sl@0
   250
                  julianday('2007-03-31','localtime'))+0.5
sl@0
   251
              AS INT)
sl@0
   252
}]
sl@0
   253
sl@0
   254
# Warn about possibly broken Windows DST implementations.
sl@0
   255
if {$::tcl_platform(platform)=="windows" && $tzoffset_new==4 && $tzoffset_old==4} {
sl@0
   256
  puts "******************************************************************"
sl@0
   257
  puts "N.B.:  The DST support provided by your current O/S seems to be"
sl@0
   258
  puts "suspect in that it is reporting incorrect DST values for dates"
sl@0
   259
  puts "prior to 2007.  This is the known case for most (all?) non-Vista"
sl@0
   260
  puts "Windows versions.  Please see ticket #2322 for more information."
sl@0
   261
  puts "******************************************************************"
sl@0
   262
}
sl@0
   263
sl@0
   264
if {$tzoffset_new==4} {
sl@0
   265
  datetest 6.1 {datetime('2000-10-29 05:59:00','localtime')}\
sl@0
   266
      {2000-10-29 01:59:00}
sl@0
   267
  datetest 6.1.1 {datetime('2006-10-29 05:59:00','localtime')}\
sl@0
   268
      {2006-10-29 01:59:00}
sl@0
   269
  datetest 6.1.2 {datetime('2007-11-04 05:59:00','localtime')}\
sl@0
   270
      {2007-11-04 01:59:00}
sl@0
   271
sl@0
   272
  # If the new and old DST rules seem to be working correctly...
sl@0
   273
  if {$tzoffset_new==4 && $tzoffset_old==5} {
sl@0
   274
    datetest 6.2 {datetime('2000-10-29 06:00:00','localtime')}\
sl@0
   275
        {2000-10-29 01:00:00}
sl@0
   276
    datetest 6.2.1 {datetime('2006-10-29 06:00:00','localtime')}\
sl@0
   277
        {2006-10-29 01:00:00}
sl@0
   278
  }
sl@0
   279
  datetest 6.2.2 {datetime('2007-11-04 06:00:00','localtime')}\
sl@0
   280
      {2007-11-04 01:00:00}
sl@0
   281
sl@0
   282
  # If the new and old DST rules seem to be working correctly...
sl@0
   283
  if {$tzoffset_new==4 && $tzoffset_old==5} {
sl@0
   284
    datetest 6.3 {datetime('2000-04-02 06:59:00','localtime')}\
sl@0
   285
        {2000-04-02 01:59:00}
sl@0
   286
    datetest 6.3.1 {datetime('2006-04-02 06:59:00','localtime')}\
sl@0
   287
        {2006-04-02 01:59:00}
sl@0
   288
  }
sl@0
   289
  datetest 6.3.2 {datetime('2007-03-11 07:00:00','localtime')}\
sl@0
   290
      {2007-03-11 03:00:00}
sl@0
   291
sl@0
   292
  datetest 6.4 {datetime('2000-04-02 07:00:00','localtime')}\
sl@0
   293
      {2000-04-02 03:00:00}
sl@0
   294
  datetest 6.4.1 {datetime('2006-04-02 07:00:00','localtime')}\
sl@0
   295
      {2006-04-02 03:00:00}
sl@0
   296
  datetest 6.4.2 {datetime('2007-03-11 07:00:00','localtime')}\
sl@0
   297
      {2007-03-11 03:00:00}
sl@0
   298
      
sl@0
   299
  datetest 6.5 {datetime('2000-10-29 01:59:00','utc')} {2000-10-29 05:59:00}
sl@0
   300
  datetest 6.5.1 {datetime('2006-10-29 01:59:00','utc')} {2006-10-29 05:59:00}
sl@0
   301
  datetest 6.5.2 {datetime('2007-11-04 01:59:00','utc')} {2007-11-04 05:59:00}
sl@0
   302
sl@0
   303
  # If the new and old DST rules seem to be working correctly...
sl@0
   304
  if {$tzoffset_new==4 && $tzoffset_old==5} {
sl@0
   305
    datetest 6.6 {datetime('2000-10-29 02:00:00','utc')} {2000-10-29 07:00:00}
sl@0
   306
    datetest 6.6.1 {datetime('2006-10-29 02:00:00','utc')} {2006-10-29 07:00:00}
sl@0
   307
  }
sl@0
   308
  datetest 6.6.2 {datetime('2007-11-04 02:00:00','utc')} {2007-11-04 07:00:00}
sl@0
   309
sl@0
   310
  # If the new and old DST rules seem to be working correctly...
sl@0
   311
  if {$tzoffset_new==4 && $tzoffset_old==5} {
sl@0
   312
    datetest 6.7 {datetime('2000-04-02 01:59:00','utc')} {2000-04-02 06:59:00}
sl@0
   313
    datetest 6.7.1 {datetime('2006-04-02 01:59:00','utc')} {2006-04-02 06:59:00}
sl@0
   314
  }
sl@0
   315
  datetest 6.7.2 {datetime('2007-03-11 01:59:00','utc')} {2007-03-11 06:59:00}
sl@0
   316
sl@0
   317
  datetest 6.8 {datetime('2000-04-02 02:00:00','utc')} {2000-04-02 06:00:00}
sl@0
   318
  datetest 6.8.1 {datetime('2006-04-02 02:00:00','utc')} {2006-04-02 06:00:00}
sl@0
   319
  datetest 6.8.2 {datetime('2007-03-11 02:00:00','utc')} {2007-03-11 06:00:00}
sl@0
   320
sl@0
   321
  datetest 6.10 {datetime('2000-01-01 12:00:00','localtime')} \
sl@0
   322
      {2000-01-01 07:00:00}
sl@0
   323
  datetest 6.11 {datetime('1969-01-01 12:00:00','localtime')} \
sl@0
   324
      {1969-01-01 07:00:00}
sl@0
   325
  datetest 6.12 {datetime('2039-01-01 12:00:00','localtime')} \
sl@0
   326
      {2039-01-01 07:00:00}
sl@0
   327
  datetest 6.13 {datetime('2000-07-01 12:00:00','localtime')} \
sl@0
   328
      {2000-07-01 08:00:00}
sl@0
   329
  datetest 6.14 {datetime('1969-07-01 12:00:00','localtime')} \
sl@0
   330
      {1969-07-01 07:00:00}
sl@0
   331
  datetest 6.15 {datetime('2039-07-01 12:00:00','localtime')} \
sl@0
   332
      {2039-07-01 07:00:00}
sl@0
   333
  set sqlite_current_time \
sl@0
   334
     [db eval {SELECT strftime('%s','2000-07-01 12:34:56')}]
sl@0
   335
  datetest 6.16 {datetime('now','localtime')} {2000-07-01 08:34:56}
sl@0
   336
  datetest 6.17 {datetime('now','localtimex')} NULL
sl@0
   337
  datetest 6.18 {datetime('now','localtim')} NULL
sl@0
   338
  set sqlite_current_time 0
sl@0
   339
}
sl@0
   340
sl@0
   341
# These two are a bit of a scam. They are added to ensure that 100% of
sl@0
   342
# the date.c file is covered by testing, even when the time-zone
sl@0
   343
# is not -0400 (the condition for running of the block of tests above).
sl@0
   344
#
sl@0
   345
datetest 6.19 {datetime('2039-07-01 12:00:00','localtime',null)} NULL
sl@0
   346
datetest 6.20 {datetime('2039-07-01 12:00:00','utc',null)} NULL
sl@0
   347
sl@0
   348
# Date-time functions that contain NULL arguments return a NULL
sl@0
   349
# result.
sl@0
   350
#
sl@0
   351
datetest 7.1 {datetime(null)} NULL
sl@0
   352
datetest 7.2 {datetime('now',null)} NULL
sl@0
   353
datetest 7.3 {datetime('now','localtime',null)} NULL
sl@0
   354
datetest 7.4 {time(null)} NULL
sl@0
   355
datetest 7.5 {time('now',null)} NULL
sl@0
   356
datetest 7.6 {time('now','localtime',null)} NULL
sl@0
   357
datetest 7.7 {date(null)} NULL
sl@0
   358
datetest 7.8 {date('now',null)} NULL
sl@0
   359
datetest 7.9 {date('now','localtime',null)} NULL
sl@0
   360
datetest 7.10 {julianday(null)} NULL
sl@0
   361
datetest 7.11 {julianday('now',null)} NULL
sl@0
   362
datetest 7.12 {julianday('now','localtime',null)} NULL
sl@0
   363
datetest 7.13 {strftime(null,'now')} NULL
sl@0
   364
datetest 7.14 {strftime('%s',null)} NULL
sl@0
   365
datetest 7.15 {strftime('%s','now',null)} NULL
sl@0
   366
datetest 7.16 {strftime('%s','now','localtime',null)} NULL
sl@0
   367
sl@0
   368
# Test modifiers when the date begins as a julian day number - to
sl@0
   369
# make sure the HH:MM:SS is preserved.  Ticket #551.
sl@0
   370
#
sl@0
   371
set sqlite_current_time [db eval {SELECT strftime('%s','2003-10-22 12:34:00')}]
sl@0
   372
datetest 8.1 {datetime('now','weekday 0')} {2003-10-26 12:34:00}
sl@0
   373
datetest 8.2 {datetime('now','weekday 1')} {2003-10-27 12:34:00}
sl@0
   374
datetest 8.3 {datetime('now','weekday 2')} {2003-10-28 12:34:00}
sl@0
   375
datetest 8.4 {datetime('now','weekday 3')} {2003-10-22 12:34:00}
sl@0
   376
datetest 8.5 {datetime('now','start of month')} {2003-10-01 00:00:00}
sl@0
   377
datetest 8.6 {datetime('now','start of year')} {2003-01-01 00:00:00}
sl@0
   378
datetest 8.7 {datetime('now','start of day')} {2003-10-22 00:00:00}
sl@0
   379
datetest 8.8 {datetime('now','1 day')} {2003-10-23 12:34:00}
sl@0
   380
datetest 8.9 {datetime('now','+1 day')} {2003-10-23 12:34:00}
sl@0
   381
datetest 8.10 {datetime('now','+1.25 day')} {2003-10-23 18:34:00}
sl@0
   382
datetest 8.11 {datetime('now','-1.0 day')} {2003-10-21 12:34:00}
sl@0
   383
datetest 8.12 {datetime('now','1 month')} {2003-11-22 12:34:00}
sl@0
   384
datetest 8.13 {datetime('now','11 month')} {2004-09-22 12:34:00}
sl@0
   385
datetest 8.14 {datetime('now','-13 month')} {2002-09-22 12:34:00}
sl@0
   386
datetest 8.15 {datetime('now','1.5 months')} {2003-12-07 12:34:00}
sl@0
   387
datetest 8.16 {datetime('now','-5 years')} {1998-10-22 12:34:00}
sl@0
   388
datetest 8.17 {datetime('now','+10.5 minutes')} {2003-10-22 12:44:30}
sl@0
   389
datetest 8.18 {datetime('now','-1.25 hours')} {2003-10-22 11:19:00}
sl@0
   390
datetest 8.19 {datetime('now','11.25 seconds')} {2003-10-22 12:34:11}
sl@0
   391
datetest 8.90 {datetime('now','abcdefghijklmnopqrstuvwyxzABCDEFGHIJLMNOP')} NULL
sl@0
   392
set sqlite_current_time 0
sl@0
   393
sl@0
   394
# Negative years work.  Example:  '-4713-11-26' is JD 1.5.
sl@0
   395
#
sl@0
   396
datetest 9.1 {julianday('-4713-11-24 12:00:00')} {0.0}
sl@0
   397
datetest 9.2 {julianday(datetime(5))} {5.0}
sl@0
   398
datetest 9.3 {julianday(datetime(10))} {10.0}
sl@0
   399
datetest 9.4 {julianday(datetime(100))} {100.0}
sl@0
   400
datetest 9.5 {julianday(datetime(1000))} {1000.0}
sl@0
   401
datetest 9.6 {julianday(datetime(10000))} {10000.0}
sl@0
   402
datetest 9.7 {julianday(datetime(100000))} {100000.0}
sl@0
   403
sl@0
   404
# datetime() with just an HH:MM:SS correctly inserts the date 2000-01-01.
sl@0
   405
#
sl@0
   406
datetest 10.1 {datetime('01:02:03')}  {2000-01-01 01:02:03}
sl@0
   407
datetest 10.2 {date('01:02:03')}  {2000-01-01}
sl@0
   408
datetest 10.3 {strftime('%Y-%m-%d %H:%M','01:02:03')} {2000-01-01 01:02}
sl@0
   409
sl@0
   410
# Test the new HH:MM:SS modifier
sl@0
   411
#
sl@0
   412
datetest 11.1 {datetime('2004-02-28 20:00:00', '-01:20:30')} \
sl@0
   413
   {2004-02-28 18:39:30}
sl@0
   414
datetest 11.2 {datetime('2004-02-28 20:00:00', '+12:30:00')} \
sl@0
   415
   {2004-02-29 08:30:00}
sl@0
   416
datetest 11.3 {datetime('2004-02-28 20:00:00', '+12:30')} \
sl@0
   417
   {2004-02-29 08:30:00}
sl@0
   418
datetest 11.4 {datetime('2004-02-28 20:00:00', '12:30')} \
sl@0
   419
   {2004-02-29 08:30:00}
sl@0
   420
datetest 11.5 {datetime('2004-02-28 20:00:00', '-12:00')} \
sl@0
   421
   {2004-02-28 08:00:00}
sl@0
   422
datetest 11.6 {datetime('2004-02-28 20:00:00', '-12:01')} \
sl@0
   423
   {2004-02-28 07:59:00}
sl@0
   424
datetest 11.7 {datetime('2004-02-28 20:00:00', '-11:59')} \
sl@0
   425
   {2004-02-28 08:01:00}
sl@0
   426
datetest 11.8 {datetime('2004-02-28 20:00:00', '11:59')} \
sl@0
   427
   {2004-02-29 07:59:00}
sl@0
   428
datetest 11.9 {datetime('2004-02-28 20:00:00', '12:01')} \
sl@0
   429
   {2004-02-29 08:01:00}
sl@0
   430
datetest 11.10 {datetime('2004-02-28 20:00:00', '12:60')} NULL
sl@0
   431
sl@0
   432
# Ticket #1964
sl@0
   433
datetest 12.1 {datetime('2005-09-01')} {2005-09-01 00:00:00}
sl@0
   434
datetest 12.2 {datetime('2005-09-01','+0 hours')} {2005-09-01 00:00:00}
sl@0
   435
sl@0
   436
# Ticket #1991
sl@0
   437
do_test date-13.1 {
sl@0
   438
  execsql {
sl@0
   439
    SELECT strftime('%Y-%m-%d %H:%M:%f', julianday('2006-09-24T10:50:26.047'))
sl@0
   440
  }
sl@0
   441
} {{2006-09-24 10:50:26.047}}
sl@0
   442
sl@0
   443
# Ticket #2153
sl@0
   444
datetest 13.2 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 12:34:59.6')} \
sl@0
   445
  {2007-01-01 12:34:59}
sl@0
   446
datetest 13.3 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 12:34:59.6')} \
sl@0
   447
  {2007-01-01 12:34:59.600}
sl@0
   448
datetest 13.4 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 12:59:59.6')} \
sl@0
   449
  {2007-01-01 12:59:59}
sl@0
   450
datetest 13.5 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 12:59:59.6')} \
sl@0
   451
  {2007-01-01 12:59:59.600}
sl@0
   452
datetest 13.6 {strftime('%Y-%m-%d %H:%M:%S', '2007-01-01 23:59:59.6')} \
sl@0
   453
  {2007-01-01 23:59:59}
sl@0
   454
datetest 13.7 {strftime('%Y-%m-%d %H:%M:%f', '2007-01-01 23:59:59.6')} \
sl@0
   455
  {2007-01-01 23:59:59.600}
sl@0
   456
sl@0
   457
# Test for issues reported by BareFeet (list.sql at tandb.com.au)
sl@0
   458
# on mailing list on 2008-06-12.
sl@0
   459
#
sl@0
   460
# Put a floating point number in the database so that we can manipulate
sl@0
   461
# raw bits using the hexio interface.
sl@0
   462
#
sl@0
   463
do_test date-14.1 {
sl@0
   464
  execsql {
sl@0
   465
    PRAGMA auto_vacuum=OFF;
sl@0
   466
    PRAGMA page_size = 1024;
sl@0
   467
    CREATE TABLE t1(x);
sl@0
   468
    INSERT INTO t1 VALUES(1.1);
sl@0
   469
  }
sl@0
   470
  db close
sl@0
   471
  hexio_write test.db 2040 4142ba32bffffff9
sl@0
   472
  sqlite3 db test.db
sl@0
   473
  db eval {SELECT * FROM t1}
sl@0
   474
} {2454629.5}
sl@0
   475
sl@0
   476
# Changing the least significant byte of the floating point value between
sl@0
   477
# 00 and FF should always generate a time of either 23:59:59 or 00:00:00,
sl@0
   478
# never 24:00:00
sl@0
   479
#
sl@0
   480
for {set i 0} {$i<=255} {incr i} {
sl@0
   481
  db close
sl@0
   482
  hexio_write test.db 2047 [format %02x $i]
sl@0
   483
  sqlite3 db test.db
sl@0
   484
  do_test date-14.2.$i {
sl@0
   485
    set date [db one {SELECT datetime(x) FROM t1}]
sl@0
   486
    expr {$date eq "2008-06-12 00:00:00" || $date eq "2008-06-11 23:59:59"}
sl@0
   487
  } {1}
sl@0
   488
}
sl@0
   489
finish_test