/*
* parse time string - user friendly date and time parser
* Copyright © 2012 Jani Nikula
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Author: Jani Nikula
*/
#include
#include
#include
#include
#include
#include
#include "parse-time-string.h"
#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
static const char *parse_time_error_strings[] = {
[PARSE_TIME_OK] = "OK",
[PARSE_TIME_ERR] = "ERR",
[PARSE_TIME_ERR_LIB] = "LIB",
[PARSE_TIME_ERR_ALREADYSET] = "ALREADYSET",
[PARSE_TIME_ERR_FORMAT] = "FORMAT",
[PARSE_TIME_ERR_DATEFORMAT] = "DATEFORMAT",
[PARSE_TIME_ERR_TIMEFORMAT] = "TIMEFORMAT",
[PARSE_TIME_ERR_INVALIDDATE] = "INVALIDDATE",
[PARSE_TIME_ERR_INVALIDTIME] = "INVALIDTIME",
[PARSE_TIME_ERR_KEYWORD] = "KEYWORD",
};
static const char *
parse_time_strerror (unsigned int errnum)
{
if (errnum < ARRAY_SIZE (parse_time_error_strings))
return parse_time_error_strings[errnum];
else
return NULL;
}
/*
* concat argv[start]...argv[end - 1], separating them by a single
* space, to a malloced string
*/
static char *
concat_args (int start, int end, char *argv[])
{
int i;
size_t len = 1;
char *p;
for (i = start; i < end; i++)
len += strlen (argv[i]) + 1;
p = malloc (len);
if (! p)
return NULL;
*p = 0;
for (i = start; i < end; i++) {
if (i != start)
strcat (p, " ");
strcat (p, argv[i]);
}
return p;
}
#define DEFAULT_FORMAT "%a %b %d %T %z %Y"
static void
usage (const char *name)
{
printf ("Usage: %s [options ...] []\n\n", name);
printf (
"Parse and display it in given format. If is\n"
"not given, parse each line in stdin according to:\n\n"
" [(==>|==_>|==^>|==^^>)] [#]\n\n"
"and produce output:\n\n"
" (==>|==_>|==^>|==^^>)