aidaemon-work 0.25.0

Goals, mandates, tasks and work coordination with a SQLite store; internal to aidaemon, no stability promise
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
//! Cron schedule parsing and next-run computation utilities.
//!
//! Extracted from the deprecated `scheduler` module. Used by health probes
//! and evergreen goal scheduling.

use chrono::{
    DateTime, Datelike, FixedOffset, Local, LocalResult, NaiveDate, NaiveDateTime, Offset,
    TimeZone, Timelike, Utc,
};
use croner::Cron;
use regex::Regex;
use std::sync::LazyLock;

/// Parse a human-friendly schedule string into a 5-field cron expression.
/// Supports natural shortcuts and raw cron pass-through.
pub fn parse_schedule(input: &str) -> anyhow::Result<String> {
    let input = input.trim().trim_end_matches(['.', '!', '?']).trim();
    let now_local = Local::now();

    // Simple keyword shortcuts
    match input.to_lowercase().as_str() {
        "hourly" => return Ok("0 * * * *".to_string()),
        "daily" => return Ok("0 0 * * *".to_string()),
        "weekly" => return Ok("0 0 * * 0".to_string()),
        "monthly" => return Ok("0 0 1 * *".to_string()),
        _ => {}
    }

    // "every Nm" / "every N minutes" / "each 5 minutes" / "1 each 5m"
    let re_minutes =
        Regex::new(r"(?i)^(?:\d+\s+)?(?:every|each)\s+(\d+)\s*(?:m|min|mins|minutes?)$")?;
    if let Some(caps) = re_minutes.captures(input) {
        let n: u32 = caps[1].parse()?;
        if n == 0 || n > 59 {
            anyhow::bail!("Minutes interval must be between 1 and 59");
        }
        return Ok(format!("*/{} * * * *", n));
    }

    // "every Nh" / "every N hours" / "each 2h" / "1 each 6 hours"
    let re_hours = Regex::new(r"(?i)^(?:\d+\s+)?(?:every|each)\s+(\d+)\s*(?:h|hrs?|hours?)$")?;
    if let Some(caps) = re_hours.captures(input) {
        let n: u32 = caps[1].parse()?;
        if n == 0 || n > 23 {
            anyhow::bail!("Hours interval must be between 1 and 23");
        }
        return Ok(format!("0 */{} * * *", n));
    }

    // Multi-time variants (minutes must match):
    // - "daily at 6am, 12pm, 6pm"
    // - "every day at 6am, 12pm, 6pm"
    // - "weekdays at 9am and 5pm"
    // - "weekends at noon"
    let re_daily_at = Regex::new(r"(?i)^daily\s+at\s+(.+)$")?;
    if let Some(caps) = re_daily_at.captures(input) {
        let times = parse_time_list(caps.get(1).unwrap().as_str())?;
        return cron_for_recurring_times(times, "*");
    }
    let re_every_day_at = Regex::new(r"(?i)^(?:every\s+day|everyday)\s+at\s+(.+)$")?;
    if let Some(caps) = re_every_day_at.captures(input) {
        let times = parse_time_list(caps.get(1).unwrap().as_str())?;
        return cron_for_recurring_times(times, "*");
    }
    let re_weekdays_at = Regex::new(r"(?i)^weekdays?\s+at\s+(.+)$")?;
    if let Some(caps) = re_weekdays_at.captures(input) {
        let times = parse_time_list(caps.get(1).unwrap().as_str())?;
        return cron_for_recurring_times(times, "1-5");
    }
    let re_every_weekdays_at = Regex::new(r"(?i)^every\s+weekdays?\s+at\s+(.+)$")?;
    if let Some(caps) = re_every_weekdays_at.captures(input) {
        let times = parse_time_list(caps.get(1).unwrap().as_str())?;
        return cron_for_recurring_times(times, "1-5");
    }
    let re_weekends_at = Regex::new(r"(?i)^weekends?\s+at\s+(.+)$")?;
    if let Some(caps) = re_weekends_at.captures(input) {
        let times = parse_time_list(caps.get(1).unwrap().as_str())?;
        return cron_for_recurring_times(times, "0,6");
    }
    let re_every_weekends_at = Regex::new(r"(?i)^every\s+weekends?\s+at\s+(.+)$")?;
    if let Some(caps) = re_every_weekends_at.captures(input) {
        let times = parse_time_list(caps.get(1).unwrap().as_str())?;
        return cron_for_recurring_times(times, "0,6");
    }

    // Specific named days: "Monday and Friday at 9am", "every Tuesday, Thursday at 3pm"
    let re_specific_days_at = Regex::new(
        r"(?i)^(?:every\s+|on\s+)?(?P<days>(?:monday|tuesday|wednesday|thursday|friday|saturday|sunday|mon|tue|wed|thu|fri|sat|sun)(?:\s*(?:,|\band\b|&)\s*(?:monday|tuesday|wednesday|thursday|friday|saturday|sunday|mon|tue|wed|thu|fri|sat|sun))*)\s+at\s+(?P<times>.+)$",
    )?;
    if let Some(caps) = re_specific_days_at.captures(input) {
        let days = parse_day_list(caps.name("days").unwrap().as_str())?;
        let times = parse_time_list(caps.name("times").unwrap().as_str())?;
        let dow = days
            .iter()
            .map(|d| d.to_string())
            .collect::<Vec<_>>()
            .join(",");
        return cron_for_recurring_times(times, &dow);
    }

    // A relative delay: "in 2h", "after 90 minutes", "in 1 hour 30 minutes".
    // The whole input must be the delay; anything else is refused rather
    // than guessed (see `parse_relative_duration`).
    if let Some(duration) = parse_relative_duration(input)? {
        let target = now_local + duration;
        return Ok(one_shot_cron_from_local(target));
    }

    // "today at 11:09pm" / "tonight at 23:10" / "tomorrow at 9am -05:00" /
    // "tomorrow at 9am Europe/Madrid": the owner's zone unless a numeric
    // offset, UTC or an IANA zone name follows.
    if let Some(target) = parse_day_time_with_optional_timezone(input, now_local)? {
        if target <= now_local {
            anyhow::bail!(
                "Schedule time '{}' is in the past for system timezone {}.",
                input,
                system_timezone_display()
            );
        }
        return Ok(one_shot_cron_from_local(target));
    }

    // Dynamic absolute datetime parser: RFC3339, ISO-like forms, and common
    // date/time formats, in the owner's zone or a trailing zone token's.
    if let Some(target) = parse_absolute_datetime(input, now_local)? {
        if target <= now_local {
            anyhow::bail!(
                "Schedule time '{}' is in the past for system timezone {}.",
                input,
                system_timezone_display()
            );
        }
        return Ok(one_shot_cron_from_local(target));
    }

    // Raw cron pass-through: validate with croner
    let parts: Vec<&str> = input.split_whitespace().collect();
    if parts.len() == 5 {
        // Try to parse as cron
        input
            .parse::<Cron>()
            .map_err(|e| anyhow::anyhow!("Invalid cron expression '{}': {}", input, e))?;
        return Ok(input.to_string());
    }

    anyhow::bail!(
        "Unrecognized schedule format '{}'. Use 'daily at 9am', 'every 5m', 'in 2 hours', 'tomorrow at 9am', an exact time like 2026-03-14T09:30:00-04:00 or '2026-03-14 09:30', or a 5-field cron expression. Interpreted in the owner's timezone {} unless a time names its zone (an IANA name like Europe/Madrid, a numeric offset or UTC).",
        input,
        system_timezone_display()
    )
}

fn one_shot_cron_from_local(target: DateTime<Local>) -> String {
    format!(
        "{} {} {} {} *",
        target.minute(),
        target.hour(),
        target.day(),
        target.month()
    )
}

/// A relative delay, only when the whole input is one: `in`/`after`, then one
/// or more `<whole number> <unit>` terms (optionally joined by `and` or a
/// comma). Units are a closed set of whole words: minutes, hours, days,
/// weeks, with their usual short forms (`m`, `min`, `h`, `hr`, `d`, `w`).
///
/// `Ok(None)` when the input does not start with `in`/`after` and a number.
/// When it does but cannot be fully read ("in 2 months", "in 10 days at
/// 9am"), it is an error asking for a precise time: a guess there fired
/// "in 2 months" after 2 minutes and dropped the "at 9am".
fn parse_relative_duration(input: &str) -> anyhow::Result<Option<chrono::Duration>> {
    static LEAD: LazyLock<Regex> =
        LazyLock::new(|| Regex::new(r"(?i)^(?:in|after)\s+\d").expect("relative lead regex"));
    static TERM: LazyLock<Regex> = LazyLock::new(|| {
        Regex::new(r"(?i)^(\d+)\s*([a-z]+)(?:\s*,\s*|\s+and\s+|\s*)").expect("relative term regex")
    });
    if !LEAD.is_match(input) {
        return Ok(None);
    }
    let refuse = || {
        anyhow::anyhow!(
            "'{input}' is not a delay this can read exactly. Give a delay in whole minutes, hours, days or weeks (e.g. 'in 90 minutes', 'in 3 days'), or an exact time like 2026-03-14T09:30:00-04:00."
        )
    };
    let mut rest = input
        .split_once(char::is_whitespace)
        .map(|(_, rest)| rest.trim_start())
        .unwrap_or("");
    let mut total = chrono::Duration::zero();
    while !rest.is_empty() {
        let caps = TERM.captures(rest).ok_or_else(refuse)?;
        let n: i64 = caps[1].parse().map_err(|_| refuse())?;
        let unit = caps[2].to_ascii_lowercase();
        let term = match unit.as_str() {
            "m" | "min" | "mins" | "minute" | "minutes" => chrono::Duration::try_minutes(n),
            "h" | "hr" | "hrs" | "hour" | "hours" => chrono::Duration::try_hours(n),
            "d" | "day" | "days" => chrono::Duration::try_days(n),
            "w" | "wk" | "wks" | "week" | "weeks" => chrono::Duration::try_weeks(n),
            _ => None,
        }
        .ok_or_else(refuse)?;
        total += term;
        rest = &rest[caps.get(0).map_or(rest.len(), |m| m.end())..];
    }
    if total <= chrono::Duration::zero() {
        return Err(refuse());
    }
    Ok(Some(total))
}

