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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "utc-offset.h"
#include "utc-mktime.h"
#include "iso8601-date.h"
#include <ctype.h>
/* RFC3339/ISO8601 date-time syntax
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
*/
struct iso8601_date_parser {
const unsigned char *cur, *end;
struct tm tm;
int timezone_offset;
};
static inline int
iso8601_date_parse_number(struct iso8601_date_parser *parser,
int digits, int *number_r)
{
int i;
if (parser->cur >= parser->end || !i_isdigit(parser->cur[0]))
return 0;
*number_r = parser->cur[0] - '0';
parser->cur++;
for (i=0; i < digits-1; i++) {
if (parser->cur >= parser->end || !i_isdigit(parser->cur[0]))
return -1;
*number_r = ((*number_r) * 10) + parser->cur[0] - '0';
parser->cur++;
}
return 1;
}
static int
iso8601_date_parse_secfrac(struct iso8601_date_parser *parser)
{
/* time-secfrac = "." 1*DIGIT
NOTE: Currently not applied anywhere, so fraction is just skipped.
*/
/* "." */
if (parser->cur >= parser->end || parser->cur[0] != '.')
return 0;
parser->cur++;
/* 1DIGIT */
if (parser->cur >= parser->end || !i_isdigit(parser->cur[0]))
return -1;
parser->cur++;
/* *DIGIT */
while (parser->cur < parser->end && i_isdigit(parser->cur[0]))
parser->cur++;
return 1;
}
static int is08601_date_parse_time_offset(struct iso8601_date_parser *parser)
{
int tz_sign = 1, tz_hour = 0, tz_min = 0;
/* time-offset = "Z" / time-numoffset
time-numoffset = ("+" / "-") time-hour ":" time-minute
time-hour = 2DIGIT ; 00-23
time-minute = 2DIGIT ; 00-59
*/
if (parser->cur >= parser->end)
return 0;
/* time-offset = "Z" / time-numoffset */
switch (parser->cur[0]) {
case '-':
tz_sign = -1;
/* fall through */
case '+':
parser->cur++;
/* time-hour = 2DIGIT */
if (iso8601_date_parse_number(parser, 2, &tz_hour) <= 0)
return -1;
if (tz_hour > 23)
return -1;
/* ":" */
if (parser->cur >= parser->end || parser->cur[0] != ':')
return -1;
parser->cur++;
/* time-minute = 2DIGIT */
if (iso8601_date_parse_number(parser, 2, &tz_min) <= 0)
return -1;
if (tz_min > 59)
return -1;
break;
case 'Z':
case 'z':
parser->cur++;
break;
default:
return -1;
}
parser->timezone_offset = tz_sign*(tz_hour*60 + tz_min);
return 1;
}
static int is08601_date_parse_full_time(struct iso8601_date_parser *parser)
{
/* full-time = partial-time time-offset
partial-time = time-hour ":" time-minute ":" time-second [time-secfrac]
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-hour = 2DIGIT */
if (iso8601_date_parse_number(parser, 2, &parser->tm.tm_hour) <= 0)
return -1;
/* ":" */
if (parser->cur >= parser->end || parser->cur[0] != ':')
return -1;
parser->cur++;
/* time-minute = 2DIGIT */
if (iso8601_date_parse_number(parser, 2, &parser->tm.tm_min) <= 0)
return -1;
/* ":" */
if (parser->cur >= parser->end || parser->cur[0] != ':')
return -1;
parser->cur++;
/* time-second = 2DIGIT */
if (iso8601_date_parse_number(parser, 2, &parser->tm.tm_sec) <= 0)
return -1;
/* [time-secfrac] */
if (iso8601_date_parse_secfrac(parser) < 0)
return -1;
/* time-offset */
if (is08601_date_parse_time_offset(parser) <= 0)
return -1;
return 1;
}
static int is08601_date_parse_full_date(struct iso8601_date_parser *parser)
{
/* full-date = date-fullyear "-" date-month "-" date-mday
date-fullyear = 4DIGIT
date-month = 2DIGIT ; 01-12
date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on
; month/year
*/
/* date-fullyear = 4DIGIT */
if (iso8601_date_parse_number(parser, 4, &parser->tm.tm_year) <= 0)
return -1;
if (parser->tm.tm_year < 1900)
return -1;
parser->tm.tm_year -= 1900;
/* "-" */
if (parser->cur >= parser->end || parser->cur[0] != '-')
return -1;
parser->cur++;
/* date-month = 2DIGIT */
if (iso8601_date_parse_number(parser, 2, &parser->tm.tm_mon) <= 0)
return -1;
parser->tm.tm_mon -= 1;
/* "-" */
if (parser->cur >= parser->end || parser->cur[0] != '-')
return -1;
parser->cur++;
/* time-second = 2DIGIT */
if (iso8601_date_parse_number(parser, 2, &parser->tm.tm_mday) <= 0)
return -1;
return 1;
}
static int iso8601_date_parse_date_time(struct iso8601_date_parser *parser)
{
/* date-time = full-date "T" full-time */
/* full-date */
if (is08601_date_parse_full_date(parser) <= 0)
return -1;
/* "T" */
if (parser->cur >= parser->end ||
(parser->cur[0] != 'T' && parser->cur[0] != 't'))
return -1;
parser->cur++;
/* full-time */
if (is08601_date_parse_full_time(parser) <= 0)
return -1;
if (parser->cur != parser->end)
return -1;
return 1;
}
static bool
iso8601_date_do_parse(const unsigned char *data, size_t size, struct tm *tm_r,
time_t *timestamp_r, int *timezone_offset_r)
{
struct iso8601_date_parser parser;
time_t timestamp;
i_zero(&parser);
parser.cur = data;
parser.end = data + size;
if (iso8601_date_parse_date_time(&parser) <= 0)
return FALSE;
parser.tm.tm_isdst = -1;
timestamp = utc_mktime(&parser.tm);
if (timestamp == (time_t)-1)
return FALSE;
*timezone_offset_r = parser.timezone_offset;
*tm_r = parser.tm;
*timestamp_r = timestamp - parser.timezone_offset * 60;
return TRUE;
}
bool iso8601_date_parse(const unsigned char *data, size_t size,
time_t *timestamp_r, int *timezone_offset_r)
{
struct tm tm;
return iso8601_date_do_parse(data, size, &tm,
timestamp_r, timezone_offset_r);
}
bool iso8601_date_parse_tm(const unsigned char *data, size_t size,
struct tm *tm_r, int *timezone_offset_r)
{
time_t timestamp;
return iso8601_date_do_parse(data, size, tm_r,
×tamp, timezone_offset_r);
}
const char *iso8601_date_create_tm(struct tm *tm, int timezone_offset)
{
const char *time_offset;
if (timezone_offset == INT_MAX)
time_offset = "Z";
else {
char sign = '+';
if (timezone_offset < 0) {
timezone_offset = -timezone_offset;
sign = '-';
}
time_offset = t_strdup_printf("%c%02d:%02d", sign,
timezone_offset / 60,
timezone_offset % 60);
}
return t_strdup_printf("%04d-%02d-%02dT%02d:%02d:%02d%s",
tm->tm_year + 1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, time_offset);
}
const char *iso8601_date_create(time_t timestamp)
{
struct tm *tm;
int timezone_offset;
tm = localtime(×tamp);
timezone_offset = utc_offset(tm, timestamp);
return iso8601_date_create_tm(tm, timezone_offset);
}
|