tztr 0.2.1

Convert timestamps between timezones. Reads from stdin or file, auto-detects timestamp formats, and preserves the original format by default.
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
//! End-to-end CLI tests for the `tztr` binary. Mirrors the CLI section of the
//! Ruby spec (spec/tztr_spec.rs).

use std::io::Write;
use std::process::{Command, Stdio};

fn bin() -> &'static str {
    env!("CARGO_BIN_EXE_tztr")
}

/// A scratch file named for this process, so concurrent test runs (the parity
/// harness builds and runs alongside) cannot collide on it.
fn temp_path(stem: &str) -> std::path::PathBuf {
    std::env::temp_dir().join(format!("{stem}-{}.log", std::process::id()))
}

struct Output {
    stdout: Vec<u8>,
    stderr: String,
    ok: bool,
}

impl Output {
    fn out(&self) -> &str {
        std::str::from_utf8(&self.stdout).expect("stdout is utf-8")
    }
}

/// Run the CLI with args + stdin. `tz` of `None` runs with `TZ` unset.
fn run_tz(input: &[u8], args: &[&str], tz: Option<&str>) -> Output {
    let mut cmd = Command::new(bin());
    cmd.args(args);
    match tz {
        Some(tz) => cmd.env("TZ", tz),
        None => cmd.env_remove("TZ"),
    };
    let mut child = cmd
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    // A run that fails on its arguments exits before reading stdin, so this
    // write races with the child and loses the pipe. That is the case under
    // test, not a failure of it.
    let mut stdin = child.stdin.take().unwrap();
    if let Err(e) = stdin.write_all(input) {
        assert_eq!(
            e.kind(),
            std::io::ErrorKind::BrokenPipe,
            "writing stdin: {e}"
        );
    }
    drop(stdin);
    let out = child.wait_with_output().unwrap();
    Output {
        stdout: out.stdout,
        stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
        ok: out.status.success(),
    }
}

/// Run the CLI with args + stdin under a fixed TZ, returning (stdout, success).
fn run(input: &str, args: &[&str], tz: &str) -> (String, bool) {
    let o = run_tz(input.as_bytes(), args, Some(tz));
    (o.out().to_string(), o.ok)
}

fn stdout(input: &str, args: &[&str]) -> String {
    let (o, ok) = run(input, args, "UTC");
    assert!(ok, "expected success");
    o.trim_end().to_string()
}

/// Run expecting failure, returning the single stderr line (without `\n`).
fn fails(input: &str, args: &[&str]) -> String {
    let o = run_tz(input.as_bytes(), args, Some("UTC"));
    assert!(!o.ok, "expected failure, got stdout {:?}", o.out());
    assert!(o.stdout.is_empty(), "expected no stdout: {:?}", o.out());
    // Only the newline — a trailing space is part of the message for `-t ''`.
    o.stderr.trim_end_matches('\n').to_string()
}

#[test]
fn converts_via_stdin() {
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["-t", "America/Los_Angeles"]),
        "2026-04-03T05:00:00-07:00"
    );
}

#[test]
fn handles_multiline_input() {
    let out = stdout(
        "first 2026-04-03T12:00:00Z\nsecond 2026-04-03T13:00:00Z\n",
        &["-t", "America/Los_Angeles"],
    );
    let lines: Vec<&str> = out.lines().collect();
    assert_eq!(lines[0], "first 2026-04-03T05:00:00-07:00");
    assert_eq!(lines[1], "second 2026-04-03T06:00:00-07:00");
}

#[test]
fn uses_tz_env_as_default_output() {
    let (o, ok) = run("2026-04-03T12:00:00Z\n", &[], "America/New_York");
    assert!(ok);
    assert_eq!(o.trim_end(), "2026-04-03T08:00:00-04:00");
}

#[test]
fn accepts_alias_and_numeric_offset() {
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["-t", "sf"]),
        "2026-04-03T05:00:00-07:00"
    );
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["-t", "-7"]),
        "2026-04-03T05:00:00-07:00"
    );
}

#[test]
fn shows_version() {
    assert_eq!(stdout("", &["-V"]), env!("CARGO_PKG_VERSION"));
}

