rupg-types 0.1.84

The value model, Datum, Vector of 1024 values, TypeId, the text and binary forms of the 197 types, numeric, date and time, JSON, arrays, ranges.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
//! The text input of `date`, `time`, `timetz`, `timestamp`, `timestamptz` and `interval`.
//!
//! This is a port of `ParseDateTime`, `DecodeDateTime`, `DecodeTimeOnly`, `DecodeInterval` and `DecodeISO8601Interval` in `src/backend/utils/adt/datetime.c`, with the helpers that they call. The structure follows the C code closely, so that a difference against the server can be found by reading the two side by side. The field masks, the token types and the error codes have the values of `datetime.h`.
//!
//! The server reads some inputs from the session: the field order of `DateStyle`, the time zone, the time zone abbreviations, the zone names and the time of the transaction start, for `now` and `today`. [`DateTimeInput`] holds all of them.
//!
//! Lifted from `crates/rudb-pgtypes/src/datetime/decode.rs` of tamnd/rudb at f5f7065a (spec/04 section 4.9).

use std::sync::{Arc, OnceLock};

use rupg_common::SqlState;

use super::{
    AbbrevMeaning, DATE_END_JULIAN, DATE_INFINITY, DATE_NEGATIVE_INFINITY, DateOrder,
    END_TIMESTAMP, Interval, IntervalStyle, MIN_TIMESTAMP, POSTGRES_EPOCH_JDATE,
    TIMESTAMP_INFINITY, TIMESTAMP_NEGATIVE_INFINITY, TimeZone, UNIX_EPOCH_JDATE, USECS_PER_DAY,
    USECS_PER_SEC, adjust_interval, adjust_time, adjust_timestamp, date2j, j2date, out_of_range,
};
use crate::error::TypeError;
use crate::float::strtod;
use crate::number::is_space;
use crate::typmod::INTERVAL_FULL_RANGE;

/// The zones that a name in the input can give, as `pg_tzset` finds them. The name is in lower case, as the input has it.
pub trait ZoneLookup {
    fn zone(&self, name: &str) -> Option<Arc<dyn TimeZone + Send + Sync>>;
}

/// A [`ZoneLookup`] that knows no zone, until the tz database is here.
#[derive(Debug, Clone, Copy, Default)]
pub struct NoZones;

impl ZoneLookup for NoZones {
    fn zone(&self, _: &str) -> Option<Arc<dyn TimeZone + Send + Sync>> {
        None
    }
}

/// One line of a `timezone_abbreviations` file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Abbrev {
    /// A fixed offset east of UTC in seconds, and whether it is daylight saving time.
    Fixed { offset: i32, dst: bool },
    /// The name of a zone, for an abbreviation whose offset changed over time.
    Zone(String),
}

/// The time zone abbreviations of the `timezone_abbreviations` setting, sorted by the lower case abbreviation.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ZoneAbbrevs {
    entries: Vec<(String, Abbrev)>,
}

impl ZoneAbbrevs {
    /// Reads a file in the format of `src/timezone/tznames`, as `ParseTzFile` does. The `@INCLUDE` and `@OVERRIDE` lines are not supported.
    pub fn parse(text: &str) -> Result<ZoneAbbrevs, String> {
        let mut entries = Vec::new();
        for (index, line) in text.lines().enumerate() {
            let lineno = index + 1;
            let line = line.trim_start_matches(|c: char| c.is_ascii() && is_space(c as u8));
            if line.is_empty() || line.starts_with('#') {
                continue;
            }
            if line.starts_with('@') {
                return Err(format!("line {lineno}: {line} is not supported"));
            }
            let mut words = line.split([' ', '\t', '\n', '\r']).filter(|w| !w.is_empty());
            let abbrev = words.next().unwrap_or_default();
            let Some(offset) = words.next() else {
                return Err(format!("missing time zone offset in line {lineno}"));
            };
            let (abbrev_value, rest) = if offset.starts_with(|c: char| c.is_ascii_digit())
                || offset.starts_with(['+', '-'])
            {
                let Ok(offset) = offset.parse::<i32>() else {
                    return Err(format!("invalid number for time zone offset in line {lineno}"));
                };
                if !(-14 * 3600..=14 * 3600).contains(&offset) {
                    return Err(format!(
                        "time zone offset {offset} is out of range in line {lineno}"
                    ));
                }
                match words.next() {
                    Some(word) if word.eq_ignore_ascii_case("D") => {
                        (Abbrev::Fixed { offset, dst: true }, words.next())
                    }
                    word => (Abbrev::Fixed { offset, dst: false }, word),
                }
            } else {
                (Abbrev::Zone(offset.to_string()), words.next())
            };
            if rest.is_some_and(|word| !word.starts_with('#')) {
                return Err(format!("invalid syntax in line {lineno}"));
            }
            if abbrev.len() > TOKMAXLEN {
                return Err(format!(
                    "time zone abbreviation \"{abbrev}\" is too long (maximum {TOKMAXLEN} characters) in line {lineno}"
                ));
            }
            entries.push((abbrev.to_ascii_lowercase(), abbrev_value));
        }
        entries.sort_by(|a, b| a.0.cmp(&b.0));
        for pair in entries.windows(2) {
            if pair[0].0 == pair[1].0 && pair[0].1 != pair[1].1 {
                return Err(format!(
                    "time zone abbreviation \"{}\" is multiply defined",
                    pair[0].0
                ));
            }
        }
        entries.dedup();
        Ok(ZoneAbbrevs { entries })
    }

    /// The `Default` set of PostgreSQL, the default of `timezone_abbreviations`.
    ///
    /// # Panics
    ///
    /// Only if the vendored file does not parse, which the unit tests check.
    pub fn postgres_default() -> &'static ZoneAbbrevs {
        static DEFAULT: OnceLock<ZoneAbbrevs> = OnceLock::new();
        DEFAULT.get_or_init(|| {
            ZoneAbbrevs::parse(include_str!("../generated/tznames_default.txt"))
                .expect("the vendored tznames file is valid")
        })
    }

    /// The abbreviation and its meaning. Only the first 10 bytes of the token count, as in `datebsearch`.
    pub fn get(&self, token: &str) -> Option<(&str, &Abbrev)> {
        let key = truncate(token.as_bytes());
        let found = self.entries.binary_search_by(|(abbrev, _)| abbrev.as_bytes().cmp(key)).ok()?;
        let (abbrev, value) = &self.entries[found];
        Some((abbrev, value))
    }

    /// Each abbreviation in lower case and its meaning, in the order of the abbreviations.
    pub fn iter(&self) -> impl Iterator<Item = (&str, &Abbrev)> {
        self.entries.iter().map(|(abbrev, value)| (abbrev.as_str(), value))
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

/// The session state that the text input of the date and time types reads.
#[derive(Clone, Copy)]
pub struct DateTimeInput<'a> {
    /// The field order part of `DateStyle`.
    pub order: DateOrder,
    /// The `TimeZone` setting.
    pub zone: &'a dyn TimeZone,
    /// The zone names, such as `america/new_york`.
    pub zones: &'a dyn ZoneLookup,
    /// The `timezone_abbreviations` setting.
    pub abbrevs: &'a ZoneAbbrevs,
    /// The start of the transaction in microseconds since 2000-01-01, for `now` and `today`.
    pub now: i64,
}

impl std::fmt::Debug for DateTimeInput<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DateTimeInput")
            .field("order", &self.order)
            .field("abbrevs", &self.abbrevs.len())
            .field("now", &self.now)
            .finish_non_exhaustive()
    }
}

// The field kinds of `ParseDateTime`.
const DTK_NUMBER: u8 = 0;
const DTK_STRING: u8 = 1;
const DTK_DATE: u8 = 2;
const DTK_TIME: u8 = 3;
const DTK_TZ: u8 = 4;
const DTK_SPECIAL: u8 = 6;

// The values of the reserved words and the units.
const DTK_EARLY: i32 = 9;
const DTK_LATE: i32 = 10;
const DTK_EPOCH: i32 = 11;
const DTK_NOW: i32 = 12;
const DTK_YESTERDAY: i32 = 13;
const DTK_TODAY: i32 = 14;
const DTK_TOMORROW: i32 = 15;
const DTK_ZULU: i32 = 16;
const DTK_DELTA: i32 = 17;
const DTK_SECOND: i32 = 18;
const DTK_MINUTE: i32 = 19;
const DTK_HOUR: i32 = 20;
const DTK_DAY: i32 = 21;
const DTK_WEEK: i32 = 22;
const DTK_MONTH: i32 = 23;
const DTK_QUARTER: i32 = 24;
const DTK_YEAR: i32 = 25;
const DTK_DECADE: i32 = 26;
const DTK_CENTURY: i32 = 27;
const DTK_MILLENNIUM: i32 = 28;
const DTK_MILLISEC: i32 = 29;
const DTK_MICROSEC: i32 = 30;
const DTK_JULIAN: i32 = 31;
const DTK_DOW: i32 = 32;
const DTK_DOY: i32 = 33;
const DTK_TZ_HOUR: i32 = 34;
const DTK_TZ_MINUTE: i32 = 35;
const DTK_ISOYEAR: i32 = 36;
const DTK_ISODOW: i32 = 37;
/// `DTK_DATE` and `DTK_TIME` as the values of a decoded input and of the `t` token.
const DTK_DATE_VALUE: i32 = 2;
const DTK_TIME_VALUE: i32 = 3;
/// `DTK_TZ` as the value of the `timezone` unit.
const DTK_TZ_VALUE: i32 = 4;

// The token types. Each one is also the bit of the field in a mask.
const RESERV: i32 = 0;
const MONTH: i32 = 1;
const YEAR: i32 = 2;
const DAY: i32 = 3;
const TZ: i32 = 5;
const DTZ: i32 = 6;
const DYNTZ: i32 = 7;
const IGNORE_DTF: i32 = 8;
const AMPM: i32 = 9;
const HOUR: i32 = 10;
const MINUTE: i32 = 11;
const SECOND: i32 = 12;
const MILLISECOND: i32 = 13;
const MICROSECOND: i32 = 14;
const DOY: i32 = 15;
const DOW: i32 = 16;
const UNITS: i32 = 17;
const ADBC: i32 = 18;
const AGO: i32 = 19;
const ISOTIME: i32 = 23;
const WEEK: i32 = 24;
const DECADE: i32 = 25;
const CENTURY: i32 = 26;
const MILLENNIUM: i32 = 27;
const DTZMOD: i32 = 28;
const UNKNOWN_FIELD: i32 = 31;

const fn m(t: i32) -> u32 {
    1 << t
}

const DATE_M: u32 = m(YEAR) | m(MONTH) | m(DAY);
const ALL_SECS_M: u32 = m(SECOND) | m(MILLISECOND) | m(MICROSECOND);
const TIME_M: u32 = m(HOUR) | m(MINUTE) | ALL_SECS_M;

const AM: i32 = 0;
const PM: i32 = 1;
const HR24: i32 = 2;
const BC: i32 = 1;

const MAXDATEFIELDS: usize = 25;
const TOKMAXLEN: usize = 10;
/// The work buffers of the input functions: `MAXDATELEN + 1`, `MAXDATELEN + MAXDATEFIELDS` and 256 for `interval`.
const DATE_BUFLEN: usize = 129;
const TIMESTAMP_BUFLEN: usize = 153;
const INTERVAL_BUFLEN: usize = 256;

const USECS_PER_MINUTE: i64 = 60 * USECS_PER_SEC;
const USECS_PER_HOUR: i64 = 3600 * USECS_PER_SEC;
const DAYS_PER_MONTH: i32 = 30;
const MAX_TZDISP_HOUR: i32 = 15;

/// The errors of the decode functions, the `DTERR` codes.
#[derive(Debug, Clone, PartialEq, Eq)]
enum DtErr {
    BadFormat,
    FieldOverflow,
    MdFieldOverflow,
    IntervalOverflow,
    TzDispOverflow,
    BadTimezone(String),
    BadZoneAbbrev { zone: String, abbrev: String },
}

use DtErr::{BadFormat, FieldOverflow};