/// The zone a schedule's wall-clock time is read in. Stored in
/// `goal_schedules.tz` as [`ScheduleZone::stored`]: `local` for the owner's
/// zone, an IANA name, or a fixed offset (`UTC`, `+05:30`).
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ScheduleZone {
    /// The owner's own clock (the system zone), with its daylight-saving rules.
    Owner,
    /// A numeric offset or UTC: no daylight saving.
    Fixed(FixedOffset),
    /// A named IANA zone such as `Europe/Madrid`, with its daylight-saving rules.
    Named(chrono_tz::Tz),
}

impl ScheduleZone {
    /// The calendar date `now` falls on in this zone.
    fn date_at(&self, now: DateTime<Local>) -> NaiveDate {
        match self {
            Self::Owner => now.date_naive(),
            Self::Fixed(offset) => now.with_timezone(offset).date_naive(),
            Self::Named(tz) => now.with_timezone(tz).date_naive(),
        }
    }

    /// The instant a wall-clock time in this zone names. A time the clocks
    /// skip (the spring-forward gap) is an error naming it; a time they pass
    /// twice (the fall-back hour) is the earlier of the two.
    fn resolve(&self, naive: NaiveDateTime) -> anyhow::Result<DateTime<Local>> {
        let resolved = match self {
            // The owner's zone by its IANA rules when it has a name the
            // system clock agrees with: the system clock's own lookup does
            // not always report a fall-back time as ambiguous.
            Self::Owner => match owner_tz() {
                Some(tz) => tz
                    .from_local_datetime(&naive)
                    .map(|dt| dt.with_timezone(&Local)),
                None => Local.from_local_datetime(&naive),
            },
            Self::Fixed(offset) => offset
                .from_local_datetime(&naive)
                .map(|dt| dt.with_timezone(&Local)),
            Self::Named(tz) => tz
                .from_local_datetime(&naive)
                .map(|dt| dt.with_timezone(&Local)),
        };
        match resolved {
            LocalResult::Single(dt) => Ok(dt),
            LocalResult::Ambiguous(earlier, _) => Ok(earlier),
            LocalResult::None => anyhow::bail!(
                "{} does not exist in {}: the clocks skip it for daylight saving. Pick a time outside the skipped hour.",
                naive.format("%Y-%m-%d %H:%M"),
                self.name()
            ),
        }
    }

    /// The zone a stored `goal_schedules.tz` names. `local` is the owner's
    /// zone; anything else is read as a zone token, so a value this cannot
    /// resolve is an error, never a silent fallback to the owner's clock.
    pub fn from_stored(tz: &str) -> anyhow::Result<Self> {
        if tz.trim() == LOCAL_TZ {
            return Ok(Self::Owner);
        }
        parse_schedule_zone(tz)
            .map_err(|error| anyhow::anyhow!("Stored schedule time zone '{tz}': {error}"))
    }

    /// The `goal_schedules.tz` value for this zone.
    pub fn stored(&self) -> String {
        match self {
            Self::Owner => LOCAL_TZ.to_string(),
            Self::Fixed(offset) if offset.local_minus_utc() == 0 => "UTC".to_string(),
            Self::Fixed(offset) => offset.to_string(),
            Self::Named(tz) => tz.name().to_string(),
        }
    }

    /// How the owner reads this zone beside a schedule's clock time
    /// ("Europe/Madrid", "UTC+05:30"); `None` for the owner's own zone.
    pub fn label(&self) -> Option<String> {
        match self {
            Self::Owner => None,
            Self::Fixed(offset) if offset.local_minus_utc() == 0 => Some("UTC".to_string()),
            Self::Fixed(offset) => Some(format!("UTC{offset}")),
            Self::Named(tz) => Some(tz.name().to_string()),
        }
    }

    fn name(&self) -> String {
        match self {
            Self::Owner => owner_zone_name().unwrap_or_else(system_timezone_display),
            Self::Fixed(offset) => offset.to_string(),
            Self::Named(tz) => tz.name().to_string(),
        }
    }
}

fn parse_day_time_with_optional_timezone(
    input: &str,
    now_local: DateTime<Local>,
) -> anyhow::Result<Option<DateTime<Local>>> {
    static RE: LazyLock<Regex> = LazyLock::new(|| {
        Regex::new(
            r"(?i)^(today|tonight|tomorrow)\s+at\s+(\d{1,2})(?::(\d{2}))?\s*(am|pm)?(?:\s+(\S+))?$",
        )
        .expect("day-time regex")
    });
    let Some(caps) = RE.captures(input) else {
        return Ok(None);
    };
    let Some(day_kw) = caps.get(1).map(|m| m.as_str().to_ascii_lowercase()) else {
        return Ok(None);
    };
    let Some((hour, minute)) = parse_time_with_offset_captures(&caps, 2, 3, 4) else {
        return Ok(None);
    };
    let zone = match caps.get(5).map(|m| m.as_str().trim()) {
        Some(token) => parse_schedule_zone(token)?,
        None => ScheduleZone::Owner,
    };
    let today = zone.date_at(now_local);
    let base_date = match day_kw.as_str() {
        "tomorrow" => today.succ_opt().unwrap_or(today),
        _ => today,
    };
    let Some(naive) = base_date.and_hms_opt(hour, minute, 0) else {
        return Ok(None);
    };
    zone.resolve(naive).map(Some)
}

/// The `goal_schedules.tz` value for the owner's own zone.
pub const LOCAL_TZ: &str = "local";

/// A schedule as stored: its cron, read on the wall clock of `tz` (a
/// `goal_schedules.tz` value, see [`ScheduleZone::stored`]).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParsedSchedule {
    pub cron: String,
    pub tz: String,
}

/// [`parse_schedule`], plus a recurring schedule with a trailing zone
/// ("every weekday at 8am Europe/Madrid", "0 9 * * 1-5 America/Guayaquil"):
/// its cron is then read on that zone's wall clock, daylight saving
/// included. The zone follows the same rules as a one-time time's (an IANA
/// name, a numeric offset or UTC; an abbreviation is refused). A one-time
/// time already resolves its zone to an instant, so it is stored `local`.
pub fn parse_schedule_with_zone(input: &str) -> anyhow::Result<ParsedSchedule> {
    let first_error = match parse_schedule(input) {
        Ok(cron) => {
            return Ok(ParsedSchedule {
                cron,
                tz: LOCAL_TZ.to_string(),
            });
        }
        Err(error) => error,
    };
    let trimmed = input.trim().trim_end_matches(['.', '!', '?']).trim();
    let Some((rest, token)) = trimmed.rsplit_once(char::is_whitespace) else {
        return Err(first_error);
    };
    let Ok(cron) = parse_schedule(rest.trim_end()) else {
        return Err(first_error);
    };
    if is_one_shot_schedule(&cron) {
        return Err(first_error);
    }
    let zone = parse_schedule_zone(token)?;
    Ok(ParsedSchedule {
        cron,
        tz: zone.stored(),
    })
}

/// The zone a time names after it: a numeric offset (`-05:00`, `+0530`),
/// `UTC`/`GMT`/`Z`, or any IANA zone name (`Europe/Madrid`,
/// `America/Guayaquil`; exact, or the same name in another letter case),
/// read with that zone's daylight-saving rules. An abbreviation such as EST
/// or PST is refused, not mapped to a fixed offset: "EST" in July is read as
/// EDT by people and would fire an hour off. So are the IANA database's
/// legacy names without a region (`EST`, `CET`, `Japan`): a real zone name
/// has a `/`.
fn parse_schedule_zone(token: &str) -> anyhow::Result<ScheduleZone> {
    static NUMERIC: LazyLock<Regex> = LazyLock::new(|| {
        Regex::new(r"^(?:UTC|GMT)?([+-])(\d{2})(?::?(\d{2}))?$").expect("numeric offset regex")
    });
    let token = token.trim();
    let upper = token.to_ascii_uppercase();
    if matches!(upper.as_str(), "UTC" | "GMT" | "Z") {
        return Ok(ScheduleZone::Fixed(
            FixedOffset::east_opt(0).expect("zero offset"),
        ));
    }
    if let Some(caps) = NUMERIC.captures(&upper) {
        let hours: i32 = caps[2].parse()?;
        let minutes: i32 = caps.get(3).map_or(Ok(0), |m| m.as_str().parse())?;
        if hours <= 23 && minutes <= 59 {
            let seconds = hours * 3600 + minutes * 60;
            let offset = if &caps[1] == "+" {
                FixedOffset::east_opt(seconds)
            } else {
                FixedOffset::west_opt(seconds)
            };
            if let Some(offset) = offset {
                return Ok(ScheduleZone::Fixed(offset));
            }
        }
    }
    if token.contains('/') {
        if let Ok(tz) = token.parse::<chrono_tz::Tz>() {
            return Ok(ScheduleZone::Named(tz));
        }
        if let Some(tz) = chrono_tz::TZ_VARIANTS
            .iter()
            .find(|tz| tz.name().eq_ignore_ascii_case(token))
        {
            return Ok(ScheduleZone::Named(*tz));
        }
        anyhow::bail!(
            "'{token}' is not an IANA time zone name. Use a name like Europe/Madrid or America/New_York, a numeric offset like -05:00, or UTC."
        );
    }
    let example = zone_abbreviation_example(&upper).unwrap_or("America/New_York or Europe/Madrid");
    anyhow::bail!(
        "Time zone '{token}' is not one this can resolve exactly: an abbreviation hides daylight saving. Name the IANA zone instead (e.g. {example}), give a numeric offset like -05:00 or UTC, or leave it out to use the owner's timezone ({}).",
        owner_zone_name().unwrap_or_else(system_timezone_display)
    )
}

