#include <stdlib.h>
#include "pool.h"
#include "date.h"
char *date_wkday[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
};
char *date_month[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
sstr_t date_format_http(
time_t t,
pool_handle_t *pool) {
sstr_t d;
d.ptr =
NULL;
d.length =
0;
struct tm date;
gmtime_r(&t, &date);
char *str = pool_malloc(pool,
32);
snprintf(
str,
32,
"%s, %02d %s %d %02d:%02d:%02d GMT",
date_wkday[date.tm_wday],
date.tm_mday,
date_month[date.tm_mon],
1900 + date.tm_year,
date.tm_hour,
date.tm_min,
date.tm_sec);
d.length =
29;
d.ptr = str;
return d;
}
sstr_t date_format_iso8601(
time_t t,
pool_handle_t *pool) {
sstr_t d;
d.ptr =
NULL;
d.length =
0;
struct tm date;
gmtime_r(&t, &date);
char *str = pool_malloc(pool,
24);
snprintf(
str,
24,
"%d-%02d-%02dT%02d:%02d:%02dZ",
1900 + date.tm_year,
date.tm_mon +
1,
date.tm_mday,
date.tm_hour,
date.tm_min,
date.tm_sec);
d.length =
20;
d.ptr = str;
return d;
}