Add the isodate utility.

This utility provides a way to display dates in ISO8601 format. The
exact output format can be controlled. It can either display dates
passed on the commandline or it can display the current date/time.
This commit is contained in:
Laurence Withers 2007-09-13 11:09:32 +00:00
parent 07a31fd8f7
commit 64c5ce0d65
7 changed files with 223 additions and 0 deletions

1
src/isodate/.params Normal file
View File

@ -0,0 +1 @@
app c isodate bin

147
src/isodate/000_TopSource.c Normal file
View File

@ -0,0 +1,147 @@
/* libiso8601/src/isodate/000_TopSource.c
*
* (c)2007, Laurence Withers, <l@lwithers.me.uk>.
* Released under the GNU GPLv2. See file COPYING or
* http://www.gnu.org/copyleft/gpl.html for details.
*/
/* Below are all the includes used throughout the application. */
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include "iso8601.h"
void usage(void)
{
fputs("Usage:\n\n"
" isodate [options] [date ...]\n\n"
"Valid options:\n"
" -h, --help Display this screen.\n"
" -e, --extended Use extended display format.\n"
" -y, --year Only display year.\n"
" -n, --month Display year, month.\n"
" -d, --day Display year, month, day [default].\n"
" -o, --ordinal Display year, ordinal day.\n"
" -w, --week Display ISO year, ISO week.\n"
" -W, --week-day Display ISO year, ISO week, ISO weekday.\n"
" -r, --hour Display hour.\n"
" -R, --hour-fraction Display hour and fraction of hour.\n"
" -m, --minute Display hour, minute.\n"
" -M, --minute-fraction Display hour, minute and fraction of\n"
" minute.\n"
" -s, --second Display hour, minute, second.\n"
" -S, --second-fraction Display hour, minute, second and fraction\n"
" of second [default].\n"
" -U, --utc-offset <n> Display time as if timezone offset was\n"
" <n> seconds.\n"
" -Z, --utc Display time in UTC.\n"
"\n"
"If specified, one or more ISO8601 dates will be parsed and displayed\n"
"in the format given. Otherwise, the system clock will be queried and\n"
"its time displayed.\n"
"\n", stdout);
}
struct option options[] = {
{ "help", no_argument, 0, 'h' },
{ "extended", no_argument, 0, 'e' },
{ "year", no_argument, 0, 'y' },
{ "month", no_argument, 0, 'n' },
{ "day", no_argument, 0, 'd' },
{ "ordinal", no_argument, 0, 'o' },
{ "week", no_argument, 0, 'w' },
{ "week-day", no_argument, 0, 'W' },
{ "hour", no_argument, 0, 'r' },
{ "hour-fraction", no_argument, 0, 'R' },
{ "minute", no_argument, 0, 'm' },
{ "minute-fraction", no_argument, 0, 'M' },
{ "second", no_argument, 0, 's' },
{ "second-fraction", no_argument, 0, 'S' },
{ "utc-offset", required_argument, 0, 'U' },
{ "utc", no_argument, 0, 'Z' },
{ 0, 0, 0, 0 }
};
int main(int argc, char* argv[])
{
struct iso8601_details det = {
.date_prec = iso8601_prec_day,
.time_prec = iso8601_prec_secfrac,
.extended = 0,
.tz_sec = 0
};
int override_tz = 0, ret = 0;
char* endp, datestr[100];
struct iso8601_date date;
struct iso8601_details det2;
while(1) {
switch(getopt_long(argc, argv, "heyndowWrRmMsSU:Z", options, 0)) {
case -1: goto opts_done;
case '?': return 1;
case 'h': usage(); return 0;
case 'e': det.extended = 1; break;
case 'y': det.date_prec = iso8601_prec_year; break;
case 'n': det.date_prec = iso8601_prec_month; break;
case 'd': det.date_prec = iso8601_prec_day; break;
case 'o': det.date_prec = iso8601_prec_ord; break;
case 'w': det.date_prec = iso8601_prec_week; break;
case 'W': det.date_prec = iso8601_prec_wday; break;
case 'r': det.time_prec = iso8601_prec_hour; break;
case 'R': det.time_prec = iso8601_prec_hourfrac; break;
case 'm': det.time_prec = iso8601_prec_min; break;
case 'M': det.time_prec = iso8601_prec_minfrac; break;
case 's': det.time_prec = iso8601_prec_sec; break;
case 'S': det.time_prec = iso8601_prec_secfrac; break;
case 'Z': det.tz_sec = 0; override_tz = 1; break;
case 'U':
endp = 0;
det.tz_sec = strtol(optarg, &endp, 0);
if(!endp || *endp || det.tz_sec <= -86400 || det.tz_sec >= 86400) {
fputs("Invalid UTC offset. Must be an integral number of seconds\n"
"between -86400 and 86400, exclusive.\n", stderr);
return 1;
}
override_tz = 1;
}
}
opts_done:
if(optind == argc) {
iso8601_now(&date, &det2);
if(!override_tz) det.tz_sec = det2.tz_sec;
iso8601_print(datestr, sizeof(datestr), &date, &det);
fputs(datestr, stdout);
putc('\n', stdout);
} else {
for(; optind != argc; ++optind) {
if(iso8601_parse(argv[optind], &date, 0, &det2)) {
fputs("Couldn't parse date.\n", stderr);
ret = 1;
continue;
}
if(!override_tz) det.tz_sec = det2.tz_sec;
iso8601_print(datestr, sizeof(datestr), &date, &det);
fputs(datestr, stdout);
putc('\n', stdout);
}
}
return ret;
}
/* options for text editors
kate: replace-trailing-space-save true; space-indent true; tab-width 4;
vim: expandtab:ts=4:sw=4
*/