/// An IANA zone a common abbreviation usually means, for the refusal's hint
/// only: an abbreviation is ambiguous (IST, CST), so it is never resolved.
fn zone_abbreviation_example(upper: &str) -> Option<&'static str> {
    Some(match upper {
        "ET" | "EST" | "EDT" => "America/New_York",
        "CT" | "CST" | "CDT" => "America/Chicago",
        "MT" | "MST" | "MDT" => "America/Denver",
        "PT" | "PST" | "PDT" => "America/Los_Angeles",
        "AKST" | "AKDT" => "America/Anchorage",
        "HST" => "Pacific/Honolulu",
        "BST" | "WET" | "WEST" => "Europe/London",
        "CET" | "CEST" | "MET" | "MEST" => "Europe/Madrid",
        "EET" | "EEST" => "Europe/Athens",
        "MSK" => "Europe/Moscow",
        "IST" => "Asia/Kolkata",
        "JST" => "Asia/Tokyo",
        "KST" => "Asia/Seoul",
        "AEST" | "AEDT" => "Australia/Sydney",
        "NZST" | "NZDT" => "Pacific/Auckland",
        "ECT" => "America/Guayaquil",
        _ => return None,
    })
}

/// An exact date or date-time, read in the owner's zone, or in the zone a
/// trailing token names (`2026-03-14 09:30 Europe/Madrid`). RFC 3339 carries
/// its own offset.
fn parse_absolute_datetime(
    input: &str,
    now_local: DateTime<Local>,
) -> anyhow::Result<Option<DateTime<Local>>> {
    if let Some(dt) = parse_absolute_datetime_in(input, ScheduleZone::Owner, now_local)? {
        return Ok(Some(dt));
    }
    if let Some((rest, token)) = input.rsplit_once(char::is_whitespace) {
        let rest = rest.trim_end();
        // Only a trailing token after a date the parser reads is a zone; the
        // token is then held to `parse_schedule_zone`, so "... EST" is refused.
        // The shape is checked in UTC, which skips no time.
        let probe = ScheduleZone::Fixed(FixedOffset::east_opt(0).expect("zero offset"));
        if !rest.is_empty()
            && parse_absolute_datetime_in(rest, probe, now_local).is_ok_and(|dt| dt.is_some())
        {
            let zone = parse_schedule_zone(token)?;
            return parse_absolute_datetime_in(rest, zone, now_local);
        }
    }
    Ok(None)
}

fn parse_absolute_datetime_in(
    input: &str,
    zone: ScheduleZone,
    now_local: DateTime<Local>,
) -> anyhow::Result<Option<DateTime<Local>>> {
    if let Ok(dt) = DateTime::parse_from_rfc3339(input) {
        return Ok(Some(dt.with_timezone(&Local)));
    }

    let dt_formats = [
        "%Y-%m-%d %H:%M",
        "%Y-%m-%d %H:%M:%S",
        "%Y/%m/%d %H:%M",
        "%Y/%m/%d %H:%M:%S",
        "%m/%d/%Y %H:%M",
        "%m/%d/%Y %I:%M %p",
        "%Y-%m-%d %I:%M %p",
        "%Y-%m-%d %I%p",
        "%b %d %Y %H:%M",
        "%b %d %Y %I:%M %p",
        "%B %d %Y %H:%M",
        "%B %d %Y %I:%M %p",
    ];
    for fmt in dt_formats {
        if let Ok(naive) = NaiveDateTime::parse_from_str(input, fmt) {
            return zone.resolve(naive).map(Some);
        }
    }

    if let Some(dt) = parse_named_month_datetime(input, zone, now_local)? {
        return Ok(Some(dt));
    }

    let date_formats = ["%Y-%m-%d", "%Y/%m/%d", "%m/%d/%Y"];
    for fmt in date_formats {
        if let Ok(date) = NaiveDate::parse_from_str(input, fmt)
            && let Some(naive) = date.and_hms_opt(0, 0, 0)
        {
            return zone.resolve(naive).map(Some);
        }
    }

    Ok(None)
}

fn parse_named_month_datetime(
    input: &str,
    zone: ScheduleZone,
    now_local: DateTime<Local>,
) -> anyhow::Result<Option<DateTime<Local>>> {
    static RE: LazyLock<Regex> = LazyLock::new(|| {
        Regex::new(
            r"(?i)^(?:on\s+)?(?P<month>jan(?:uary)?|feb(?:ruary)?|mar(?:ch)?|apr(?:il)?|may|jun(?:e)?|jul(?:y)?|aug(?:ust)?|sep(?:t(?:ember)?)?|oct(?:ober)?|nov(?:ember)?|dec(?:ember)?)\s+(?P<day>\d{1,2})(?:st|nd|rd|th)?(?:,\s*(?P<year>\d{4}))?(?:\s+at\s+(?P<hour>\d{1,2})(?::(?P<minute>\d{2}))?\s*(?P<ampm>am|pm)?)?$",
        )
        .expect("named-month regex")
    });
    let Some(caps) = RE.captures(input.trim()) else {
        return Ok(None);
    };

    let Some(month) = caps
        .name("month")
        .and_then(|m| parse_month_name(m.as_str()))
    else {
        return Ok(None);
    };
    let Some(day) = caps
        .name("day")
        .and_then(|m| m.as_str().parse::<u32>().ok())
    else {
        return Ok(None);
    };
    let explicit_year = caps
        .name("year")
        .and_then(|m| m.as_str().parse::<i32>().ok());

    let mut hour: u32 = caps
        .name("hour")
        .and_then(|m| m.as_str().parse::<u32>().ok())
        .unwrap_or(0);
    let minute: u32 = caps
        .name("minute")
        .and_then(|m| m.as_str().parse::<u32>().ok())
        .unwrap_or(0);
    if let Some(ampm) = caps.name("ampm") {
        let ampm = ampm.as_str().to_ascii_lowercase();
        if ampm == "pm" && hour < 12 {
            hour += 12;
        } else if ampm == "am" && hour == 12 {
            hour = 0;
        }
    }
    if hour > 23 || minute > 59 {
        return Ok(None);
    }

    let build = |year: i32| -> anyhow::Result<Option<DateTime<Local>>> {
        let Some(naive) =
            NaiveDate::from_ymd_opt(year, month, day).and_then(|d| d.and_hms_opt(hour, minute, 0))
        else {
            return Ok(None);
        };
        zone.resolve(naive).map(Some)
    };
    let year = explicit_year.unwrap_or_else(|| zone.date_at(now_local).year());
    let candidate = build(year)?;
    if explicit_year.is_none() && candidate.is_some_and(|dt| dt <= now_local) {
        return build(year + 1);
    }
    Ok(candidate)
}

fn parse_month_name(month: &str) -> Option<u32> {
    match month.trim().to_ascii_lowercase().as_str() {
        "jan" | "january" => Some(1),
        "feb" | "february" => Some(2),
        "mar" | "march" => Some(3),
        "apr" | "april" => Some(4),
        "may" => Some(5),
        "jun" | "june" => Some(6),
        "jul" | "july" => Some(7),
        "aug" | "august" => Some(8),
        "sep" | "sept" | "september" => Some(9),
        "oct" | "october" => Some(10),
        "nov" | "november" => Some(11),
        "dec" | "december" => Some(12),
        _ => None,
    }
}

fn parse_time_with_offset_captures(
    caps: &regex::Captures,
    hour_idx: usize,
    minute_idx: usize,
    ampm_idx: usize,
) -> Option<(u32, u32)> {
    let mut hour: u32 = caps.get(hour_idx)?.as_str().parse().ok()?;
    let minute: u32 = caps
        .get(minute_idx)
        .map_or(Some(0), |m| m.as_str().parse().ok())?;
    if let Some(ampm) = caps.get(ampm_idx) {
        let ampm = ampm.as_str().to_ascii_lowercase();
        if ampm == "pm" && hour < 12 {
            hour += 12;
        } else if ampm == "am" && hour == 12 {
            hour = 0;
        }
    }
    if hour > 23 || minute > 59 {
        return None;
    }
    Some((hour, minute))
}

fn parse_time_list(raw: &str) -> anyhow::Result<Vec<(u32, u32)>> {
    let raw = raw.trim();
    if raw.is_empty() {
        anyhow::bail!("Missing time after 'at'");
    }

    // Split on commas + "and" (case-insensitive); keep it permissive for LLM text.
    let and_re = Regex::new(r"(?i)\s+and\s+")?;
    let normalized = and_re.replace_all(raw, ",").replace('&', ",");
    let parts: Vec<&str> = normalized
        .split(',')
        .map(str::trim)
        .filter(|p| !p.is_empty())
        .collect();
    if parts.is_empty() {
        anyhow::bail!("Missing time after 'at'");
    }

    let mut out = Vec::new();
    for p in parts {
        out.push(parse_time_token(p)?);
    }
    Ok(out)
}

fn parse_time_token(token: &str) -> anyhow::Result<(u32, u32)> {
    let token = token.trim();
    if token.is_empty() {
        anyhow::bail!("Empty time token");
    }
    let lower = token.to_ascii_lowercase();
    if lower == "noon" {
        return Ok((12, 0));
    }
    if lower == "midnight" {
        return Ok((0, 0));
    }

    let re = Regex::new(r"(?i)^(\d{1,2})(?::(\d{2}))?\s*(am|pm)?$")?;
    let Some(caps) = re.captures(token) else {
        anyhow::bail!("Unrecognized time token '{}'", token);
    };

    let mut hour: u32 = caps.get(1).unwrap().as_str().parse()?;
    let minute: u32 = caps.get(2).map_or(Ok(0), |m| m.as_str().parse())?;
    if let Some(ampm) = caps.get(3) {
        let ampm = ampm.as_str().to_ascii_lowercase();
        if ampm == "pm" && hour < 12 {
            hour += 12;
        } else if ampm == "am" && hour == 12 {
            hour = 0;
        }
    }

    if hour > 23 {
        anyhow::bail!("Hour must be between 0 and 23");
    }
    if minute > 59 {
        anyhow::bail!("Minute must be between 0 and 59");
    }
    Ok((hour, minute))
}