#[test]
fn shows_help() {
    let out = stdout("", &["-h"]);
    assert!(out.contains("Usage: tztr"));
    assert!(out.contains("Timezone Translator"));
}

#[test]
fn bundles_short_flags() {
    // Bundled short flags expand like Ruby's OptionParser: a value-taking flag
    // consumes the rest of the cluster as its value.
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["-tsf"]),
        "2026-04-03T05:00:00-07:00"
    );
    // boolean + value-taking: -vtsf == -v -t sf (verbose goes to stderr).
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["-vtsf"]),
        "2026-04-03T05:00:00-07:00"
    );
    // -hj == -h -j -> JSON help.
    let help = stdout("", &["-hj"]);
    assert!(serde_json::from_str::<serde_json::Value>(&help).is_ok());
}

#[test]
fn shows_help_as_json() {
    // -h -j emits the option schema as a JSON object (agent-friendly), not text.
    let out = stdout("", &["-h", "-j"]);
    let doc: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(doc["name"], "tztr");
    assert_eq!(doc["version"], env!("CARGO_PKG_VERSION"));

    let opts = doc["options"].as_array().unwrap();
    assert!(opts.iter().any(|o| o["long"] == "--from"));
    assert!(opts.iter().any(|o| o["long"] == "--detect"));

    // Every documented option's long flag also appears in the text help.
    let text = stdout("", &["-h"]);
    for o in opts {
        let long = o["long"].as_str().unwrap();
        assert!(text.contains(long), "text help missing {long}");
    }

    // -h -J emits the same document as a single NDJSON line.
    let nd = stdout("", &["-h", "-J"]);
    assert_eq!(nd.lines().count(), 1);
    let nd_doc: serde_json::Value = serde_json::from_str(&nd).unwrap();
    assert_eq!(nd_doc, doc);
}

#[test]
fn lists_aliases() {
    let out = stdout("", &["-l"]);
    assert!(out.contains("sf"));
    assert!(out.contains("America/Los_Angeles"));
}

#[test]
fn emits_json_array() {
    let out = stdout("from 15:30 UTC to 16:45 UTC", &["-t", "pst", "-j"]);
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v[0]["translated"], "08:30 PDT");
    assert_eq!(v[1]["translated"], "09:45 PDT");
    assert_eq!(v[0]["detected_tz"], "UTC");
}

#[test]
fn emits_ndjson() {
    let out = stdout(
        "2026-04-03T12:00:00Z\n2026-04-03T13:00:00Z\n",
        &["-t", "pst", "-J"],
    );
    let converted: Vec<String> = out
        .lines()
        .map(|l| {
            serde_json::from_str::<serde_json::Value>(l).unwrap()["translated"]
                .as_str()
                .unwrap()
                .to_string()
        })
        .collect();
    assert_eq!(
        converted,
        vec!["2026-04-03T05:00:00-07:00", "2026-04-03T06:00:00-07:00"]
    );
}

#[test]
fn detect_reports_without_translating() {
    let out = stdout("2026-04-03T12:00:00Z", &["--detect"]);
    assert_eq!(out, "2026-04-03T12:00:00Z\tiso\tZ");

    let json = stdout("2026-04-03T12:00:00Z", &["--detect", "-j"]);
    let v: serde_json::Value = serde_json::from_str(&json).unwrap();
    assert!(v[0].get("translated").is_none());
    assert_eq!(v[0]["detected_format"], "iso");
}

#[test]
fn applies_reference_date() {
    assert_eq!(
        stdout("15:30 PST", &["-t", "utc", "-d", "2026-01-15"]),
        "23:30 UTC"
    );
    assert_eq!(
        stdout("15:30 PST", &["-t", "utc", "-d", "January 15, 2026"]),
        "23:30 UTC"
    );
}

#[test]
fn rejects_combining_inplace_with_json() {
    let (out, ok) = run("15:30 PST\n", &["-i", "-j", "/tmp/whatever.txt"], "UTC");
    assert!(!ok);
    assert!(out.is_empty());
}

#[test]
fn aborts_on_unparseable_date() {
    let (_out, ok) = run("15:30 PST\n", &["-d", "not-a-date"], "UTC");
    assert!(!ok);
}