impl DtErr {
    /// `DateTimeParseError`.
    fn into_error(self, input: &str, type_name: &str) -> TypeError {
        let overflow = || {
            TypeError::new(
                SqlState::DATETIME_FIELD_OVERFLOW,
                format!("date/time field value out of range: \"{input}\""),
            )
        };
        match self {
            BadFormat => TypeError::new(
                SqlState::INVALID_DATETIME_FORMAT,
                format!("invalid input syntax for type {type_name}: \"{input}\""),
            ),
            FieldOverflow => overflow(),
            DtErr::MdFieldOverflow => TypeError {
                hint: Some("Perhaps you need a different \"DateStyle\" setting.".to_string()),
                ..overflow()
            },
            DtErr::IntervalOverflow => TypeError::new(
                SqlState::INTERVAL_FIELD_OVERFLOW,
                format!("interval field value out of range: \"{input}\""),
            ),
            DtErr::TzDispOverflow => TypeError::new(
                SqlState::INVALID_TIME_ZONE_DISPLACEMENT_VALUE,
                format!("time zone displacement out of range: \"{input}\""),
            ),
            DtErr::BadTimezone(zone) => TypeError::new(
                SqlState::INVALID_PARAMETER_VALUE,
                format!("time zone \"{zone}\" not recognized"),
            ),
            DtErr::BadZoneAbbrev { zone, abbrev } => TypeError {
                detail: Some(format!(
                    "This time zone name appears in the configuration file for time zone abbreviation \"{abbrev}\"."
                )),
                ..TypeError::new(
                    SqlState::CONFIG_FILE_ERROR,
                    format!("time zone \"{zone}\" not recognized"),
                )
            },
        }
    }
}

/// The first `TOKMAXLEN` bytes of a token, which is all that the token tables compare.
fn truncate(key: &[u8]) -> &[u8] {
    &key[..key.len().min(TOKMAXLEN)]
}

/// A row of `datetktbl`.
fn date_token(key: &[u8]) -> Option<(i32, i32)> {
    Some(match truncate(key) {
        b"+infinity" | b"infinity" => (RESERV, DTK_LATE),
        b"-infinity" => (RESERV, DTK_EARLY),
        b"ad" => (ADBC, 0),
        b"allballs" => (RESERV, DTK_ZULU),
        b"am" => (AMPM, AM),
        b"apr" | b"april" => (MONTH, 4),
        b"at" | b"on" => (IGNORE_DTF, 0),
        b"aug" | b"august" => (MONTH, 8),
        b"bc" => (ADBC, BC),
        b"d" => (UNITS, DTK_DAY),
        b"dec" | b"december" => (MONTH, 12),
        b"dow" => (UNITS, DTK_DOW),
        b"doy" => (UNITS, DTK_DOY),
        b"dst" => (DTZMOD, 3600),
        b"epoch" => (RESERV, DTK_EPOCH),
        b"feb" | b"february" => (MONTH, 2),
        b"fri" | b"friday" => (DOW, 5),
        b"h" => (UNITS, DTK_HOUR),
        b"isodow" => (UNITS, DTK_ISODOW),
        b"isoyear" => (UNITS, DTK_ISOYEAR),
        b"j" | b"jd" | b"julian" => (UNITS, DTK_JULIAN),
        b"jan" | b"january" => (MONTH, 1),
        b"jul" | b"july" => (MONTH, 7),
        b"jun" | b"june" => (MONTH, 6),
        b"m" => (UNITS, DTK_MONTH),
        b"mar" | b"march" => (MONTH, 3),
        b"may" => (MONTH, 5),
        b"mm" => (UNITS, DTK_MINUTE),
        b"mon" | b"monday" => (DOW, 1),
        b"nov" | b"november" => (MONTH, 11),
        b"now" => (RESERV, DTK_NOW),
        b"oct" | b"october" => (MONTH, 10),
        b"pm" => (AMPM, PM),
        b"s" => (UNITS, DTK_SECOND),
        b"sat" | b"saturday" => (DOW, 6),
        b"sep" | b"sept" | b"september" => (MONTH, 9),
        b"sun" | b"sunday" => (DOW, 0),
        b"t" => (ISOTIME, DTK_TIME_VALUE),
        b"thu" | b"thur" | b"thurs" | b"thursday" => (DOW, 4),
        b"today" => (RESERV, DTK_TODAY),
        b"tomorrow" => (RESERV, DTK_TOMORROW),
        b"tue" | b"tues" | b"tuesday" => (DOW, 2),
        b"wed" | b"wednesday" | b"weds" => (DOW, 3),
        b"y" => (UNITS, DTK_YEAR),
        b"yesterday" => (RESERV, DTK_YESTERDAY),
        _ => return None,
    })
}

/// A row of `deltatktbl`.
fn delta_token(key: &[u8]) -> Option<(i32, i32)> {
    let unit = match truncate(key) {
        b"@" => return Some((IGNORE_DTF, 0)),
        b"ago" => return Some((AGO, 0)),
        b"c" | b"cent" | b"centuries" | b"century" => DTK_CENTURY,
        b"d" | b"day" | b"days" => DTK_DAY,
        b"dec" | b"decade" | b"decades" | b"decs" => DTK_DECADE,
        b"h" | b"hour" | b"hours" | b"hr" | b"hrs" => DTK_HOUR,
        b"m" | b"min" | b"mins" | b"minute" | b"minutes" => DTK_MINUTE,
        b"microsecon" | b"us" | b"usec" | b"usecond" | b"useconds" | b"usecs" => DTK_MICROSEC,
        b"mil" | b"millennia" | b"millennium" | b"mils" => DTK_MILLENNIUM,
        b"millisecon" | b"ms" | b"msec" | b"msecond" | b"mseconds" | b"msecs" => DTK_MILLISEC,
        b"mon" | b"mons" | b"month" | b"months" => DTK_MONTH,
        b"qtr" | b"quarter" => DTK_QUARTER,
        b"s" | b"sec" | b"second" | b"seconds" | b"secs" => DTK_SECOND,
        b"timezone" => DTK_TZ_VALUE,
        b"timezone_h" => DTK_TZ_HOUR,
        b"timezone_m" => DTK_TZ_MINUTE,
        b"w" | b"week" | b"weeks" => DTK_WEEK,
        b"y" | b"year" | b"years" | b"yr" | b"yrs" => DTK_YEAR,
        _ => return None,
    };
    Some((UNITS, unit))
}

/// A unit of `EXTRACT`, `date_part` and `date_trunc`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DateUnit {
    Microsecond,
    Millisecond,
    Second,
    Minute,
    Hour,
    Day,
    Week,
    Month,
    Quarter,
    Year,
    Decade,
    Century,
    Millennium,
    Julian,
    IsoYear,
    Dow,
    IsoDow,
    Doy,
    Timezone,
    TimezoneHour,
    TimezoneMinute,
    /// The reserved word `epoch`.
    Epoch,
    /// Another reserved word, for example `now`.
    Reserved,
    /// A token of another type, or no token.
    Unknown,
}

impl DateUnit {
    /// `DecodeUnits`, and then `DecodeSpecial` when `DecodeUnits` finds no token. `lowunits` is in lower case.
    pub fn decode(lowunits: &str) -> DateUnit {
        let key = lowunits.as_bytes();
        DateUnit::of(delta_token(key).or_else(|| date_token(key)))
    }

    /// `DecodeUnits` alone, as `date_trunc` calls it. `lowunits` is in lower case.
    pub fn decode_units(lowunits: &str) -> DateUnit {
        DateUnit::of(delta_token(lowunits.as_bytes()))
    }

    fn of(token: Option<(i32, i32)>) -> DateUnit {
        match token {
            Some((UNITS, unit)) => match unit {
                DTK_MICROSEC => DateUnit::Microsecond,
                DTK_MILLISEC => DateUnit::Millisecond,
                DTK_SECOND => DateUnit::Second,
                DTK_MINUTE => DateUnit::Minute,
                DTK_HOUR => DateUnit::Hour,
                DTK_DAY => DateUnit::Day,
                DTK_WEEK => DateUnit::Week,
                DTK_MONTH => DateUnit::Month,
                DTK_QUARTER => DateUnit::Quarter,
                DTK_YEAR => DateUnit::Year,
                DTK_DECADE => DateUnit::Decade,
                DTK_CENTURY => DateUnit::Century,
                DTK_MILLENNIUM => DateUnit::Millennium,
                DTK_JULIAN => DateUnit::Julian,
                DTK_ISOYEAR => DateUnit::IsoYear,
                DTK_DOW => DateUnit::Dow,
                DTK_ISODOW => DateUnit::IsoDow,
                DTK_DOY => DateUnit::Doy,
                DTK_TZ_VALUE => DateUnit::Timezone,
                DTK_TZ_HOUR => DateUnit::TimezoneHour,
                DTK_TZ_MINUTE => DateUnit::TimezoneMinute,
                _ => DateUnit::Unknown,
            },
            Some((RESERV, DTK_EPOCH)) => DateUnit::Epoch,
            Some((RESERV, _)) => DateUnit::Reserved,
            _ => DateUnit::Unknown,
        }
    }

    /// Whether the token has the type `UNITS`.
    pub fn is_units(self) -> bool {
        !matches!(self, DateUnit::Epoch | DateUnit::Reserved | DateUnit::Unknown)
    }
}

/// The byte at `i`, or 0 at the end, as a C string reads.
fn at(s: &[u8], i: usize) -> u8 {
    s.get(i).copied().unwrap_or(0)
}

/// A field as text. The fields hold only ASCII, so this does not fail.
fn text(field: &[u8]) -> &str {
    std::str::from_utf8(field).unwrap_or_default()
}

/// The input up to the first NUL, which ends a C string.
fn c_string(input: &str) -> &[u8] {
    let bytes = input.as_bytes();
    &bytes[..bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len())]
}

/// `strtol` in base 10 from `start`: the value, the end, and whether the value saturated. With no digits the end is `start`.
fn strtol(s: &[u8], start: usize) -> (i64, usize, bool) {
    let mut i = start;
    while is_space(at(s, i)) {
        i += 1;
    }
    let negative = at(s, i) == b'-';
    if matches!(at(s, i), b'-' | b'+') {
        i += 1;
    }
    let digits = i;
    let (mut value, mut range) = (0i64, false);
    while at(s, i).is_ascii_digit() {
        let digit = i64::from(at(s, i) - b'0');
        if !range {
            let next = value
                .checked_mul(10)
                .and_then(|v| if negative { v.checked_sub(digit) } else { v.checked_add(digit) });
            match next {
                Some(next) => value = next,
                None => {
                    range = true;
                    value = if negative { i64::MIN } else { i64::MAX };
                }
            }
        }
        i += 1;
    }
    if i == digits {
        return (0, start, false);
    }
    (value, i, range)
}

/// `strtoint`: `strtol` with `ERANGE` when the value does not fit in an `int`.
fn strtoint(s: &[u8], start: usize) -> (i32, usize, bool) {
    let (value, end, range) = strtol(s, start);
    (value as i32, end, range || i64::from(value as i32) != value)
}

/// `atoi`, which is `strtol` cut to an `int`.
fn atoi(s: &[u8]) -> i32 {
    strtol(s, 0).0 as i32
}

/// `ParseFraction`: the field from the decimal point to the end, as a number from 0 to 1.
fn parse_fraction(s: &[u8]) -> Result<f64, DtErr> {
    if s.len() == 1 {
        return Ok(0.0);
    }
    if !s[1..].iter().all(u8::is_ascii_digit) {
        return Err(BadFormat);
    }
    match strtod::<f64>(s) {
        Some((value, end, false)) if end == s.len() => Ok(value),
        _ => Err(BadFormat),
    }
}

/// `ParseFractionalSecond`: the fraction in microseconds.
fn parse_fractional_second(s: &[u8]) -> Result<i32, DtErr> {
    Ok((parse_fraction(s)? * 1e6).round_ties_even() as i32)
}

/// The fields of `ParseDateTime` in one buffer, with the bounds and the kind of each field.
struct Parsed {
    buf: [u8; INTERVAL_BUFLEN],
    bounds: [(u16, u16); MAXDATEFIELDS],
    kinds: [u8; MAXDATEFIELDS],
    n: usize,
}

impl Parsed {
    fn field(&self, i: usize) -> &[u8] {
        let (start, end) = self.bounds[i];
        &self.buf[start as usize..end as usize]
    }
}

