#define _GNU_SOURCE
#define _XOPEN_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <locale.h>
#include <err.h>
#include <errno.h>

#define SBUF_SIZE 1000

void usage(void);

int
main(int argc, char *argv[])
{
    struct tm tm;
    char sbuf[SBUF_SIZE];
    char *ofmt;

    if (argc < 3 || strcmp(argv[1], "--help") == 0)
        usage();

    if (setlocale(LC_ALL, "") == NULL)
        errx(EXIT_FAILURE, "setlocale");

    memset(&tm, 0, sizeof(struct tm));
    if (strptime(argv[1], argv[2], &tm) == NULL)
        errx(EXIT_FAILURE, "strptime");

    tm.tm_isdst = -1;

    printf("calendar time (seconds sice Epoch): %ld\n", (long) mktime(&tm));

    ofmt = (argc > 3) ? argv[3] : "%H:%M:%S, %d %B %Y %Z";
    if (strftime(sbuf, SBUF_SIZE, ofmt, &tm) == 0)
        err(EXIT_FAILURE, "strftime");
    printf("strftime() yields: %s\n", sbuf);

    exit(EXIT_SUCCESS);
}

void
usage(void)
{
    errx(EXIT_FAILURE, "usage: %s input-date-time in-format [out-format]",
            program_invocation_name);
}