43
src/isodate/build.app Normal file
View File

@ -0,0 +1,43 @@
# These are external variables, and shouldn't clash with anything else
# isodate
# isodate_BUILT
#
build_target libiso8601
if [ -z ${isodate_BUILT} ]
then
isodate="obj/isodate"
EXTRAS="${libiso8601} ${libiso8601_DEP_CFLAGS} ${libiso8601_DEP_LIBS}"
echo "Building application ${isodate}..."
do_cmd source src/isodate/build.monolithic || return 1
MODIFIED=0
for test in ${MONOLITHIC_TESTS} ${SRC}
do
if [ ${test} -nt ${isodate} ]
then
MODIFIED=1
break
fi
done
if [ ${MODIFIED} -ne 0 ]
then
echo " Compiling..."
do_cmd ${CC} ${CFLAGS} -I obj -o "${isodate}" ${SRC} ${EXTRAS} || return 1
print_success "Application built"
else
print_success "Application up to date"
fi
isodate_BUILT=1
fi
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -0,0 +1 @@
source src/isodate/build.app

View File

@ -0,0 +1 @@
source src/isodate/build.install-app

View File

@ -0,0 +1,12 @@
build_target isodate
# make paths (this is for Gentoo in particular)
build_dir_tree "${BINDIR}" || return 1
# install binary
echo "Installing binaries into '${BINDIR}'"
install_file "${isodate}" "${BINDIR}" 0755 || return 1
print_success "Done"
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: syntax=sh:expandtab:ts=4:sw=4

View File

@ -0,0 +1,18 @@
# These are external variables, and shouldn't clash with anything else
# isodate_MONOLITHIC
#
SRC="obj/isodate.c"
MONOLITHIC_TESTS="src/isodate/build.app src/isodate/build.monolithic"
if [ -z "${isodate_MONOLITHIC}" ]
then
MONOLITHIC_SOURCE="$(echo src/isodate/*.c)"
make_monolithic ${SRC} C || return 1
isodate_MONOLITHIC=1
MONOLITHIC_DOC="${MONOLITHIC_DOC} ${SRC}"
fi
# kate: replace-trailing-space-save true; space-indent true; tab-width 4;
# vim: syntax=sh:expandtab:ts=4:sw=4