/// `ParseDateTime`: splits the input into fields, in lower case, and gives each one a kind. `buflen` is the size of the work buffer of the caller, which limits the length of the input.
fn parse_date_time(s: &[u8], buflen: usize) -> Result<Parsed, DtErr> {
    let mut out = Parsed {
        buf: [0; INTERVAL_BUFLEN],
        bounds: [(0, 0); MAXDATEFIELDS],
        kinds: [0; MAXDATEFIELDS],
        n: 0,
    };
    let mut pos = 0;
    let mut cp = 0;
    macro_rules! push {
        ($c:expr) => {{
            if pos + 1 >= buflen {
                return Err(BadFormat);
            }
            out.buf[pos] = $c;
            pos += 1;
        }};
    }
    while cp < s.len() {
        let c = s[cp];
        if is_space(c) {
            cp += 1;
            continue;
        }
        if out.n >= MAXDATEFIELDS {
            return Err(BadFormat);
        }
        let start = pos;
        let kind;
        if c.is_ascii_digit() {
            push!(c);
            cp += 1;
            while at(s, cp).is_ascii_digit() {
                push!(s[cp]);
                cp += 1;
            }
            if at(s, cp) == b':' {
                kind = DTK_TIME;
                push!(b':');
                cp += 1;
                while matches!(at(s, cp), b'0'..=b'9' | b':' | b'.') {
                    push!(s[cp]);
                    cp += 1;
                }
            } else if matches!(at(s, cp), b'-' | b'/' | b'.') {
                let delim = s[cp];
                push!(delim);
                cp += 1;
                if at(s, cp).is_ascii_digit() {
                    let mut k = if delim == b'.' { DTK_NUMBER } else { DTK_DATE };
                    while at(s, cp).is_ascii_digit() {
                        push!(s[cp]);
                        cp += 1;
                    }
                    if at(s, cp) == delim {
                        k = DTK_DATE;
                        push!(delim);
                        cp += 1;
                        while at(s, cp).is_ascii_digit() || at(s, cp) == delim {
                            push!(s[cp]);
                            cp += 1;
                        }
                    }
                    kind = k;
                } else {
                    kind = DTK_DATE;
                    while at(s, cp).is_ascii_alphanumeric() || at(s, cp) == delim {
                        push!(s[cp].to_ascii_lowercase());
                        cp += 1;
                    }
                }
            } else {
                kind = DTK_NUMBER;
            }
        } else if c == b'.' {
            push!(c);
            cp += 1;
            while at(s, cp).is_ascii_digit() {
                push!(s[cp]);
                cp += 1;
            }
            kind = DTK_NUMBER;
        } else if c.is_ascii_alphabetic() {
            push!(c.to_ascii_lowercase());
            cp += 1;
            while at(s, cp).is_ascii_alphabetic() {
                push!(s[cp].to_ascii_lowercase());
                cp += 1;
            }
            // A date such as `jan-01-2001`, or a zone name such as `america/new_york` or `etc/gmt+8`, unless the word is a keyword before a digit or a sign.
            let next = at(s, cp);
            let is_date = matches!(next, b'-' | b'/' | b'.')
                || ((next == b'+' || next.is_ascii_digit())
                    && date_token(&out.buf[start..pos]).is_none());
            if is_date {
                kind = DTK_DATE;
                loop {
                    push!(s[cp].to_ascii_lowercase());
                    cp += 1;
                    let next = at(s, cp);
                    if !(matches!(next, b'+' | b'-' | b'/' | b'_' | b'.' | b':')
                        || next.is_ascii_alphanumeric())
                    {
                        break;
                    }
                }
            } else {
                kind = DTK_STRING;
            }
        } else if c == b'+' || c == b'-' {
            push!(c);
            cp += 1;
            while is_space(at(s, cp)) {
                cp += 1;
            }
            if at(s, cp).is_ascii_digit() {
                kind = DTK_TZ;
                push!(s[cp]);
                cp += 1;
                while matches!(at(s, cp), b'0'..=b'9' | b':' | b'.' | b'-') {
                    push!(s[cp]);
                    cp += 1;
                }
            } else if at(s, cp).is_ascii_alphabetic() {
                kind = DTK_SPECIAL;
                push!(s[cp].to_ascii_lowercase());
                cp += 1;
                while at(s, cp).is_ascii_alphabetic() {
                    push!(s[cp].to_ascii_lowercase());
                    cp += 1;
                }
            } else {
                return Err(BadFormat);
            }
        } else if c.is_ascii_punctuation() {
            cp += 1;
            continue;
        } else {
            return Err(BadFormat);
        }
        out.bounds[out.n] = (start as u16, pos as u16);
        out.kinds[out.n] = kind;
        out.n += 1;
        // The NUL after the field, which the C code writes without a check.
        pos += 1;
    }
    Ok(out)
}

/// `struct pg_tm`, with only the fields that the input uses.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct Tm {
    year: i32,
    mon: i32,
    mday: i32,
    hour: i32,
    min: i32,
    sec: i32,
    yday: i32,
}

/// The result of `DecodeDateTime`: the kind of value, the fields, the microseconds and the zone in seconds west of UTC.
struct Decoded {
    dtype: i32,
    tm: Tm,
    fsec: i32,
    tz: i32,
}

/// A zone that the input named: the session zone or a zone from [`ZoneLookup`].
enum ZoneRef {
    Session,
    Other(Arc<dyn TimeZone + Send + Sync>),
}

impl ZoneRef {
    fn get<'z>(&'z self, session: &'z dyn TimeZone) -> &'z dyn TimeZone {
        match self {
            ZoneRef::Session => session,
            ZoneRef::Other(zone) => &**zone,
        }
    }
}

fn is_leap(year: i32) -> bool {
    year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
}

fn days_in_month(year: i32, month: i32) -> i32 {
    const DAYS: [i32; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    DAYS[month as usize - 1] + i32::from(month == 2 && is_leap(year))
}

/// `IS_VALID_JULIAN`: the date is in the range of the Julian day functions, to the month.
fn is_valid_julian(year: i32, month: i32) -> bool {
    (year > -4713 || (year == -4713 && month >= 11))
        && (year < 5874898 || (year == 5874898 && month < 6))
}

/// `j2date` into the fields.
fn set_date(tm: &mut Tm, julian: i32) {
    let (year, month, day) = j2date(julian);
    (tm.year, tm.mon, tm.mday) = (year, month as i32, day as i32);
}

/// `dt2time` for a time of day in microseconds.
fn set_time(tm: &mut Tm, fsec: &mut i32, time: i64) {
    tm.hour = (time / USECS_PER_HOUR) as i32;
    let time = time - i64::from(tm.hour) * USECS_PER_HOUR;
    tm.min = (time / USECS_PER_MINUTE) as i32;
    let time = time - i64::from(tm.min) * USECS_PER_MINUTE;
    tm.sec = (time / USECS_PER_SEC) as i32;
    *fsec = (time - i64::from(tm.sec) * USECS_PER_SEC) as i32;
}

/// `time_overflows`: the time is not in 00:00:00 to 24:00:00.
fn time_overflows(hour: i32, min: i32, sec: i32, fsec: i32) -> bool {
    if !(0..=24).contains(&hour)
        || !(0..60).contains(&min)
        || !(0..=60).contains(&sec)
        || !(0..=1_000_000).contains(&fsec)
    {
        return true;
    }
    ((i64::from(hour) * 60 + i64::from(min)) * 60 + i64::from(sec)) * USECS_PER_SEC
        + i64::from(fsec)
        > USECS_PER_DAY
}

/// `GetCurrentTimeUsec`: the local fields of the transaction start in the session zone, the microseconds, and the zone in seconds west of UTC.
fn current_time(cx: &DateTimeInput<'_>) -> (Tm, i32, i32) {
    let unix = cx.now.div_euclid(USECS_PER_SEC) + 946_684_800;
    let fsec = cx.now.rem_euclid(USECS_PER_SEC) as i32;
    let offset = cx.zone.at(unix).0;
    let local = unix + i64::from(offset);
    let mut tm = Tm::default();
    set_date(&mut tm, (local.div_euclid(86400) + i64::from(UNIX_EPOCH_JDATE)) as i32);
    let seconds = local.rem_euclid(86400) as i32;
    (tm.hour, tm.min, tm.sec) = (seconds / 3600, seconds / 60 % 60, seconds % 60);
    (tm, fsec, -offset)
}

/// `DetermineTimeZoneOffsetInternal`: the offset west of UTC of a local time in a zone, and the instant in seconds since 1970-01-01 UTC.
fn determine_offset(tm: &Tm, zone: &dyn TimeZone) -> (i32, i64) {
    if !is_valid_julian(tm.year, tm.mon) {
        return (0, 0);
    }
    let day = i64::from(date2j(tm.year, tm.mon, tm.mday) - UNIX_EPOCH_JDATE) * 86400;
    local_offset(day + i64::from(tm.sec) + (i64::from(tm.min) + i64::from(tm.hour) * 60) * 60, zone)
}

/// The part of `DetermineTimeZoneOffsetInternal` after the fields: the offset west of UTC of a local time in seconds since 1970-01-01 in a zone, and the instant. In a gap the local time is in the offset before the change, and in an overlap it is in the offset after the change.
pub fn local_offset(mytime: i64, zone: &dyn TimeZone) -> (i32, i64) {
    let (before, change) = zone.next_change(mytime - 86400);
    let before_time = mytime - i64::from(before);
    let Some((boundary, after)) = change else {
        return (-before, before_time);
    };
    let after_time = mytime - i64::from(after);
    if before_time < boundary && after_time < boundary {
        return (-before, before_time);
    }
    if before_time > boundary && after_time >= boundary {
        return (-after, after_time);
    }
    if before_time > after_time { (-before, before_time) } else { (-after, after_time) }
}

/// `DetermineTimeZoneAbbrevOffset`: the offset west of UTC of a local time with an abbreviation whose meaning changed over time.
fn determine_abbrev_offset(tm: &Tm, abbrev: &[u8], zone: &dyn TimeZone) -> i32 {
    let (offset, instant) = determine_offset(tm, zone);
    match zone.abbrev_at(&text(abbrev).to_ascii_uppercase(), instant) {
        Some((east, _)) => -east,
        None => offset,
    }
}

/// `DecodeTimezoneAbbrev`: the token type, the offset east of UTC, and the zone of a dynamic abbreviation.
fn decode_timezone_abbrev(
    field: &[u8],
    cx: &DateTimeInput<'_>,
) -> Result<(i32, i32, Option<ZoneRef>), DtErr> {
    let token = text(field);
    match cx.zone.abbrev_meaning(&token.to_ascii_uppercase()) {
        Some(AbbrevMeaning::Fixed { offset, dst }) => {
            return Ok((if dst { DTZ } else { TZ }, offset, None));
        }
        Some(AbbrevMeaning::Varies) => return Ok((DYNTZ, 0, Some(ZoneRef::Session))),
        None => {}
    }
    match cx.abbrevs.get(token) {
        None => Ok((UNKNOWN_FIELD, 0, None)),
        Some((_, Abbrev::Fixed { offset, dst })) => {
            Ok((if *dst { DTZ } else { TZ }, *offset, None))
        }
        Some((abbrev, Abbrev::Zone(name))) => match cx.zones.zone(name) {
            Some(zone) => Ok((DYNTZ, 0, Some(ZoneRef::Other(zone)))),
            None => Err(DtErr::BadZoneAbbrev { zone: name.clone(), abbrev: abbrev.to_string() }),
        },
    }
}

/// What `DecodeTimezoneName` makes of the name of a zone.
pub enum ZoneName {
    /// An abbreviation with one offset, in seconds east of UTC.
    Fixed(i32),
    /// An abbreviation whose offset changed over time, and its zone. `None` is the zone of the session.
    Dynamic(Option<Arc<dyn TimeZone + Send + Sync>>),
    /// A zone of the data.
    Zone(Arc<dyn TimeZone + Send + Sync>),
}

impl std::fmt::Debug for ZoneName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ZoneName::Fixed(offset) => f.debug_tuple("Fixed").field(offset).finish(),
            ZoneName::Dynamic(zone) => f.debug_tuple("Dynamic").field(&zone.is_some()).finish(),
            ZoneName::Zone(_) => f.write_str("Zone"),
        }
    }
}

