Skip to content

Commit 31d46aa

Browse files
committed
Add support for epoch timestamps
Fixes NLnetLabs#110.
1 parent c765838 commit 31d46aa

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

src/generic/time.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ nonnull_all
3232
static really_inline int32_t parse_time(
3333
parser_t *parser,
3434
const type_info_t *type,
35-
const rdata_info_t *item,
35+
const rdata_info_t *field,
3636
rdata_t *rdata,
3737
const token_t *token)
3838
{
39+
if (token->length != 14)
40+
return parse_int32(parser, type, field, rdata, token);
41+
3942
uint64_t d[14]; // YYYYmmddHHMMSS
4043
const char *p = token->data;
4144
for (int i = 0; i < 14; i++) {
4245
d[i] = (uint8_t)p[i] - '0';
4346
if (d[i] > 9)
44-
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(item), NAME(type));
47+
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), NAME(type));
4548
}
4649

47-
if (token->length != 14)
48-
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(item), NAME(type));
49-
5050
// code adapted from Python 2.4.1 sources (Lib/calendar.py)
5151
const uint64_t year = (d[0] * 1000) + (d[1] * 100) + (d[2] * 10) + d[3];
5252
const uint64_t mon = (d[4] * 10) + d[5];
@@ -56,17 +56,17 @@ static really_inline int32_t parse_time(
5656
const uint64_t sec = (d[12] * 10) + d[13];
5757

5858
if (year < 1970 || year > 2106)
59-
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(item), NAME(type));
59+
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), NAME(type));
6060

6161
uint64_t leap_year = is_leap_year(year);
6262
uint64_t days = 365 * (year - 1970) + leap_days(1970, year);
6363

6464
if (!mon || mon > 12)
65-
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(item), NAME(type));
65+
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), NAME(type));
6666
if (!mday || mday > days_in_month[mon] + (leap_year & (mon == 2)))
67-
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(item), NAME(type));
67+
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), NAME(type));
6868
if (hour > 23 || min > 59 || sec > 59)
69-
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(item), NAME(type));
69+
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), NAME(type));
7070

7171
days += days_to_month[mon];
7272
days += (mon > 2) & leap_year;

src/westmere/time.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ static really_inline int32_t parse_time(
125125
{
126126
uint32_t time;
127127

128-
// FIXME: once support for specifying as an unsigned number of seconds is
129-
// implemented, update this check too
130-
if (token->length != 14 || !sse_parse_time(token->data, &time))
128+
if (unlikely(token->length != 14))
129+
return parse_int32(parser, type, field, rdata, token);
130+
if (!sse_parse_time(token->data, &time))
131131
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), NAME(type));
132132

133133
time = htobe32(time);

tests/time.c

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ void time_stamp_syntax(void **state)
4646
uint32_t seconds;
4747
int32_t result;
4848
} tests[] = {
49+
// Time specified in seconds since epoch
50+
{ "4294967295", 4294967295, ZONE_SUCCESS },
51+
// one second over maximum value
52+
{ "4294967296", 0, ZONE_SYNTAX_ERROR },
53+
// starts with zero
54+
{ "01", 0, ZONE_SYNTAX_ERROR },
55+
// Time specified as YYYYMMDDHHmmSS
4956
// bad number of digits
5057
{ "202301010101", 0, ZONE_SYNTAX_ERROR },
5158
{ "202301010101010", 0, ZONE_SYNTAX_ERROR },
@@ -91,6 +98,7 @@ void time_stamp_syntax(void **state)
9198
char *rr = malloc((size_t)size + 1);
9299
(void)snprintf(rr, size, FORMAT, tests[i].timestamp);
93100

101+
fprintf(stderr, "INPUT: %s\n", rr);
94102
options.accept.callback = add_rr;
95103
options.origin.octets = origin;
96104
options.origin.length = sizeof(origin);

0 commit comments

Comments
 (0)