tztr 0.1.0

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
//! `tztr` CLI — Rust port of `bin/tztr`. Kept functionally identical to the
//! Ruby reference (same flags, output, and behavior); see CLAUDE.md.

use serde_json::{Map, Value};
use std::env;
use std::fs;
use std::io::{self, BufRead, BufReader, IsTerminal, Write};
use std::process::ExitCode;

use tztr::{matches, resolve_tz, timezone_aliases, translate, Format, Match};

const VERSION: &str = env!("CARGO_PKG_VERSION");

const HELP: &str = "\
Usage: tztr [options] [file]

Timezone Translator - convert timestamps timezone. Reads from stdin or file.

    -f, --from TZ        Input timezone (default: auto-detect)
    -t, --to TZ          Output timezone (default: $TZ, else UTC)
    -l, --list           List timezone aliases
    -i, --in-place       Edit file in place
    -F, --format FMT     Output format: iso, short, time (default: preserve input)
    -d, --date DATE      Reference date for time-only inputs (resolves DST)
    -j, --json           Emit a JSON array of matches
    -J, --ndjson         Emit newline-delimited JSON (one object per match)
        --detect         Report detected format/zone without converting
    -v, --verbose        Print diagnostics to stderr
    -V, --version        Show version
    -h, --help           Show this help

Environment:
  TZ    Sets default output timezone (overridden by -t)

Examples:
  echo '2026-04-03T12:00:00Z' | tztr -t sf
  echo '15:30 UTC' | tztr -t pst
  echo '12:00 EST' | tztr -t -8
  tail -f app.log | tztr -t nyc
  echo '15:30 UTC' | tztr -t pst -j
  tail -f app.log | tztr -t nyc -J
  echo '2026-04-03T12:00:00Z' | tztr --detect -j
  echo '15:30 PST' | tztr -t utc -d 2026-01-15";

struct Options {
    from: Option<String>,
    to: String,
    format: Option<Format>,
    date: Option<String>,
    local: bool,
    inplace: bool,
    json: bool,
    ndjson: bool,
    detect: bool,
    verbose: bool,
    files: Vec<String>,
}

fn main() -> ExitCode {
    match run() {
        Ok(code) => code,
        Err(msg) => {
            eprintln!("tztr: {msg}");
            ExitCode::FAILURE
        }
    }
}

fn run() -> Result<ExitCode, String> {
    let local_tz = env::var("TZ").ok().filter(|s| !s.is_empty());

    let mut from: Option<String> = None;
    let mut to_arg: Option<String> = None;
    let mut format: Option<Format> = None;
    let mut date: Option<String> = None;
    let mut inplace = false;
    let mut json = false;
    let mut ndjson = false;
    let mut detect = false;
    let mut verbose = false;
    let mut files: Vec<String> = Vec::new();

    let mut args = env::args().skip(1).peekable();
    while let Some(arg) = args.next() {
        // Split `--opt=value` into name + inline value.
        let (name, inline) = match arg.split_once('=') {
            Some((n, v)) if arg.starts_with("--") => (n.to_string(), Some(v.to_string())),
            _ => (arg.clone(), None),
        };
        let mut take_value = |inline: Option<String>| -> Result<String, String> {
            if let Some(v) = inline {
                return Ok(v);
            }
            args.next()
                .ok_or_else(|| format!("missing argument for {name}"))
        };

        match name.as_str() {
            "-f" | "--from" => from = Some(take_value(inline)?),
            "-t" | "--to" => to_arg = Some(take_value(inline)?),
            "-d" | "--date" => date = Some(take_value(inline)?),
            "-F" | "--format" => {
                format = Some(parse_format(&take_value(inline)?)?);
            }
            "-l" | "--list" => {
                list_aliases();
                return Ok(ExitCode::SUCCESS);
            }
            "-i" | "--in-place" => inplace = true,
            "-j" | "--json" => json = true,
            "-J" | "--ndjson" => ndjson = true,
            "--detect" => detect = true,
            "-v" | "--verbose" => verbose = true,
            "-V" | "--version" => {
                println!("{VERSION}");
                return Ok(ExitCode::SUCCESS);
            }
            "-h" | "--help" => {
                println!("{HELP}");
                return Ok(ExitCode::SUCCESS);
            }
            other if other.starts_with('-') && other != "-" => {
                return Err(format!("invalid option: {other}"));
            }
            _ => files.push(arg),
        }
    }

    let to = resolve_tz(to_arg.as_deref().or(local_tz.as_deref()).unwrap_or("UTC"));
    let from = from
        .as_deref()
        .map(resolve_tz)
        .or_else(|| local_tz.as_deref().map(resolve_tz));
    let local = to == resolve_tz(local_tz.as_deref().unwrap_or("UTC"));

    let date = match date {
        Some(d) => Some(normalize_date(&d).ok_or_else(|| format!("invalid date: {d}"))?),
        None => None,
    };

    let opts = Options {
        from,
        to,
        format,
        date,
        local,
        inplace,
        json,
        ndjson,
        detect,
        verbose,
        files,
    };

    let json_mode = opts.json || opts.ndjson;

    if opts.inplace && (json_mode || opts.detect) {
        return Err("-i cannot be combined with --json/--ndjson/--detect".to_string());
    }

    if opts.verbose {
        eprintln!(
            "tztr: from={} to={}",
            opts.from.as_deref().unwrap_or("auto"),
            opts.to
        );
    }

    if opts.files.is_empty() && io::stdin().is_terminal() {
        println!("{HELP}");
        return Ok(ExitCode::SUCCESS);
    }

    if opts.inplace {
        return run_inplace(&opts);
    }

    run_stream(&opts, json_mode)
}

