Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_time_formats.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33
34#include "nilorea/n_common.h"
35#include "nilorea/n_log.h"
36#include "nilorea/n_time.h"
37
39static int log_level = LOG_ERR;
41static int failures = 0;
42
46static void expect_true(const char* what, int ok) {
47 if (!ok) {
48 n_log(LOG_ERR, "FAIL: %s", what);
49 failures++;
50 } else {
51 n_log(LOG_INFO, "ok: %s", what);
52 }
53}
54
59static int process_args(int argc, char** argv) {
60 int opt = 0;
61
62 while ((opt = getopt(argc, argv, "V:")) != -1) {
63 if (opt == 'V') {
64 log_level = atoi(optarg);
65 } else {
66 return -1;
67 }
68 }
69 return 0;
70}
71
73typedef struct {
74 long long t;
75 const char* http;
76 const char* rss_utc;
77 const char* c_utc;
78 long paris;
79 const char* rss_paris;
80 const char* c_paris;
82
84static void test_format(void) {
85 static const date_vector v[] = {
86 {0, "Thu, 01 Jan 1970 00:00:00 GMT", "Thu, 01 Jan 1970 00:00:00 +0000", "1970-01-01T00:00:00+00:00", 3600,
87 "Thu, 01 Jan 1970 01:00:00 +0100", "1970-01-01T01:00:00+01:00"},
88 {784111777, "Sun, 06 Nov 1994 08:49:37 GMT", "Sun, 06 Nov 1994 08:49:37 +0000", "1994-11-06T08:49:37+00:00", 3600,
89 "Sun, 06 Nov 1994 09:49:37 +0100", "1994-11-06T09:49:37+01:00"},
90 {951782400, "Tue, 29 Feb 2000 00:00:00 GMT", "Tue, 29 Feb 2000 00:00:00 +0000", "2000-02-29T00:00:00+00:00", 3600,
91 "Tue, 29 Feb 2000 01:00:00 +0100", "2000-02-29T01:00:00+01:00"},
92 {1792794600, "Fri, 23 Oct 2026 22:30:00 GMT", "Fri, 23 Oct 2026 22:30:00 +0000", "2026-10-23T22:30:00+00:00", 7200,
93 "Sat, 24 Oct 2026 00:30:00 +0200", "2026-10-24T00:30:00+02:00"},
94 {-1, "Wed, 31 Dec 1969 23:59:59 GMT", "Wed, 31 Dec 1969 23:59:59 +0000", "1969-12-31T23:59:59+00:00", 3600,
95 "Thu, 01 Jan 1970 00:59:59 +0100", "1970-01-01T00:59:59+01:00"},
96 };
97 char buf[64];
98 size_t i;
99 for (i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
100 time_t t = (time_t)v[i].t;
101 expect_true(v[i].http, n_time_http_date(t, buf, sizeof(buf)) && strcmp(buf, v[i].http) == 0);
102 expect_true(v[i].rss_utc, n_time_rfc2822(t, 0, buf, sizeof(buf)) && strcmp(buf, v[i].rss_utc) == 0);
103 expect_true(v[i].c_utc, n_time_rfc3339(t, 0, buf, sizeof(buf)) && strcmp(buf, v[i].c_utc) == 0);
104 expect_true(v[i].rss_paris, n_time_rfc2822(t, v[i].paris, buf, sizeof(buf)) && strcmp(buf, v[i].rss_paris) == 0);
105 expect_true(v[i].c_paris, n_time_rfc3339(t, v[i].paris, buf, sizeof(buf)) && strcmp(buf, v[i].c_paris) == 0);
106 }
107 expect_true("negative offset", n_time_rfc3339(0, -(5 * 3600 + 30 * 60), buf, sizeof(buf)) && strcmp(buf, "1969-12-31T18:30:00-05:30") == 0);
108 expect_true("negative offset RFC 2822", n_time_rfc2822(0, -(5 * 3600 + 30 * 60), buf, sizeof(buf)) && strcmp(buf, "Wed, 31 Dec 1969 18:30:00 -0530") == 0);
109 if (sizeof(time_t) >= 8) {
110 expect_true("year 9999", n_time_http_date((time_t)253402300799LL, buf, sizeof(buf)) && strcmp(buf, "Fri, 31 Dec 9999 23:59:59 GMT") == 0);
111 expect_true("year 1", n_time_http_date((time_t)-62135596800LL, buf, sizeof(buf)) && strcmp(buf, "Mon, 01 Jan 0001 00:00:00 GMT") == 0);
112 expect_true("year 10000 refused", n_time_http_date((time_t)253402300800LL, buf, sizeof(buf)) == FALSE);
113 expect_true("year 10000 by offset refused", n_time_rfc3339((time_t)253402300799LL, 3600, buf, sizeof(buf)) == FALSE);
114 expect_true("year 0 refused", n_time_http_date((time_t)-62135596801LL, buf, sizeof(buf)) == FALSE);
115 }
116 expect_true("small buffer refused", n_time_http_date(0, buf, N_TIME_HTTP_DATE_LEN - 1) == FALSE);
117 expect_true("exact buffer", n_time_http_date(0, buf, N_TIME_HTTP_DATE_LEN) == TRUE && strlen(buf) == N_TIME_HTTP_DATE_LEN - 1);
118 expect_true("exact RFC 2822 buffer", n_time_rfc2822(0, 0, buf, N_TIME_RFC2822_LEN) == TRUE && strlen(buf) == N_TIME_RFC2822_LEN - 1);
119 expect_true("exact RFC 3339 buffer", n_time_rfc3339(0, 0, buf, N_TIME_RFC3339_LEN) == TRUE && strlen(buf) == N_TIME_RFC3339_LEN - 1);
120 expect_true("NULL buffer refused", n_time_http_date(0, NULL, 64) == FALSE);
121 expect_true("offset not a whole minute refused", n_time_rfc3339(0, 561, buf, sizeof(buf)) == FALSE);
122 expect_true("offset of a day refused", n_time_rfc2822(0, 86400, buf, sizeof(buf)) == FALSE);
123}
124
131static int parses(int http, const char* s, int ok, long long want) {
132 time_t t = (time_t)12345;
133 int r = http ? n_time_parse_http_date(s, strlen(s), &t) : n_time_parse_rfc3339(s, strlen(s), &t);
134 if (!ok)
135 return r == FALSE && t == (time_t)12345;
136 return r == TRUE && (long long)t == want;
137}
138
140static void test_parse(void) {
141 char buf[64];
142 long long t;
143
144 expect_true("HTTP date parses", parses(1, "Sun, 06 Nov 1994 08:49:37 GMT", 1, 784111777));
145 expect_true("HTTP date, 29 Feb 2000", parses(1, "Tue, 29 Feb 2000 00:00:00 GMT", 1, 951782400));
146 expect_true("HTTP date before 1970", parses(1, "Wed, 31 Dec 1969 23:59:59 GMT", 1, -1));
147 expect_true("wrong weekday refused", parses(1, "Mon, 06 Nov 1994 08:49:37 GMT", 0, 0));
148 expect_true("29 Feb 2100 refused", parses(1, "Mon, 29 Feb 2100 00:00:00 GMT", 0, 0));
149 expect_true("hour 24 refused", parses(1, "Sun, 06 Nov 1994 24:00:00 GMT", 0, 0));
150 expect_true("leap second refused", parses(1, "Sun, 06 Nov 1994 23:59:60 GMT", 0, 0));
151 expect_true("lowercase month refused", parses(1, "Sun, 06 nov 1994 08:49:37 GMT", 0, 0));
152 expect_true("other zone refused", parses(1, "Sun, 06 Nov 1994 08:49:37 UTC", 0, 0));
153 expect_true("RFC 850 form refused", parses(1, "Sunday, 06-Nov-94 08:49:37 GMT", 0, 0));
154 expect_true("asctime form refused", parses(1, "Sun Nov 6 08:49:37 1994", 0, 0));
155 expect_true("trailing byte refused", parses(1, "Sun, 06 Nov 1994 08:49:37 GMTX", 0, 0));
156 expect_true("sign in a number refused", parses(1, "Sun, +6 Nov 1994 08:49:37 GMT", 0, 0));
157
158 expect_true("SQL form is UTC", parses(0, "2026-10-23 22:30:00", 1, 1792794600));
159 expect_true("RFC 3339 Z", parses(0, "2026-10-23T22:30:00Z", 1, 1792794600));
160 expect_true("RFC 3339 offset", parses(0, "2026-10-24T00:30:00+02:00", 1, 1792794600));
161 expect_true("RFC 3339 negative offset", parses(0, "1969-12-31T18:30:00-05:30", 1, 0));
162 expect_true("bad day refused", parses(0, "2026-02-30 00:00:00", 0, 0));
163 expect_true("month 13 refused", parses(0, "2026-13-01 00:00:00", 0, 0));
164 expect_true("fraction refused", parses(0, "2026-10-23T22:30:00.5Z", 0, 0));
165 expect_true("short refused", parses(0, "2026-10-23", 0, 0));
166 expect_true("bad offset refused", parses(0, "2026-10-23T22:30:00+24:00", 0, 0));
167 expect_true("NULL refused", n_time_parse_rfc3339(NULL, 0, NULL) == FALSE && n_time_parse_http_date(NULL, 0, NULL) == FALSE);
168
169 /* format then parse gives the same instant, both ways */
170 for (t = -2000000000LL; t < 4000000000LL; t += 7777777) {
171 time_t back = 0;
172 if (sizeof(time_t) < 8 && (t > 2147483647LL || t < -2147483647LL))
173 continue;
174 if (!n_time_http_date((time_t)t, buf, sizeof(buf)) || !n_time_parse_http_date(buf, strlen(buf), &back) || (long long)back != t) {
175 n_log(LOG_ERR, "FAIL: HTTP date round trip at %lld (%s)", t, buf);
176 failures++;
177 break;
178 }
179 if (!n_time_rfc3339((time_t)t, -7200, buf, sizeof(buf)) || !n_time_parse_rfc3339(buf, strlen(buf), &back) || (long long)back != t) {
180 n_log(LOG_ERR, "FAIL: RFC 3339 round trip at %lld (%s)", t, buf);
181 failures++;
182 break;
183 }
184 }
185}
186
191int main(int argc, char** argv) {
192 if (process_args(argc, argv) != 0) {
193 fprintf(stderr, "usage: %s [-V LOG_LEVEL]\n", argv[0]);
194 return 1;
195 }
197
198 test_format();
199 test_parse();
200
201 if (failures) {
202 n_log(LOG_ERR, "ex_time_formats: %d failure(s)", failures);
203 return 1;
204 }
205 n_log(LOG_NOTICE, "ex_time_formats: all checks passed");
206 return 0;
207}
void process_args(int argc, char **argv)
static int failures
Definition ex_atlas.c:51
int main(void)
Definition ex_atlas.c:117
static void expect_true(const char *label, int cond)
Definition ex_atlas.c:53
static int log_level
static void test_parse(void)
Definition ex_dns.c:102
const char * rss_utc
gmdate(DATE_RSS)
const char * rss_paris
const char * c_paris
long paris
Europe/Paris offset at t, seconds.
static void test_format(void)
formatting, compared with PHP's output
const char * c_utc
gmdate('c')
const char * http
gmdate('D, d M Y H:i:s') .
static int parses(int http, const char *s, int ok, long long want)
parse s with the given parser and compare with want (or expect failure)
a timestamp and what PHP prints for it
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:113
#define LOG_ERR
error conditions
Definition n_log.h:100
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
Definition n_log.c:123
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:104
#define LOG_INFO
informational
Definition n_log.h:106
int n_time_parse_http_date(const char *s, size_t len, time_t *t)
parse an IMF-fixdate HTTP date strictly
Definition n_time.c:313
int n_time_rfc2822(time_t t, long offset, char *out, size_t size)
format t as an RFC 2822 date at offset seconds east of UTC
Definition n_time.c:254
int n_time_parse_rfc3339(const char *s, size_t len, time_t *t)
parse "YYYY-MM-DD[T| ]HH:MM:SS[Z|+hh:mm|-hh:mm]" (UTC without a zone)
Definition n_time.c:340
#define N_TIME_RFC2822_LEN
buffer size for n_time_rfc2822(): "Thu, 01 Jan 1970 00:00:00 +0000" + NUL
Definition n_time.h:92
#define N_TIME_HTTP_DATE_LEN
buffer size for n_time_http_date(): "Thu, 01 Jan 1970 00:00:00 GMT" + NUL
Definition n_time.h:90
int n_time_http_date(time_t t, char *out, size_t size)
format t as an HTTP date (RFC 9110 IMF-fixdate, always GMT)
Definition n_time.c:237
int n_time_rfc3339(time_t t, long offset, char *out, size_t size)
format t as RFC 3339 at offset seconds east of UTC ("+00:00" for UTC)
Definition n_time.c:272
#define N_TIME_RFC3339_LEN
buffer size for n_time_rfc3339(): "1970-01-01T00:00:00+00:00" + NUL
Definition n_time.h:94
Common headers and low-level functions & define.
Generic log system.
Timing utilities.