/// Parse a day name (or abbreviation) to cron day-of-week number (0=Sun..6=Sat).
fn day_name_to_cron(name: &str) -> Option<u32> {
    match name.trim().to_ascii_lowercase().as_str() {
        "sun" | "sunday" => Some(0),
        "mon" | "monday" => Some(1),
        "tue" | "tuesday" => Some(2),
        "wed" | "wednesday" => Some(3),
        "thu" | "thursday" => Some(4),
        "fri" | "friday" => Some(5),
        "sat" | "saturday" => Some(6),
        _ => None,
    }
}

/// Parse a comma/and-separated day-name string into sorted cron day numbers.
fn parse_day_list(raw: &str) -> anyhow::Result<Vec<u32>> {
    let and_re = Regex::new(r"(?i)\s+and\s+")?;
    let normalized = and_re.replace_all(raw, ",").replace('&', ",");
    let parts: Vec<&str> = normalized
        .split(',')
        .map(str::trim)
        .filter(|p| !p.is_empty())
        .collect();
    if parts.is_empty() {
        anyhow::bail!("No day names found");
    }
    let mut days = Vec::new();
    for p in parts {
        let d =
            day_name_to_cron(p).ok_or_else(|| anyhow::anyhow!("Unrecognized day name '{}'", p))?;
        if !days.contains(&d) {
            days.push(d);
        }
    }
    days.sort_unstable();
    Ok(days)
}

fn cron_for_recurring_times(times: Vec<(u32, u32)>, dow: &str) -> anyhow::Result<String> {
    if times.is_empty() {
        anyhow::bail!("Missing time after 'at'");
    }
    let minute = times[0].1;
    if times.iter().any(|(_, m)| *m != minute) {
        anyhow::bail!(
            "Multiple times must share the same minute (e.g. 'daily at 6am, 12pm, 6pm'). For different minutes, create multiple schedules."
        );
    }
    let mut hours: Vec<u32> = times.iter().map(|(h, _)| *h).collect();
    hours.sort_unstable();
    hours.dedup();
    let hour_field = hours
        .into_iter()
        .map(|h| h.to_string())
        .collect::<Vec<_>>()
        .join(",");
    Ok(format!("{} {} * * {}", minute, hour_field, dow))
}

/// Compute the next occurrence from a cron expression using croner.
pub fn compute_next_run(cron_expr: &str) -> anyhow::Result<DateTime<Utc>> {
    Ok(compute_next_run_local(cron_expr)?.with_timezone(&Utc))
}

/// Compute the next occurrence from a cron expression in system-local timezone.
pub fn compute_next_run_local(cron_expr: &str) -> anyhow::Result<DateTime<Local>> {
    let cron: Cron = cron_expr
        .parse()
        .map_err(|e| anyhow::anyhow!("Failed to parse cron '{}': {}", cron_expr, e))?;

    cron.find_next_occurrence(&Local::now(), false)
        .map_err(|e| anyhow::anyhow!("No next occurrence for '{}': {}", cron_expr, e))
}

/// Compute a fixed number of occurrences after an already-computed next run,
/// in the zone `next` carries (wall-clock times stay put across a
/// daylight-saving change).
pub fn following_runs<Tz: TimeZone>(
    cron_expr: &str,
    next: &DateTime<Tz>,
    count: usize,
) -> anyhow::Result<Vec<DateTime<Tz>>> {
    let cron: Cron = cron_expr
        .parse()
        .map_err(|e| anyhow::anyhow!("Failed to parse cron '{}': {}", cron_expr, e))?;
    let mut cursor = next.clone();
    let mut runs = Vec::with_capacity(count);
    for _ in 0..count {
        cursor = cron
            .find_next_occurrence(&cursor, false)
            .map_err(|e| anyhow::anyhow!("No next occurrence for '{}': {}", cron_expr, e))?;
        runs.push(cursor.clone());
    }
    Ok(runs)
}

/// The runs an owner confirms before scheduled work is saved: `first` (the
/// already-computed next run) and, for a recurring schedule, the ones after
/// it, `count` in all. A one-time schedule has only its single time. A cron
/// with no later occurrence lists what it has.
pub fn upcoming_runs<Tz: TimeZone>(
    cron_expr: &str,
    first: &DateTime<Tz>,
    one_shot: bool,
    count: usize,
) -> Vec<DateTime<Tz>> {
    let mut runs = vec![first.clone()];
    if !one_shot && count > 1 {
        runs.extend(following_runs(cron_expr, first, count - 1).unwrap_or_default());
    }
    runs.truncate(count.max(1));
    runs
}

#[cfg(test)]
mod following_runs_tests {
    use chrono::{Local, TimeZone};

    #[test]
    fn weekday_runs_skip_the_weekend_and_do_not_repeat_next() {
        let friday = Local
            .with_ymd_and_hms(2026, 11, 20, 8, 0, 0)
            .single()
            .unwrap();
        let runs = super::following_runs("0 8 * * 1-5", &friday, 2).unwrap();
        assert_eq!(runs[0].date_naive().to_string(), "2026-11-23");
        assert_eq!(runs[1].date_naive().to_string(), "2026-11-24");
        assert!(runs.iter().all(|run| run.time() == friday.time()));
    }

    /// US Eastern time (rules since 2007), test-only, so the daylight-saving
    /// cases do not depend on the machine's zone and need no zone crate.
    #[derive(Debug, Clone, Copy)]
    struct Eastern;

    impl Eastern {
        fn nth_sunday(year: i32, month: u32, n: u32) -> chrono::NaiveDate {
            use chrono::Datelike;
            let first = chrono::NaiveDate::from_ymd_opt(year, month, 1).unwrap();
            let to_sunday = (7 - first.weekday().num_days_from_sunday()) % 7;
            first + chrono::Duration::days(i64::from(to_sunday + 7 * (n - 1)))
        }

        fn utc_offset(utc: &chrono::NaiveDateTime) -> chrono::FixedOffset {
            use chrono::Datelike;
            // 2:00 AM local both times: 7:00 UTC in March, 6:00 UTC in November.
            let starts = Self::nth_sunday(utc.year(), 3, 2)
                .and_hms_opt(7, 0, 0)
                .unwrap();
            let ends = Self::nth_sunday(utc.year(), 11, 1)
                .and_hms_opt(6, 0, 0)
                .unwrap();
            let hours = if (starts..ends).contains(utc) { -4 } else { -5 };
            chrono::FixedOffset::east_opt(hours * 3600).unwrap()
        }
    }

    impl TimeZone for Eastern {
        type Offset = chrono::FixedOffset;

        fn from_offset(_offset: &chrono::FixedOffset) -> Self {
            Eastern
        }

        fn offset_from_local_date(
            &self,
            local: &chrono::NaiveDate,
        ) -> chrono::LocalResult<chrono::FixedOffset> {
            self.offset_from_local_datetime(&local.and_hms_opt(0, 0, 0).unwrap())
        }

        fn offset_from_local_datetime(
            &self,
            local: &chrono::NaiveDateTime,
        ) -> chrono::LocalResult<chrono::FixedOffset> {
            // Daylight time first, so an ambiguous time lists the earlier instant first.
            let fits: Vec<chrono::FixedOffset> = [-4, -5]
                .into_iter()
                .map(|hours| chrono::FixedOffset::east_opt(hours * 3600).unwrap())
                .filter(|offset| {
                    Self::utc_offset(
                        &(*local - chrono::Duration::seconds(offset.local_minus_utc().into())),
                    ) == *offset
                })
                .collect();
            match fits.as_slice() {
                [one] => chrono::LocalResult::Single(*one),
                [earlier, later] => chrono::LocalResult::Ambiguous(*earlier, *later),
                _ => chrono::LocalResult::None,
            }
        }

        fn offset_from_utc_date(&self, utc: &chrono::NaiveDate) -> chrono::FixedOffset {
            Self::utc_offset(&utc.and_hms_opt(0, 0, 0).unwrap())
        }

        fn offset_from_utc_datetime(&self, utc: &chrono::NaiveDateTime) -> chrono::FixedOffset {
            Self::utc_offset(utc)
        }
    }

    fn et(y: i32, mo: u32, d: u32, h: u32, mi: u32) -> chrono::DateTime<Eastern> {
        Eastern
            .with_ymd_and_hms(y, mo, d, h, mi, 0)
            .single()
            .unwrap()
    }

    /// Formatted as every owner-facing time is, as seen on Mon Nov 16, 2026.
    fn shown(runs: &[chrono::DateTime<Eastern>]) -> Vec<String> {
        let now = et(2026, 11, 16, 9, 17).fixed_offset();
        runs.iter()
            .map(|run| super::format_owner_time(&run.fixed_offset(), &now, "ET"))
            .collect()
    }

    #[test]
    fn upcoming_runs_keep_the_wall_clock_across_the_fall_back() {
        // Daylight time ends Sun Nov 1, 2026: 9:00 AM stays 9:00 AM ET, and
        // that day's run is 25 hours after the last one.
        let runs = super::upcoming_runs("0 9 * * *", &et(2026, 10, 31, 9, 0), false, 3);
        assert_eq!(
            shown(&runs),
            vec![
                "Sat, Oct 31, 9:00 AM ET",
                "Sun, Nov 1, 9:00 AM ET",
                "Mon, Nov 2, 9:00 AM ET",
            ]
        );
        assert_eq!(runs[0].offset().local_minus_utc(), -4 * 3600);
        assert_eq!(runs[1].offset().local_minus_utc(), -5 * 3600);
        assert_eq!(runs[1] - runs[0], chrono::Duration::hours(25));
    }