// --- B16: -v discloses what it assumed --------------------------------------

/// `-v` over a bare time, with `$TZ` supplying the source zone.
fn verbose_bare(args: &[&str]) -> Output {
    run_tz(b"15:30\n", args, Some("America/Los_Angeles"))
}

#[test]
fn verbose_discloses_the_assumptions_a_bare_timestamp_forces() {
    let o = verbose_bare(&["-v", "-t", "nyc"]);
    assert!(o.ok, "{}", o.stderr);
    let lines: Vec<&str> = o.stderr.lines().collect();
    assert!(
        lines.contains(&"tztr: from=America/Los_Angeles (implicit, from $TZ) to=America/New_York"),
        "{lines:?}"
    );
    assert!(
        lines
            .iter()
            .any(|l| l.starts_with("tztr: no -d given, assuming ")
                && l.ends_with(" for DST resolution")),
        "{lines:?}"
    );
    assert!(!o.out().contains("tztr:"), "diagnostics leaked to stdout");
}

#[test]
fn verbose_suppresses_the_startup_line_when_the_source_zone_is_implicit() {
    // Otherwise it and the disclosure line say the same thing twice.
    let o = verbose_bare(&["-v", "-t", "nyc"]);
    assert!(
        !o.stderr.contains("tztr: from=America/Los_Angeles to="),
        "{}",
        o.stderr
    );
    // An explicit -f keeps the plain startup line.
    let o = verbose_bare(&["-v", "-f", "utc", "-t", "nyc"]);
    assert!(
        o.stderr.contains("tztr: from=UTC to=America/New_York"),
        "{}",
        o.stderr
    );
    // No -f and no $TZ: the system zone, disclosed as such; with none to be
    // found, the plain startup line.
    let o = run_tz(b"15:30\n", &["-v"], None);
    let expected = match tztr::system_zone() {
        Some(zone) => format!("tztr: from={zone} (implicit, from the system zone) to=UTC"),
        None => "tztr: from=auto to=UTC".to_string(),
    };
    assert!(o.stderr.contains(&expected), "{}", o.stderr);
}

#[test]
fn a_timestamp_with_no_zone_is_read_in_the_system_zone_when_tz_is_unset() {
    let Some(zone) = tztr::system_zone() else {
        return;
    };
    let o = run_tz(b"2026-01-15 12:00:00\n", &["-t", "UTC"], None);
    assert_eq!(
        o.out().trim_end(),
        tztr::translate("2026-01-15 12:00:00", "UTC", Some(&zone), None, None)
    );
}

#[test]
fn verbose_dates_the_dst_assumption_where_the_timestamp_was_written() {
    // A bare time borrows $TZ as its source zone, so "today" is today there,
    // not in the output zone -- the date the conversion actually used.
    let o = verbose_bare(&["-v", "-t", "Pacific/Auckland"]);
    let expected = format!(
        "tztr: no -d given, assuming {} for DST resolution",
        tztr::today_in_zone("America/Los_Angeles")
    );
    assert!(o.stderr.contains(&expected), "{}", o.stderr);
}

#[test]
fn verbose_discloses_the_date_assumption_even_when_the_match_names_a_zone() {
    // `15:30 UTC` into New York still has to assume a date to know whether the
    // answer is EDT or EST -- the DST exposure is on the *output* side, so a
    // match carrying its own zone is not off the hook.
    let o = run_tz(b"15:30 UTC\n", &["-v", "-t", "nyc"], None);
    assert!(o.ok, "{}", o.stderr);
    let expected = format!(
        "tztr: no -d given, assuming {} for DST resolution",
        tztr::today_in_zone("UTC")
    );
    assert!(o.stderr.contains(&expected), "{}", o.stderr);

    // ...and `-d` answers it, so nothing is assumed.
    let o = run_tz(
        b"15:30 UTC\n",
        &["-v", "-t", "nyc", "-d", "2026-01-15"],
        None,
    );
    assert!(!o.stderr.contains("no -d given"), "{}", o.stderr);
    assert_eq!(o.out().trim_end(), "10:30 EST");
}

