diff options
| author | Julian Hurst <julian.hurst@digdash.com> | 2024-10-30 18:21:05 +0100 |
|---|---|---|
| committer | Julian Hurst <julian.hurst@digdash.com> | 2024-10-30 18:21:05 +0100 |
| commit | daf372c98d4a9df43c8270bdf9b414e9820aea90 (patch) | |
| tree | 24bdc537045cf91d0537740b7411a0723ab749e9 | |
| parent | 951a1d7c777902c3d93c0629c5940ac25f827f62 (diff) | |
| download | distamp-daf372c98d4a9df43c8270bdf9b414e9820aea90.tar.gz | |
Add config support for specifying a timezone
| -rw-r--r-- | config.ha | 57 | ||||
| -rw-r--r-- | distamp.ha | 16 |
2 files changed, 68 insertions, 5 deletions
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); + }; +}; @@ -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("<t:{}:R>", 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; |
