aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--distamp.ha27
1 files changed, 26 insertions, 1 deletions
diff --git a/distamp.ha b/distamp.ha
index 9c7de55..1609419 100644
--- a/distamp.ha
+++ b/distamp.ha
@@ -1,5 +1,7 @@
use fmt;
use time;
+use time::date;
+use time::chrono;
use os;
use strconv;
use strings;
@@ -10,6 +12,7 @@ type parseerror = !strconv::error;
export fn main() void = {
const cmd = getopt::parse(os::args,
"discord relative timestamp generator",
+ ('a', "date", "Absolute date (this option is exclusive any other options will be ignored)"),
('d', "days", "Number of days"),
('w', "weeks", "Number of weeks"),
"m[:s]",
@@ -20,6 +23,19 @@ export fn main() void = {
let weeks: time::duration = 0;
for (let opt .. cmd.opts) {
switch (opt.0) {
+ case 'a' =>
+ match (abs(opt.1)) {
+ case let i: i64 =>
+ printdistamp(i);
+ return;
+ 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);
+ fmt::fprintln(os::stderr, s)!;
+ return;
+ case let e: date::error =>
+ fmt::fatal(date::strerror(e));
+ };
case 'w' =>
weeks = match (toweeks(opt.1)) {
case let i: time::duration =>
@@ -52,7 +68,16 @@ export fn main() void = {
li = time::add(li, days);
li = time::add(li, weeks);
- fmt::printfln("<t:{}:R>", time::unix(li))!;
+ printdistamp(time::unix(li));
+};
+
+fn printdistamp(i: i64) void = {
+ fmt::printfln("<t:{}:R>", i)!;
+};
+
+fn abs(s: str) (i64 | date::error) = {
+ let d = date::from_str("%Y-%m-%d %H:%M:%S", s, chrono::LOCAL)?;
+ return time::unix(*(&d: *time::instant));
};
fn parsemainargs(args: []str) (time::instant | parseerror) = {