From ae0a40933be26f24c0fa4bc423d028cb12f91674 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Tue, 12 Nov 2024 17:06:30 +0100 Subject: abs: Support a fallback to parsing only time using current date When -a flag is used parse first "%F %T", on error fallback to "%T" using the "%F" format date of the localnow date. --- distamp.ha | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/distamp.ha b/distamp.ha index acb673f..aa5e7f3 100644 --- a/distamp.ha +++ b/distamp.ha @@ -3,9 +3,12 @@ use time; use time::date; use time::chrono; use os; +use io; use strconv; use strings; use getopt; +use memio; +use encoding::utf8; type parseerror = !strconv::error; @@ -97,11 +100,21 @@ fn printdistamp(i: i64) void = { fmt::printfln("", i)!; }; -fn abs(conf: config, s: str) (i64 | date::error) = { +fn abs(conf: config, s: str) (i64 | date::error | io::error | utf8::invalid) = { let v = date::newvirtual(); v.vloc = conf.tz; v.zoff = date::zflag::LAP_EARLY | date::zflag::GAP_END; - date::parse(&v, "%Y-%m-%d %T", s)?; + match (date::parse(&v, "%Y-%m-%d %T", s)) { + case date::parsefail => + const now = date::localnow(); + const buf: [10]u8 = [0...]; + const st = memio::fixed(buf); + date::format(&st, "%F", &now)?; + date::parse(&v, "%F", memio::string(&st)?)?; + date::parse(&v, "%T", s)?; + case void => + void; + }; v.nanosecond = 0; let d = date::realize(v, chrono::LOCAL)?; //date::format(os::stderr, "%Y-%m-%d %T %L", &d)!; -- cgit v1.2.3