    #[test]
    fn upcoming_runs_keep_the_wall_clock_across_the_spring_forward() {
        // Daylight time starts Sun Mar 8, 2026: that day's run is 23 hours later.
        let runs = super::upcoming_runs("30 8 * * *", &et(2026, 3, 7, 8, 30), false, 3);
        assert_eq!(
            shown(&runs),
            vec![
                "Sat, Mar 7, 8:30 AM ET",
                "Sun, Mar 8, 8:30 AM ET",
                "Mon, Mar 9, 8:30 AM ET",
            ]
        );
        assert_eq!(runs[1] - runs[0], chrono::Duration::hours(23));
    }

    #[test]
    fn a_wrong_cadence_shows_in_the_upcoming_runs() {
        // "Every minute" where "every month" was meant reads as minutes apart.
        let first = et(2026, 12, 1, 9, 0);
        assert_eq!(
            shown(&super::upcoming_runs("* * * * *", &first, false, 3)),
            vec![
                "Tue, Dec 1, 9:00 AM ET",
                "Tue, Dec 1, 9:01 AM ET",
                "Tue, Dec 1, 9:02 AM ET"
            ]
        );
        assert_eq!(
            shown(&super::upcoming_runs("0 9 1 * *", &first, false, 3)),
            vec![
                "Tue, Dec 1, 9:00 AM ET",
                "Jan 1, 2027, 9:00 AM ET",
                "Feb 1, 2027, 9:00 AM ET"
            ]
        );
    }

    #[test]
    fn a_one_time_schedule_has_its_single_time() {
        let first = et(2026, 11, 20, 9, 15);
        let runs = super::upcoming_runs("15 9 20 11 *", &first, true, 3);
        assert_eq!(shown(&runs), vec!["Fri, Nov 20, 9:15 AM ET"]);
        assert_eq!(super::upcoming_runs("0 9 * * *", &first, false, 1).len(), 1);
    }
}

/// The next run of `cron_expr` on the wall clock of a stored zone `tz`
/// (`goal_schedules.tz`). `local` is exactly [`compute_next_run`]. In a
/// named zone a fixed-time run that falls in the spring-forward gap runs at
/// the first instant after the gap (8:30 in a 2:00-3:00 gap runs at 3:00),
/// and one in the fall-back hour runs once, at the earlier instant.
pub fn compute_next_run_in(cron_expr: &str, tz: &str) -> anyhow::Result<DateTime<Utc>> {
    if tz.trim() == LOCAL_TZ {
        return compute_next_run(cron_expr);
    }
    next_run_after_in(cron_expr, tz, Utc::now())
}

/// The first run of `cron_expr` after `after`, on the wall clock of the
/// stored zone `tz` (see [`compute_next_run_in`]).
pub fn next_run_after_in(
    cron_expr: &str,
    tz: &str,
    after: DateTime<Utc>,
) -> anyhow::Result<DateTime<Utc>> {
    fn next<Z: TimeZone>(
        cron: &Cron,
        cron_expr: &str,
        after: DateTime<Z>,
    ) -> anyhow::Result<DateTime<Utc>> {
        cron.find_next_occurrence(&after, false)
            .map(|at| at.with_timezone(&Utc))
            .map_err(|e| anyhow::anyhow!("No next occurrence for '{}': {}", cron_expr, e))
    }
    let cron: Cron = cron_expr
        .parse()
        .map_err(|e| anyhow::anyhow!("Failed to parse cron '{}': {}", cron_expr, e))?;
    match ScheduleZone::from_stored(tz)? {
        ScheduleZone::Owner => next(&cron, cron_expr, after.with_timezone(&Local)),
        ScheduleZone::Fixed(offset) => next(&cron, cron_expr, after.with_timezone(&offset)),
        ScheduleZone::Named(zone) => next(&cron, cron_expr, after.with_timezone(&zone)),
    }
}

/// [`upcoming_runs`] for a schedule stored in zone `tz`, computed on that
/// zone's wall clock and shown on the owner's.
pub fn upcoming_runs_in(
    cron_expr: &str,
    tz: &str,
    first: DateTime<Utc>,
    one_shot: bool,
    count: usize,
) -> Vec<DateTime<Local>> {
    fn runs<Z: TimeZone>(
        cron_expr: &str,
        first: DateTime<Z>,
        one_shot: bool,
        count: usize,
    ) -> Vec<DateTime<Local>> {
        upcoming_runs(cron_expr, &first, one_shot, count)
            .iter()
            .map(|at| at.with_timezone(&Local))
            .collect()
    }
    match ScheduleZone::from_stored(tz) {
        Ok(ScheduleZone::Fixed(offset)) => {
            runs(cron_expr, first.with_timezone(&offset), one_shot, count)
        }
        Ok(ScheduleZone::Named(zone)) => {
            runs(cron_expr, first.with_timezone(&zone), one_shot, count)
        }
        Ok(ScheduleZone::Owner) | Err(_) => {
            runs(cron_expr, first.with_timezone(&Local), one_shot, count)
        }
    }
}

/// [`catch_up_window`] for a schedule stored in zone `tz`.
pub fn catch_up_window_in(
    cron_expr: &str,
    tz: &str,
    due: DateTime<Utc>,
) -> Option<chrono::Duration> {
    if tz.trim() == LOCAL_TZ {
        return catch_up_window(cron_expr, due);
    }
    let after = next_run_after_in(cron_expr, tz, due).ok()?;
    Some(window_for_period(after - due))
}

fn window_for_period(period: chrono::Duration) -> chrono::Duration {
    (period / 2)
        .max(chrono::Duration::minutes(5))
        .min(chrono::Duration::hours(2))
}

/// How late a recurring run may start and still be worth starting: half its
/// period, between 5 minutes and 2 hours. A daily 9:00 AM digest the daemon
/// was off for is not sent at 11 PM; an hourly check missed by a few
/// minutes still runs, and a busy daemon's slow tick never skips one. `None` when the cron has no later occurrence.
pub fn catch_up_window(cron_expr: &str, due: DateTime<Utc>) -> Option<chrono::Duration> {
    let cron: Cron = cron_expr.parse().ok()?;
    let due_local = due.with_timezone(&Local);
    let after = cron.find_next_occurrence(&due_local, false).ok()?;
    Some(window_for_period(after.with_timezone(&Utc) - due))
}

#[cfg(test)]
mod catch_up_tests {
    use super::{catch_up_window, compute_next_run};
    use chrono::{Duration, Utc};

    #[test]
    fn a_late_run_may_start_within_half_its_period_between_5_minutes_and_2_hours() {
        let window = |cron: &str| catch_up_window(cron, compute_next_run(cron).unwrap());
        assert_eq!(window("0 9 * * *"), Some(Duration::hours(2)));
        assert_eq!(window("0 * * * *"), Some(Duration::minutes(30)));
        assert_eq!(window("* * * * *"), Some(Duration::minutes(5)));
        assert_eq!(catch_up_window("not a cron", Utc::now()), None);
    }
}

/// Human-readable system timezone label used in schedule confirmations: the
/// same short label every owner-facing time carries ([`owner_time`]).
pub fn system_timezone_display() -> String {
    owner_zone_label(Local::now().offset().local_minus_utc())
}

/// The IANA name of the owner's time zone. The daemon runs on the owner's
/// machine, so the owner's zone is the system one: `TZ` when it names a zone,
/// else the system setting.
fn owner_zone_name() -> Option<String> {
    static NAME: LazyLock<Option<String>> = LazyLock::new(|| {
        std::env::var("TZ")
            .ok()
            .map(|tz| tz.trim().trim_start_matches(':').to_string())
            .filter(|tz| tz.contains('/') || tz.eq_ignore_ascii_case("UTC"))
            .or_else(|| iana_time_zone::get_timezone().ok())
    });
    NAME.clone()
}

/// The owner's zone as an IANA zone, when its name is one and it keeps the
/// system clock's current offset (a `TZ` the name lookup misread is never
/// used over the clock itself).
fn owner_tz() -> Option<chrono_tz::Tz> {
    let tz = owner_zone_name()?.parse::<chrono_tz::Tz>().ok()?;
    let now = Utc::now();
    (now.with_timezone(&tz).offset().fix() == now.with_timezone(&Local).offset().fix())
        .then_some(tz)
}

/// The owner's zone as a short label at a given UTC offset.
pub fn owner_zone_label(offset_secs: i32) -> String {
    zone_label(owner_zone_name().as_deref(), offset_secs)
}

/// A short label for a zone: the everyday name for zones people say by
/// name ("ET", "PT"), else the offset ("UTC+5:30"). One label per zone all
/// year, so a time never reads "EST" in one message and "UTC-05:00" in the
/// next.
pub fn zone_label(iana: Option<&str>, offset_secs: i32) -> String {
    const NAMED: &[(&str, &str)] = &[
        ("America/New_York", "ET"),
        ("America/Detroit", "ET"),
        ("America/Toronto", "ET"),
        ("America/Indiana/Indianapolis", "ET"),
        ("America/Kentucky/Louisville", "ET"),
        ("US/Eastern", "ET"),
        ("America/Chicago", "CT"),
        ("America/Winnipeg", "CT"),
        ("US/Central", "CT"),
        ("America/Denver", "MT"),
        ("America/Edmonton", "MT"),
        ("America/Boise", "MT"),
        ("America/Phoenix", "MT"),
        ("US/Mountain", "MT"),
        ("America/Los_Angeles", "PT"),
        ("America/Vancouver", "PT"),
        ("US/Pacific", "PT"),
        ("America/Anchorage", "AKT"),
        ("Pacific/Honolulu", "HT"),
        ("UTC", "UTC"),
        ("Etc/UTC", "UTC"),
        ("Etc/GMT", "UTC"),
    ];
    if let Some(label) = iana.and_then(|name| {
        NAMED
            .iter()
            .find(|(zone, _)| *zone == name)
            .map(|(_, label)| *label)
    }) {
        return label.to_string();
    }
    if offset_secs == 0 {
        return "UTC".to_string();
    }
    let sign = if offset_secs < 0 { '-' } else { '+' };
    let minutes = offset_secs.unsigned_abs() / 60;
    match minutes % 60 {
        0 => format!("UTC{sign}{}", minutes / 60),
        rest => format!("UTC{sign}{}:{rest:02}", minutes / 60),
    }
}

