From: drh <> Date: Fri, 11 Feb 2022 14:08:05 +0000 (+0000) Subject: Use the enhanced SQLITE_TESTCTRL_LOCALTIME_FAULT (2) capability to do better X-Git-Tag: version-3.38.0~28 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=85548fac8d11c6e5b2922bdea95d169852342e13;p=thirdparty%2Fsqlite.git Use the enhanced SQLITE_TESTCTRL_LOCALTIME_FAULT (2) capability to do better testing of the 'localtime' and 'utc' modifiers to date/time functions. FossilOrigin-Name: f38071946ac2e5bb61e6c0620507e4c94464999ac700d86c1a704b94eb1c009a --- diff --git a/manifest b/manifest index 1210bd4cf0..bc6b62935d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sthe\sbind2.test\stest\sscript\sso\sthat\sit\sworks\seven\sif\nSQLITE_ENABLE_PREUPDATE_HOOK\sis\snot\sdefined.\nFix\sfor\stest-case\sbreakage\sfrom\scheck-in\s[c006515ae6faff65]. -D 2022-02-11T12:06:37.779 +C Use\sthe\senhanced\sSQLITE_TESTCTRL_LOCALTIME_FAULT\s(2)\scapability\sto\sdo\sbetter\ntesting\sof\sthe\s'localtime'\sand\s'utc'\smodifiers\sto\sdate/time\sfunctions. +D 2022-02-11T14:08:05.256 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -562,7 +562,7 @@ F src/sqliteLimit.h d7323ffea5208c6af2734574bae933ca8ed2ab728083caa117c9738581a3 F src/status.c 4b8bc2a6905163a38b739854a35b826c737333fab5b1f8e03fa7eb9a4799c4c1 F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1 F src/tclsqlite.c 48f291e1a7e672a7204884d4c164a8ed3a522ff087c361ada2991f5d54e987f6 -F src/test1.c a5d93408dfc5d91f35af12a9c6e90562cc1713f435a8642c9dbd3ffdb4d39ad2 +F src/test1.c 87fda59eea3ac1eba1baef37c1967565cb1b8d6d264649f2e57f252ca5989914 F src/test2.c 3efb99ab7f1fc8d154933e02ae1378bac9637da5 F src/test3.c 61798bb0d38b915067a8c8e03f5a534b431181f802659a6616f9b4ff7d872644 F src/test4.c 7c4420e01c577b5c4add2cb03119743b1a357543d347773b9e717195ea967159 @@ -837,7 +837,7 @@ F test/ctime.test 340f362f41f92972bbd71f44e10569a5cc694062b692231bd08aa6fe6c1c47 F test/cursorhint.test 0175e4404181ace3ceca8b114eb0a98eae600d565aa4e2705abbe6614c7fe201 F test/cursorhint2.test 6f3aa9cb19e7418967a10ec6905209bcbb5968054da855fc36c8beee9ae9c42f F test/dataversion1.test 6e5e86ac681f0782e766ebcb56c019ae001522d114e0e111e5ebf68ccf2a7bb8 -F test/date.test 7e1da08fca5492081605a2bf445370b06be6a95db1150f3133b70eb672b33cb3 +F test/date.test 50d5b1eeec57041c000d2be5e9ab995026ded4255507f0206c81c1ca3b34b840 F test/date2.test 7e12ec14aaf4d5e6294b4ba140445b0eca06ea50062a9c3a69c4ee13d0b6f8b1 F test/date3.test a1b77abf05c6772fe5ca2337cac1398892f2a41e62bce7e6be0f4a08a0e64ae5 F test/dbdata.test 042f49acff3438f940eeba5868d3af080ae64ddf26ae78f80c92bec3ca7d8603 @@ -1944,8 +1944,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P eae3ab0a050079d050f339b2510eebd55afe4464e9b410ddacb7523f89981144 -R 79f6972005a4c1979207db9e22e0082a +P 937d3a45b3fef354dbdea2085fbb7d9c752a96e8ebb62f61931deb5a64132946 +R a16f91be526745131b2dd2bc3f00c944 U drh -Z da1ae444693eca5c95a17e499729f262 +Z 614bb1714108c365812b64395ec64ab7 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 5d68a80c5c..910522f286 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -937d3a45b3fef354dbdea2085fbb7d9c752a96e8ebb62f61931deb5a64132946 \ No newline at end of file +f38071946ac2e5bb61e6c0620507e4c94464999ac700d86c1a704b94eb1c009a \ No newline at end of file diff --git a/src/test1.c b/src/test1.c index 3eba084497..455c4b2579 100644 --- a/src/test1.c +++ b/src/test1.c @@ -7207,6 +7207,55 @@ static int SQLITE_TCLAPI test_print_eqp( } #endif /* SQLITE_OMIT_EXPLAIN */ +#include +/* +** This is an alternative localtime_r() implementation used for testing +** the 'localtime' and 'utc' modifiers of date-time functions. Because +** the OS-supplied localtime_r() is locale-dependent, this alternative is +** provided as a stable test platform. +** +** Operation: +** +** (1) Localtime is 30 minutes earlier than (west of) UTC on +** even days (counting from 1970-01-01) +** +** (2) Localtime is 30 minutes later than (east of) UTC on odd days. +** +** (3) The function fails for the specific date/time value +** of 2000-05-29 14:16:00 in order to test the ability of +** SQLite to deal with localtime_r() failures. +*/ +static int testLocaltime(const void *aliasT, void *aliasTM){ + const time_t t = *(const time_t*)aliasT; + struct tm *pTm = (struct tm *)aliasTM; + time_t altT; + sqlite3_int64 iJD; + int Z, A, B, C, D, E, X1, S; + + if( (t/86400) & 1 ){ + altT = t + 1800; /* 30 minutes later on odd days */ + }else{ + altT = t - 1800; /* 30 minutes earlier on even days */ + } + iJD = (sqlite3_int64)(altT + 210866760000); + Z = (int)((iJD + 43200)/86400); + A = (int)((Z - 1867216.25)/36524.25); + A = Z + 1 + A - (A/4); + B = A + 1524; + C = (int)((B - 122.1)/365.25); + D = (36525*(C&32767))/100; + E = (int)((B-D)/30.6001); + X1 = (int)(30.6001*E); + pTm->tm_mday = B - D - X1; + pTm->tm_mon = E<14 ? E-2 : E-14; + pTm->tm_year = (pTm->tm_mon>1 ? C - 4716 : C - 4715) - 1900; + S = (int)((iJD + 43200)%86400); + pTm->tm_hour = S/3600; + pTm->tm_min = (S/60)%60; + pTm->tm_sec = S % 60; + return t==959609760; /* Special case: 2000-05-29 14:16:00 fails */ +} + /* ** sqlite3_test_control VERB ARGS... */ @@ -7254,11 +7303,11 @@ static int SQLITE_TCLAPI test_test_control( case SQLITE_TESTCTRL_LOCALTIME_FAULT: { int val; if( objc!=3 ){ - Tcl_WrongNumArgs(interp, 2, objv, "ONOFF"); + Tcl_WrongNumArgs(interp, 2, objv, "0|1|2"); return TCL_ERROR; } - if( Tcl_GetBooleanFromObj(interp, objv[2], &val) ) return TCL_ERROR; - sqlite3_test_control(iFlag, val!=0); + if( Tcl_GetIntFromObj(interp, objv[2], &val) ) return TCL_ERROR; + sqlite3_test_control(iFlag, val, testLocaltime); break; } diff --git a/test/date.test b/test/date.test index 2e5859ad29..a9a34065c1 100644 --- a/test/date.test +++ b/test/date.test @@ -239,138 +239,70 @@ datetest 5.13 {datetime('1994-04-16 14:00:00Zulu')} NULL datetest 5.14 {datetime('1994-04-16 14:00:00Z +05:00')} NULL datetest 5.15 {datetime('1994-04-16 14:00:00 +05:00 Z')} NULL -# localtime->utc and utc->localtime conversions. These tests only work -# if the localtime is in the US Eastern Time (the time in Charlotte, NC -# and in New York.) +# localtime->utc and utc->localtime conversions. # -# On non-Vista Windows platform, '2006-03-31' is treated incorrectly as being -# in DST giving a 4 hour offset instead of 5. In 2007, DST was extended to -# start three weeks earlier (second Sunday in March) and end one week -# later (first Sunday in November). Older Windows systems apply this -# new rule incorrectly to dates prior to 2007. +# Use SQLITE_TESTCTRL_LOCALTIME_FAULT=2 to set an alternative localtime_r() +# implementation that is not locale-dependent. This testing localtime_r() +# operates as follows: # -# It might be argued that this is masking a problem on non-Vista Windows -# platform. A ticket has already been opened for this issue -# (http://www.sqlite.org/cvstrac/tktview?tn=2322). This is just to prevent -# more confusion/reports of the issue. +# (1) Localtime is 30 minutes earlier than (west of) UTC on +# even days (counting from 1970-01-01) # - -# $tzoffset_old should be 5 if DST is working correctly. -set tzoffset_old [db one { - SELECT CAST(24*(julianday('2006-03-31') - - julianday('2006-03-31','localtime'))+0.5 - AS INT) -}] - -# $tzoffset_new should be 4 if DST is working correctly. -set tzoffset_new [db one { - SELECT CAST(24*(julianday('2007-03-31') - - julianday('2007-03-31','localtime'))+0.5 - AS INT) -}] - -# Warn about possibly broken Windows DST implementations. -if {$::tcl_platform(platform)=="windows" && $tzoffset_new==4 && $tzoffset_old==4} { - puts "******************************************************************" - puts "N.B.: The DST support provided by your current O/S seems to be" - puts "suspect in that it is reporting incorrect DST values for dates" - puts "prior to 2007. This is the known case for most (all?) non-Vista" - puts "Windows versions. Please see ticket #2322 for more information." - puts "******************************************************************" +# (2) Localtime is 30 minutes later than (east of) UTC on odd days. +# +# (3) The function fails for the specific date/time value +# of 2000-05-29 14:16:00 in order to test the ability of +# SQLite to deal with localtime_r() failures. +# +proc local_to_utc {tn utc local} { + do_execsql_test date-$tn "SELECT datetime('$utc','localtime')" [list $local] +} +proc utc_to_local {tn local utc} { + do_execsql_test date-$tn "SELECT datetime('$local','utc')" [list $utc] } -if {$tzoffset_new==4} { - datetest 6.1 {datetime('2000-10-29 05:59:00','localtime')}\ - {2000-10-29 01:59:00} - datetest 6.1.1 {datetime('2006-10-29 05:59:00','localtime')}\ - {2006-10-29 01:59:00} - datetest 6.1.2 {datetime('2007-11-04 05:59:00','localtime')}\ - {2007-11-04 01:59:00} - - # If the new and old DST rules seem to be working correctly... - if {$tzoffset_new==4 && $tzoffset_old==5} { - datetest 6.2 {datetime('2000-10-29 06:00:00','localtime')}\ - {2000-10-29 01:00:00} - datetest 6.2.1 {datetime('2006-10-29 06:00:00','localtime')}\ - {2006-10-29 01:00:00} - } - datetest 6.2.2 {datetime('2007-11-04 06:00:00','localtime')}\ - {2007-11-04 01:00:00} - - # If the new and old DST rules seem to be working correctly... - if {$tzoffset_new==4 && $tzoffset_old==5} { - datetest 6.3 {datetime('2000-04-02 06:59:00','localtime')}\ - {2000-04-02 01:59:00} - datetest 6.3.1 {datetime('2006-04-02 06:59:00','localtime')}\ - {2006-04-02 01:59:00} - } - datetest 6.3.2 {datetime('2007-03-11 07:00:00','localtime')}\ - {2007-03-11 03:00:00} - - datetest 6.4 {datetime('2000-04-02 07:00:00','localtime')}\ - {2000-04-02 03:00:00} - datetest 6.4.1 {datetime('2006-04-02 07:00:00','localtime')}\ - {2006-04-02 03:00:00} - datetest 6.4.2 {datetime('2007-03-11 07:00:00','localtime')}\ - {2007-03-11 03:00:00} - - datetest 6.5 {datetime('2000-10-29 01:59:00','utc')} {2000-10-29 05:59:00} - datetest 6.5.1 {datetime('2006-10-29 01:59:00','utc')} {2006-10-29 05:59:00} - datetest 6.5.2 {datetime('2007-11-04 01:59:00','utc')} {2007-11-04 05:59:00} - - # If the new and old DST rules seem to be working correctly... - if {$tzoffset_new==4 && $tzoffset_old==5} { - datetest 6.6 {datetime('2000-10-29 02:00:00','utc')} {2000-10-29 07:00:00} - datetest 6.6.1 {datetime('2006-10-29 02:00:00','utc')} {2006-10-29 07:00:00} - } - datetest 6.6.2 {datetime('2007-11-04 02:00:00','utc')} {2007-11-04 07:00:00} +sqlite3_test_control SQLITE_TESTCTRL_LOCALTIME_FAULT 2 +local_to_utc 6.1 {2000-10-29 12:00:00} {2000-10-29 12:30:00} +utc_to_local 6.2 {2000-10-29 12:30:00} {2000-10-29 12:00:00} +local_to_utc 6.3 {2000-10-30 12:00:00} {2000-10-30 11:30:00} +utc_to_local 6.4 {2000-10-30 11:30:00} {2000-10-30 12:00:00} - # If the new and old DST rules seem to be working correctly... - if {$tzoffset_new==4 && $tzoffset_old==5} { - datetest 6.7 {datetime('2000-04-02 01:59:00','utc')} {2000-04-02 06:59:00} - datetest 6.7.1 {datetime('2006-04-02 01:59:00','utc')} {2006-04-02 06:59:00} - } - datetest 6.7.2 {datetime('2007-03-11 01:59:00','utc')} {2007-03-11 06:59:00} - - datetest 6.8 {datetime('2000-04-02 02:00:00','utc')} {2000-04-02 07:00:00} - datetest 6.8.1 {datetime('2006-04-02 02:00:00','utc')} {2006-04-02 07:00:00} - datetest 6.8.2 {datetime('2007-03-11 02:00:00','utc')} {2007-03-11 07:00:00} - - # The 'utc' modifier is a no-op if the LHS is known to already be in UTC - datetest 6.9.1 {datetime('2015-12-23 12:00:00','utc')} {2015-12-23 17:00:00} - datetest 6.9.2 {datetime('2015-12-23 12:00:00z','utc')} {2015-12-23 12:00:00} - datetest 6.9.3 {datetime('2015-12-23 12:00:00-03:00','utc')} \ - {2015-12-23 15:00:00} - datetest 6.9.4 {datetime('2015-12-23 12:00:00','utc','utc','utc')} \ - {2015-12-23 17:00:00} - - - datetest 6.10 {datetime('2000-01-01 12:00:00','localtime')} \ - {2000-01-01 07:00:00} - datetest 6.11 {datetime('1969-01-01 12:00:00','localtime')} \ - {1969-01-01 07:00:00} - datetest 6.12 {datetime('2039-01-01 12:00:00','localtime')} \ - {2039-01-01 07:00:00} - datetest 6.13 {datetime('2000-07-01 12:00:00','localtime')} \ - {2000-07-01 08:00:00} - datetest 6.14 {datetime('1969-11-01 12:00:00','localtime')} \ - {1969-11-01 07:00:00} - datetest 6.15 {datetime('2039-02-01 12:00:00','localtime')} \ - {2039-02-01 07:00:00} - set sqlite_current_time \ - [db eval {SELECT strftime('%s','2000-07-01 12:34:56')}] - datetest 6.16 {datetime('now','localtime')} {2000-07-01 08:34:56} - datetest 6.17 {datetime('now','localtimex')} NULL - datetest 6.18 {datetime('now','localtim')} NULL - set sqlite_current_time 0 -} +local_to_utc 6.5 {2000-10-28 23:59:59} {2000-10-28 23:29:59} +local_to_utc 6.6 {2000-10-29 00:00:00} {2000-10-29 00:30:00} -# These two are a bit of a scam. They are added to ensure that 100% of -# the date.c file is covered by testing, even when the time-zone -# is not -0400 (the condition for running of the block of tests above). +# The previous two cases establish that no such localtime as +# 2000-10-29 00:10:00 exists. Verify that we get a reasonable +# answer if we try to convert this non-existant localtime to utc? # -datetest 6.19 {datetime('2039-07-01 12:00:00','localtime',null)} NULL -datetest 6.20 {datetime('2039-07-01 12:00:00','utc',null)} NULL +utc_to_local 6.7 {2000-10-29 00:10:00} {2000-10-28 23:40:00} + +local_to_utc 6.8 {2022-02-10 23:59:59} {2022-02-11 00:29:59} +local_to_utc 6.9 {2022-02-11 00:00:00} {2022-02-10 23:30:00} +local_to_utc 6.10 {2022-02-10 23:45:00} {2022-02-11 00:15:00} +local_to_utc 6.11 {2022-02-11 00:45:00} {2022-02-11 00:15:00} + +# The previous two cases show that two different UTC values give +# the same localtime of 2022-02-11 00:15:00. When converting from +# that localtime back to UTC, we should get one or the other of +# the two UTC values. +# +utc_to_local 6.12 {2022-02-11 00:15:00} {2022-02-11 00:45:00} + +# If localtime_r() fails, the datetime() SQL function should raise an error +# +do_catchsql_test date-6.20 { + SELECT datetime('2000-05-29 14:16:00','localtime'); +} {1 {local time unavailable}} + +# Modifiers work for dates that are way out of band for localtime_r() +# +local_to_utc 6.21 {1800-10-29 12:00:00} {1800-10-29 12:30:00} +utc_to_local 6.22 {1800-10-29 12:30:00} {1800-10-29 12:00:00} +local_to_utc 6.23 {3000-10-30 12:00:00} {3000-10-30 11:30:00} +utc_to_local 6.24 {3000-10-30 11:30:00} {3000-10-30 12:00:00} + +# Restore the use of the OS localtime_r() before going on... +sqlite3_test_control SQLITE_TESTCTRL_LOCALTIME_FAULT 0 # Date-time functions that contain NULL arguments return a NULL # result.