blob: 4ca8be672494c204b2c91bf1d76169a46173d7c1 [file] [log] [blame]
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00007#include <ngx_config.h>
Igor Sysoevd59a0472003-11-10 21:09:22 +00008#include <ngx_core.h>
9
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000010
Igor Sysoevfcb5a702008-08-30 19:52:07 +000011/*
12 * FreeBSD does not test /etc/localtime change, however, we can workaround it
13 * by calling tzset() with TZ and then without TZ to update timezone.
14 * The trick should work since FreeBSD 2.1.0.
15 *
16 * Linux does not test /etc/localtime change in localtime(),
17 * but may stat("/etc/localtime") several times in every strftime(),
18 * therefore we use it to update timezone.
19 *
20 * Solaris does not test /etc/TIMEZONE change too and no workaround available.
21 */
22
23void
24ngx_timezone_update(void)
25{
26#if (NGX_FREEBSD)
27
28 if (getenv("TZ")) {
29 return;
30 }
31
32 putenv("TZ=UTC");
33
34 tzset();
35
36 unsetenv("TZ");
37
38 tzset();
39
40#elif (NGX_LINUX)
41 time_t s;
42 struct tm *t;
43 char buf[4];
44
45 s = time(0);
46
47 t = localtime(&s);
48
49 strftime(buf, 4, "%H", t);
50
51#endif
52}
53
54
Igor Sysoevc2068d02005-10-19 12:33:58 +000055void
56ngx_localtime(time_t s, ngx_tm_t *tm)
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000057{
Igor Sysoevf6906042004-11-25 16:17:31 +000058#if (NGX_HAVE_LOCALTIME_R)
Igor Sysoevc2068d02005-10-19 12:33:58 +000059 (void) localtime_r(&s, tm);
Igor Sysoevd59a0472003-11-10 21:09:22 +000060
Igor Sysoev27c30f92003-11-11 18:13:43 +000061#else
62 ngx_tm_t *t;
63
Igor Sysoevc2068d02005-10-19 12:33:58 +000064 t = localtime(&s);
Igor Sysoev27c30f92003-11-11 18:13:43 +000065 *tm = *t;
66
67#endif
68
Igor Sysoev5eef6182002-12-15 21:08:04 +000069 tm->ngx_tm_mon++;
70 tm->ngx_tm_year += 1900;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000071}
Igor Sysoev899b44e2005-05-12 14:58:06 +000072
73
Igor Sysoevc2068d02005-10-19 12:33:58 +000074void
75ngx_libc_localtime(time_t s, struct tm *tm)
Igor Sysoev899b44e2005-05-12 14:58:06 +000076{
77#if (NGX_HAVE_LOCALTIME_R)
Igor Sysoevc2068d02005-10-19 12:33:58 +000078 (void) localtime_r(&s, tm);
Igor Sysoev899b44e2005-05-12 14:58:06 +000079
80#else
Igor Sysoev899b44e2005-05-12 14:58:06 +000081 struct tm *t;
82
Igor Sysoevc2068d02005-10-19 12:33:58 +000083 t = localtime(&s);
Igor Sysoev899b44e2005-05-12 14:58:06 +000084 *tm = *t;
85
86#endif
87}
88
89
Igor Sysoevc2068d02005-10-19 12:33:58 +000090void
91ngx_libc_gmtime(time_t s, struct tm *tm)
Igor Sysoev899b44e2005-05-12 14:58:06 +000092{
93#if (NGX_HAVE_LOCALTIME_R)
Igor Sysoevc2068d02005-10-19 12:33:58 +000094 (void) gmtime_r(&s, tm);
Igor Sysoev899b44e2005-05-12 14:58:06 +000095
96#else
Igor Sysoev899b44e2005-05-12 14:58:06 +000097 struct tm *t;
98
Igor Sysoevc2068d02005-10-19 12:33:58 +000099 t = gmtime(&s);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000100 *tm = *t;
101
102#endif
103}