From daf372c98d4a9df43c8270bdf9b414e9820aea90 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Wed, 30 Oct 2024 18:21:05 +0100 Subject: Add config support for specifying a timezone --- config.ha | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ distamp.ha | 16 +++++++++++----- 2 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 config.ha diff --git a/config.ha b/config.ha new file mode 100644 index 0000000..6a343eb --- /dev/null +++ b/config.ha @@ -0,0 +1,57 @@ +use fmt; +use dirs; +use format::ini; +use time::chrono; +use path; +use io; +use fs; +use os; +use errors; + +type config = struct { + tz: chrono::locality, +}; + +type noexist = !str; + +type conferror = !(path::error | noexist | fs::error | chrono::tzdberror); + +fn loadconfig() (config | conferror) = { + const confdir = dirs::config("distamp"); + const buf = path::init()?; + const confpath = path::push(&buf, confdir, "config.ini")?; + const f = match (os::open(confpath)) { + case let e: errors::noentry => + fmt::errorln("warn: No config file found")!; + return config { + tz = chrono::LOCAL, + }; + case let e: fs::error => + return e; + case let f: io::file => + yield f; + }; + defer io::close(f)!; + const sc = ini::scan(f); + + let tz: chrono::locality = chrono::LOCAL; + for (const e => ini::next(&sc)!) { + if (e.1 == "tz") { + tz = chrono::tz(e.2)?; + }; + }; + return config { + tz = tz, + }; +}; + +fn strconferror(e: conferror) str = { + match (e) { + case let e: path::error => + return path::strerror(e); + case let e: fs::error => + return fs::strerror(e); + case let e: chrono::tzdberror => + return chrono::strerror(e); + }; +}; diff --git a/distamp.ha b/distamp.ha index be2ce02..0b908ed 100644 --- a/distamp.ha +++ b/distamp.ha @@ -19,17 +19,22 @@ export fn main() void = { ); defer getopt::finish(&cmd); + const conf = match (loadconfig()) { + case let e: conferror => + fmt::fatal(strconferror(e)); + case let c: config => + yield c; + }; + let days: time::duration = 0; let weeks: time::duration = 0; for (let opt .. cmd.opts) { switch (opt.0) { case 'a' => - match (abs(opt.1)) { + match (abs(conf, opt.1)) { case let i: i64 => printdistamp(i); return; - case let e: chrono::tzdberror => - fmt::fatal(chrono::strerror(e)); case let e: date::parsefail => let s = strings::concat(date::strerror(e), "\n", "Date format must be 'year-month-day hour:minute:second'"); defer free(s); @@ -77,9 +82,10 @@ fn printdistamp(i: i64) void = { fmt::printfln("", i)!; }; -fn abs(s: str) (i64 | date::error | chrono::tzdberror) = { +fn abs(conf: config, s: str) (i64 | date::error) = { let v = date::newvirtual(); - v.vloc = chrono::tz("Europe/Paris")?; + v.vloc = conf.tz; + //v.vloc = chrono::tz("Europe/Paris")?; v.zoff = date::zflag::LAP_EARLY | date::zflag::GAP_END; date::parse(&v, "%Y-%m-%d %T", s)?; v.nanosecond = 0; -- cgit v1.2.3