fn run_inplace(opts: &Options) -> Result<ExitCode, String> {
    if opts.files.is_empty() {
        return Err("-i requires a file argument".to_string());
    }
    for file in &opts.files {
        let content = fs::read_to_string(file).map_err(|e| format!("{file}: {e}"))?;
        let translated: String = content
            .split_inclusive('\n')
            .map(|line| {
                translate(
                    line,
                    &opts.to,
                    opts.from.as_deref(),
                    opts.format,
                    opts.local,
                    opts.date.as_deref(),
                )
            })
            .collect();
        if translated != content {
            fs::write(file, translated).map_err(|e| format!("{file}: {e}"))?;
        }
    }
    Ok(ExitCode::SUCCESS)
}

fn run_stream(opts: &Options, json_mode: bool) -> Result<ExitCode, String> {
    let stdout = io::stdout();
    let mut out = stdout.lock();
    let mut collected: Vec<Match> = Vec::new();

    let result = (|| -> io::Result<()> {
        if opts.files.is_empty() {
            let stdin = io::stdin();
            for_each_line(stdin.lock(), |line| {
                handle_line(opts, json_mode, line, &mut out, &mut collected)
            })?;
        } else {
            for file in &opts.files {
                let f = fs::File::open(file)?;
                for_each_line(BufReader::new(f), |line| {
                    handle_line(opts, json_mode, line, &mut out, &mut collected)
                })?;
            }
        }
        if opts.json {
            let arr = Value::Array(
                collected
                    .iter()
                    .map(|m| json_value(m, opts.detect))
                    .collect(),
            );
            writeln!(out, "{}", serde_json::to_string_pretty(&arr).unwrap())?;
        }
        Ok(())
    })();

    result.map_err(|e| e.to_string())?;
    Ok(ExitCode::SUCCESS)
}

fn handle_line(
    opts: &Options,
    json_mode: bool,
    line: &str,
    out: &mut dyn Write,
    collected: &mut Vec<Match>,
) -> io::Result<()> {
    if json_mode {
        let ms = matches(
            line,
            &opts.to,
            opts.from.as_deref(),
            opts.format,
            opts.local,
            opts.detect,
            opts.date.as_deref(),
        );
        if opts.ndjson {
            for m in ms {
                writeln!(out, "{}", to_json(&m, opts.detect))?;
            }
        } else {
            collected.extend(ms);
        }
    } else if opts.detect {
        for m in matches(
            line,
            &opts.to,
            opts.from.as_deref(),
            None,
            false,
            true,
            None,
        ) {
            writeln!(
                out,
                "{}\t{}\t{}",
                m.original,
                m.detected_format,
                m.detected_tz.unwrap_or_default()
            )?;
        }
    } else {
        write!(
            out,
            "{}",
            translate(
                line,
                &opts.to,
                opts.from.as_deref(),
                opts.format,
                opts.local,
                opts.date.as_deref(),
            )
        )?;
    }
    Ok(())
}