/// `DecodeTimezoneAbbrevPrefix`: the longest abbreviation at the start of the text, in up to 10 letters of any case, as `DecodeTimezoneAbbrev` finds it, and its length in bytes. A dynamic abbreviation whose zone is not in the data does not match.
pub fn decode_timezone_abbrev_prefix(
    text: &str,
    cx: &DateTimeInput<'_>,
) -> Option<(usize, ZoneName)> {
    let low = text.bytes().take(TOKMAXLEN).take_while(u8::is_ascii_alphabetic).collect::<Vec<_>>();
    let low = low.to_ascii_lowercase();
    (1..=low.len()).rev().find_map(|len| match decode_timezone_abbrev(&low[..len], cx) {
        Ok((TZ | DTZ, offset, _)) => Some((len, ZoneName::Fixed(offset))),
        Ok((DYNTZ, _, Some(ZoneRef::Other(zone)))) => Some((len, ZoneName::Dynamic(Some(zone)))),
        Ok((DYNTZ, _, _)) => Some((len, ZoneName::Dynamic(None))),
        _ => None,
    })
}

/// `DecodeTimezoneName`: the abbreviations first, as `DecodeTimezoneAbbrev` finds them, and then the zones of the data. The abbreviations use the first 63 bytes of the name in lower case.
///
/// # Errors
///
/// `22023` when the name is not a zone, and `F0000` when the zone of an abbreviation is not in the data.
pub fn decode_timezone_name(name: &str, cx: &DateTimeInput<'_>) -> Result<ZoneName, TypeError> {
    let mut end = name.len().min(63);
    while !name.is_char_boundary(end) {
        end -= 1;
    }
    let low = name[..end].to_ascii_lowercase();
    let (ty, offset, zone) =
        decode_timezone_abbrev(low.as_bytes(), cx).map_err(|e| e.into_error(name, "text"))?;
    match (ty, zone) {
        (TZ | DTZ, _) => Ok(ZoneName::Fixed(offset)),
        (DYNTZ, Some(ZoneRef::Other(zone))) => Ok(ZoneName::Dynamic(Some(zone))),
        (DYNTZ, _) => Ok(ZoneName::Dynamic(None)),
        _ => cx.zones.zone(name).map(ZoneName::Zone).ok_or_else(|| {
            TypeError::new(
                SqlState::INVALID_PARAMETER_VALUE,
                format!("time zone \"{name}\" not recognized"),
            )
        }),
    }
}

/// `DecodeTimezone` as `parse_sane_timezone` uses it: the offset in seconds west of UTC of a numeric zone, or `None` for a name that is not a numeric zone.
///
/// # Errors
///
/// `22023` when the hours, the minutes or the seconds of a numeric zone are out of range.
pub fn decode_numeric_timezone(name: &str) -> Result<Option<i32>, TypeError> {
    match decode_timezone(name.as_bytes()) {
        Ok(west) => Ok(Some(west)),
        Err(BadFormat) => Ok(None),
        Err(DtErr::TzDispOverflow) => Err(TypeError::new(
            SqlState::INVALID_PARAMETER_VALUE,
            format!("numeric time zone \"{name}\" out of range"),
        )),
        Err(_) => Err(TypeError::new(
            SqlState::INVALID_PARAMETER_VALUE,
            format!("time zone \"{name}\" not recognized"),
        )),
    }
}

/// `DecodeTimezone`: a numeric zone such as `+05`, `-0530` or `+05:30:15`, in seconds west of UTC.
fn decode_timezone(s: &[u8]) -> Result<i32, DtErr> {
    let sign = at(s, 0);
    if sign != b'+' && sign != b'-' {
        return Err(BadFormat);
    }
    let (mut hr, mut end, range) = strtoint(s, 1);
    if range {
        return Err(DtErr::TzDispOverflow);
    }
    let (mut min, mut sec) = (0, 0);
    if at(s, end) == b':' {
        let range;
        (min, end, range) = strtoint(s, end + 1);
        if range {
            return Err(DtErr::TzDispOverflow);
        }
        if at(s, end) == b':' {
            let range;
            (sec, end, range) = strtoint(s, end + 1);
            if range {
                return Err(DtErr::TzDispOverflow);
            }
        }
    } else if end == s.len() && s.len() > 3 {
        min = hr % 100;
        hr /= 100;
    }
    if !(0..=MAX_TZDISP_HOUR).contains(&hr) || !(0..60).contains(&min) || !(0..60).contains(&sec) {
        return Err(DtErr::TzDispOverflow);
    }
    if end != s.len() {
        return Err(BadFormat);
    }
    let tz = (hr * 60 + min) * 60 + sec;
    Ok(if sign == b'-' { tz } else { -tz })
}

/// `DecodeTimeCommon`: `hh:mm`, `hh:mm:ss`, `mm:ss.fff` and so on. Gives the hours, the minutes, the seconds and the microseconds.
fn decode_time_common(s: &[u8], range: i32) -> Result<(i64, i32, i32, i32), DtErr> {
    let (mut hour, end, overflow) = strtol(s, 0);
    if overflow {
        return Err(FieldOverflow);
    }
    if at(s, end) != b':' {
        return Err(BadFormat);
    }
    let (mut min, end, overflow) = strtoint(s, end + 1);
    if overflow {
        return Err(FieldOverflow);
    }
    let mut sec = 0;
    let mut fsec = 0;
    let minutes_seconds = (1 << MINUTE) | (1 << SECOND);
    if end == s.len() {
        if range == minutes_seconds {
            if i32::try_from(hour).is_err() {
                return Err(FieldOverflow);
            }
            (sec, min, hour) = (min, hour as i32, 0);
        }
    } else if at(s, end) == b'.' {
        fsec = parse_fractional_second(&s[end..])?;
        if i32::try_from(hour).is_err() {
            return Err(FieldOverflow);
        }
        (sec, min, hour) = (min, hour as i32, 0);
    } else if at(s, end) == b':' {
        let (value, end, overflow) = strtoint(s, end + 1);
        if overflow {
            return Err(FieldOverflow);
        }
        sec = value;
        if at(s, end) == b'.' {
            fsec = parse_fractional_second(&s[end..])?;
        } else if end != s.len() {
            return Err(BadFormat);
        }
    } else {
        return Err(BadFormat);
    }
    if hour < 0
        || !(0..60).contains(&min)
        || !(0..=60).contains(&sec)
        || !(0..=1_000_000).contains(&fsec)
    {
        return Err(FieldOverflow);
    }
    Ok((hour, min, sec, fsec))
}

/// `DecodeTime`: a time field of a date and time. Gives the mask of the fields.
fn decode_time(s: &[u8], tm: &mut Tm, fsec: &mut i32) -> Result<u32, DtErr> {
    let (hour, min, sec, usec) = decode_time_common(s, INTERVAL_FULL_RANGE)?;
    let hour = i32::try_from(hour).map_err(|_| FieldOverflow)?;
    (tm.hour, tm.min, tm.sec, *fsec) = (hour, min, sec, usec);
    Ok(TIME_M)
}

/// `DecodeNumberField`: a run of digits that holds a date (`yyyymmdd`, `yymmdd`) or a time (`hhmmss`, `hhmm`), with an optional fraction. Gives the mask of the fields.
fn decode_number_field(
    s: &[u8],
    fmask: u32,
    tm: &mut Tm,
    fsec: &mut i32,
    is2digits: &mut bool,
) -> Result<u32, DtErr> {
    if !s.iter().all(|&c| c.is_ascii_digit() || c == b'.') {
        return Err(BadFormat);
    }
    let mut s = s;
    if let Some(dot) = s.iter().position(|&c| c == b'.') {
        *fsec = parse_fractional_second(&s[dot..])?;
        s = &s[..dot];
    } else if fmask & DATE_M != DATE_M && s.len() >= 6 {
        let len = s.len();
        tm.mday = atoi(&s[len - 2..]);
        tm.mon = atoi(&s[len - 4..len - 2]);
        tm.year = atoi(&s[..len - 4]);
        if len - 4 == 2 {
            *is2digits = true;
        }
        return Ok(DATE_M);
    }
    if fmask & TIME_M != TIME_M {
        if s.len() == 6 {
            tm.sec = atoi(&s[4..]);
            tm.min = atoi(&s[2..4]);
            tm.hour = atoi(&s[..2]);
            return Ok(TIME_M);
        }
        if s.len() == 4 {
            tm.sec = 0;
            tm.min = atoi(&s[2..]);
            tm.hour = atoi(&s[..2]);
            return Ok(TIME_M);
        }
    }
    Err(BadFormat)
}

/// `DecodeNumber`: one number of a date, read in the field order of `DateStyle`. Gives the mask of the fields.
fn decode_number(
    s: &[u8],
    have_text_month: bool,
    fmask: u32,
    tm: &mut Tm,
    fsec: &mut i32,
    is2digits: &mut bool,
    order: DateOrder,
) -> Result<u32, DtErr> {
    let flen = s.len();
    let (val, end, overflow) = strtoint(s, 0);
    if overflow {
        return Err(FieldOverflow);
    }
    if end == 0 {
        return Err(BadFormat);
    }
    if at(s, end) == b'.' {
        // More than two digits before the point is a date or a time run together.
        if end > 2 {
            return decode_number_field(s, fmask | DATE_M, tm, fsec, is2digits);
        }
        *fsec = parse_fractional_second(&s[end..])?;
    } else if end != flen {
        return Err(BadFormat);
    }
    // A day of the year after a year.
    if flen == 3 && fmask & DATE_M == m(YEAR) && (1..=366).contains(&val) {
        tm.yday = val;
        return Ok(m(DOY) | m(MONTH) | m(DAY));
    }
    let tmask = match fmask & DATE_M {
        0 => {
            if flen >= 3 || order == DateOrder::Ymd {
                tm.year = val;
                m(YEAR)
            } else if order == DateOrder::Dmy {
                tm.mday = val;
                m(DAY)
            } else {
                tm.mon = val;
                m(MONTH)
            }
        }
        mask if mask == m(YEAR) => {
            tm.mon = val;
            m(MONTH)
        }
        mask if mask == m(MONTH) => {
            if have_text_month && (flen >= 3 || order == DateOrder::Ymd) {
                tm.year = val;
                m(YEAR)
            } else {
                tm.mday = val;
                m(DAY)
            }
        }
        mask if mask == m(YEAR) | m(MONTH) => {
            if have_text_month && flen >= 3 && *is2digits {
                tm.mday = tm.year;
                tm.year = val;
                *is2digits = false;
            } else {
                tm.mday = val;
            }
            m(DAY)
        }
        mask if mask == m(DAY) => {
            tm.mon = val;
            m(MONTH)
        }
        mask if mask == m(MONTH) | m(DAY) => {
            tm.year = val;
            m(YEAR)
        }
        DATE_M => return decode_number_field(s, fmask, tm, fsec, is2digits),
        _ => return Err(BadFormat),
    };
    if tmask == m(YEAR) {
        *is2digits = flen <= 2;
    }
    Ok(tmask)
}

/// `DecodeDate`: a date with separators, such as `2001-02-03` or `feb-03-2001`. Gives the mask of the fields.
fn decode_date(
    s: &[u8],
    mut fmask: u32,
    is2digits: &mut bool,
    tm: &mut Tm,
    order: DateOrder,
) -> Result<u32, DtErr> {
    let mut fields = [(0usize, 0usize); MAXDATEFIELDS];
    let mut nf = 0;
    let mut i = 0;
    while i < s.len() && nf < MAXDATEFIELDS {
        while i < s.len() && !s[i].is_ascii_alphanumeric() {
            i += 1;
        }
        if i == s.len() {
            return Err(BadFormat);
        }
        let start = i;
        if s[i].is_ascii_digit() {
            while at(s, i).is_ascii_digit() {
                i += 1;
            }
        } else {
            while at(s, i).is_ascii_alphabetic() {
                i += 1;
            }
        }
        fields[nf] = (start, i);
        nf += 1;
        // The C code ends the field with a NUL over the next byte, whatever it is.
        if i < s.len() {
            i += 1;
        }
    }
    let mut tmask = 0;
    let mut have_text_month = false;
    let mut numeric = [false; MAXDATEFIELDS];
    for (k, &(start, end)) in fields[..nf].iter().enumerate() {
        let field = &s[start..end];
        if !field[0].is_ascii_alphabetic() {
            numeric[k] = true;
            continue;
        }
        let (ty, val) = date_token(field).unwrap_or((UNKNOWN_FIELD, 0));
        if ty == IGNORE_DTF {
            continue;
        }
        if ty != MONTH {
            return Err(BadFormat);
        }
        tm.mon = val;
        have_text_month = true;
        if fmask & m(MONTH) != 0 {
            return Err(BadFormat);
        }
        fmask |= m(MONTH);
        tmask |= m(MONTH);
    }
    let mut fsec = 0;
    for (k, &(start, end)) in fields[..nf].iter().enumerate() {
        if !numeric[k] {
            continue;
        }
        let dmask =
            decode_number(&s[start..end], have_text_month, fmask, tm, &mut fsec, is2digits, order)?;
        if fmask & dmask != 0 {
            return Err(BadFormat);
        }
        fmask |= dmask;
        tmask |= dmask;
    }
    if fmask & !(m(DOY) | m(TZ)) != DATE_M {
        return Err(BadFormat);
    }
    Ok(tmask)
}

