aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Hurst <julian.hurst@digdash.com>2024-11-12 17:06:30 +0100
committerJulian Hurst <julian.hurst@digdash.com>2024-11-12 17:08:15 +0100
commitae0a40933be26f24c0fa4bc423d028cb12f91674 (patch)
tree6eab426bcf33ce3fb76858c435136e0ff1c2f44c
parent87254486a840b4d557f3fb6673c3f8c3073f8521 (diff)
downloaddistamp-ae0a40933be26f24c0fa4bc423d028cb12f91674.tar.gz
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.
-rw-r--r--distamp.ha17
1 files 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("<t:{}:R>", 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)!;