cmogstored 1.8.1 - use default system stack size
[cmogstored.git] / http_date.c
blob1c711324e13d3168ff19f914f56aad20675b54f5
1 /*
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>
4 */
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);
17 struct tm tm;
20 * call gmtime_r once in the main loop to load, TZ info.
21 * Initial run uses a lot of stack on *BSDs
23 gmtime_r(&now, &tm);
26 char *mog_http_date(char *dst, size_t len, const time_t *timep)
28 int rc;
29 struct tm tm;
31 assert(len >= MOG_HTTPDATE_CAPA && "date length incorrect");
32 gmtime_r(timep, &tm);
34 /* snprintf eats stack :( */
35 rc = snprintf(dst, len, "%s, %02d %s %4d %02d:%02d:%02d GMT",
36 week + (tm.tm_wday * 4),
37 tm.tm_mday,
38 months + (tm.tm_mon * 4),
39 tm.tm_year + 1900,
40 tm.tm_hour,
41 tm.tm_min,
42 tm.tm_sec);
43 assert(rc == DATELEN && "bad sprintf return value");
45 return dst + rc;
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)
54 return &now;
56 now.ntime = tnow;
57 mog_http_date(now.httpdate, sizeof(now.httpdate), &tnow);
59 return &now;