/// `ValidateDate`: the checks and the changes of the year, the month and the day after all the fields are read.
fn validate_date(
    fmask: u32,
    isjulian: bool,
    is2digits: bool,
    bc: bool,
    tm: &mut Tm,
) -> Result<(), DtErr> {
    if fmask & m(YEAR) != 0 {
        if isjulian {
            // The year came from a Julian day.
        } else if bc {
            if tm.year <= 0 {
                return Err(FieldOverflow);
            }
            tm.year = -(tm.year - 1);
        } else if is2digits {
            if tm.year < 0 {
                return Err(FieldOverflow);
            }
            if tm.year < 70 {
                tm.year += 2000;
            } else if tm.year < 100 {
                tm.year += 1900;
            }
        } else if tm.year <= 0 {
            return Err(FieldOverflow);
        }
    }
    if fmask & m(DOY) != 0 {
        set_date(tm, date2j(tm.year, 1, 1).wrapping_add(tm.yday).wrapping_sub(1));
    }
    if fmask & m(MONTH) != 0 && !(1..=12).contains(&tm.mon) {
        return Err(DtErr::MdFieldOverflow);
    }
    if fmask & m(DAY) != 0 && !(1..=31).contains(&tm.mday) {
        return Err(DtErr::MdFieldOverflow);
    }
    if fmask & DATE_M == DATE_M && tm.mday > days_in_month(tm.year, tm.mon) {
        return Err(FieldOverflow);
    }
    Ok(())
}

/// The `AM` and `PM` change of the hour, the same in `DecodeDateTime` and `DecodeTimeOnly`.
fn apply_meridian(mer: i32, tm: &mut Tm) -> Result<(), DtErr> {
    if mer != HR24 && tm.hour > 12 {
        return Err(FieldOverflow);
    }
    if mer == AM && tm.hour == 12 {
        tm.hour = 0;
    } else if mer == PM && tm.hour != 12 {
        tm.hour += 12;
    }
    Ok(())
}

/// `DecodeDateTime`: the fields of a date or a timestamp.
fn decode_date_time(p: &Parsed, cx: &DateTimeInput<'_>) -> Result<Decoded, DtErr> {
    let mut fmask = 0u32;
    let mut ptype = 0;
    let mut dtype = DTK_DATE_VALUE;
    let mut tm = Tm::default();
    let mut fsec = 0;
    let mut tz = 0;
    let mut mer = HR24;
    let mut have_text_month = false;
    let mut isjulian = false;
    let mut is2digits = false;
    let mut bc = false;
    let mut named: Option<Arc<dyn TimeZone + Send + Sync>> = None;
    let mut dynamic: Option<(ZoneRef, &[u8])> = None;

    for i in 0..p.n {
        let field = p.field(i);
        let tmask = match p.kinds[i] {
            DTK_DATE => {
                if ptype == DTK_JULIAN {
                    // A Julian day with a zone, such as `j2451187-08`.
                    let (jday, end, overflow) = strtoint(field, 0);
                    if overflow || jday < 0 {
                        return Err(FieldOverflow);
                    }
                    set_date(&mut tm, jday);
                    isjulian = true;
                    tz = decode_timezone(&field[end..])?;
                    ptype = 0;
                    DATE_M | TIME_M | m(TZ)
                } else if ptype != 0 || fmask & (m(MONTH) | m(DAY)) == m(MONTH) | m(DAY) {
                    // A zone name, or a time run together with a zone such as `040506-08`.
                    if field[0].is_ascii_digit() || ptype != 0 {
                        if ptype != 0 {
                            if ptype != DTK_TIME_VALUE {
                                return Err(BadFormat);
                            }
                            ptype = 0;
                        }
                        if fmask & TIME_M == TIME_M {
                            return Err(BadFormat);
                        }
                        let Some(dash) = field.iter().position(|&c| c == b'-') else {
                            return Err(BadFormat);
                        };
                        tz = decode_timezone(&field[dash..])?;
                        let tmask = decode_number_field(
                            &field[..dash],
                            fmask,
                            &mut tm,
                            &mut fsec,
                            &mut is2digits,
                        )?;
                        tmask | m(TZ)
                    } else {
                        let Some(zone) = cx.zones.zone(text(field)) else {
                            return Err(DtErr::BadTimezone(text(field).to_string()));
                        };
                        named = Some(zone);
                        m(TZ)
                    }
                } else {
                    decode_date(field, fmask, &mut is2digits, &mut tm, cx.order)?
                }
            }
            DTK_TIME => {
                if ptype != 0 {
                    if ptype != DTK_TIME_VALUE {
                        return Err(BadFormat);
                    }
                    ptype = 0;
                }
                let tmask = decode_time(field, &mut tm, &mut fsec)?;
                if time_overflows(tm.hour, tm.min, tm.sec, fsec) {
                    return Err(FieldOverflow);
                }
                tmask
            }
            DTK_TZ => {
                tz = decode_timezone(field)?;
                m(TZ)
            }
            DTK_NUMBER if ptype != 0 => {
                // The field after a `j` or a `t`.
                let (value, end, overflow) = strtoint(field, 0);
                if overflow {
                    return Err(FieldOverflow);
                }
                if at(field, end) != b'.' && end != field.len() {
                    return Err(BadFormat);
                }
                let tmask = match ptype {
                    DTK_JULIAN => {
                        if value < 0 {
                            return Err(FieldOverflow);
                        }
                        set_date(&mut tm, value);
                        isjulian = true;
                        if at(field, end) == b'.' {
                            let time = parse_fraction(&field[end..])? * USECS_PER_DAY as f64;
                            set_time(&mut tm, &mut fsec, time as i64);
                            DATE_M | TIME_M
                        } else {
                            DATE_M
                        }
                    }
                    DTK_TIME_VALUE => {
                        let tmask = decode_number_field(
                            field,
                            fmask | DATE_M,
                            &mut tm,
                            &mut fsec,
                            &mut is2digits,
                        )?;
                        if tmask != TIME_M {
                            return Err(BadFormat);
                        }
                        tmask
                    }
                    _ => return Err(BadFormat),
                };
                ptype = 0;
                dtype = DTK_DATE_VALUE;
                tmask
            }
            DTK_NUMBER => {
                let flen = field.len();
                let dot = field.iter().position(|&c| c == b'.');
                if dot.is_some() && fmask & DATE_M == 0 {
                    decode_date(field, fmask, &mut is2digits, &mut tm, cx.order)?
                } else if dot.is_some_and(|dot| dot > 2)
                    || (flen >= 6 && (fmask & DATE_M == 0 || fmask & TIME_M == 0))
                {
                    decode_number_field(field, fmask, &mut tm, &mut fsec, &mut is2digits)?
                } else {
                    decode_number(
                        field,
                        have_text_month,
                        fmask,
                        &mut tm,
                        &mut fsec,
                        &mut is2digits,
                        cx.order,
                    )?
                }
            }
            DTK_STRING | DTK_SPECIAL => {
                let (mut ty, mut val, valtz) = decode_timezone_abbrev(field, cx)?;
                if ty == UNKNOWN_FIELD {
                    (ty, val) = date_token(field).unwrap_or((UNKNOWN_FIELD, 0));
                }
                if ty == IGNORE_DTF {
                    continue;
                }
                let mut tmask = m(ty);
                match ty {
                    RESERV => match val {
                        DTK_NOW => {
                            tmask = DATE_M | TIME_M | m(TZ);
                            dtype = DTK_DATE_VALUE;
                            (tm, fsec, tz) = current_time(cx);
                        }
                        DTK_YESTERDAY | DTK_TODAY | DTK_TOMORROW => {
                            tmask = DATE_M;
                            dtype = DTK_DATE_VALUE;
                            let (now, _, _) = current_time(cx);
                            let shift = match val {
                                DTK_YESTERDAY => -1,
                                DTK_TOMORROW => 1,
                                _ => 0,
                            };
                            set_date(&mut tm, date2j(now.year, now.mon, now.mday) + shift);
                        }
                        DTK_ZULU => {
                            tmask = TIME_M | m(TZ);
                            dtype = DTK_DATE_VALUE;
                            (tm.hour, tm.min, tm.sec) = (0, 0, 0);
                            tz = 0;
                        }
                        _ => {
                            tmask = DATE_M | TIME_M | m(TZ);
                            dtype = val;
                        }
                    },
                    MONTH => {
                        // A number before a month name was the day, as in `1 jan 2001`.
                        if fmask & m(MONTH) != 0
                            && !have_text_month
                            && fmask & m(DAY) == 0
                            && (1..=31).contains(&tm.mon)
                        {
                            tm.mday = tm.mon;
                            tmask = m(DAY);
                        }
                        have_text_month = true;
                        tm.mon = val;
                    }
                    DTZMOD => {
                        tmask |= m(DTZ);
                        tz -= val;
                    }
                    DTZ => {
                        tmask |= m(TZ);
                        tz = -val;
                    }
                    TZ => tz = -val,
                    DYNTZ => {
                        tmask |= m(TZ);
                        dynamic = valtz.map(|zone| (zone, field));
                    }
                    AMPM => mer = val,
                    ADBC => bc = val == BC,
                    DOW => {}
                    UNITS => {
                        tmask = 0;
                        if ptype != 0 {
                            return Err(BadFormat);
                        }
                        ptype = val;
                    }
                    ISOTIME => {
                        tmask = 0;
                        if fmask & DATE_M != DATE_M || ptype != 0 {
                            return Err(BadFormat);
                        }
                        ptype = val;
                    }
                    UNKNOWN_FIELD => {
                        named = Some(cx.zones.zone(text(field)).ok_or(BadFormat)?);
                        tmask = m(TZ);
                    }
                    _ => return Err(BadFormat),
                }
                tmask
            }
            _ => return Err(BadFormat),
        };
        if tmask & fmask != 0 {
            return Err(BadFormat);
        }
        fmask |= tmask;
    }
    if ptype != 0 {
        return Err(BadFormat);
    }
    if dtype == DTK_DATE_VALUE {
        validate_date(fmask, isjulian, is2digits, bc, &mut tm)?;
        apply_meridian(mer, &mut tm)?;
        // A time with no date is an error for every caller.
        if fmask & DATE_M != DATE_M {
            return Err(BadFormat);
        }
        if let Some(zone) = &named {
            if fmask & m(DTZMOD) != 0 {
                return Err(BadFormat);
            }
            tz = determine_offset(&tm, &**zone).0;
        }
        if let Some((zone, abbrev)) = &dynamic {
            if fmask & m(DTZMOD) != 0 {
                return Err(BadFormat);
            }
            tz = determine_abbrev_offset(&tm, abbrev, zone.get(cx.zone));
        }
        if fmask & m(TZ) == 0 {
            if fmask & m(DTZMOD) != 0 {
                return Err(BadFormat);
            }
            tz = determine_offset(&tm, cx.zone).0;
        }
    }
    Ok(Decoded { dtype, tm, fsec, tz })
}