#[test]
fn verbose_does_not_claim_a_source_zone_the_timestamp_carried_itself() {
    // `15:30 UTC` took its source zone from the timestamp, not from $TZ.
    // Saying otherwise under -v asserts something false, and -v exists to be
    // believed. The date is still assumed, so that line stays.
    let o = verbose_bare(&["-v", "-t", "nyc"]);
    assert!(o.stderr.contains("(implicit, from $TZ)"), "{}", o.stderr);

    let o = run_tz(
        b"15:30 UTC\n",
        &["-v", "-t", "nyc"],
        Some("America/Los_Angeles"),
    );
    assert!(o.ok, "{}", o.stderr);
    assert!(!o.stderr.contains("(implicit, from $TZ)"), "{}", o.stderr);
    assert!(o.stderr.contains("no -d given"), "{}", o.stderr);
}

#[test]
fn verbose_discloses_each_assumption_the_first_time_it_is_made() {
    // The date is assumed on line 1, the source zone only on line 2 — each
    // gets said once, when it first actually happens.
    let o = run_tz(
        b"15:30 UTC\n15:30\n15:30\n",
        &["-v", "-t", "nyc"],
        Some("America/Los_Angeles"),
    );
    assert!(o.ok, "{}", o.stderr);
    assert_eq!(o.stderr.matches("no -d given").count(), 1, "{}", o.stderr);
    assert_eq!(
        o.stderr.matches("(implicit, from $TZ)").count(),
        1,
        "{}",
        o.stderr
    );
    // ...and the date line comes first, having been assumed first.
    let date_at = o.stderr.find("no -d given").unwrap();
    let zone_at = o.stderr.find("(implicit, from $TZ)").unwrap();
    assert!(date_at < zone_at, "{}", o.stderr);
}

#[test]
fn verbose_discloses_under_in_place_too() {
    // -i rewrites the file on the strength of these assumptions, so it has
    // more reason to state them, not less.
    let path = temp_path("tztr-inplace-verbose");
    std::fs::write(&path, "15:30\n").unwrap();
    let o = run_tz(
        b"",
        &["-i", "-v", "-t", "nyc", path.to_str().unwrap()],
        Some("America/Los_Angeles"),
    );
    let written = std::fs::read_to_string(&path).unwrap();
    std::fs::remove_file(&path).unwrap();

    assert!(o.ok, "{}", o.stderr);
    assert!(o.stderr.contains("(implicit, from $TZ)"), "{}", o.stderr);
    assert!(o.stderr.contains("no -d given"), "{}", o.stderr);
    assert_ne!(written, "15:30\n", "file should have been translated");
}

#[test]
fn verbose_stays_quiet_when_nothing_was_assumed() {
    // -f and -d given: both assumptions are the user's, not ours.
    let o = verbose_bare(&["-v", "-f", "pst", "-t", "nyc", "-d", "2026-01-15"]);
    assert!(!o.stderr.contains("implicit"), "{}", o.stderr);
    assert!(!o.stderr.contains("no -d given"), "{}", o.stderr);

    // A timestamp carrying its own zone and date assumes nothing.
    let o = run_tz(
        b"2026-04-03T12:00:00Z\n",
        &["-v", "-t", "nyc"],
        Some("America/Los_Angeles"),
    );
    assert!(!o.stderr.contains("implicit"), "{}", o.stderr);
    assert!(!o.stderr.contains("no -d given"), "{}", o.stderr);
}