/// Iterate lines preserving their trailing newline, like Ruby's `each_line`.
fn for_each_line<R: BufRead>(
    mut reader: R,
    mut f: impl FnMut(&str) -> io::Result<()>,
) -> io::Result<()> {
    let mut buf = Vec::new();
    loop {
        buf.clear();
        let n = reader.read_until(b'\n', &mut buf)?;
        if n == 0 {
            break;
        }
        let line = String::from_utf8_lossy(&buf);
        f(&line)?;
    }
    Ok(())
}

fn parse_format(v: &str) -> Result<Format, String> {
    match v {
        "iso" => Ok(Format::Iso),
        "short" => Ok(Format::Short),
        "time" => Ok(Format::Time),
        _ => Err(format!("invalid format: {v} (expected iso, short, time)")),
    }
}

fn list_aliases() {
    for (k, v) in timezone_aliases() {
        println!("{k:<12} {v}");
    }
}

fn json_value(m: &Match, detect: bool) -> Value {
    let mut obj = Map::new();
    obj.insert("original".into(), Value::String(m.original.clone()));
    obj.insert(
        "detected_format".into(),
        Value::String(m.detected_format.clone()),
    );
    obj.insert(
        "detected_tz".into(),
        m.detected_tz.clone().map_or(Value::Null, Value::String),
    );
    if !detect {
        obj.insert(
            "translated".into(),
            m.translated.clone().map_or(Value::Null, Value::String),
        );
    }
    Value::Object(obj)
}

fn to_json(m: &Match, detect: bool) -> String {
    serde_json::to_string(&json_value(m, detect)).unwrap()
}

/// Normalize a flexible date string to `YYYY-MM-DD`, or `None` if unparseable.
/// Covers the common forms Ruby's `Date.parse` accepts for our use; ambiguous
/// day-first slash dates (e.g. `1/15/2026`) are rejected, as in Ruby.
fn normalize_date(input: &str) -> Option<String> {
    use regex::Regex;
    let input = input.trim();

    // ISO and year-first slash: YYYY-MM-DD / YYYY/MM/DD
    let iso = Regex::new(r"^(\d{4})[-/](\d{1,2})[-/](\d{1,2})$").unwrap();
    if let Some(c) = iso.captures(input) {
        return build_date(&c[1], c[2].parse().ok()?, c[3].parse().ok()?);
    }

    // "Month D, YYYY" / "Month D YYYY"
    let mdy = Regex::new(r"(?i)^([a-z]+)\.?\s+(\d{1,2}),?\s+(\d{4})$").unwrap();
    if let Some(c) = mdy.captures(input) {
        let month = month_number(&c[1])?;
        return build_date(&c[3], month, c[2].parse().ok()?);
    }

    // "D Month YYYY"
    let dmy = Regex::new(r"(?i)^(\d{1,2})\s+([a-z]+)\.?,?\s+(\d{4})$").unwrap();
    if let Some(c) = dmy.captures(input) {
        let month = month_number(&c[2])?;
        return build_date(&c[3], month, c[1].parse().ok()?);
    }

    None
}

fn build_date(year: &str, month: u32, day: u32) -> Option<String> {
    if !(1..=12).contains(&month) || !(1..=31).contains(&day) {
        return None;
    }
    Some(format!("{year}-{month:02}-{day:02}"))
}

fn month_number(name: &str) -> Option<u32> {
    let n = name.to_lowercase();
    let months = [
        "january",
        "february",
        "march",
        "april",
        "may",
        "june",
        "july",
        "august",
        "september",
        "october",
        "november",
        "december",
    ];
    months
        .iter()
        .position(|m| *m == n || m.starts_with(&n) && n.len() >= 3)
        .map(|i| i as u32 + 1)
}