/// `DecodeTimeOnly`: the fields of a `time` or a `timetz`. Gives the fields, the microseconds and the zone in seconds west of UTC.
fn decode_time_only(p: &Parsed, cx: &DateTimeInput<'_>) -> Result<(Tm, i32, i32), DtErr> {
    let nf = p.n;
    let mut fmask = 0u32;
    let mut ptype = 0;
    let mut tm = Tm::default();
    let mut fsec = 0;
    let mut tz = 0;
    let mut isjulian = false;
    let mut is2digits = false;
    let mut bc = false;
    let mut mer = HR24;
    let mut named: Option<Arc<dyn TimeZone + Send + Sync>> = None;
    let mut dynamic: Option<(ZoneRef, &[u8])> = None;

    for i in 0..nf {
        let field = p.field(i);
        let tmask = match p.kinds[i] {
            DTK_DATE => {
                if i == 0 && nf >= 2 && (p.kinds[nf - 1] == DTK_DATE || p.kinds[1] == DTK_TIME) {
                    decode_date(field, fmask, &mut is2digits, &mut tm, cx.order)?
                } else if field[0].is_ascii_digit() {
                    if fmask & TIME_M == TIME_M {
                        return Err(BadFormat);
                    }
                    let Some(dash) = field.iter().position(|&c| c == b'-') else {
                        return Err(BadFormat);
                    };
                    tz = decode_timezone(&field[dash..])?;
                    let tmask = decode_number_field(
                        &field[..dash],
                        fmask | DATE_M,
                        &mut tm,
                        &mut fsec,
                        &mut is2digits,
                    )?;
                    tmask | m(TZ)
                } else {
                    let Some(zone) = cx.zones.zone(text(field)) else {
                        return Err(DtErr::BadTimezone(text(field).to_string()));
                    };
                    named = Some(zone);
                    m(TZ)
                }
            }
            DTK_TIME => {
                if ptype != 0 {
                    if ptype != DTK_TIME_VALUE {
                        return Err(BadFormat);
                    }
                    ptype = 0;
                }
                decode_time(field, &mut tm, &mut fsec)?
            }
            DTK_TZ => {
                tz = decode_timezone(field)?;
                m(TZ)
            }
            DTK_NUMBER if ptype != 0 => {
                let (value, end, overflow) = strtoint(field, 0);
                if overflow {
                    return Err(FieldOverflow);
                }
                if at(field, end) != b'.' && end != field.len() {
                    return Err(BadFormat);
                }
                let tmask = match ptype {
                    DTK_JULIAN => {
                        if value < 0 {
                            return Err(FieldOverflow);
                        }
                        set_date(&mut tm, value);
                        isjulian = true;
                        if at(field, end) == b'.' {
                            let time = parse_fraction(&field[end..])? * USECS_PER_DAY as f64;
                            set_time(&mut tm, &mut fsec, time as i64);
                            DATE_M | TIME_M
                        } else {
                            DATE_M
                        }
                    }
                    DTK_TIME_VALUE => {
                        let tmask = decode_number_field(
                            field,
                            fmask | DATE_M,
                            &mut tm,
                            &mut fsec,
                            &mut is2digits,
                        )?;
                        if tmask != TIME_M {
                            return Err(BadFormat);
                        }
                        tmask
                    }
                    _ => return Err(BadFormat),
                };
                ptype = 0;
                tmask
            }
            DTK_NUMBER => {
                let flen = field.len();
                match field.iter().position(|&c| c == b'.') {
                    Some(_) if i == 0 && nf >= 2 && p.kinds[nf - 1] == DTK_DATE => {
                        decode_date(field, fmask, &mut is2digits, &mut tm, cx.order)?
                    }
                    Some(dot) if dot > 2 => decode_number_field(
                        field,
                        fmask | DATE_M,
                        &mut tm,
                        &mut fsec,
                        &mut is2digits,
                    )?,
                    Some(_) => return Err(BadFormat),
                    None if flen > 4 => decode_number_field(
                        field,
                        fmask | DATE_M,
                        &mut tm,
                        &mut fsec,
                        &mut is2digits,
                    )?,
                    None => decode_number(
                        field,
                        false,
                        fmask | DATE_M,
                        &mut tm,
                        &mut fsec,
                        &mut is2digits,
                        cx.order,
                    )?,
                }
            }
            DTK_STRING | DTK_SPECIAL => {
                let (mut ty, mut val, valtz) = decode_timezone_abbrev(field, cx)?;
                if ty == UNKNOWN_FIELD {
                    (ty, val) = date_token(field).unwrap_or((UNKNOWN_FIELD, 0));
                }
                if ty == IGNORE_DTF {
                    continue;
                }
                let mut tmask = m(ty);
                match ty {
                    RESERV => match val {
                        DTK_NOW => {
                            tmask = TIME_M;
                            (tm, fsec, _) = current_time(cx);
                        }
                        DTK_ZULU => {
                            tmask = TIME_M | m(TZ);
                            (tm.hour, tm.min, tm.sec) = (0, 0, 0);
                        }
                        _ => return Err(BadFormat),
                    },
                    DTZMOD => {
                        tmask |= m(DTZ);
                        tz -= val;
                    }
                    DTZ => {
                        tmask |= m(TZ);
                        tz = -val;
                    }
                    TZ => tz = -val,
                    DYNTZ => {
                        tmask |= m(TZ);
                        dynamic = valtz.map(|zone| (zone, field));
                    }
                    AMPM => mer = val,
                    ADBC => bc = val == BC,
                    UNITS | ISOTIME => {
                        tmask = 0;
                        if ptype != 0 {
                            return Err(BadFormat);
                        }
                        ptype = val;
                    }
                    UNKNOWN_FIELD => {
                        named = Some(cx.zones.zone(text(field)).ok_or(BadFormat)?);
                        tmask = m(TZ);
                    }
                    _ => return Err(BadFormat),
                }
                tmask
            }
            _ => return Err(BadFormat),
        };
        if tmask & fmask != 0 {
            return Err(BadFormat);
        }
        fmask |= tmask;
    }
    if ptype != 0 {
        return Err(BadFormat);
    }
    validate_date(fmask, isjulian, is2digits, bc, &mut tm)?;
    apply_meridian(mer, &mut tm)?;
    if time_overflows(tm.hour, tm.min, tm.sec, fsec) {
        return Err(FieldOverflow);
    }
    if fmask & TIME_M != TIME_M {
        return Err(BadFormat);
    }
    // The date of the zone rules: the date of the input, or today when there is none.
    let local = |tm: &Tm| -> Result<Tm, DtErr> {
        let mut local = if fmask & DATE_M == 0 {
            current_time(cx).0
        } else if fmask & DATE_M != DATE_M {
            return Err(BadFormat);
        } else {
            *tm
        };
        (local.hour, local.min, local.sec) = (tm.hour, tm.min, tm.sec);
        Ok(local)
    };
    if let Some(zone) = &named {
        if fmask & m(DTZMOD) != 0 {
            return Err(BadFormat);
        }
        tz = match zone.fixed_offset() {
            Some(offset) => -offset,
            None => {
                if fmask & DATE_M != DATE_M {
                    return Err(BadFormat);
                }
                determine_offset(&tm, &**zone).0
            }
        };
    }
    if let Some((zone, abbrev)) = &dynamic {
        if fmask & m(DTZMOD) != 0 {
            return Err(BadFormat);
        }
        tz = determine_abbrev_offset(&local(&tm)?, abbrev, zone.get(cx.zone));
    }
    if fmask & m(TZ) == 0 {
        if fmask & m(DTZMOD) != 0 {
            return Err(BadFormat);
        }
        tz = determine_offset(&local(&tm)?, cx.zone).0;
    }
    Ok((tm, fsec, tz))
}

/// `struct pg_itm_in`: the fields of an interval while it is read.
#[derive(Debug, Clone, Copy, Default)]
struct Itm {
    usec: i64,
    mday: i32,
    mon: i32,
    year: i32,
}

impl Itm {
    /// `AdjustFractMicroseconds`.
    fn fract_usecs(&mut self, frac: f64, scale: i64) -> bool {
        if frac == 0.0 {
            return true;
        }
        let frac = frac * scale as f64;
        let mut usec = frac as i64;
        let rest = frac - usec as f64;
        if rest > 0.5 {
            usec += 1;
        } else if rest < -0.5 {
            usec -= 1;
        }
        self.usec.checked_add(usec).map(|usec| self.usec = usec).is_some()
    }

    /// `AdjustFractDays`.
    fn fract_days(&mut self, frac: f64, scale: i32) -> bool {
        if frac == 0.0 {
            return true;
        }
        let frac = frac * f64::from(scale);
        let days = frac as i32;
        match self.mday.checked_add(days) {
            Some(mday) => self.mday = mday,
            None => return false,
        }
        self.fract_usecs(frac - f64::from(days), USECS_PER_DAY)
    }

    /// `AdjustFractYears`.
    fn fract_years(&mut self, frac: f64, scale: i32) -> bool {
        let months = (frac * f64::from(scale) * 12.0).round_ties_even() as i32;
        self.mon.checked_add(months).map(|mon| self.mon = mon).is_some()
    }

    /// `AdjustMicroseconds`.
    fn usecs(&mut self, val: i64, fval: f64, scale: i64) -> bool {
        mul_add(val, scale, &mut self.usec) && self.fract_usecs(fval, scale)
    }

    /// `AdjustDays`.
    fn days(&mut self, val: i64, scale: i32) -> bool {
        let Ok(val) = i32::try_from(val) else { return false };
        match val.checked_mul(scale).and_then(|days| self.mday.checked_add(days)) {
            Some(mday) => {
                self.mday = mday;
                true
            }
            None => false,
        }
    }

    /// `AdjustMonths`.
    fn months(&mut self, val: i64) -> bool {
        let Ok(val) = i32::try_from(val) else { return false };
        self.mon.checked_add(val).map(|mon| self.mon = mon).is_some()
    }

    /// `AdjustYears`.
    fn years(&mut self, val: i64, scale: i32) -> bool {
        let Ok(val) = i32::try_from(val) else { return false };
        match val.checked_mul(scale).and_then(|years| self.year.checked_add(years)) {
            Some(year) => {
                self.year = year;
                true
            }
            None => false,
        }
    }
}

/// `int64_multiply_add`.
fn mul_add(val: i64, multiplier: i64, sum: &mut i64) -> bool {
    match val.checked_mul(multiplier).and_then(|product| sum.checked_add(product)) {
        Some(value) => {
            *sum = value;
            true
        }
        None => false,
    }
}

fn check(ok: bool) -> Result<(), DtErr> {
    if ok { Ok(()) } else { Err(FieldOverflow) }
}

/// `DecodeTimeForInterval`. The time replaces the microseconds read so far, as in the C code.
fn decode_time_for_interval(s: &[u8], range: i32, itm: &mut Itm) -> Result<(), DtErr> {
    let (hour, min, sec, fsec) = decode_time_common(s, range)?;
    itm.usec = i64::from(fsec);
    check(
        mul_add(hour, USECS_PER_HOUR, &mut itm.usec)
            && mul_add(i64::from(min), USECS_PER_MINUTE, &mut itm.usec)
            && mul_add(i64::from(sec), USECS_PER_SEC, &mut itm.usec),
    )
}

/// The unit of the rightmost number of an interval with no unit, from the range of the typmod.
fn default_unit(range: i32) -> i32 {
    let mask = |fields: &[i32]| fields.iter().fold(0, |mask, &field| mask | 1 << field);
    match range {
        r if r == mask(&[YEAR]) => DTK_YEAR,
        r if r == mask(&[MONTH]) || r == mask(&[YEAR, MONTH]) => DTK_MONTH,
        r if r == mask(&[DAY]) => DTK_DAY,
        r if r == mask(&[HOUR]) || r == mask(&[DAY, HOUR]) => DTK_HOUR,
        r if r == mask(&[MINUTE])
            || r == mask(&[HOUR, MINUTE])
            || r == mask(&[DAY, HOUR, MINUTE]) =>
        {
            DTK_MINUTE
        }
        _ => DTK_SECOND,
    }
}