#[test]
fn verbose_names_a_mixed_case_zone_it_ignored_once() {
    let o = run_tz(b"15:30 Pst\n16:00 Pst\n", &["-v", "-d", "2026-04-03"], None);
    assert_eq!(
        o.stderr.matches(r#"ignored "Pst""#).count(),
        1,
        "{}",
        o.stderr
    );
    assert!(o.stderr.contains(
        r#"tztr: ignored "Pst": a zone abbreviation is matched in all uppercase or all lowercase"#
    ));

    let o = run_tz(
        "à 15:30 est annulée\n".as_bytes(),
        &["-v", "-d", "2026-04-03"],
        None,
    );
    assert!(!o.stderr.contains("ignored"), "{}", o.stderr);
}

#[test]
fn verbose_discloses_once_and_leaves_structured_stdout_clean() {
    let o = run_tz(
        b"15:30\n16:30\n17:30\n",
        &["-v", "-t", "nyc", "-j"],
        Some("America/Los_Angeles"),
    );
    assert!(o.ok, "{}", o.stderr);
    serde_json::from_str::<serde_json::Value>(o.out()).expect("stdout is still JSON");
    assert_eq!(o.stderr.matches("(implicit, from $TZ)").count(), 1);
    assert_eq!(o.stderr.matches("no -d given").count(), 1);
}

// --- B12/B15: help text and the `--` terminator -----------------------------

#[test]
fn help_option_lines_use_option_parsers_columns() {
    // Ruby renders `-h` through OptionParser: four-space indent, flag column
    // padded to 33, description at column 37. Plain `-h` has to match it byte
    // for byte, so pin the geometry here.
    let text = stdout("", &["-h"]);
    let lines: Vec<&str> = text
        .lines()
        .filter(|l| l.starts_with("    -") || l.starts_with("        --"))
        .collect();
    assert_eq!(lines.len(), 12, "expected one line per option");
    for l in lines {
        let b = l.as_bytes();
        assert!(b.len() > 37, "{l:?}");
        assert_eq!(b[36], b' ', "flag column not padded to 33: {l:?}");
        assert_ne!(b[37], b' ', "description not at column 37: {l:?}");
    }
}

#[test]
fn accepts_the_posix_double_dash_terminator() {
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["-t", "pst", "--"]),
        "2026-04-03T05:00:00-07:00"
    );
    // ...and everything after it is a file, not a flag.
    let path = std::env::temp_dir().join("-tztr-dashed.log");
    std::fs::write(&path, "2026-04-03T12:00:00Z\n").unwrap();
    let o = run_tz(
        b"",
        &["-t", "pst", "--", path.to_str().unwrap()],
        Some("UTC"),
    );
    assert!(o.ok, "{}", o.stderr);
    assert_eq!(o.out().trim_end(), "2026-04-03T05:00:00-07:00");
    std::fs::remove_file(&path).unwrap();
}

// --- B10: one documented set of -d date forms -------------------------------

#[test]
fn accepts_the_documented_date_forms() {
    for date in [
        "2026-01-15",
        "2026/01/15",
        "20260115",
        "January 15, 2026",
        "Jan 15 2026",
        "Jan. 15, 2026",
        "Sep 15 2026", // three letters exactly; `Sept` is not a form we take
        "15 January 2026",
    ] {
        assert_eq!(
            stdout("15:30 PST", &["-t", "utc", "-d", date]),
            "23:30 UTC",
            "{date}"
        );
    }
}

#[test]
fn rejects_dates_outside_that_set_and_dates_that_do_not_exist() {
    for date in [
        "2026-02-30", // April has 30 days, February does not
        "2026-13-01",
        "2026-00-10",
        "not-a-date",
        "01/15/2026", // ambiguous day-first/month-first
        "15-01-2026",
        "2026-01-15T00:00:00Z",
        "Sept 15, 2026", // full name or exactly three letters, nothing between
        "Janu 15 2026",
        "2026-1-5", // numeric forms want two digits
        "2026/1/5",
    ] {
        assert_eq!(
            fails("15:30 PST\n", &["-t", "utc", "-d", date]),
            format!("tztr: invalid date: {date}"),
            "{date}"
        );
    }
}

// --- B9: one clean error line, whichever path hit it ------------------------

#[test]
fn an_error_about_a_file_names_the_file() {
    // With several file arguments the bare message says nothing about which
    // one failed, so both code paths name it.
    let missing = "/nonexistent/tztr-does-not-exist.log";
    assert_eq!(
        fails("", &["-t", "utc", missing]),
        format!("tztr: {missing}: No such file or directory (os error 2)")
    );
    assert_eq!(
        fails("", &["-i", "-t", "utc", missing]),
        format!("tztr: {missing}: No such file or directory (os error 2)")
    );
    assert_eq!(
        fails("", &["-t", "utc", "/tmp"]),
        "tztr: /tmp: Is a directory (os error 21)"
    );
    assert_eq!(
        fails("", &["-i", "-t", "utc", "/tmp"]),
        "tztr: /tmp: Is a directory (os error 21)"
    );
}

#[test]
fn a_bad_file_is_reported_and_the_rest_still_run() {
    // As cat and sed -i do. Stopping at the bad file left -i half-applied.
    let a = temp_path("tztr-continue-a");
    let b = temp_path("tztr-continue-b");
    let (a, b) = (a.to_str().unwrap(), b.to_str().unwrap());
    for f in [a, b] {
        std::fs::write(f, "2026-04-03T12:00:00Z\n").unwrap();
    }

    let o = run_tz(b"", &["-t", "pst", a, "/tmp", a], None);
    assert!(!o.ok);
    assert_eq!(o.out(), "2026-04-03T05:00:00-07:00\n".repeat(2));
    assert_eq!(
        o.stderr.trim_end(),
        "tztr: /tmp: Is a directory (os error 21)"
    );

    let o = run_tz(b"", &["-i", "-t", "pst", a, "/tmp", b], None);
    assert!(!o.ok);
    assert_eq!(
        o.stderr.trim_end(),
        "tztr: /tmp: Is a directory (os error 21)"
    );
    for f in [a, b] {
        assert_eq!(
            std::fs::read_to_string(f).unwrap(),
            "2026-04-03T05:00:00-07:00\n"
        );
        std::fs::remove_file(f).unwrap();
    }
}

#[test]
fn a_broken_pipe_is_not_blamed_on_the_input_file() {
    // `tztr big.log | head -1` is the commonest way this tool gets stopped.
    // EPIPE comes from writing to stdout — the input file was fine, and naming
    // it sends the user to look in the wrong place.
    let path = temp_path("tztr-epipe");
    let body = "2026-04-03T12:00:00Z\n".repeat(50_000);
    std::fs::write(&path, body).unwrap();

    let out = Command::new("sh")
        .arg("-c")
        .arg(format!(
            "{} -t pst {} | head -1",
            bin(),
            path.to_str().unwrap()
        ))
        .env("TZ", "UTC")
        .output()
        .unwrap();
    let stderr = String::from_utf8_lossy(&out.stderr);
    std::fs::remove_file(&path).unwrap();

    assert!(
        stderr.is_empty() || stderr.trim_end() == "tztr: Broken pipe (os error 32)",
        "unexpected stderr: {stderr:?}"
    );
}

#[test]
fn rejects_an_inline_value_on_a_flag_that_takes_none() {
    // Accepting `--json=true` and throwing the value away teaches the user
    // nothing -- and `--format=iso` does work, so the shape looks plausible.
    for flag in [
        "--json",
        "--ndjson",
        "--detect",
        "--verbose",
        "--list",
        "--in-place",
        "--version",
        "--help",
    ] {
        assert_eq!(
            fails("2026-04-03T12:00:00Z\n", &[&format!("{flag}=foo")]),
            format!("tztr: {flag} takes no argument"),
            "{flag}"
        );
    }
}

#[test]
fn still_accepts_an_inline_value_where_one_belongs() {
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["--format=short", "--to=nyc"]),
        "2026-04-03 08:00 EDT"
    );
    assert_eq!(
        stdout("15:30 PST", &["--to=utc", "--date=2026-01-15"]),
        "23:30 UTC"
    );
    // Short flags have no `=value` form to police.
    let out = stdout("2026-04-03T12:00:00Z", &["-j", "-t", "utc"]);
    let v: serde_json::Value = serde_json::from_str(&out).unwrap();
    assert_eq!(v[0]["translated"], "2026-04-03T12:00:00Z");
}

#[test]
fn errors_not_about_a_file_stay_bare() {
    assert_eq!(
        fails("2026-04-03T12:00:00Z\n", &["--bogus"]),
        "tztr: invalid option: --bogus"
    );
    assert_eq!(
        fails("2026-04-03T12:00:00Z\n", &["-t", "Bogus/Zone"]),
        "tztr: unknown timezone: Bogus/Zone"
    );
    assert_eq!(
        fails("15:30\n", &["-d", "not-a-date"]),
        "tztr: invalid date: not-a-date"
    );
}

// --- B8: non-UTF-8 input keeps its bytes ------------------------------------

