patch: add fmt_gmtime() according to RFC 1123
This commit is contained in:
parent
f8b6ec4c24
commit
b3d47ec333
2 changed files with 39 additions and 0 deletions
|
@ -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);
|
||||
|
||||
|
||||
|
|
|
@ -3,10 +3,48 @@
|
|||
*
|
||||
* Copyright (C) 2010 Creytiv.com
|
||||
*/
|
||||
#include <time.h>
|
||||
#include <re_types.h>
|
||||
#include <re_fmt.h>
|
||||
|
||||
|
||||
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
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue