1 ex__time__formats_8c
gullradriel edited this page 2026-10-25 10:46:33 +01:00

examples/ex_time_formats.c

Structures

Name
struct date_vector
a timestamp and what PHP prints for it

Functions

Name
void test_parse(void )
parsing
void test_format(void )
formatting, compared with PHP's output
int process_args(int argc, char ** argv)
read -V
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)
int main(int argc, char ** argv)
run the checks
void expect_true(const char * what, int ok)
count a failed check

Attributes

Name
int log_level
log level from -V
int failures
failed checks

Functions Documentation

function test_parse

static void test_parse(
    void 
)

parsing

function test_format

static void test_format(
    void 
)

formatting, compared with PHP's output

function process_args

static int process_args(
    int argc,
    char ** argv
)

read -V

Parameters:

  • argc argument count
  • argv arguments

Return: 0, or -1 on a bad argument

function parses

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)

Parameters:

  • http non-zero: HTTP date parser, else RFC 3339
  • s the text
  • ok expected success
  • want expected time when ok

Return: non-zero when the outcome is the expected one

function main

int main(
    int argc,
    char ** argv
)

run the checks

Parameters:

  • argc argument count
  • argv arguments

Return: 0 when every check passes

function expect_true

static void expect_true(
    const char * what,
    int ok
)

count a failed check

Parameters:

  • what the check
  • ok its result

Attributes Documentation

variable log_level

static int log_level = 3;

log level from -V

variable failures

static int failures = 0;

failed checks

Source code

/*
 * 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_common.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);
        failures++;
    } 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;
} date_vector;

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);
            failures++;
            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);
            failures++;
            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;
    }
    set_log_level(log_level);

    test_format();
    test_parse();

    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;
}