#[test]
fn passes_invalid_utf8_lines_through_byte_for_byte() {
    // from_utf8_lossy silently rewrote these two bytes to U+FFFD, corrupting
    // the user's log on the way past.
    let input: &[u8] = b"2026-04-03T12:00:00Z \xff\xfe junk\n";
    let o = run_tz(input, &["-t", "pst"], Some("UTC"));
    assert!(o.ok, "{}", o.stderr);
    assert_eq!(
        o.stdout,
        b"2026-04-03T05:00:00-07:00 \xff\xfe junk\n".to_vec()
    );
}

#[test]
fn an_invalid_line_does_not_stop_the_valid_ones() {
    let input: &[u8] = b"2026-04-03T12:00:00Z\n\xff\xfe\n2026-04-03T13:00:00Z\n";
    let o = run_tz(input, &["-t", "utc"], Some("UTC"));
    assert!(o.ok, "{}", o.stderr);
    assert_eq!(
        o.stdout,
        b"2026-04-03T12:00:00Z\n\xff\xfe\n2026-04-03T13:00:00Z\n".to_vec()
    );
}

#[test]
fn in_place_edit_keeps_bytes_it_cannot_decode() {
    // `-i` used to abort on the whole file ("stream did not contain valid
    // UTF-8") and leave it untranslated.
    let path = temp_path("tztr-inplace-bytes");
    std::fs::write(&path, b"2026-04-03T12:00:00Z \xff\xfe junk\n").unwrap();
    let o = run_tz(
        b"",
        &["-i", "-t", "utc", path.to_str().unwrap()],
        Some("pst"),
    );
    assert!(o.ok, "{}", o.stderr);
    assert_eq!(
        std::fs::read(&path).unwrap(),
        b"2026-04-03T12:00:00Z \xff\xfe junk\n".to_vec()
    );
    std::fs::remove_file(&path).unwrap();
}

#[test]
fn structured_output_still_sees_timestamps_on_an_invalid_line() {
    // One stray byte must not cost the line its timestamp here either. The
    // match itself is ASCII, so the JSON stays well-formed.
    let input: &[u8] = b"2026-04-03T12:00:00Z \xff\xfe junk\n";
    let o = run_tz(input, &["-t", "pst", "-j"], Some("UTC"));
    assert!(o.ok, "{}", o.stderr);
    let v: serde_json::Value = serde_json::from_str(o.out()).unwrap();
    assert_eq!(v.as_array().unwrap().len(), 1);
    assert_eq!(v[0]["original"], "2026-04-03T12:00:00Z");
    assert_eq!(v[0]["translated"], "2026-04-03T05:00:00-07:00");

    let o = run_tz(input, &["-t", "pst", "--detect"], Some("UTC"));
    assert_eq!(o.out().trim_end(), "2026-04-03T12:00:00Z\tiso\tZ");
}

// --- B14: `-F short` always says which zone ---------------------------------

#[test]
fn short_format_always_labels_the_zone() {
    // The zone label used to be dropped whenever the output zone happened to
    // equal $TZ -- which is every invocation that simply omits `-t`. This
    // output gets pasted into tickets; it has to say which zone it is in.
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["-F", "short"]),
        "2026-04-03 12:00 UTC"
    );
    let o = run_tz(b"2026-04-03T12:00:00Z\n", &["-F", "short"], None);
    assert_eq!(o.out().trim_end(), "2026-04-03 12:00 UTC");
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["-F", "short", "-t", "nyc"]),
        "2026-04-03 08:00 EDT"
    );
    // ...including inside structured output.
    let json = stdout("2026-04-03T12:00:00Z", &["-F", "short", "-j"]);
    let v: serde_json::Value = serde_json::from_str(&json).unwrap();
    assert_eq!(v[0]["translated"], "2026-04-03 12:00 UTC");
}

// --- B4/B5/B13: an unresolvable timezone is a hard error --------------------

#[test]
fn rejects_unknown_timezones() {
    for (args, expected) in [
        (["-t", "Bogus/Zone"], "tztr: unknown timezone: Bogus/Zone"),
        (["-t", ""], "tztr: unknown timezone: "),
        (
            ["-t", "America/New York"],
            "tztr: unknown timezone: America/New York",
        ),
        (["-f", "Bogus/Zone"], "tztr: unknown timezone: Bogus/Zone"),
    ] {
        assert_eq!(fails("2026-04-03T12:00:00Z\n", &args), expected);
    }
}