/// `DecodeInterval`: the fields of an interval in the PostgreSQL and the SQL formats. The fields are read from the right, so that a unit comes before its number.
fn decode_interval(p: &Parsed, range: i32, style: IntervalStyle) -> Result<(i32, Itm), DtErr> {
    let nf = p.n;
    let mut is_before = false;
    let mut parsing_unit_val = false;
    let mut fmask = 0u32;
    let mut ty = IGNORE_DTF;
    let mut dtype = DTK_DELTA;
    let mut itm = Itm::default();

    // In the SQL standard style, a leading minus applies to all the fields when no other field has a sign.
    let force_negative = style == IntervalStyle::SqlStandard
        && nf > 0
        && p.field(0)[0] == b'-'
        && !(1..nf).any(|i| matches!(p.field(i)[0], b'-' | b'+'));

    for i in (0..nf).rev() {
        let field = p.field(i);
        let kind = p.kinds[i];
        let tmask = if kind == DTK_TIME {
            decode_time_for_interval(field, range, &mut itm)?;
            if force_negative && itm.usec > 0 {
                itm.usec = -itm.usec;
            }
            ty = DTK_DAY;
            parsing_unit_val = false;
            TIME_M
        } else if kind == DTK_TZ
            && field[1..].contains(&b':')
            && decode_time_for_interval(&field[1..], range, &mut itm).is_ok()
        {
            // A signed time such as `-01:02:03`.
            if field[0] == b'-' {
                if itm.usec == i64::MIN {
                    return Err(FieldOverflow);
                }
                itm.usec = -itm.usec;
            }
            if force_negative && itm.usec > 0 {
                itm.usec = -itm.usec;
            }
            ty = DTK_DAY;
            parsing_unit_val = false;
            TIME_M
        } else if matches!(kind, DTK_TZ | DTK_DATE | DTK_NUMBER) {
            if ty == IGNORE_DTF {
                ty = default_unit(range);
            }
            let (mut val, end, overflow) = strtol(field, 0);
            if overflow {
                return Err(FieldOverflow);
            }
            let mut fval;
            match at(field, end) {
                b'-' => {
                    // The SQL `years-months` form.
                    let (val2, end, overflow) = strtoint(field, end + 1);
                    if overflow || !(0..12).contains(&val2) {
                        return Err(FieldOverflow);
                    }
                    if end != field.len() {
                        return Err(BadFormat);
                    }
                    ty = DTK_MONTH;
                    let val2 = if field[0] == b'-' { -val2 } else { val2 };
                    val = val
                        .checked_mul(12)
                        .and_then(|v| v.checked_add(i64::from(val2)))
                        .ok_or(FieldOverflow)?;
                    fval = 0.0;
                }
                b'.' => {
                    fval = parse_fraction(&field[end..])?;
                    if field[0] == b'-' {
                        fval = -fval;
                    }
                }
                _ if end == field.len() => fval = 0.0,
                _ => return Err(BadFormat),
            }
            if force_negative {
                if val > 0 {
                    val = -val;
                }
                if fval > 0.0 {
                    fval = -fval;
                }
            }
            let tmask = match ty {
                DTK_MICROSEC => {
                    check(itm.usecs(val, fval, 1))?;
                    m(MICROSECOND)
                }
                DTK_MILLISEC => {
                    check(itm.usecs(val, fval, 1000))?;
                    m(MILLISECOND)
                }
                DTK_SECOND => {
                    check(itm.usecs(val, fval, USECS_PER_SEC))?;
                    // A fraction of a second also fills the smaller units.
                    if fval == 0.0 { m(SECOND) } else { ALL_SECS_M }
                }
                DTK_MINUTE => {
                    check(itm.usecs(val, fval, USECS_PER_MINUTE))?;
                    m(MINUTE)
                }
                DTK_HOUR => {
                    check(itm.usecs(val, fval, USECS_PER_HOUR))?;
                    ty = DTK_DAY;
                    m(HOUR)
                }
                DTK_DAY => {
                    check(itm.days(val, 1) && itm.fract_usecs(fval, USECS_PER_DAY))?;
                    m(DAY)
                }
                DTK_WEEK => {
                    check(itm.days(val, 7) && itm.fract_days(fval, 7))?;
                    m(WEEK)
                }
                DTK_MONTH => {
                    check(itm.months(val) && itm.fract_days(fval, DAYS_PER_MONTH))?;
                    m(MONTH)
                }
                DTK_YEAR => {
                    check(itm.years(val, 1) && itm.fract_years(fval, 1))?;
                    m(YEAR)
                }
                DTK_DECADE => {
                    check(itm.years(val, 10) && itm.fract_years(fval, 10))?;
                    m(DECADE)
                }
                DTK_CENTURY => {
                    check(itm.years(val, 100) && itm.fract_years(fval, 100))?;
                    m(CENTURY)
                }
                DTK_MILLENNIUM => {
                    check(itm.years(val, 1000) && itm.fract_years(fval, 1000))?;
                    m(MILLENNIUM)
                }
                _ => return Err(BadFormat),
            };
            parsing_unit_val = false;
            tmask
        } else if matches!(kind, DTK_STRING | DTK_SPECIAL) {
            if parsing_unit_val {
                return Err(BadFormat);
            }
            let (t, uval) =
                delta_token(field).or_else(|| date_token(field)).unwrap_or((UNKNOWN_FIELD, 0));
            ty = t;
            if ty == IGNORE_DTF {
                continue;
            }
            match ty {
                UNITS => {
                    ty = uval;
                    parsing_unit_val = true;
                    0
                }
                AGO => {
                    // Only at the end.
                    if i != nf - 1 {
                        return Err(BadFormat);
                    }
                    is_before = true;
                    ty = uval;
                    0
                }
                RESERV => {
                    // Only `infinity` and `-infinity`, and nothing after them.
                    if (uval != DTK_LATE && uval != DTK_EARLY) || i != nf - 1 {
                        return Err(BadFormat);
                    }
                    dtype = uval;
                    DATE_M | TIME_M
                }
                _ => return Err(BadFormat),
            }
        } else {
            return Err(BadFormat);
        };
        if tmask & fmask != 0 {
            return Err(BadFormat);
        }
        fmask |= tmask;
    }
    if fmask == 0 || parsing_unit_val {
        return Err(BadFormat);
    }
    if is_before {
        if itm.usec == i64::MIN
            || itm.mday == i32::MIN
            || itm.mon == i32::MIN
            || itm.year == i32::MIN
        {
            return Err(FieldOverflow);
        }
        itm = Itm { usec: -itm.usec, mday: -itm.mday, mon: -itm.mon, year: -itm.year };
    }
    Ok((dtype, itm))
}

/// `ParseISO8601Number`: a number as `strtod` reads it, cut into the integer part and the fraction. Gives the two parts and the end.
fn iso8601_number(s: &[u8], pos: usize) -> Result<(i64, f64, usize), DtErr> {
    let c = at(s, pos);
    if !(c.is_ascii_digit() || c == b'-' || c == b'.') {
        return Err(BadFormat);
    }
    let (value, len) = match strtod::<f64>(&s[pos..]) {
        Some((value, len, false)) if len > 0 => (value, len),
        _ => return Err(BadFormat),
    };
    if value.is_nan() || !(-1.0e15..=1.0e15).contains(&value) {
        return Err(FieldOverflow);
    }
    let ipart = value.trunc() as i64;
    Ok((ipart, value - ipart as f64, pos + len))
}

/// `ISO8601IntegerWidth`: the number of digits before the point, after an optional minus.
fn iso8601_width(s: &[u8], pos: usize) -> usize {
    let pos = pos + usize::from(at(s, pos) == b'-');
    s[pos.min(s.len())..].iter().take_while(|c| c.is_ascii_digit()).count()
}

/// `DecodeISO8601Interval`: `P1Y2M3DT4H5M6S` and the alternative forms `P0001-02-03T04:05:06` and `P00010203T040506`.
fn decode_iso8601_interval(s: &[u8]) -> Result<(i32, Itm), DtErr> {
    let mut itm = Itm::default();
    if s.len() < 2 || s[0] != b'P' {
        return Err(BadFormat);
    }
    let mut pos = 1;
    let mut datepart = true;
    let mut havefield = false;
    while at(s, pos) != 0 {
        if at(s, pos) == b'T' {
            datepart = false;
            havefield = false;
            pos += 1;
            continue;
        }
        let fieldstart = pos;
        let (val, fval, end) = iso8601_number(s, pos)?;
        let unit = at(s, end);
        pos = end + 1;
        if datepart {
            match unit {
                b'Y' => check(itm.years(val, 1) && itm.fract_years(fval, 1))?,
                b'M' => check(itm.months(val) && itm.fract_days(fval, DAYS_PER_MONTH))?,
                b'W' => check(itm.days(val, 7) && itm.fract_days(fval, 7))?,
                b'D' => check(itm.days(val, 1) && itm.fract_usecs(fval, USECS_PER_DAY))?,
                b'T' | 0 | b'-' => {
                    if unit != b'-' && iso8601_width(s, fieldstart) == 8 && !havefield {
                        // The basic alternative form, `yyyymmdd`.
                        check(
                            itm.years(val / 10000, 1)
                                && itm.months((val / 100) % 100)
                                && itm.days(val % 100, 1)
                                && itm.fract_usecs(fval, USECS_PER_DAY),
                        )?;
                        if unit == 0 {
                            return Ok((DTK_DELTA, itm));
                        }
                        datepart = false;
                        havefield = false;
                        continue;
                    }
                    // The extended alternative form, `yyyy-mm-dd`.
                    if havefield {
                        return Err(BadFormat);
                    }
                    check(itm.years(val, 1) && itm.fract_years(fval, 1))?;
                    if unit == 0 {
                        return Ok((DTK_DELTA, itm));
                    }
                    if unit == b'T' {
                        datepart = false;
                        havefield = false;
                        continue;
                    }
                    let (val, fval, end) = iso8601_number(s, pos)?;
                    pos = end;
                    check(itm.months(val) && itm.fract_days(fval, DAYS_PER_MONTH))?;
                    if at(s, pos) == 0 {
                        return Ok((DTK_DELTA, itm));
                    }
                    if at(s, pos) == b'T' {
                        datepart = false;
                        havefield = false;
                        continue;
                    }
                    if at(s, pos) != b'-' {
                        return Err(BadFormat);
                    }
                    let (val, fval, end) = iso8601_number(s, pos + 1)?;
                    pos = end;
                    check(itm.days(val, 1) && itm.fract_usecs(fval, USECS_PER_DAY))?;
                    if at(s, pos) == 0 {
                        return Ok((DTK_DELTA, itm));
                    }
                    if at(s, pos) == b'T' {
                        datepart = false;
                        havefield = false;
                        continue;
                    }
                    return Err(BadFormat);
                }
                _ => return Err(BadFormat),
            }
        } else {
            match unit {
                b'H' => check(itm.usecs(val, fval, USECS_PER_HOUR))?,
                b'M' => check(itm.usecs(val, fval, USECS_PER_MINUTE))?,
                b'S' => check(itm.usecs(val, fval, USECS_PER_SEC))?,
                0 | b':' => {
                    if unit == 0 && iso8601_width(s, fieldstart) == 6 && !havefield {
                        // The basic alternative form, `hhmmss`.
                        check(
                            itm.usecs(val / 10000, 0.0, USECS_PER_HOUR)
                                && itm.usecs((val / 100) % 100, 0.0, USECS_PER_MINUTE)
                                && itm.usecs(val % 100, 0.0, USECS_PER_SEC)
                                && itm.fract_usecs(fval, 1),
                        )?;
                        return Ok((DTK_DELTA, itm));
                    }
                    // The extended alternative form, `hh:mm:ss`.
                    if havefield {
                        return Err(BadFormat);
                    }
                    check(itm.usecs(val, fval, USECS_PER_HOUR))?;
                    if unit == 0 {
                        return Ok((DTK_DELTA, itm));
                    }
                    let (val, fval, end) = iso8601_number(s, pos)?;
                    pos = end;
                    check(itm.usecs(val, fval, USECS_PER_MINUTE))?;
                    if at(s, pos) == 0 {
                        return Ok((DTK_DELTA, itm));
                    }
                    if at(s, pos) != b':' {
                        return Err(BadFormat);
                    }
                    let (val, fval, end) = iso8601_number(s, pos + 1)?;
                    pos = end;
                    check(itm.usecs(val, fval, USECS_PER_SEC))?;
                    if at(s, pos) == 0 {
                        return Ok((DTK_DELTA, itm));
                    }
                    return Err(BadFormat);
                }
                _ => return Err(BadFormat),
            }
        }
        havefield = true;
    }
    Ok((DTK_DELTA, itm))
}

