xref: /unit/src/nxt_time.h (revision 2084:7d479274f334)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_UNIX_TIME_H_INCLUDED_
8 #define _NXT_UNIX_TIME_H_INCLUDED_
9 
10 
11 typedef uint64_t           nxt_nsec_t;
12 typedef int64_t            nxt_nsec_int_t;
13 #define NXT_INFINITE_NSEC  ((nxt_nsec_t) -1)
14 
15 
16 typedef struct {
17     nxt_time_t             sec;
18     nxt_uint_t             nsec;
19 } nxt_realtime_t;
20 
21 
22 /*
23  * nxt_monotonic_time_t includes nxt_realtime_t to eliminate
24  * surplus gettimeofday() call on platform without monotonic time.
25  */
26 
27 typedef struct {
28     nxt_realtime_t         realtime;
29     nxt_nsec_t             monotonic;
30     nxt_nsec_t             update;
31 
32 #if !(NXT_HAVE_CLOCK_MONOTONIC || NXT_SOLARIS || NXT_HPUX || NXT_MACOSX)
33     nxt_nsec_t             previous;
34 #endif
35 } nxt_monotonic_time_t;
36 
37 
38 NXT_EXPORT void nxt_realtime(nxt_realtime_t *now);
39 NXT_EXPORT void nxt_monotonic_time(nxt_monotonic_time_t *now);
40 NXT_EXPORT void nxt_localtime(nxt_time_t s, struct tm *tm);
41 NXT_EXPORT void nxt_timezone_update(void);
42 
43 /*
44  * Both localtime() and localtime_r() are not Async-Signal-Safe, therefore,
45  * they can not be used in signal handlers.  Since Daylight Saving Time (DST)
46  * state changes no more than twice a year, a simple workaround is to use
47  * a previously cached GMT offset value and nxt_gmtime():
48  *
49  *     nxt_gmtime(GMT seconds + GMT offset, tm);
50  *
51  * GMT offset with account of current DST state can be obtained only
52  * using localtime()'s struct tm because:
53  *
54  * 1) gettimeofday() does not return GMT offset at almost all platforms.
55  *    MacOSX returns a value cached after the first localtime() call.
56  *    AIX returns GMT offset without account of DST state and indicates
57  *    only that timezone has DST, but does not indicate current DST state.
58  *
59  * 2) There are the "timezone" and "daylight" variables on Linux, Solaris,
60  *    HP-UX, IRIX, and other systems.  The "daylight" variable indicates
61  *    only that timezone has DST, but does not indicate current DST state.
62  *
63  * 3) Solaris and IRIX have the "altzone" variable which contains GMT offset
64  *    for timezone with DST applied, but without account of DST state.
65  *
66  * 4) There is the "struct tm.tm_gmtoff" field on BSD systems and modern Linux.
67  *    This field contains GMT offset with account of DST state.
68  *
69  * 5) The "struct tm.tm_isdst" field returned by localtime() indicates
70  *    current DST state on all platforms.  This field may have three values:
71  *    positive means DST in effect, zero means DST is not in effect, and
72  *    negative means DST state is unknown.
73  */
74 
75 #if (NXT_HAVE_TM_GMTOFF)
76 
77 #define nxt_timezone(tm)                                                      \
78     ((tm)->tm_gmtoff)
79 
80 #elif (NXT_HAVE_ALTZONE)
81 
82 #define nxt_timezone(tm)                                                      \
83     (-(((tm)->tm_isdst > 0) ? altzone : timezone))
84 
85 #else
86 
87 #define nxt_timezone(tm)                                                      \
88     (-(((tm)->tm_isdst > 0) ? timezone + 3600 : timezone))
89 
90 #endif
91 
92 
93 typedef uint32_t           nxt_msec_t;
94 typedef int32_t            nxt_msec_int_t;
95 #define NXT_INFINITE_MSEC  ((nxt_msec_t) -1)
96 
97 
98 /*
99  * Since nxt_msec_t values are stored just in 32 bits, they overflow
100  * every 49 days.  This signed subtraction takes into account that overflow.
101  * "nxt_msec_diff(m1, m2) < 0" means that m1 is lesser than m2.
102  */
103 #define nxt_msec_diff(m1, m2)                                                 \
104     ((int32_t) ((m1) - (m2)))
105 
106 
107 #endif /* _NXT_UNIX_TIME_H_INCLUDED_ */
108