diff --git a/include/re_fmt.h b/include/re_fmt.h index 884cd0b..720880b 100644 --- a/include/re_fmt.h +++ b/include/re_fmt.h @@ -108,6 +108,7 @@ size_t str_len(const char *s); /* time */ +int fmt_gmtime(struct re_printf *pf, void *ts); int fmt_human_time(struct re_printf *pf, const uint32_t *seconds); diff --git a/src/fmt/time.c b/src/fmt/time.c index 23f3f0a..e86396b 100644 --- a/src/fmt/time.c +++ b/src/fmt/time.c @@ -3,10 +3,48 @@ * * Copyright (C) 2010 Creytiv.com */ +#include #include #include +static const char *dayv[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; + +static const char *monv[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + + +/** + * Print Greenwich Mean Time + * + * @param pf Print function for output + * @param ts Time in seconds since the Epoch or NULL for current time + * + * @return 0 if success, otherwise errorcode + */ +int fmt_gmtime(struct re_printf *pf, void *ts) +{ + const struct tm *tm; + time_t t; + + if (!ts) { + t = time(NULL); + ts = &t; + } + + tm = gmtime(ts); + if (!tm) + return EINVAL; + + return re_hprintf(pf, "%s, %02u %s %u %02u:%02u:%02u GMT", + dayv[min((unsigned)tm->tm_wday, ARRAY_SIZE(dayv)-1)], + tm->tm_mday, + monv[min((unsigned)tm->tm_mon, ARRAY_SIZE(monv)-1)], + tm->tm_year + 1900, + tm->tm_hour, tm->tm_min, tm->tm_sec); +} + + /** * Print the human readable time *