1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include <stdlib.h>
30 #include "pool.h"
31
32 #include "date.h"
33
34 char *date_wkday[] = {
35 "Sun",
36 "Mon",
37 "Tue",
38 "Wed",
39 "Thu",
40 "Fri",
41 "Sat"
42 };
43
44 char *date_month[] = {
45 "Jan",
46 "Feb",
47 "Mar",
48 "Apr",
49 "May",
50 "Jun",
51 "Jul",
52 "Aug",
53 "Sep",
54 "Oct",
55 "Nov",
56 "Dec"
57 };
58
59 sstr_t date_format_http(
time_t t,
pool_handle_t *pool) {
60 sstr_t d;
61 d.ptr =
NULL;
62 d.length =
0;
63
64 struct tm date;
65 gmtime_r(&t, &date);
66
67 char *str = pool_malloc(pool,
32);
68 snprintf(
69 str,
70 32,
71 "%s, %02d %s %d %02d:%02d:%02d GMT",
72 date_wkday[date.tm_wday],
73 date.tm_mday,
74 date_month[date.tm_mon],
75 1900 + date.tm_year,
76 date.tm_hour,
77 date.tm_min,
78 date.tm_sec);
79
80 d.length =
29;
81 d.ptr = str;
82
83 return d;
84 }
85
86 sstr_t date_format_iso8601(
time_t t,
pool_handle_t *pool) {
87 sstr_t d;
88 d.ptr =
NULL;
89 d.length =
0;
90
91 struct tm date;
92 gmtime_r(&t, &date);
93
94 char *str = pool_malloc(pool,
24);
95 snprintf(
96 str,
97 24,
98 "%d-%02d-%02dT%02d:%02d:%02dZ",
99 1900 + date.tm_year,
100 date.tm_mon +
1,
101 date.tm_mday,
102 date.tm_hour,
103 date.tm_min,
104 date.tm_sec);
105
106 d.length =
20;
107 d.ptr = str;
108
109 return d;
110 }
111