/// A time as the owner reads it, in their zone and one format: "2:39 PM ET"
/// today, "Thu, Nov 13, 2:39 PM ET" another day this year, and
/// "Jan 4, 2027, 2:39 PM ET" another year. Tool outputs keep the RFC 3339
/// value beside it where the model needs one.
pub fn owner_time<Tz: TimeZone>(at: &DateTime<Tz>) -> String {
    let at = at.with_timezone(&Local);
    let label = owner_zone_label(at.offset().local_minus_utc());
    format_owner_time(&at.fixed_offset(), &Local::now().fixed_offset(), &label)
}

/// A moment in full, in the owner's zone, for the model's prompt: the
/// weekday and date it needs plus the same clock and zone label as
/// [`owner_time`] ("Thursday, November 12, 2026, 2:39 PM ET"), so replies
/// speak the owner's time instead of UTC.
pub fn owner_date_time_long<Tz: TimeZone>(at: &DateTime<Tz>) -> String {
    let at = at.with_timezone(&Local);
    let label = owner_zone_label(at.offset().local_minus_utc());
    format_owner_date_time_long(&at.fixed_offset(), &label)
}

/// Testable core of [`owner_date_time_long`]: `at` in the owner's zone.
pub fn format_owner_date_time_long(at: &DateTime<FixedOffset>, zone_label: &str) -> String {
    format!(
        "{}, {} {zone_label}",
        at.format("%A, %B %-d, %Y"),
        at.format("%-I:%M %p")
    )
}

/// [`owner_time`] for an RFC 3339 timestamp; `None` when it does not parse.
pub fn owner_time_rfc3339(value: &str) -> Option<String> {
    DateTime::parse_from_rfc3339(value)
        .ok()
        .map(|at| owner_time(&at))
}

/// Testable core of [`owner_time`]: `at` and `now` in the owner's zone.
pub fn format_owner_time(
    at: &DateTime<FixedOffset>,
    now: &DateTime<FixedOffset>,
    zone_label: &str,
) -> String {
    let clock = at.format("%-I:%M %p");
    if at.date_naive() == now.with_timezone(at.offset()).date_naive() {
        format!("{clock} {zone_label}")
    } else if at.year() == now.year() {
        format!("{}, {clock} {zone_label}", at.format("%a, %b %-d"))
    } else {
        format!("{}, {clock} {zone_label}", at.format("%b %-d, %Y"))
    }
}

/// Render a run time the way a person would say it: "today at 1:46 PM
/// (in 1 minute)", "tomorrow at 9:00 AM", "Friday at 9:00 AM", or
/// "on Jul 15 at 9:00 AM" for dates further out.
pub fn humanize_run_time(dt: DateTime<Local>) -> String {
    humanize_run_time_from(dt, Local::now())
}

/// Testable core of [`humanize_run_time`] with an explicit "now".
pub fn humanize_run_time_from(dt: DateTime<Local>, now: DateTime<Local>) -> String {
    let time = dt.format("%-I:%M %p").to_string();
    let days_ahead = (dt.date_naive() - now.date_naive()).num_days();
    match days_ahead {
        0 => {
            let mins = (dt - now).num_minutes();
            if (1..60).contains(&mins) {
                let unit = if mins == 1 { "minute" } else { "minutes" };
                format!("today at {} (in {} {})", time, mins, unit)
            } else {
                format!("today at {}", time)
            }
        }
        1 => format!("tomorrow at {}", time),
        2..=6 => format!("{} at {}", dt.format("%A"), time),
        _ if dt.year() == now.year() => format!("on {} at {}", dt.format("%b %-d"), time),
        _ => format!("on {} at {}", dt.format("%b %-d, %Y"), time),
    }
}

