#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <err.h>
#include <errno.h>
#include "../lpi.h"

void usage(void);

int
main(int argc, char *argv[])
{
    long t;
    struct tm *tmptr;

    if (argc < 2)
        usage();

    if (toLong(argv[1], &t) != 0)
        err(EXIT_FAILURE, "toLong");

    if (argc <= 2 || strcmp(argv[2], "gmtime") == 0)
        tmptr = gmtime((time_t *) &t);
    else if (strcmp(argv[2], "localtime") == 0)
        tmptr = localtime((time_t *) &t);
    else
        usage();

    printf("%s", asctime(tmptr));

    exit(EXIT_SUCCESS);
}

void
usage(void)
{
    fprintf(stderr, "usage: %s secondes-since-epoch {gmtime|localtime}\n",
            program_invocation_name);
    exit(EXIT_FAILURE);
}