#[test]
fn rejects_out_of_range_numeric_offsets() {
    for arg in ["15", "-13", "99"] {
        assert_eq!(
            fails("2026-04-03T12:00:00Z\n", &["-t", arg]),
            format!("tztr: offset out of range: {arg} (expected -12..14)")
        );
    }
    // ...and accepts the edges.
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["-t", "14"]),
        "2026-04-04T02:00:00+14:00"
    );
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["-t", "-12"]),
        "2026-04-03T00:00:00-12:00"
    );
}

#[test]
fn rejects_sub_hour_numeric_offsets() {
    // Silently mis-signing these is worse than refusing; half-hour zones are
    // reached by name (`-t ist`).
    assert_eq!(
        fails("2026-04-03T12:00:00Z\n", &["-t", "+5:30"]),
        "tztr: unknown timezone: +5:30"
    );
    assert_eq!(
        stdout("2026-04-03T12:00:00Z", &["-t", "ist"]),
        "2026-04-03T17:30:00+05:30"
    );
}

#[test]
fn rejects_an_unresolvable_tz_env() {
    let o = run_tz(b"2026-04-03T12:00:00Z\n", &[], Some("Bogus/Zone"));
    assert!(!o.ok);
    assert_eq!(o.stderr.trim_end(), "tztr: unknown timezone: Bogus/Zone");
}

#[test]
fn honors_the_posix_leading_colon_on_tz() {
    // POSIX spells TZ with a leading colon; without this, TimeZone::get failed
    // and *every* conversion silently came out UTC.
    let o = run_tz(b"2026-04-03T12:00:00Z\n", &[], Some(":America/New_York"));
    assert!(o.ok, "{}", o.stderr);
    assert_eq!(o.out().trim_end(), "2026-04-03T08:00:00-04:00");
}

#[test]
fn treats_an_empty_tz_as_unset() {
    let o = run_tz(b"2026-04-03T12:00:00Z\n", &[], Some(""));
    assert!(o.ok, "{}", o.stderr);
    assert_eq!(o.out().trim_end(), "2026-04-03T12:00:00Z");
}

// --- `tztr now` ---------------------------------------------------------------

#[test]
fn now_prints_the_current_time_in_iso_8601() {
    let o = run_tz(b"", &["now"], None);
    assert!(o.ok, "{}", o.stderr);
    let out = o.out().trim_end();
    let when: jiff::Timestamp = out.parse().expect("ISO 8601");
    let drift = jiff::Timestamp::now().duration_since(when).as_secs().abs();
    assert!(out.ends_with('Z') && drift < 5, "{out}");

    let o = run_tz(b"", &["now"], Some("America/Los_Angeles"));
    let out = o.out().trim_end();
    assert!(out.ends_with("-07:00") || out.ends_with("-08:00"), "{out}");
}

#[test]
fn now_works_with_every_output_flag() {
    let o = run_tz(b"", &["now", "-t", "tokyo", "-F", "short"], None);
    assert!(o.out().trim_end().ends_with(" JST"), "{}", o.out());
    let o = run_tz(b"", &["now", "-t", "tokyo", "-j"], None);
    let v: serde_json::Value = serde_json::from_str(o.out()).unwrap();
    assert!(v[0]["translated"].as_str().unwrap().ends_with("+09:00"));
    let o = run_tz(b"", &["now", "--detect"], None);
    assert!(o.out().trim_end().ends_with("\tiso\tZ"), "{}", o.out());
    assert_eq!(
        fails("", &["-i", "now"]),
        "tztr: -i cannot be combined with now"
    );
}

#[test]
fn verbose_does_not_flag_a_capitalized_word() {
    let o = run_tz(
        b"um 15:30 Ist das\n12:00 Mt. Everest\n15:30 Et al\n",
        &["-v", "-d", "2026-04-03"],
        None,
    );
    assert!(!o.stderr.contains("ignored"), "{}", o.stderr);
}