1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
use fmt;
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;
def MAJOR = 2;
def MINOR = 1;
def PATCH = 0;
// hare build -RD DEV=false for release version
def DEV = true;
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"),
('v', "Print version"),
"m[:s]",
);
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(conf, 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::errorln(s)!;
return;
case let e: date::insufficient =>
fmt::fatal(date::strerror(e));
case let e: date::invalid =>
fmt::fatal(date::strerror(e));
case let e: date::zfunresolved =>
fmt::fatal(date::strerror(e));
case let e: io::error =>
fmt::fatal(io::strerror(e));
case let e: utf8::invalid =>
fmt::fatal(utf8::strerror(e));
case nomem =>
fmt::fatal("No memory left");
};
case 'w' =>
weeks = match (toweeks(opt.1)) {
case let i: time::duration =>
yield i;
case let e: strconv::error =>
fmt::fatal(strconv::strerror(e));
};
case 'd' =>
days = match (todays(opt.1)) {
case let d: time::duration =>
yield d;
case let e: parseerror =>
fmt::fatal(strerror(e));
};
case 'v' =>
const dev = if (DEV) {
yield "_dev";
} else {
yield "";
};
fmt::printfln("{}.{}.{}{}-{}_{}", MAJOR, MINOR, PATCH, dev, os::sysname(), os::arch_name(os::architecture()))!;
return;
case => abort();
};
};
if (len(cmd.args) > 1) {
os::exit(os::status::FAILURE);
};
let li = match (parsemainargs(cmd.args)) {
case let i: time::instant =>
yield i;
case let e: parseerror =>
fmt::fatal(strerror(e));
};
li = time::add(li, days);
li = time::add(li, weeks);
printdistamp(li.sec);
};
fn printdistamp(i: i64) void = {
fmt::printfln("<t:{}:R>", i)!;
};
fn abs(conf: config, s: str) (i64 | date::invalid | date::insufficient | date::zfunresolved | date::parsefail | io::error | utf8::invalid | nomem) = {
let v = date::newvirtual();
v.vloc = conf.tz;
v.zoff = date::zflag::LAP_EARLY | date::zflag::GAP_END;
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, date::LOCAL)?;
//date::format(os::stderr, "%Y-%m-%d %T %L", &d)!;
return (&d: *time::instant).sec;
};
fn parsemainargs(args: []str) (time::instant | parseerror) = {
return if (len(args) == 1) {
let timestr = args[0];
let i = time::now(time::clock::REALTIME);
let spl = strings::split(timestr, ":")!;
defer free(spl);
yield if (len(spl) > 2) {
fmt::fatal("Duration format: m or m:s");
} else {
yield parsetime(i, spl)?;
};
} else {
yield time::now(time::clock::REALTIME);
};
};
fn strerror(e: parseerror) str = {
return strconv::strerror(e);
};
fn parsetime(clk: time::instant, spl: []str) (time::instant | parseerror) = {
return if (len(spl) == 2) {
let m = strconv::stoi64(spl[0])?;
let s = strconv::stoi64(spl[1])?;
let d = (m * time::MINUTE) + (s * time::SECOND);
yield time::add(clk, d);
} else {
let m = strconv::stoi64(spl[0])?;
let d = m * time::MINUTE;
yield time::add(clk, d);
};
};
fn todays(days: str) (time::duration | parseerror) = {
let d = strconv::stoi(days)?;
return d * 24 * time::HOUR;
};
fn toweeks(weeks: str) (time::duration | parseerror) = {
let d = strconv::stoi(weeks)?;
return d * 7 * 24 * time::HOUR;
};
|