Debian Codegraph @

lib/usual/time.c

raw symbols SCIP
1 /*
2 * Common time functions.
3 *
4 * Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <usual/time.h>
20
21 #include <usual/string.h>
22
23 #include <stdio.h>
24
25 char *format_time_ms(usec_t time, char *dest, unsigned destlen)
26 {
27 struct tm *tm, tmbuf;
28 struct timeval tv;
29 time_t sec;
30
31 if (!time) {
32 gettimeofday(&tv, NULL);
33 } else {
34 tv.tv_sec = time / USEC;
35 tv.tv_usec = time % USEC;
36 }
37
38 sec = tv.tv_sec;
39 tm = localtime_r(&sec, &tmbuf);
40 snprintf(dest, destlen, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s",
41 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
42 tm->tm_hour, tm->tm_min, tm->tm_sec,
43 (int)(tv.tv_usec / 1000),
44 tzname[tm->tm_isdst > 0 ? 1 : 0]);
45 return dest;
46 }
47
48 char *format_time_s(usec_t time, char *dest, unsigned destlen)
49 {
50 time_t s;
51 struct tm tbuf, *tm;
52 if (!time) {
53 struct timeval tv;
54 gettimeofday(&tv, NULL);
55 s = tv.tv_sec;
56 } else {
57 s = time / USEC;
58 }
59 tm = localtime_r(&s, &tbuf);
60 snprintf(dest, destlen, "%04d-%02d-%02d %02d:%02d:%02d %s",
61 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
62 tm->tm_hour, tm->tm_min, tm->tm_sec,
63 tzname[tm->tm_isdst > 0 ? 1 : 0]);
64 return dest;
65 }
66
67
68 /* read current time */
69 usec_t get_time_usec(void)
70 {
71 struct timeval tv;
72 gettimeofday(&tv, NULL);
73 return (usec_t)tv.tv_sec * USEC + tv.tv_usec;
74 }
75
76 static usec_t _time_cache;
77
78 /* read cached time */
79 usec_t get_cached_time(void)
80 {
81 if (!_time_cache)
82 _time_cache = get_time_usec();
83 return _time_cache;
84 }
85
86 /* forget cached time, let next read fill it */
87 void reset_time_cache(void)
88 {
89 _time_cache = 0;
90 }
91
92 /*
93 * win32 compat
94 */
95
96 #ifdef WIN32
97
98 /* unix epoch (1970) in seconds from windows epoch (1601) */
99 #define UNIX_EPOCH 11644473600LL
100
101 /* 1 sec in 100 nsec units */
102 #define FT_SEC 10000000LL
103
104 static void ft2tv(FILETIME *src, struct timeval *dest, bool use_epoch)
105 {
106 ULARGE_INTEGER tmp;
107 tmp.LowPart = src->dwLowDateTime;
108 tmp.HighPart = src->dwHighDateTime;
109 dest->tv_sec = (tmp.QuadPart / FT_SEC) - (use_epoch ? UNIX_EPOCH : 0);
110 dest->tv_usec = (tmp.QuadPart % FT_SEC) / 10;
111 }
112
113 #ifndef HAVE_GETTIMEOFDAY
114
115 int gettimeofday(struct timeval * tp, void * tzp)
116 {
117 FILETIME file_time;
118 SYSTEMTIME system_time;
119
120 /* read UTC timestamp */
121 GetSystemTime(&system_time);
122 SystemTimeToFileTime(&system_time, &file_time);
123
124 /* convert to timeval */
125 ft2tv(&file_time, tp, true);
126
127 return 0;
128 }
129
130 #endif /* !HAVE_GETTIMEOFDAY */
131
132 #ifndef HAVE_LOCALTIME_R
133
134 struct tm *localtime_r(const time_t *tp, struct tm *result)
135 {
136 ULARGE_INTEGER utc;
137 FILETIME ft_utc;
138 SYSTEMTIME st_utc, st_local;
139
140 /* convert time_t to FILETIME */
141 utc.QuadPart = (*tp + UNIX_EPOCH) * FT_SEC;
142 ft_utc.dwLowDateTime = utc.LowPart;
143 ft_utc.dwHighDateTime = utc.HighPart;
144
145 /* split to parts and get local time */
146 if (!FileTimeToSystemTime(&ft_utc, &st_utc))
147 return NULL;
148 if (!SystemTimeToTzSpecificLocalTime(NULL, &st_utc, &st_local))
149 return NULL;
150
151 /* fill struct tm */
152 result->tm_sec = st_local.wSecond;
153 result->tm_min = st_local.wMinute;
154 result->tm_hour = st_local.wHour;
155 result->tm_mday = st_local.wDay;
156 result->tm_mon = st_local.wMonth - 1;
157 result->tm_year = st_local.wYear - 1900;
158 result->tm_wday = st_local.wDayOfWeek;
159 result->tm_yday = 0;
160 result->tm_isdst = -1;
161 return result;
162 }
163
164 #endif /* !HAVE_LOCALTIME_R */
165
166 #ifndef HAVE_GETRUSAGE
167
168 int getrusage(int who, struct rusage *r_usage)
169 {
170 FILETIME tcreate, texit, tkern, tuser;
171 if (who != RUSAGE_SELF) {
172 errno = EINVAL;
173 return -1;
174 }
175 if (!GetProcessTimes(GetCurrentProcess(), &tcreate, &texit, &tkern, &tuser))
176 return -1;
177 ft2tv(&tuser, &r_usage->ru_utime, false);
178 ft2tv(&tkern, &r_usage->ru_stime, false);
179 return 0;
180 }
181
182 #endif /* !HAVE_GETRUSAGE */
183
184
185 #endif /* WIN32 */
186
187 #ifndef HAVE_TIMEGM
188
189 time_t timegm(struct tm *tm)
190 {
191 #ifdef WIN32
192 return _mkgmtime(tm);
193 #else
194 char buf[128], *tz, *old = NULL;
195 time_t secs;
196
197 tz = getenv("TZ");
198 if (tz) {
199 old = strdup(tz);
200 if (!old) {
201 strlcpy(buf, tz, sizeof buf);
202 old = buf;
203 }
204 }
205 setenv("TZ", "", 1);
206 tzset();
207
208 secs = mktime(tm);
209
210 if (old) {
211 setenv("TZ", old, 1);
212 } else {
213 unsetenv("TZ");
214 }
215 tzset();
216 if (old && old != buf)
217 free(old);
218 return secs;
219 #endif
220 }
221
222 #endif /* HAVE_TIMEGM */