2 * Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
3 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
5 #include "cmogstored.h"
7 /* avoiding strftime() since it's locale-aware */
9 #define DATELEN (MOG_HTTPDATE_CAPA - 1)
10 static const char week
[] = "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat";
11 static const char months
[] = "Jan\0Feb\0Mar\0Apr\0May\0Jun\0"
12 "Jul\0Aug\0Sep\0Oct\0Nov\0Dec";
14 __attribute__((constructor
)) static void http_date_init(void)
16 time_t now
= time(NULL
);
20 * call gmtime_r once in the main loop to load, TZ info.
21 * Initial run uses a lot of stack on *BSDs
26 char *mog_http_date(char *dst
, size_t len
, const time_t *timep
)
31 assert(len
>= MOG_HTTPDATE_CAPA
&& "date length incorrect");
34 /* snprintf eats stack :( */
35 rc
= snprintf(dst
, len
, "%s, %02d %s %4d %02d:%02d:%02d GMT",
36 week
+ (tm
.tm_wday
* 4),
38 months
+ (tm
.tm_mon
* 4),
43 assert(rc
== DATELEN
&& "bad sprintf return value");
48 struct mog_now
*mog_now(void)
50 static __thread
struct mog_now now
;
51 time_t tnow
= time(NULL
); /* not a syscall on modern 64-bit systems */
53 if (now
.ntime
== tnow
)
57 mog_http_date(now
.httpdate
, sizeof(now
.httpdate
), &tnow
);