/// `tm2timestamp`: the timestamp of the fields, with the zone in seconds west of UTC for a `timestamptz`.
fn tm2timestamp(tm: &Tm, fsec: i32, tz: Option<i32>) -> Option<i64> {
    if !is_valid_julian(tm.year, tm.mon) {
        return None;
    }
    let date = i64::from(date2j(tm.year, tm.mon, tm.mday) - POSTGRES_EPOCH_JDATE);
    let time = ((i64::from(tm.hour) * 60 + i64::from(tm.min)) * 60 + i64::from(tm.sec))
        * USECS_PER_SEC
        + i64::from(fsec);
    let mut ts = date.checked_mul(USECS_PER_DAY)?.checked_add(time)?;
    if let Some(tz) = tz {
        ts = ts.wrapping_add(i64::from(tz) * USECS_PER_SEC);
    }
    (MIN_TIMESTAMP..END_TIMESTAMP).contains(&ts).then_some(ts)
}

/// `date_in`: the days since 2000-01-01.
pub fn date_in(input: &str, cx: &DateTimeInput<'_>) -> Result<i32, TypeError> {
    match iso_date(input.as_bytes()) {
        Some(date) => Ok(date),
        None => parsed_date(input, cx),
    }
}

/// `date_in` through `ParseDateTime` and `DecodeDateTime`.
fn parsed_date(input: &str, cx: &DateTimeInput<'_>) -> Result<i32, TypeError> {
    let error = |e: DtErr| e.into_error(input, "date");
    let parsed = parse_date_time(c_string(input), DATE_BUFLEN).map_err(error)?;
    let decoded = decode_date_time(&parsed, cx).map_err(error)?;
    let tm = match decoded.dtype {
        DTK_DATE_VALUE => decoded.tm,
        DTK_EPOCH => Tm { year: 1970, mon: 1, mday: 1, ..Tm::default() },
        DTK_LATE => return Ok(DATE_INFINITY),
        DTK_EARLY => return Ok(DATE_NEGATIVE_INFINITY),
        _ => return Err(error(BadFormat)),
    };
    let range = || {
        TypeError::new(
            SqlState::DATETIME_VALUE_OUT_OF_RANGE,
            format!("date out of range: \"{input}\""),
        )
    };
    if !is_valid_julian(tm.year, tm.mon) {
        return Err(range());
    }
    let date = date2j(tm.year, tm.mon, tm.mday) - POSTGRES_EPOCH_JDATE;
    if !(-POSTGRES_EPOCH_JDATE..DATE_END_JULIAN - POSTGRES_EPOCH_JDATE).contains(&date) {
        return Err(range());
    }
    Ok(date)
}

/// The date of a valid `YYYY-MM-DD`, which every `DateStyle` reads the same way. Other input gives `None` and goes through the full parser, which also gives the errors.
fn iso_date(b: &[u8]) -> Option<i32> {
    if b.len() != 10 || b[4] != b'-' || b[7] != b'-' {
        return None;
    }
    let number = |range: std::ops::Range<usize>| {
        b[range]
            .iter()
            .try_fold(0, |n: i32, &c| c.is_ascii_digit().then(|| n * 10 + (c - b'0') as i32))
    };
    let (year, month, day) = (number(0..4)?, number(5..7)?, number(8..10)?);
    if year == 0 || !(1..=12).contains(&month) || day == 0 || day > days_in_month(year, month) {
        return None;
    }
    Some(date2j(year, month, day) - POSTGRES_EPOCH_JDATE)
}

/// `DecodeTimeOnly` for `time_in` and `timetz_in`: the time in microseconds and the zone.
fn time_value(
    input: &str,
    type_name: &str,
    cx: &DateTimeInput<'_>,
) -> Result<(i64, i32), TypeError> {
    let error = |e: DtErr| e.into_error(input, type_name);
    let parsed = parse_date_time(c_string(input), DATE_BUFLEN).map_err(error)?;
    let (tm, fsec, tz) = decode_time_only(&parsed, cx).map_err(error)?;
    let time = ((i64::from(tm.hour) * 60 + i64::from(tm.min)) * 60 + i64::from(tm.sec))
        * USECS_PER_SEC
        + i64::from(fsec);
    Ok((time, tz))
}

/// `time_in`: the microseconds since midnight, rounded to the typmod.
pub fn time_in(input: &str, typmod: i32, cx: &DateTimeInput<'_>) -> Result<i64, TypeError> {
    let (time, _) = time_value(input, "time", cx)?;
    Ok(adjust_time(time, typmod))
}

/// `timetz_in`: the time and the zone in seconds west of UTC.
pub fn timetz_in(
    input: &str,
    typmod: i32,
    cx: &DateTimeInput<'_>,
) -> Result<(i64, i32), TypeError> {
    let (time, tz) = time_value(input, "time with time zone", cx)?;
    Ok((adjust_time(time, typmod), tz))
}

fn timestamp_value(
    input: &str,
    typmod: i32,
    cx: &DateTimeInput<'_>,
    with_zone: bool,
) -> Result<i64, TypeError> {
    let type_name = if with_zone { "timestamp with time zone" } else { "timestamp" };
    let error = |e: DtErr| e.into_error(input, type_name);
    let parsed = parse_date_time(c_string(input), TIMESTAMP_BUFLEN).map_err(error)?;
    let decoded = decode_date_time(&parsed, cx).map_err(error)?;
    let ts = match decoded.dtype {
        DTK_DATE_VALUE => tm2timestamp(&decoded.tm, decoded.fsec, with_zone.then_some(decoded.tz))
            .ok_or_else(|| {
                TypeError::new(
                    SqlState::DATETIME_VALUE_OUT_OF_RANGE,
                    format!("timestamp out of range: \"{input}\""),
                )
            })?,
        DTK_EPOCH => -946_684_800 * USECS_PER_SEC,
        DTK_LATE => TIMESTAMP_INFINITY,
        _ => TIMESTAMP_NEGATIVE_INFINITY,
    };
    adjust_timestamp(ts, typmod)
}

/// `timestamp_in`: the microseconds since 2000-01-01 in local time, rounded to the typmod.
pub fn timestamp_in(input: &str, typmod: i32, cx: &DateTimeInput<'_>) -> Result<i64, TypeError> {
    timestamp_value(input, typmod, cx, false)
}

/// `timestamptz_in`: the microseconds since 2000-01-01 UTC, rounded to the typmod. A value with no zone is in the session zone.
pub fn timestamptz_in(input: &str, typmod: i32, cx: &DateTimeInput<'_>) -> Result<i64, TypeError> {
    timestamp_value(input, typmod, cx, true)
}

/// `interval_in`: the PostgreSQL, SQL and ISO 8601 forms. The typmod gives the unit of a number with no unit, and the result is cut and rounded to it.
pub fn interval_in(input: &str, typmod: i32, style: IntervalStyle) -> Result<Interval, TypeError> {
    let range = if typmod >= 0 { (typmod >> 16) & 0x7fff } else { INTERVAL_FULL_RANGE };
    let bytes = c_string(input);
    let mut result =
        parse_date_time(bytes, INTERVAL_BUFLEN).and_then(|p| decode_interval(&p, range, style));
    if matches!(result, Err(BadFormat)) {
        result = decode_iso8601_interval(bytes);
    }
    let (dtype, itm) = result.map_err(|e| {
        let e = if e == FieldOverflow { DtErr::IntervalOverflow } else { e };
        e.into_error(input, "interval")
    })?;
    let period = match dtype {
        DTK_LATE => Interval::INFINITY,
        DTK_EARLY => Interval::NEGATIVE_INFINITY,
        _ => {
            let months = i64::from(itm.year) * 12 + i64::from(itm.mon);
            let month = i32::try_from(months).map_err(|_| out_of_range("interval"))?;
            Interval { time: itm.usec, day: itm.mday, month }
        }
    };
    adjust_interval(period, typmod)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::datetime::FixedZone;

    fn fields(input: &str) -> Vec<(u8, String)> {
        let p = parse_date_time(input.as_bytes(), INTERVAL_BUFLEN).unwrap();
        (0..p.n).map(|i| (p.kinds[i], text(p.field(i)).to_string())).collect()
    }

    #[test]
    fn the_fields_have_the_kinds_of_parse_date_time() {
        assert_eq!(
            fields("Feb-7-1997 15:23:27.5 +05:30"),
            [
                (DTK_DATE, "feb-7-1997".to_string()),
                (DTK_TIME, "15:23:27.5".to_string()),
                (DTK_TZ, "+05:30".to_string()),
            ]
        );
        assert_eq!(fields("1997.038"), [(DTK_NUMBER, "1997.038".to_string())]);
        assert_eq!(fields("America/New_York"), [(DTK_DATE, "america/new_york".to_string())]);
        assert_eq!(
            fields("j2451187"),
            [(DTK_STRING, "j".to_string()), (DTK_NUMBER, "2451187".to_string())]
        );
        assert_eq!(fields("- infinity"), [(DTK_SPECIAL, "-infinity".to_string())]);
        assert_eq!(parse_date_time("1 \u{e9}".as_bytes(), 256).err(), Some(BadFormat));
    }

    #[test]
    fn the_zone_abbreviations_parse() {
        let abbrevs = ZoneAbbrevs::postgres_default();
        assert!(abbrevs.len() > 150);
        assert_eq!(
            abbrevs.get("est"),
            Some(("est", &Abbrev::Fixed { offset: -18000, dst: false }))
        );
        assert_eq!(abbrevs.get("pdt"), Some(("pdt", &Abbrev::Fixed { offset: -25200, dst: true })));
        assert_eq!(abbrevs.get("msk"), Some(("msk", &Abbrev::Zone("Europe/Moscow".to_string()))));
        assert!(ZoneAbbrevs::parse("TOOLONGABBREV 0").is_err());
        assert!(ZoneAbbrevs::parse("X 0 junk").is_err());
        assert!(ZoneAbbrevs::parse("X 0\nX 3600").is_err());
    }

    #[test]
    fn the_input_reads_the_common_forms() {
        let utc = FixedZone::utc();
        let cx = DateTimeInput {
            order: DateOrder::Mdy,
            zone: &utc,
            zones: &NoZones,
            abbrevs: ZoneAbbrevs::postgres_default(),
            now: 0,
        };
        assert_eq!(date_in("2000-01-02", &cx), Ok(1));
        assert_eq!(date_in("today", &cx), Ok(0));
        assert_eq!(timestamp_in("2000-01-01 00:00:01.5", -1, &cx), Ok(1_500_000));
        assert_eq!(timestamptz_in("2000-01-01 00:00 EST", -1, &cx), Ok(5 * USECS_PER_HOUR));
        assert_eq!(time_in("allballs", -1, &cx), Ok(0));
        assert_eq!(
            timetz_in("04:05 -08", -1, &cx),
            Ok((4 * USECS_PER_HOUR + 5 * USECS_PER_MINUTE, 28800))
        );
        let day = Interval { time: 0, day: 1, month: 0 };
        assert_eq!(interval_in("1 day", -1, IntervalStyle::Postgres), Ok(day));
        assert_eq!(interval_in("P1D", -1, IntervalStyle::Postgres), Ok(day));
        let error = date_in("2000-13-01", &cx).unwrap_err();
        assert_eq!(
            error.hint.as_deref(),
            Some("Perhaps you need a different \"DateStyle\" setting.")
        );
    }

    #[test]
    fn the_iso_date_path_agrees_with_the_parser() {
        let utc = FixedZone::utc();
        for order in [DateOrder::Mdy, DateOrder::Dmy, DateOrder::Ymd] {
            let cx = DateTimeInput {
                order,
                zone: &utc,
                zones: &NoZones,
                abbrevs: ZoneAbbrevs::postgres_default(),
                now: 0,
            };
            for year in [1, 4, 100, 1582, 1900, 1999, 2000, 2024, 2100, 9999] {
                for month in 0..=13 {
                    for day in 0..=32 {
                        let input = format!("{year:04}-{month:02}-{day:02}");
                        match iso_date(input.as_bytes()) {
                            Some(date) => assert_eq!(parsed_date(&input, &cx), Ok(date), "{input}"),
                            None => assert!(parsed_date(&input, &cx).is_err(), "{input}"),
                        }
                    }
                }
            }
        }
    }
}