Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_time_formats.c

n_time date formats: HTTP dates (format and strict parse), RFC 2822 for RSS, RFC 3339 (format and parse); expected strings from PHP 8.4

n_time date formats: HTTP dates (format and strict parse), RFC 2822 for RSS, RFC 3339 (format and parse); expected strings from PHP 8.4

Author
Castagnier Mickael
Version
1.0
Date
23/10/2026
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "nilorea/n_log.h"
#include "nilorea/n_time.h"
static int log_level = LOG_ERR;
static int failures = 0;
static void expect_true(const char* what, int ok) {
if (!ok) {
n_log(LOG_ERR, "FAIL: %s", what);
} else {
n_log(LOG_INFO, "ok: %s", what);
}
}
static int process_args(int argc, char** argv) {
int opt = 0;
while ((opt = getopt(argc, argv, "V:")) != -1) {
if (opt == 'V') {
log_level = atoi(optarg);
} else {
return -1;
}
}
return 0;
}
typedef struct {
long long t;
const char* http;
const char* rss_utc;
const char* c_utc;
long paris;
const char* rss_paris;
const char* c_paris;
static void test_format(void) {
static const date_vector v[] = {
{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,
"Thu, 01 Jan 1970 01:00:00 +0100", "1970-01-01T01:00:00+01:00"},
{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,
"Sun, 06 Nov 1994 09:49:37 +0100", "1994-11-06T09:49:37+01:00"},
{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,
"Tue, 29 Feb 2000 01:00:00 +0100", "2000-02-29T01:00:00+01:00"},
{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,
"Sat, 24 Oct 2026 00:30:00 +0200", "2026-10-24T00:30:00+02:00"},
{-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,
"Thu, 01 Jan 1970 00:59:59 +0100", "1970-01-01T00:59:59+01:00"},
};
char buf[64];
size_t i;
for (i = 0; i < sizeof(v) / sizeof(v[0]); i++) {
time_t t = (time_t)v[i].t;
expect_true(v[i].http, n_time_http_date(t, buf, sizeof(buf)) && strcmp(buf, v[i].http) == 0);
expect_true(v[i].rss_utc, n_time_rfc2822(t, 0, buf, sizeof(buf)) && strcmp(buf, v[i].rss_utc) == 0);
expect_true(v[i].c_utc, n_time_rfc3339(t, 0, buf, sizeof(buf)) && strcmp(buf, v[i].c_utc) == 0);
expect_true(v[i].rss_paris, n_time_rfc2822(t, v[i].paris, buf, sizeof(buf)) && strcmp(buf, v[i].rss_paris) == 0);
expect_true(v[i].c_paris, n_time_rfc3339(t, v[i].paris, buf, sizeof(buf)) && strcmp(buf, v[i].c_paris) == 0);
}
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);
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);
if (sizeof(time_t) >= 8) {
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);
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);
expect_true("year 10000 refused", n_time_http_date((time_t)253402300800LL, buf, sizeof(buf)) == FALSE);
expect_true("year 10000 by offset refused", n_time_rfc3339((time_t)253402300799LL, 3600, buf, sizeof(buf)) == FALSE);
expect_true("year 0 refused", n_time_http_date((time_t)-62135596801LL, buf, sizeof(buf)) == FALSE);
}
expect_true("small buffer refused", n_time_http_date(0, buf, N_TIME_HTTP_DATE_LEN - 1) == FALSE);
expect_true("exact buffer", n_time_http_date(0, buf, N_TIME_HTTP_DATE_LEN) == TRUE && strlen(buf) == N_TIME_HTTP_DATE_LEN - 1);
expect_true("exact RFC 2822 buffer", n_time_rfc2822(0, 0, buf, N_TIME_RFC2822_LEN) == TRUE && strlen(buf) == N_TIME_RFC2822_LEN - 1);
expect_true("exact RFC 3339 buffer", n_time_rfc3339(0, 0, buf, N_TIME_RFC3339_LEN) == TRUE && strlen(buf) == N_TIME_RFC3339_LEN - 1);
expect_true("NULL buffer refused", n_time_http_date(0, NULL, 64) == FALSE);
expect_true("offset not a whole minute refused", n_time_rfc3339(0, 561, buf, sizeof(buf)) == FALSE);
expect_true("offset of a day refused", n_time_rfc2822(0, 86400, buf, sizeof(buf)) == FALSE);
}
static int parses(int http, const char* s, int ok, long long want) {
time_t t = (time_t)12345;
int r = http ? n_time_parse_http_date(s, strlen(s), &t) : n_time_parse_rfc3339(s, strlen(s), &t);
if (!ok)
return r == FALSE && t == (time_t)12345;
return r == TRUE && (long long)t == want;
}
static void test_parse(void) {
char buf[64];
long long t;
expect_true("HTTP date parses", parses(1, "Sun, 06 Nov 1994 08:49:37 GMT", 1, 784111777));
expect_true("HTTP date, 29 Feb 2000", parses(1, "Tue, 29 Feb 2000 00:00:00 GMT", 1, 951782400));
expect_true("HTTP date before 1970", parses(1, "Wed, 31 Dec 1969 23:59:59 GMT", 1, -1));
expect_true("wrong weekday refused", parses(1, "Mon, 06 Nov 1994 08:49:37 GMT", 0, 0));
expect_true("29 Feb 2100 refused", parses(1, "Mon, 29 Feb 2100 00:00:00 GMT", 0, 0));
expect_true("hour 24 refused", parses(1, "Sun, 06 Nov 1994 24:00:00 GMT", 0, 0));
expect_true("leap second refused", parses(1, "Sun, 06 Nov 1994 23:59:60 GMT", 0, 0));
expect_true("lowercase month refused", parses(1, "Sun, 06 nov 1994 08:49:37 GMT", 0, 0));
expect_true("other zone refused", parses(1, "Sun, 06 Nov 1994 08:49:37 UTC", 0, 0));
expect_true("RFC 850 form refused", parses(1, "Sunday, 06-Nov-94 08:49:37 GMT", 0, 0));
expect_true("asctime form refused", parses(1, "Sun Nov 6 08:49:37 1994", 0, 0));
expect_true("trailing byte refused", parses(1, "Sun, 06 Nov 1994 08:49:37 GMTX", 0, 0));
expect_true("sign in a number refused", parses(1, "Sun, +6 Nov 1994 08:49:37 GMT", 0, 0));
expect_true("SQL form is UTC", parses(0, "2026-10-23 22:30:00", 1, 1792794600));
expect_true("RFC 3339 Z", parses(0, "2026-10-23T22:30:00Z", 1, 1792794600));
expect_true("RFC 3339 offset", parses(0, "2026-10-24T00:30:00+02:00", 1, 1792794600));
expect_true("RFC 3339 negative offset", parses(0, "1969-12-31T18:30:00-05:30", 1, 0));
expect_true("bad day refused", parses(0, "2026-02-30 00:00:00", 0, 0));
expect_true("month 13 refused", parses(0, "2026-13-01 00:00:00", 0, 0));
expect_true("fraction refused", parses(0, "2026-10-23T22:30:00.5Z", 0, 0));
expect_true("short refused", parses(0, "2026-10-23", 0, 0));
expect_true("bad offset refused", parses(0, "2026-10-23T22:30:00+24:00", 0, 0));
expect_true("NULL refused", n_time_parse_rfc3339(NULL, 0, NULL) == FALSE && n_time_parse_http_date(NULL, 0, NULL) == FALSE);
/* format then parse gives the same instant, both ways */
for (t = -2000000000LL; t < 4000000000LL; t += 7777777) {
time_t back = 0;
if (sizeof(time_t) < 8 && (t > 2147483647LL || t < -2147483647LL))
continue;
if (!n_time_http_date((time_t)t, buf, sizeof(buf)) || !n_time_parse_http_date(buf, strlen(buf), &back) || (long long)back != t) {
n_log(LOG_ERR, "FAIL: HTTP date round trip at %lld (%s)", t, buf);
break;
}
if (!n_time_rfc3339((time_t)t, -7200, buf, sizeof(buf)) || !n_time_parse_rfc3339(buf, strlen(buf), &back) || (long long)back != t) {
n_log(LOG_ERR, "FAIL: RFC 3339 round trip at %lld (%s)", t, buf);
break;
}
}
}
int main(int argc, char** argv) {
if (process_args(argc, argv) != 0) {
fprintf(stderr, "usage: %s [-V LOG_LEVEL]\n", argv[0]);
return 1;
}
if (failures) {
n_log(LOG_ERR, "ex_time_formats: %d failure(s)", failures);
return 1;
}
n_log(LOG_NOTICE, "ex_time_formats: all checks passed");
return 0;
}
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
static void test_format(void)
formatting, compared with PHP's output
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.