(*--------------------------------------------------------------------------- Copyright (c) 2025 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (** Tests for {!Sql.Private.Datetime}: SQLite's deterministic date/time functions (lang_datefunc.html). Vectors are the values sqlite3 returns. *) module D = Sql.Private.Datetime let text s = Sql.Ast.Text s (* Lossless rendering of a result value for exact assertions. *) let vstr : Sql.Ast.value -> string = function | Null -> "NULL" | Int i -> Fmt.str "int:%Ld" i | Float f -> Fmt.str "float:%.6f" f | Text s -> Fmt.str "text:%s" s | Blob b -> Fmt.str "blob:%s" b let check label expected fn args = Alcotest.(check string) label expected (vstr (D.call fn args)) (* The same functions reached through the engine, so the parse and the scalar dispatch are exercised alongside {!Sql.Private.Datetime.call}. Every expected value below is the sqlite3 CLI's output for the same call. *) let cat = lazy (Catalog.of_mem []) let one sql = match Sql.exec (Sql.v (Lazy.force cat)) sql with | Sql.Rows [ [ v ] ] -> v | _ -> Alcotest.failf "not a single scalar row: %s" sql let sql_check label expected sql = Alcotest.(check string) label (vstr expected) (vstr (one sql)) let test_explicit_forms () = check "date passes a valid date through" "text:2024-02-29" "date" [ text "2024-02-29" ]; check "datetime keeps the time" "text:2024-01-01 12:30:00" "datetime" [ text "2024-01-01 12:30:00" ]; check "time drops the date" "text:12:30:00" "time" [ text "2024-01-01 12:30:00" ] let test_julian_and_unix () = check "julianday at the J2000 midnight" "float:2451544.500000" "julianday" [ text "2000-01-01" ]; check "unixepoch at 2024-01-01" "int:1704067200" "unixepoch" [ text "2024-01-01" ] let test_modifiers () = check "add one day, crossing a month" "text:2024-02-01" "date" [ text "2024-01-31"; text "+1 day" ]; check "start of month truncates" "text:2024-06-01" "date" [ text "2024-06-15"; text "start of month" ] let test_strftime () = check "strftime year-month" "text:2024-06" "strftime" [ text "%Y-%m"; text "2024-06-15" ] let test_null_results () = Alcotest.(check string) "the wall clock is NULL with no clock injected" "NULL" (vstr (D.call "date" [ text "now" ])); Alcotest.(check string) "an unparseable time is NULL" "NULL" (vstr (D.call "date" [ text "not-a-date" ])) (* The CURRENT_* keywords are the same three functions over 'now' (sqlite/src/date.c: ctimestampFunc is datetime, cdateFunc is date, ctimeFunc is time), so they reach the wall clock by the one path the modifier does and read NULL with none injected. The keyword resolves -- what it resolves to is the clock's business, not the parser's. *) let test_sql_current_keywords () = sql_check "CURRENT_TIMESTAMP" Null "SELECT CURRENT_TIMESTAMP"; sql_check "CURRENT_DATE" Null "SELECT CURRENT_DATE"; sql_check "CURRENT_TIME" Null "SELECT CURRENT_TIME"; sql_check "the same value date('now') reads" (D.call "datetime" [ text "now" ]) "SELECT CURRENT_TIMESTAMP" let test_unknown_function () = match D.call "haversine" [] with | _ -> Alcotest.fail "a non-datetime function should raise" | exception Invalid_argument _ -> () (* date / time / datetime formatting from an ISO string. *) let test_sql_formatting () = sql_check "date" (text "2024-01-15") "SELECT date('2024-01-15')"; sql_check "time" (text "13:45:30") "SELECT time('2024-01-15 13:45:30')"; sql_check "datetime" (text "2024-01-15 13:45:30") "SELECT datetime('2024-01-15 13:45:30')"; sql_check "datetime from date" (text "2024-01-15 00:00:00") "SELECT datetime('2024-01-15')"; sql_check "time-only takes 2000-01-01" (text "2000-01-01") "SELECT date('13:45')" (* Julian Day and unix epoch conversions, both directions. *) let test_sql_julian_unix () = sql_check "julianday midnight" (Float 2451544.5) "SELECT julianday('2000-01-01')"; sql_check "julianday noon" (Float 2451545.0) "SELECT julianday('2000-01-01 12:00:00')"; sql_check "datetime of JD" (text "2000-01-01 12:00:00") "SELECT datetime(2451545.0)"; sql_check "unixepoch" (Int 1705276800L) "SELECT unixepoch('2024-01-15')"; sql_check "datetime of unix" (text "2024-01-15 00:00:00") "SELECT datetime(1705276800,'unixepoch')" (* Modifiers: unit offsets (with day/month overflow normalisation) and the "start of" snaps. *) let test_sql_modifiers () = sql_check "month then day" (text "2024-02-14") "SELECT date('2024-01-15','+1 month','-1 day')"; sql_check "month overflow Jan31 to Mar2" (text "2024-03-02") "SELECT date('2024-01-31','+1 month')"; sql_check "year overflow Feb29 to Mar1" (text "2025-03-01") "SELECT date('2024-02-29','+1 year')"; sql_check "start of day" (text "2024-01-15 00:00:00") "SELECT datetime('2024-01-15 13:45:30','start of day')"; sql_check "start of month" (text "2024-01-01") "SELECT date('2024-01-15','start of month')"; sql_check "36 hours crosses a day" (text "2024-01-16 12:00:00") "SELECT datetime('2024-01-15','+36 hours')" (* strftime conversions. *) let test_sql_strftime () = sql_check "Y/m/d H:M" (text "2024/03/04 05:06") "SELECT strftime('%Y/%m/%d %H:%M','2024-03-04 05:06:07')"; sql_check "%s unix seconds" (text "1705276800") "SELECT strftime('%s','2024-01-15')"; sql_check "%j day-of-year, %w day-of-week, %W week" (text "015 1 03") "SELECT strftime('%j %w %W','2024-01-15')" (* A format that is not text is read as its text, a real as CAST(r AS TEXT) renders it, and a NULL format is a NULL answer. With sqlite3 3.54.1: $ sqlite3 :memory: "SELECT quote(strftime(1.0, '2024-01-01')), quote(strftime(0.1234567, '2024-01-01')), quote(strftime(1e20, '2024-01-01')), quote(strftime(0.1 + 0.2, '2024-01-01')), quote(strftime(NULL, '2024-01-01'))" '1.0'|'0.1234567'|'1.0e+20'|'0.30000000000000004'|NULL *) let test_sql_strftime_real_format () = sql_check "1.0" (text "1.0") "SELECT strftime(1.0, '2024-01-01')"; sql_check "seven digits" (text "0.1234567") "SELECT strftime(0.1234567, '2024-01-01')"; sql_check "an exponent" (text "1.0e+20") "SELECT strftime(1e20, '2024-01-01')"; sql_check "seventeen digits" (text "0.30000000000000004") "SELECT strftime(0.1 + 0.2, '2024-01-01')"; sql_check "NULL" Null "SELECT strftime(NULL, '2024-01-01')" (* An unparseable value, an out-of-range field, or an unknown modifier is NULL, never an error -- matching sqlite3. *) let test_sql_null_on_bad_input () = sql_check "garbage" Null "SELECT date('garbage')"; sql_check "month 13" Null "SELECT date('2024-13-01')"; sql_check "hour 25" Null "SELECT time('25:00')"; sql_check "null input" Null "SELECT date(NULL)"; sql_check "unknown modifier" Null "SELECT date('2024-01-15','no such mod')" (* A trailing timezone designator (Z / +HH:MM / -HH:MM) shifts the time to UTC, which sqlite3 stores; the 'T' date/time separator is accepted too. *) let test_sql_timezone () = sql_check "Z is UTC, no shift" (text "2000-10-29 12:00:00") "SELECT datetime('2000-10-29 12:00:00Z')"; sql_check "+05:00 shifts back to UTC" (text "2000-10-29 07:00:00") "SELECT datetime('2000-10-29 12:00:00+05:00')"; sql_check "-05:00 shifts forward to UTC" (text "2000-10-29 17:00:00") "SELECT datetime('2000-10-29 12:00:00-05:00')"; sql_check "T separator with Z" (text "2000-10-29 12:00:00") "SELECT datetime('2000-10-29T12:00:00Z')"; (* 'utc' is a no-op once the value carries a timezone (already UTC), so it keeps the time; a space before the offset is allowed. *) sql_check "utc modifier is a no-op on a Z time" (text "2000-10-29 12:00:00") "SELECT datetime('2000-10-29 12:00Z','utc','utc')"; sql_check "utc after an explicit offset keeps the UTC time" (text "2000-10-29 07:00:00") "SELECT datetime('2000-10-29 12:00:00+05:00','utc')"; sql_check "a space before the offset is accepted" (text "2000-10-29 12:00:00") "SELECT datetime('2000-10-29 12:00 -00:00','utc')"; (* With no zone injected the offset is zero, so 'utc' and 'localtime' on a timezone-less local time keep the time -- what the sqlite3 CLI answers with TZ=UTC. *) sql_check "utc on a local time under the default zone" (text "2000-10-29 12:00:00") "SELECT datetime('2000-10-29 12:00:00','utc')"; sql_check "localtime on a local time under the default zone" (text "2000-10-29 12:00:00") "SELECT datetime('2000-10-29 12:00:00','localtime')" let test_zone () = let paris (_ : int64) = 3600 in let z label expected fn args = Alcotest.(check string) label expected (vstr (D.call ~zone:paris fn args)) in z "localtime adds the offset" "text:2000-10-29 13:00:00" "datetime" [ text "2000-10-29 12:00:00"; text "localtime" ]; z "utc subtracts it" "text:2000-10-29 11:00:00" "datetime" [ text "2000-10-29 12:00:00"; text "utc" ]; (* 'utc' is a no-op on a value that already carries a timezone; 'localtime' is not -- sqlite3 reads tzSet in the one and not the other. *) z "utc keeps a timezone-bearing value" "text:2000-10-29 12:00:00" "datetime" [ text "2000-10-29 12:00:00Z"; text "utc" ]; z "localtime shifts one anyway" "text:2000-10-29 13:00:00" "datetime" [ text "2000-10-29 12:00:00Z"; text "localtime" ] (* 'now' answers from the instant the caller injects, in milliseconds since the unix epoch; with none injected it is NULL. The comparison is on the value as written, case-folded, so ' now ' is not 'now' -- the rule {!Sql.Private.Datetime.reads_the_clock} states. *) let test_now () = let epoch = 1_704_067_200_000L in let n label expected fn args = Alcotest.(check string) label expected (vstr (D.call ~now:epoch fn args)) in n "datetime" "text:2024-01-01 00:00:00" "datetime" [ text "now" ]; n "date" "text:2024-01-01" "date" [ text "now" ]; n "time" "text:00:00:00" "time" [ text "now" ]; n "unixepoch" "int:1704067200" "unixepoch" [ text "now" ]; n "julianday" "float:2460310.500000" "julianday" [ text "now" ]; n "modifiers apply to it" "text:2024-01-02" "date" [ text "now"; text "+1 day" ]; n "the name is case-folded" "text:2024-01-01" "date" [ text "NOW" ]; n "a padded 'now' is not one" "NULL" "date" [ text " now " ]; Alcotest.(check string) "with no instant injected it is NULL" "NULL" (vstr (D.call "datetime" [ text "now" ])) let test_sql_weekday () = sql_check "weekday 0 goes on to Sunday, keeping the time" (text "2024-01-21 13:45:30") "SELECT datetime('2024-01-15 13:45:30','weekday 0')"; sql_check "weekday 1 on a Monday is a no-op" (text "2024-01-15 13:45:30") "SELECT datetime('2024-01-15 13:45:30','weekday 1')"; sql_check "weekday 2 goes on one day" (text "2024-01-16 13:45:30") "SELECT datetime('2024-01-15 13:45:30','weekday 2')"; sql_check "weekday 6 goes on five" (text "2024-01-20") "SELECT date('2024-01-15','weekday 6')"; sql_check "two of them compose" (text "2024-01-24 13:45:30") "SELECT datetime('2024-01-15 13:45:30','weekday 0','weekday 3')"; sql_check "a later offset still applies" (text "2024-01-17 14:45:30") "SELECT datetime('2024-01-15 13:45:30','weekday 3','+1 hour')"; sql_check "a whole float is whole" (text "2024-01-15") "SELECT date('2024-01-15','weekday 1.0')"; sql_check "the argument may be padded" (text "2024-01-16") "SELECT date('2024-01-15','weekday 2')"; sql_check "the name is case-folded" (text "2024-01-16") "SELECT date('2024-01-15','WEEKDAY 2')"; sql_check "7 is out of range" Null "SELECT datetime('2024-01-15 13:45:30','weekday 7')"; sql_check "-1 is out of range" Null "SELECT datetime('2024-01-15 13:45:30','weekday -1')"; sql_check "1.5 is not whole" Null "SELECT datetime('2024-01-15 13:45:30','weekday 1.5')"; sql_check "no argument at all" Null "SELECT datetime('2024-01-15 13:45:30','weekday')"; sql_check "no space is not the modifier" Null "SELECT date('2024-01-15','weekday2')"; sql_check "a leading space is not the modifier" Null "SELECT date('2024-01-15',' weekday 2')" let test_sql_raw_number () = sql_check "text spelling a Julian day" (text "2000-01-01 12:00:00") "SELECT datetime('2451545.0')"; sql_check "with no fraction written" (text "2000-01-01 12:00:00") "SELECT datetime('2451545')"; sql_check "date() of the same" (text "2000-01-01") "SELECT date('2451545.0')"; sql_check "julianday() gives it back" (Float 2451545.0) "SELECT julianday('2451545.0')"; sql_check "strftime reads it too" (text "946728000") "SELECT strftime('%s','2451545.0')"; sql_check "a modifier still folds on top" (text "2000-01-02 12:00:00") "SELECT datetime('2451545.0','+1 day')"; (* the spellings AtoF accepts *) sql_check "an explicit plus" (text "2000-01-01 12:00:00") "SELECT datetime('+2451545.0')"; sql_check "leading whitespace is skipped" (text "2000-01-01 12:00:00") "SELECT datetime(' 2451545.0')"; sql_check "trailing whitespace too" (text "2000-01-01 12:00:00") "SELECT datetime('2451545.0 ')"; sql_check "an exponent" (text "-4710-08-20 12:00:00") "SELECT datetime('1e3')"; sql_check "a bare fraction" (text "-4713-11-25 00:00:00") "SELECT datetime('.5')"; sql_check "zero is Julian day zero" (text "-4713-11-24 12:00:00") "SELECT datetime('0')"; check "a blob of digits reads as the text it spells" "text:2000-01-01 12:00:00" "datetime" [ Sql.Ast.Blob "2451545" ]; (* only inside the Julian window, for text and number alike *) sql_check "just under the top of the window" (text "9999-12-31 21:36:00") "SELECT datetime('5373484.4')"; sql_check "at the top there is no day" Null "SELECT datetime('5373484.5')"; sql_check "the number argument stops there too" Null "SELECT datetime(5373484.5)"; sql_check "a negative number names no day" Null "SELECT datetime('-1')"; sql_check "nor as a number argument" Null "SELECT julianday(-1)"; sql_check "unix seconds are no Julian day" Null "SELECT datetime('1704067200')"; sql_check "nor when written as a number" Null "SELECT datetime(1704067200)"; sql_check "julianday() of one is NULL as well" Null "SELECT julianday(1704067200)"; (* which is what 'auto' and 'unixepoch' are for *) sql_check "auto reads it as unix seconds" (text "2024-01-01 00:00:00") "SELECT datetime('1704067200','auto')"; sql_check "auto leaves a Julian day alone" (text "2000-01-01 12:00:00") "SELECT datetime('2451545.0','auto')"; sql_check "auto on a negative number" (text "1969-12-31 23:59:59") "SELECT datetime('-1','auto')"; sql_check "auto then a modifier" (text "2024-01-02 00:00:00") "SELECT datetime('1704067200','auto','+1 day')"; sql_check "auto keeps the sub-second the render drops" (text "2024-01-01 00:00:00") "SELECT datetime('1704067200.5','auto')"; sql_check "unixepoch reads a Julian-looking number as seconds" (text "1970-01-29 08:59:05") "SELECT datetime('2451545.0','unixepoch')"; (* the windows 'auto' and 'unixepoch' each read within: 'auto' compares the seconds against whole bounds (date.c: -21086676*10000 to 25340230*10000 +799), 'unixepoch' the milliseconds it computes against 464269060800000, so half a second past the last whole second is inside one and not the other *) sql_check "auto at its last second" (text "9999-12-31 23:59:59") "SELECT datetime('253402300799','auto')"; sql_check "one second past it" Null "SELECT datetime('253402300800','auto')"; sql_check "half a second past is past it for auto" Null "SELECT datetime('253402300799.5','auto')"; sql_check "and inside the window unixepoch reads" (text "9999-12-31 23:59:59") "SELECT datetime('253402300799.5','unixepoch')"; sql_check "unixepoch stops a second later" Null "SELECT datetime('253402300800','unixepoch')"; sql_check "auto at its first second" (text "-4713-11-24 12:00:00") "SELECT datetime('-210866760000','auto')"; sql_check "half a second before it" Null "SELECT datetime('-210866760000.5','auto')"; (* text that is not wholly a number is no number at all *) sql_check "a date is still read as a date" (text "2020-06-15 00:00:00") "SELECT datetime('2020-06-15')"; sql_check "a date without its dashes is neither" Null "SELECT datetime('20200615')"; sql_check "a time is still read as a time" (text "2000-01-01 12:30:00") "SELECT datetime('12:30')"; sql_check "words are not numbers" Null "SELECT datetime('not-a-date')"; sql_check "digits with a tail are not" Null "SELECT datetime('123abc')"; sql_check "a hexadecimal literal is not" Null "SELECT datetime('0x10')"; sql_check "digits with a separator are not" Null "SELECT datetime('1_000')"; sql_check "the empty string is not" Null "SELECT datetime('')"; sql_check "whitespace alone is not" Null "SELECT datetime(' ')" (* The 'auto' modifier reads a bare number as a Julian day when it is in [0, 5373484.5) and as unix seconds otherwise, and is a no-op on text. Like 'unixepoch' it says how the value itself is read, so sqlite3 honours it only as the first modifier (sqlite/src/date.c: "if( idx>1 ) return 1"). *) let test_sql_auto () = sql_check "just under the split is a Julian day" (text "9999-12-31 21:36:00") "SELECT datetime(5373484.4,'auto')"; sql_check "at the split it is unix seconds" (text "1970-03-04 04:38:04") "SELECT datetime(5373484.5,'auto')"; sql_check "above the split too" (text "1970-03-04 04:38:04") "SELECT datetime(5373484.6,'auto')"; sql_check "a negative number is unix seconds" (text "1969-12-31 23:59:59") "SELECT datetime(-1,'auto')"; sql_check "zero is Julian day zero" (text "-4713-11-24 12:00:00") "SELECT datetime(0,'auto')"; sql_check "a plain Julian day is read as one" (text "2000-01-01 12:00:00") "SELECT datetime(2451545.0,'auto')"; sql_check "unixepoch of an auto Julian day" (Int 946728000L) "SELECT unixepoch(2451545.0,'auto')"; sql_check "julianday of auto unix seconds" (Float 2460310.5) "SELECT julianday(1704067200,'auto')"; sql_check "auto on a date string is a no-op" (text "2024-01-15 00:00:00") "SELECT datetime('2024-01-15','auto')"; sql_check "auto on a date-time string is a no-op" (text "2024-01-15 12:00:00") "SELECT datetime('2024-01-15 12:00:00','auto')"; sql_check "date() with auto" (text "2024-01-15") "SELECT date('2024-01-15','auto')"; (* Only in first position, and only as written. *) sql_check "after another modifier it is none" Null "SELECT datetime('2024-01-15','+1 day','auto')"; sql_check "twice is once too many" Null "SELECT datetime(2451545.0,'auto','auto')"; sql_check "after unixepoch" Null "SELECT datetime(1704067200,'unixepoch','auto')"; sql_check "after subsec" Null "SELECT datetime(2451545.0,'subsec','auto')"; sql_check "a leading space is not the modifier" Null "SELECT datetime(2451545.0,' auto')"; (* 'unixepoch' is first-position for the same reason. *) sql_check "unixepoch before another modifier" (text "2024-01-16 00:00:00") "SELECT datetime(1705276800,'unixepoch','+1 day')"; sql_check "unixepoch after one is none" Null "SELECT datetime(1705276800,'+1 day','unixepoch')"; sql_check "unixepoch after auto" Null "SELECT datetime(1704067200,'auto','unixepoch')" (* 'subsec' (and its longer spelling 'subsecond') is a rendering flag, not arithmetic: it makes the seconds carry their millisecond wherever seconds are printed, and turns unixepoch()'s integer into a real (sqlite/src/date.c: useSubsec). date() prints no seconds and is unaffected, and julianday() is a real either way. It may sit at any position. *) let test_sql_subsec () = sql_check "datetime gains the millisecond" (text "2020-06-15 12:00:00.000") "SELECT datetime('2020-06-15 12:00:00','subsec')"; sql_check "'subsecond' is the same flag" (text "2020-06-15 12:00:00.000") "SELECT datetime('2020-06-15 12:00:00','subsecond')"; sql_check "the value's own fraction survives" (text "2020-06-15 12:00:00.456") "SELECT datetime('2020-06-15 12:00:00.456','subsec')"; sql_check "without the flag it is dropped" (text "2020-06-15 12:00:00") "SELECT datetime('2020-06-15 12:00:00.456')"; sql_check "time gains it too" (text "13:45:30.000") "SELECT time('2024-01-15 13:45:30','subsec')"; sql_check "date prints no seconds to gain" (text "2020-06-15") "SELECT date('2020-06-15 12:00:00','subsec')"; sql_check "unixepoch turns real" (Float 1592222400.0) "SELECT unixepoch('2020-06-15 12:00:00','subsec')"; sql_check "and keeps the fraction" (Float 1592222400.456) "SELECT unixepoch('2020-06-15 12:00:00.456','subsec')"; sql_check "julianday is a real either way" (Float 2459016.0) "SELECT julianday('2020-06-15 12:00:00','subsec')"; sql_check "strftime %s gains three digits" (text "1592222400.000") "SELECT strftime('%s','2020-06-15 12:00:00','subsec')"; sql_check "strftime %s keeps the fraction" (text "1592222400.456") "SELECT strftime('%s','2020-06-15 12:00:00.456','subsec')"; sql_check "strftime %H:%M:%S is unaffected" (text "12:00:00") "SELECT strftime('%H:%M:%S','2020-06-15 12:00:00','subsec')"; sql_check "it may come first" (text "2024-01-17 13:45:30.000") "SELECT datetime('2024-01-15 13:45:30','subsec','weekday 3')"; sql_check "or last" (text "2020-06-16 12:00:00.000") "SELECT datetime('2020-06-15 12:00:00','+1 day','subsec')"; sql_check "a leading space is not the modifier" Null "SELECT datetime('2020-06-15 12:00:00',' subsec')" let test_sql_modifier_blanks () = (* the unit offsets *) sql_check "the offset itself" (text "2020-06-16 00:00:00") "SELECT datetime('2020-06-15','+1 day')"; sql_check "with a trailing blank it is no modifier" Null "SELECT datetime('2020-06-15','+1 day ')"; sql_check "nor with a leading one" Null "SELECT datetime('2020-06-15',' +1 day')"; sql_check "a blank between the number and the unit is skipped" (text "2020-06-16 00:00:00") "SELECT datetime('2020-06-15','+1 day')"; sql_check "a tab pads as a space does" Null "SELECT datetime('2020-06-15','+1 day\t')"; sql_check "a negative offset the same" Null "SELECT datetime('2020-06-15','-1 day ')"; sql_check "hours" Null "SELECT datetime('2020-06-15','+1 hour ')"; sql_check "months" Null "SELECT datetime('2020-06-15','+1 month ')"; sql_check "years" Null "SELECT date('2024-01-15','+1 year ')"; sql_check "a plural unit" Null "SELECT date('2024-01-15','+36 hours ')"; sql_check "an unsigned amount answers" (text "2024-01-16") "SELECT date('2024-01-15','1 day')"; sql_check "and padded is no modifier either" Null "SELECT date('2024-01-15','1 day ')"; sql_check "the second modifier is read the same way" Null "SELECT datetime('2024-01-15','+1 day','+1 day ')"; (* the "start of TTTTT" snaps *) sql_check "the snap itself" (text "2020-06-01") "SELECT date('2020-06-15','start of month')"; sql_check "a trailing blank is part of the tail" Null "SELECT date('2020-06-15','start of month ')"; sql_check "a blank on each side" Null "SELECT date('2020-06-15',' start of month ')"; sql_check "start of day" Null "SELECT datetime('2020-06-15','start of day ')"; sql_check "start of year, padded in front" Null "SELECT date('2020-06-15',' start of year')"; sql_check "the blank inside is not skipped here" Null "SELECT date('2020-06-15','start of month')"; (* the zone modifiers; with no zone injected both are the no-op the CLI performs under TZ=UTC *) sql_check "utc" (text "2000-10-29 12:00:00") "SELECT datetime('2000-10-29 12:00:00','utc')"; sql_check "utc with a trailing blank" Null "SELECT datetime('2000-10-29 12:00:00','utc ')"; sql_check "utc with a blank on each side" Null "SELECT datetime('2000-10-29 12:00:00',' utc ')"; sql_check "localtime" (text "2000-10-29 12:00:00") "SELECT datetime('2000-10-29 12:00:00','localtime')"; sql_check "localtime with a trailing blank" Null "SELECT datetime('2000-10-29 12:00:00','localtime ')"; (* 'weekday N', where the blank belongs to the argument *) sql_check "weekday" (text "2024-01-16") "SELECT date('2024-01-15','weekday 2')"; sql_check "weekday keeps its trailing blank" (text "2024-01-16") "SELECT date('2024-01-15','weekday 2 ')"; sql_check "a padded float argument too" (text "2024-01-15") "SELECT date('2024-01-15','weekday 1.0 ')"; (* the same rule at the value layer, where the modifier is matched *) check "the call reads the modifier as written" "NULL" "datetime" [ text "2020-06-15"; text "+1 day " ]; check "and answers the one written without a blank" "text:2020-06-16 00:00:00" "datetime" [ text "2020-06-15"; text "+1 day" ] let test_sql_strftime_julian () = sql_check "midnight is the half day" (text "2459015.5") "SELECT strftime('%J','2020-06-15')"; sql_check "noon is the whole one, with no point" (text "2459016") "SELECT strftime('%J','2020-06-15 12:00:00')"; sql_check "the J2000 noon" (text "2451545") "SELECT strftime('%J','2000-01-01 12:00:00')"; sql_check "the unix epoch's noon" (text "2440588") "SELECT strftime('%J','1970-01-01 12:00:00')"; sql_check "a quarter day" (text "2459016.25") "SELECT strftime('%J','2020-06-15 18:00:00')"; sql_check "three quarters" (text "2459015.75") "SELECT strftime('%J','2020-06-15 06:00:00')"; sql_check "one second past noon keeps sixteen digits" (text "2459016.000011574") "SELECT strftime('%J','2020-06-15 12:00:01')"; sql_check "an arbitrary time of day too" (text "2460325.073263889") "SELECT strftime('%J','2024-01-15 13:45:30')"; sql_check "it substitutes inside a longer format" (text "2459016 2020") "SELECT strftime('%J %Y','2020-06-15 12:00:00')"; sql_check "subsec does not reach it" (text "2459016") "SELECT strftime('%J','2020-06-15 12:00:00','subsec')"; sql_check "julianday() of the same instant is the real" (Float 2459016.0) "SELECT julianday('2020-06-15 12:00:00')" (* A modifier spelled as a clock reading, [(+|-)HH:MM[:SS[.FFF]]], shifts the time by that much (lang_datefunc.html, modifiers 5 to 7; sqlite/src/date.c, parseModifier, the [z[n]==':'] case, which reads the tail with parseHhMmSs). Every expected value is the sqlite3 3.54.0 CLI's output under TZ=UTC. *) let test_sql_clock_offset () = let at = "'2020-06-15 10:00:00'" in let shifted label expected m = sql_check label expected (Fmt.str "SELECT datetime(%s, '%s')" at m) in shifted "hours and minutes" (text "2020-06-15 11:30:00") "+01:30"; shifted "seconds, subtracted" (text "2020-06-15 09:29:45") "-00:30:15"; shifted "with no sign it adds" (text "2020-06-15 11:30:00") "01:30"; shifted "a fraction of a second is kept" (text "2020-06-15 11:30:15") "+01:30:15.250"; sql_check "and shows under %f" (text "2020-06-15 11:30:15.250") (Fmt.str "SELECT strftime('%%Y-%%m-%%d %%H:%%M:%%f', %s, '+01:30:15.250')" at); sql_check "a subtracted fraction" (text "08:29:59.500") (Fmt.str "SELECT strftime('%%H:%%M:%%f', %s, '-01:30:00.5')" at); sql_check "a sub-millisecond fraction rounds" (text "10:00:00.001") (Fmt.str "SELECT strftime('%%H:%%M:%%f', %s, '+00:00:00.0005')" at); shifted "the shift crosses midnight" (text "2020-06-16 00:00:00") "+14:00:00"; shifted "a whole day of it is no shift" (text "2020-06-15 10:00:00") "+24:00"; shifted "nor subtracted" (text "2020-06-15 10:00:00") "-24:00"; shifted "a trailing blank is skipped" (text "2020-06-15 11:30:15") "+01:30:15 "; shifted "a trailing Z is the UTC zone" (text "2020-06-15 11:30:00") "+01:30Z"; shifted "a trailing zone is taken off the reading, modulo a day" (text "2020-06-16 09:30:00") "+01:30 +02:00"; shifted "an hour has two digits" Null "+1:30"; shifted "a minute is at most 59" Null "+01:60"; shifted "a second is at most 59" Null "+01:30:60"; shifted "an hour is at most 24" Null "+25:00"; shifted "a point takes a digit after it" Null "+01:30:15."; shifted "nothing else may follow" Null "+01:30x"; shifted "the minutes are digits" Null "+01:xx"; shifted "a blank after the sign is no reading" Null "+ 01:30"; shifted "one sign only" Null "+-01:30"; shifted "a fractional hour is no reading" Null "+1.5:30" let suite = ( "datetime", [ Alcotest.test_case "explicit date/time/datetime" `Quick test_explicit_forms; Alcotest.test_case "julianday and unixepoch" `Quick test_julian_and_unix; Alcotest.test_case "unit and start-of modifiers" `Quick test_modifiers; Alcotest.test_case "strftime" `Quick test_strftime; Alcotest.test_case "NULL results" `Quick test_null_results; Alcotest.test_case "unknown function raises" `Quick test_unknown_function; Alcotest.test_case "the injected zone" `Quick test_zone; Alcotest.test_case "the injected instant" `Quick test_now; Alcotest.test_case "SQL: the CURRENT_* keywords" `Quick test_sql_current_keywords; Alcotest.test_case "SQL: date/time/datetime formatting" `Quick test_sql_formatting; Alcotest.test_case "SQL: timezone suffix" `Quick test_sql_timezone; Alcotest.test_case "SQL: julian day + unix epoch" `Quick test_sql_julian_unix; Alcotest.test_case "SQL: modifiers" `Quick test_sql_modifiers; Alcotest.test_case "SQL: strftime" `Quick test_sql_strftime; Alcotest.test_case "SQL: strftime reads a real format as its text" `Quick test_sql_strftime_real_format; Alcotest.test_case "SQL: null on bad input" `Quick test_sql_null_on_bad_input; Alcotest.test_case "SQL: the weekday modifier" `Quick test_sql_weekday; Alcotest.test_case "SQL: a bare number as the time value" `Quick test_sql_raw_number; Alcotest.test_case "SQL: the auto modifier" `Quick test_sql_auto; Alcotest.test_case "SQL: the subsec flag" `Quick test_sql_subsec; Alcotest.test_case "SQL: blanks around a modifier" `Quick test_sql_modifier_blanks; Alcotest.test_case "SQL: a clock reading as a modifier" `Quick test_sql_clock_offset; Alcotest.test_case "SQL: the %J julian day" `Quick test_sql_strftime_julian; ] )