-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfc3339.c3
198 lines (155 loc) · 3.18 KB
/
rfc3339.c3
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
module toml::rfc3339;
import toml::rfc3339::parser @public;
import std::time::datetime;
// Parse date and time formats according to RFC 3339.
// https://www.rfc-editor.org/rfc/rfc3339
/*
date-fullyear = 4DIGIT
date-month = 2DIGIT ; 01-12
date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on
; month/year
time-hour = 2DIGIT ; 00-23
time-minute = 2DIGIT ; 00-59
time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second
; rules
time-secfrac = "." 1*DIGIT
time-numoffset = ("+" / "-") time-hour ":" time-minute
time-offset = "Z" / time-numoffset
partial-time = time-hour ":" time-minute ":" time-second
[time-secfrac]
full-date = date-fullyear "-" date-month "-" date-mday
full-time = partial-time time-offset
date-time = full-date "T" full-time
*/
// 1996-12-19T16:39:57-08:00
fn DateTime! parse(String s)
{
return Parser{s}.parse();
}
module toml::rfc3339::parser @private;
import std::time::datetime;
fault Error
{
INVALID_DATE_FORMAT,
INVALID_INPUT,
INVALID_NUMBER,
}
struct Date
{
int year;
int month;
int day;
}
fn void! Date.parse(&d, Parser* p)
{
if (p.s.len < 10) return Error.INVALID_INPUT?;
d.year = p.consumeInt()!;
p.consume('-')!;
d.month = p.consumeInt()!;
p.consume('-')!;
d.day = p.consumeInt()!;
}
struct Time
{
int hour;
int min;
int sec;
int us;
bool utc;
}
fn void! Time.parse(&t, Parser* p)
{
if (p.s.len < 8) return Error.INVALID_INPUT?;
t.hour = p.consumeInt()!;
p.consume(':')!;
t.min = p.consumeInt()!;
p.consume(':')!;
t.sec = p.consumeInt()!;
t.us = 0;
if (!p.empty() && p.peek() == '.') {
p.consume('.')!;
t.us = p.consumeInt()!;
}
if (try p.consume('Z')) t.utc = true;
}
struct Offset
{
int hour;
int min;
}
fn void! Offset.parse(&o, Parser* p)
{
if (p.s.len < 6) return Error.INVALID_INPUT?;
int sign;
switch (p.peek())
{
case '+':
sign = 1;
p.consume('+')!;
case '-':
sign = -1;
p.consume('-')!;
default:
return;
}
o.hour = sign * p.consumeInt()!;
p.consume(':')!;
o.min = p.consumeInt()!;
}
fn int Offset.to_sec(&o)
{
return o.hour * 3600 + o.min * 60;
}
fault ParserError
{
INVALID_LENGTH,
UNEXPECTED_CHAR,
}
struct Parser
{
String s;
}
fn DateTime! Parser.parse(&p)
{
Date d;
d.parse(p)!;
if (p.peek() == ' ' || p.peek() == 'T') p.consume(p.peek())!;
Time t;
t.parse(p)!;
DateTime dt = datetime::from_date(
d.year, (Month)(d.month-1), d.day,
t.hour, t.min, t.sec, t.us);
Offset off;
if (try off.parse(p))
{
TzDateTime tzdt = dt.to_gmt_offset(off.to_sec());
dt = tzdt.date_time;
}
return dt;
}
fn bool Parser.empty(&p)
{
return p.s.len == 0;
}
fn char Parser.peek(&p)
{
return p.s[0];
}
fn void! Parser.consume(&p, char c)
{
if (p.empty() || p.peek() != c) return ParserError.UNEXPECTED_CHAR?;
p.s = p.s[1..];
}
fn int! Parser.consumeInt(&p)
{
int n, v;
while (n < p.s.len && p.s[n].is_digit()) n++;
if (!n) return ParserError.INVALID_LENGTH?;
v = p.s[:n].to_int()!;
p.s = p.s[n..];
return v;
}
fn void Parser.skipSpace(&p)
{
while (p.s.len && (p.s[0] == ' ' || p.s[0] == '\t')) p.s = p.s[1..];
}