/// Heuristic: whether a cron expression looks like a one-shot absolute schedule.
///
/// This is intentionally conservative and should only be used as a fallback when
/// higher-confidence intent classification data is unavailable.
pub fn is_one_shot_schedule(cron_expr: &str) -> bool {
    let parts: Vec<&str> = cron_expr.split_whitespace().collect();
    if parts.len() != 5 {
        return false;
    }

    let dom = parts[2];
    let month = parts[3];
    dom != "*" && month != "*" && !dom.contains('/') && !month.contains('/')
}

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

    #[test]
    fn the_prompt_gets_the_full_date_in_the_owners_zone() {
        let eastern = FixedOffset::west_opt(5 * 3600).unwrap();
        let at = eastern.with_ymd_and_hms(2026, 11, 12, 13, 57, 0).unwrap();
        assert_eq!(
            format_owner_date_time_long(&at, "ET"),
            "Thursday, November 12, 2026, 1:57 PM ET"
        );
    }

    #[test]
    fn owner_time_is_one_local_format_with_one_zone_label() {
        let eastern = FixedOffset::west_opt(5 * 3600).unwrap();
        let now = eastern.with_ymd_and_hms(2026, 11, 12, 9, 0, 0).unwrap();
        let today = eastern.with_ymd_and_hms(2026, 11, 12, 14, 39, 0).unwrap();
        let tomorrow = eastern.with_ymd_and_hms(2026, 11, 13, 14, 39, 0).unwrap();
        let next_year = eastern.with_ymd_and_hms(2027, 1, 4, 6, 0, 0).unwrap();
        assert_eq!(format_owner_time(&today, &now, "ET"), "2:39 PM ET");
        assert_eq!(
            format_owner_time(&tomorrow, &now, "ET"),
            "Fri, Nov 13, 2:39 PM ET"
        );
        assert_eq!(
            format_owner_time(&next_year, &now, "ET"),
            "Jan 4, 2027, 6:00 AM ET"
        );
    }

    #[test]
    fn zone_label_names_common_zones_and_falls_back_to_the_offset() {
        assert_eq!(zone_label(Some("America/New_York"), -5 * 3600), "ET");
        // The label does not change with daylight saving time.
        assert_eq!(zone_label(Some("America/New_York"), -4 * 3600), "ET");
        assert_eq!(zone_label(Some("America/Los_Angeles"), -8 * 3600), "PT");
        assert_eq!(zone_label(Some("Asia/Kolkata"), 19_800), "UTC+5:30");
        assert_eq!(zone_label(Some("Europe/Berlin"), 3600), "UTC+1");
        assert_eq!(zone_label(None, -5 * 3600), "UTC-5");
        assert_eq!(zone_label(None, 0), "UTC");
    }

    #[test]
    fn test_humanize_run_time() {
        let now = Local.with_ymd_and_hms(2026, 6, 11, 13, 45, 0).unwrap();
        let in_1m = Local.with_ymd_and_hms(2026, 6, 11, 13, 46, 0).unwrap();
        assert_eq!(
            humanize_run_time_from(in_1m, now),
            "today at 1:46 PM (in 1 minute)"
        );
        let later_today = Local.with_ymd_and_hms(2026, 6, 11, 18, 0, 0).unwrap();
        assert_eq!(humanize_run_time_from(later_today, now), "today at 6:00 PM");
        let tomorrow = Local.with_ymd_and_hms(2026, 6, 12, 9, 0, 0).unwrap();
        assert_eq!(humanize_run_time_from(tomorrow, now), "tomorrow at 9:00 AM");
        // 2026-06-15 is a Monday, 4 days out
        let monday = Local.with_ymd_and_hms(2026, 6, 15, 9, 0, 0).unwrap();
        assert_eq!(humanize_run_time_from(monday, now), "Monday at 9:00 AM");
        let next_month = Local.with_ymd_and_hms(2026, 7, 15, 9, 0, 0).unwrap();
        assert_eq!(
            humanize_run_time_from(next_month, now),
            "on Jul 15 at 9:00 AM"
        );
        let next_year = Local.with_ymd_and_hms(2027, 1, 5, 9, 0, 0).unwrap();
        assert_eq!(
            humanize_run_time_from(next_year, now),
            "on Jan 5, 2027 at 9:00 AM"
        );
    }

    #[test]
    fn test_parse_schedule_keywords() {
        assert_eq!(parse_schedule("hourly").unwrap(), "0 * * * *");
        assert_eq!(parse_schedule("daily").unwrap(), "0 0 * * *");
        assert_eq!(parse_schedule("weekly").unwrap(), "0 0 * * 0");
        assert_eq!(parse_schedule("monthly").unwrap(), "0 0 1 * *");
    }

    #[test]
    fn test_parse_schedule_every_minutes() {
        assert_eq!(parse_schedule("every 5m").unwrap(), "*/5 * * * *");
        assert_eq!(parse_schedule("every 15 minutes").unwrap(), "*/15 * * * *");
        assert_eq!(parse_schedule("every 1 min").unwrap(), "*/1 * * * *");
        assert_eq!(parse_schedule("each 5 minutes").unwrap(), "*/5 * * * *");
        assert_eq!(parse_schedule("each 5m").unwrap(), "*/5 * * * *");
        assert_eq!(parse_schedule("1 each 5m").unwrap(), "*/5 * * * *");
    }

    #[test]
    fn test_parse_schedule_every_hours() {
        assert_eq!(parse_schedule("every 2h").unwrap(), "0 */2 * * *");
        assert_eq!(parse_schedule("every 4 hours").unwrap(), "0 */4 * * *");
        assert_eq!(parse_schedule("each 6h").unwrap(), "0 */6 * * *");
    }

    #[test]
    fn test_parse_schedule_daily_at() {
        assert_eq!(parse_schedule("daily at 9am").unwrap(), "0 9 * * *");
        assert_eq!(parse_schedule("daily at 14:30").unwrap(), "30 14 * * *");
        assert_eq!(parse_schedule("daily at 2pm").unwrap(), "0 14 * * *");
        assert_eq!(parse_schedule("daily at 2:30pm").unwrap(), "30 14 * * *");
        assert_eq!(parse_schedule("daily at 12am").unwrap(), "0 0 * * *");
        assert_eq!(
            parse_schedule("daily at 6am, 12pm, 6pm").unwrap(),
            "0 6,12,18 * * *"
        );
        assert_eq!(
            parse_schedule("daily at 6, 12 and 6 pm").unwrap(),
            "0 6,12,18 * * *"
        );
        assert_eq!(
            parse_schedule("every day at 6, 12 and 6 pm").unwrap(),
            "0 6,12,18 * * *"
        );
        assert_eq!(
            parse_schedule("everyday at 6am, 12pm, 6pm").unwrap(),
            "0 6,12,18 * * *"
        );
    }

    #[test]
    fn test_parse_schedule_weekdays() {
        assert_eq!(parse_schedule("weekdays at 8:30").unwrap(), "30 8 * * 1-5");
        assert_eq!(parse_schedule("weekdays at 9am").unwrap(), "0 9 * * 1-5");
        assert_eq!(
            parse_schedule("weekdays at 9am and 5pm").unwrap(),
            "0 9,17 * * 1-5"
        );
        assert_eq!(
            parse_schedule("every weekday at 9am and 5pm").unwrap(),
            "0 9,17 * * 1-5"
        );
    }

    #[test]
    fn test_parse_schedule_weekends() {
        assert_eq!(parse_schedule("weekends at 10am").unwrap(), "0 10 * * 0,6");
        assert_eq!(
            parse_schedule("weekends at noon, 6pm").unwrap(),
            "0 12,18 * * 0,6"
        );
        assert_eq!(
            parse_schedule("every weekend at noon, 6pm").unwrap(),
            "0 12,18 * * 0,6"
        );
    }

    #[test]
    fn test_parse_schedule_cron_passthrough() {
        assert_eq!(parse_schedule("0 9 * * 1-5").unwrap(), "0 9 * * 1-5");
        assert_eq!(parse_schedule("*/5 * * * *").unwrap(), "*/5 * * * *");
    }

    #[test]
    fn test_parse_schedule_tomorrow_at() {
        let cron = parse_schedule("tomorrow at 9am").unwrap();
        let parts: Vec<&str> = cron.split_whitespace().collect();
        assert_eq!(parts.len(), 5);
        assert_eq!(parts[0], "0");
        assert_eq!(parts[1], "9");
        assert_eq!(parts[4], "*");
    }

    #[test]
    fn test_parse_schedule_day_time_with_numeric_offset() {
        let offset = FixedOffset::west_opt(5 * 3600).unwrap();
        let now_there = Local::now().with_timezone(&offset);
        let target = now_there + chrono::Duration::minutes(30);
        let day_kw = if target.date_naive() == now_there.date_naive() {
            "today"
        } else {
            "tomorrow"
        };
        let input = format!("{} at {} -05:00.", day_kw, target.format("%I:%M%P"));
        let cron = parse_schedule(&input).unwrap();
        let local = target.with_timezone(&Local);
        assert_eq!(
            cron,
            format!(
                "{} {} {} {} *",
                local.minute(),
                local.hour(),
                local.day(),
                local.month()
            )
        );
    }

    #[test]
    fn a_zone_abbreviation_is_refused_not_mapped_to_a_fixed_offset() {
        for input in [
            "tomorrow at 9am EST",
            "tomorrow at 9am PST",
            "tomorrow at 9am CET",
        ] {
            let error = parse_schedule(input).unwrap_err().to_string();
            assert!(error.contains("numeric offset"), "{input}: {error}");
        }
    }

    #[test]
    fn test_parse_schedule_named_month_with_ordinal() {
        let cron = parse_schedule("on March 5th at 3pm").unwrap();
        let parts: Vec<&str> = cron.split_whitespace().collect();
        assert_eq!(parts.len(), 5);
        assert_eq!(parts[0], "0");
        assert_eq!(parts[1], "15");
        assert_eq!(parts[2], "5");
        assert_eq!(parts[3], "3");
    }

    #[test]
    fn test_is_one_shot_schedule() {
        assert!(is_one_shot_schedule("15 9 12 3 *"));
        assert!(!is_one_shot_schedule("0 */6 * * *"));
        assert!(!is_one_shot_schedule("0 9 * * 1-5"));
    }

    #[test]
    fn test_parse_schedule_invalid() {
        assert!(parse_schedule("never").is_err());
        assert!(parse_schedule("every 0m").is_err());
        assert!(parse_schedule("daily at 25:00").is_err());
    }

    #[test]
    fn test_parse_schedule_zone_numeric_utc_owner_and_iana() {
        let seconds = |token: &str| match parse_schedule_zone(token).unwrap() {
            ScheduleZone::Fixed(offset) => offset.local_minus_utc(),
            other => panic!("{token}: {other:?}"),
        };
        assert_eq!(seconds("+05:30"), 5 * 3600 + 30 * 60);
        assert_eq!(seconds("-0400"), -4 * 3600);
        assert_eq!(seconds("UTC"), 0);
        assert_eq!(seconds("UTC-03:00"), -3 * 3600);
        // The owner's own zone named is that named zone (same rules), so a
        // schedule that names it keeps it if the owner's clock moves.
        if let Some(zone) = owner_zone_name().filter(|zone| zone.contains('/')) {
            match parse_schedule_zone(&zone).unwrap() {
                ScheduleZone::Named(tz) => assert!(tz.name().eq_ignore_ascii_case(&zone)),
                other => panic!("{zone}: {other:?}"),
            }
        }
        // Any other IANA zone, exact or in another letter case.
        for (token, name) in [
            ("Asia/Tokyo", "Asia/Tokyo"),
            ("america/guayaquil", "America/Guayaquil"),
            ("EUROPE/MADRID", "Europe/Madrid"),
        ] {
            match parse_schedule_zone(token).unwrap() {
                ScheduleZone::Named(tz) => assert_eq!(tz.name(), name),
                other => panic!("{token}: {other:?}"),
            }
        }
        assert!(parse_schedule_zone("EDT").is_err());
    }

    /// Noon UTC on a fixed day, as the owner's clock: a fixed "now" so the
    /// zone tests do not depend on the date they run.
    fn fixed_now(y: i32, mo: u32, d: u32) -> DateTime<Local> {
        Utc.with_ymd_and_hms(y, mo, d, 12, 0, 0)
            .unwrap()
            .with_timezone(&Local)
    }

    fn utc(input: &str, now: DateTime<Local>) -> String {
        parse_absolute_datetime(input, now)
            .unwrap()
            .unwrap_or_else(|| panic!("{input}: not read"))
            .with_timezone(&Utc)
            .to_rfc3339()
    }

    #[test]
    fn a_named_zone_follows_its_daylight_saving_rules() {
        let now = fixed_now(2030, 1, 1);
        // Madrid is UTC+1 in winter and UTC+2 in summer.
        assert_eq!(
            utc("2030-01-15 09:00 Europe/Madrid", now),
            "2030-01-15T08:00:00+00:00"
        );
        assert_eq!(
            utc("2030-07-15 09:00 Europe/Madrid", now),
            "2030-07-15T07:00:00+00:00"
        );
        assert_eq!(
            utc("July 15, 2030 at 9am Europe/Madrid", now),
            "2030-07-15T07:00:00+00:00"
        );
        // Guayaquil keeps UTC-5 all year.
        assert_eq!(
            utc("2030-01-15 09:00 America/Guayaquil", now),
            "2030-01-15T14:00:00+00:00"
        );
        assert_eq!(
            utc("2030-07-15 09:00 America/Guayaquil", now),
            "2030-07-15T14:00:00+00:00"
        );
        assert_eq!(
            utc("2030-07-15 09:00 Asia/Tokyo", now),
            "2030-07-15T00:00:00+00:00"
        );
        // A fall-back time the clocks pass twice is the earlier one (CEST).
        assert_eq!(
            utc("2030-10-27 02:30 Europe/Madrid", now),
            "2030-10-27T00:30:00+00:00"
        );
        // The owner's zone and numeric offsets still read as before.
        assert_eq!(
            utc("2030-07-15 09:00 -05:00", now),
            "2030-07-15T14:00:00+00:00"
        );
        assert_eq!(
            utc("2030-07-15 09:00 UTC", now),
            "2030-07-15T09:00:00+00:00"
        );
    }

    #[test]
    fn tomorrow_at_a_time_in_a_named_zone_uses_that_zones_calendar_and_offset() {
        let resolve = |input: &str, now: DateTime<Local>| {
            parse_day_time_with_optional_timezone(input, now)
                .unwrap()
                .unwrap()
                .with_timezone(&Utc)
                .to_rfc3339()
        };
        assert_eq!(
            resolve("tomorrow at 9am Europe/Madrid", fixed_now(2030, 1, 14)),
            "2030-01-15T08:00:00+00:00"
        );
        assert_eq!(
            resolve("tomorrow at 9am Europe/Madrid", fixed_now(2030, 7, 14)),
            "2030-07-15T07:00:00+00:00"
        );
        assert_eq!(
            resolve("tomorrow at 9am America/Guayaquil", fixed_now(2030, 7, 14)),
            "2030-07-15T14:00:00+00:00"
        );
        // 12:00 UTC on the 14th is already the 14th at 21:00 in Tokyo.
        assert_eq!(
            resolve("tomorrow at 9:30am Asia/Tokyo", fixed_now(2030, 7, 14)),
            "2030-07-15T00:30:00+00:00"
        );
    }

    #[test]
    fn a_time_the_clocks_skip_in_a_named_zone_is_an_error_naming_it() {
        let now = fixed_now(2030, 1, 1);
        let error = parse_absolute_datetime("2030-03-31 02:30 Europe/Madrid", now)
            .unwrap_err()
            .to_string();
        assert!(error.contains("2030-03-31 02:30"), "{error}");
        assert!(error.contains("Europe/Madrid"), "{error}");
        assert!(error.contains("daylight saving"), "{error}");
        let error = parse_day_time_with_optional_timezone(
            "tomorrow at 2:30am Europe/Madrid",
            fixed_now(2030, 3, 30),
        )
        .unwrap_err()
        .to_string();
        assert!(error.contains("does not exist"), "{error}");
    }

    fn at(y: i32, mo: u32, d: u32, h: u32, mi: u32) -> DateTime<Utc> {
        Utc.with_ymd_and_hms(y, mo, d, h, mi, 0).unwrap()
    }

    /// The runs after `after` of a schedule stored in `tz`, in UTC.
    fn runs_after(cron: &str, tz: &str, after: DateTime<Utc>, count: usize) -> Vec<String> {
        let first = next_run_after_in(cron, tz, after).unwrap();
        upcoming_runs_in(cron, tz, first, false, count)
            .iter()
            .map(|run| run.with_timezone(&Utc).format("%a %m-%d %H:%M").to_string())
            .collect()
    }

    #[test]
    fn a_recurring_schedule_with_a_zone_keeps_that_zone() {
        let parsed = |input: &str| parse_schedule_with_zone(input).unwrap();
        assert_eq!(
            parsed("every weekday at 8am Europe/Madrid"),
            ParsedSchedule {
                cron: "0 8 * * 1-5".to_string(),
                tz: "Europe/Madrid".to_string()
            }
        );
        let daily = parsed("daily at 9:30 america/guayaquil");
        assert_eq!(daily.cron, "30 9 * * *");
        assert_eq!(daily.tz, "America/Guayaquil");
        assert_eq!(parsed("0 9 * * 1-5 Asia/Tokyo").tz, "Asia/Tokyo");
        assert_eq!(parsed("weekdays at 9am +05:30").tz, "+05:30");
        assert_eq!(parsed("daily at 7am UTC").tz, "UTC");
        assert_eq!(parsed("every weekday at 8am").tz, LOCAL_TZ);
        let error = parse_schedule_with_zone("every weekday at 8am EST")
            .unwrap_err()
            .to_string();
        assert!(error.contains("America/New_York"), "{error}");
        assert!(parse_schedule_with_zone("every weekday at 8am Mars/Olympus").is_err());
        assert!(parse_schedule_with_zone("whenever the moon is full").is_err());
    }

    #[test]
    fn weekday_runs_in_madrid_follow_its_clock_across_both_switches() {
        // Madrid moves to CEST on Sun Mar 31 2030 and back on Sun Oct 27.
        assert_eq!(
            runs_after("0 8 * * 1-5", "Europe/Madrid", at(2030, 3, 27, 12, 0), 3),
            vec!["Thu 03-28 07:00", "Fri 03-29 07:00", "Mon 04-01 06:00"]
        );
        assert_eq!(
            runs_after("0 8 * * 1-5", "Europe/Madrid", at(2030, 10, 24, 12, 0), 3),
            vec!["Fri 10-25 06:00", "Mon 10-28 07:00", "Tue 10-29 07:00"]
        );
        // A run in the skipped hour runs at the first instant after it; one
        // in the repeated hour runs once, at the earlier instant.
        assert_eq!(
            runs_after("30 2 * * *", "Europe/Madrid", at(2030, 3, 30, 12, 0), 2),
            vec!["Sun 03-31 01:00", "Mon 04-01 00:30"]
        );
        assert_eq!(
            runs_after("30 2 * * *", "Europe/Madrid", at(2030, 10, 26, 12, 0), 2),
            vec!["Sun 10-27 00:30", "Mon 10-28 01:30"]
        );
    }

    #[test]
    fn daily_runs_in_guayaquil_keep_one_offset_all_year() {
        assert_eq!(
            runs_after("30 9 * * *", "America/Guayaquil", at(2030, 3, 30, 0, 0), 2),
            vec!["Sat 03-30 14:30", "Sun 03-31 14:30"]
        );
        assert_eq!(
            runs_after("30 9 * * *", "America/Guayaquil", at(2030, 10, 26, 0, 0), 2),
            vec!["Sat 10-26 14:30", "Sun 10-27 14:30"]
        );
        assert_eq!(
            runs_after("0 9 * * *", "+05:30", at(2030, 7, 1, 0, 0), 1),
            vec!["Mon 07-01 03:30"]
        );
    }

    #[test]
    fn a_local_schedule_is_computed_exactly_as_before() {
        for cron in ["0 9 * * *", "0 8 * * 1-5", "15 9 12 3 *"] {
            assert_eq!(
                compute_next_run_in(cron, LOCAL_TZ).unwrap(),
                compute_next_run(cron).unwrap()
            );
            let due = at(2030, 7, 1, 9, 0);
            assert_eq!(
                catch_up_window_in(cron, LOCAL_TZ, due),
                catch_up_window(cron, due)
            );
        }
        assert_eq!(
            ScheduleZone::from_stored("local").unwrap(),
            ScheduleZone::Owner
        );
    }

    #[test]
    fn a_stored_zone_this_cannot_read_is_an_error_not_the_owners_clock() {
        for tz in ["Mars/Olympus", "EST", ""] {
            assert!(ScheduleZone::from_stored(tz).is_err(), "{tz:?}");
            assert!(compute_next_run_in("0 9 * * *", tz).is_err(), "{tz:?}");
        }
        for tz in ["Europe/Madrid", "UTC", "+05:30", "-04:00"] {
            assert_eq!(ScheduleZone::from_stored(tz).unwrap().stored(), tz);
        }
    }

    #[test]
    fn an_abbreviation_or_unknown_zone_after_a_time_is_refused_with_a_hint() {
        let now = fixed_now(2030, 1, 1);
        let error = parse_absolute_datetime("2030-07-15 09:00 EST", now)
            .unwrap_err()
            .to_string();
        assert!(error.contains("America/New_York"), "{error}");
        let error = parse_schedule("tomorrow at 9am CET")
            .unwrap_err()
            .to_string();
        assert!(error.contains("Europe/Madrid"), "{error}");
        // IANA's legacy region-less names are abbreviations too.
        assert!(parse_schedule_zone("EST").is_err());
        assert!(parse_schedule_zone("Japan").is_err());
        let error = parse_absolute_datetime("2030-07-15 09:00 Europe/Atlantis", now)
            .unwrap_err()
            .to_string();
        assert!(error.contains("not an IANA time zone"), "{error}");
        assert!(parse_schedule("tomorrow at 9am Mars/Olympus").is_err());
    }

    #[test]
    fn test_parse_schedule_relative_duration_phrase() {
        let before = Local::now();
        let cron = parse_schedule("in 1 hour 30 minutes").unwrap();
        let expected = before + chrono::Duration::minutes(90);
        let parts: Vec<&str> = cron.split_whitespace().collect();
        assert_eq!(parts[1], expected.hour().to_string());
        for input in [
            "in 2h30m",
            "after 90 minutes",
            "in 1 hour, 30 min",
            "in 1 day and 2 hours",
            "in 3 weeks",
        ] {
            assert!(parse_schedule(input).is_ok(), "{input}");
        }
    }

    #[test]
    fn a_relative_delay_it_cannot_fully_read_is_refused_not_guessed() {
        // "2 months" once read as 2 minutes ("m"), and "at 9am" was dropped.
        for input in [
            "in 2 months",
            "in 10 days at 9am",
            "in 1 year",
            "in 5 minutes or so",
            "after 0 minutes",
        ] {
            let error = parse_schedule(input).unwrap_err().to_string();
            assert!(error.contains("not a delay"), "{input}: {error}");
        }
    }

    #[test]
    fn test_parse_schedule_absolute_datetime_iso() {
        let target = Local::now() + chrono::Duration::hours(2);
        let input = target.format("%Y-%m-%d %H:%M").to_string();
        let cron = parse_schedule(&input).unwrap();
        let parts: Vec<&str> = cron.split_whitespace().collect();
        assert_eq!(parts.len(), 5);
        assert_eq!(parts[0], target.minute().to_string());
        assert_eq!(parts[1], target.hour().to_string());
    }

    #[test]
    fn test_system_timezone_display_non_empty() {
        assert!(!system_timezone_display().is_empty());
    }

    #[test]
    fn test_compute_next_run() {
        let next = compute_next_run("* * * * *").unwrap();
        assert!(next > Utc::now());
    }

    #[test]
    fn test_compute_next_run_local() {
        let next = compute_next_run_local("* * * * *").unwrap();
        assert!(next > Local::now());
    }

    #[test]
    fn test_parse_schedule_specific_days() {
        assert_eq!(
            parse_schedule("Monday and Friday at 3pm").unwrap(),
            "0 15 * * 1,5"
        );
        assert_eq!(
            parse_schedule("every Monday and Friday at 9am").unwrap(),
            "0 9 * * 1,5"
        );
        assert_eq!(
            parse_schedule("every Tuesday, Thursday at 2pm").unwrap(),
            "0 14 * * 2,4"
        );
    }
}