s3:dbwrap: move the db_open_rbt() prototype to a new header dbwrap_rbt.h
[Samba.git] / source3 / lib / time.c
blob7fe53928ab72fdf96c59de0f124c69cb1c37e2d4
1 /*
2 Unix SMB/CIFS implementation.
3 time handling functions
5 Copyright (C) Andrew Tridgell 1992-2004
6 Copyright (C) Stefan (metze) Metzmacher 2002
7 Copyright (C) Jeremy Allison 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
25 /**
26 * @file
27 * @brief time handling functions
31 #define NTTIME_INFINITY (NTTIME)0x8000000000000000LL
33 #if (SIZEOF_LONG == 8)
34 #define TIME_FIXUP_CONSTANT_INT 11644473600L
35 #elif (SIZEOF_LONG_LONG == 8)
36 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
37 #endif
40 /**
41 parse a nttime as a large integer in a string and return a NTTIME
43 NTTIME nttime_from_string(const char *s)
45 return strtoull(s, NULL, 0);
48 /**************************************************************
49 Handle conversions between time_t and uint32, taking care to
50 preserve the "special" values.
51 **************************************************************/
53 uint32_t convert_time_t_to_uint32_t(time_t t)
55 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
56 /* time_t is 64-bit. */
57 if (t == 0x8000000000000000LL) {
58 return 0x80000000;
59 } else if (t == 0x7FFFFFFFFFFFFFFFLL) {
60 return 0x7FFFFFFF;
62 #endif
63 return (uint32_t)t;
66 time_t convert_uint32_t_to_time_t(uint32_t u)
68 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
69 /* time_t is 64-bit. */
70 if (u == 0x80000000) {
71 return (time_t)0x8000000000000000LL;
72 } else if (u == 0x7FFFFFFF) {
73 return (time_t)0x7FFFFFFFFFFFFFFFLL;
75 #endif
76 return (time_t)u;
79 /****************************************************************************
80 Check if NTTIME is 0.
81 ****************************************************************************/
83 bool nt_time_is_zero(const NTTIME *nt)
85 return (*nt == 0);
88 /****************************************************************************
89 Convert ASN.1 GeneralizedTime string to unix-time.
90 Returns 0 on failure; Currently ignores timezone.
91 ****************************************************************************/
93 time_t generalized_to_unix_time(const char *str)
95 struct tm tm;
97 ZERO_STRUCT(tm);
99 if (sscanf(str, "%4d%2d%2d%2d%2d%2d",
100 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
101 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
102 return 0;
104 tm.tm_year -= 1900;
105 tm.tm_mon -= 1;
107 return timegm(&tm);
110 /*******************************************************************
111 Accessor function for the server time zone offset.
112 set_server_zone_offset() must have been called first.
113 ******************************************************************/
115 static int server_zone_offset;
117 int get_server_zone_offset(void)
119 return server_zone_offset;
122 /*******************************************************************
123 Initialize the server time zone offset. Called when a client connects.
124 ******************************************************************/
126 int set_server_zone_offset(time_t t)
128 server_zone_offset = get_time_zone(t);
129 return server_zone_offset;
132 /***************************************************************************
133 Server versions of the above functions.
134 ***************************************************************************/
136 void srv_put_dos_date(char *buf,int offset,time_t unixdate)
138 push_dos_date((uint8_t *)buf, offset, unixdate, server_zone_offset);
141 void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
143 push_dos_date2((uint8_t *)buf, offset, unixdate, server_zone_offset);
146 void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
148 push_dos_date3((uint8_t *)buf, offset, unixdate, server_zone_offset);
151 void round_timespec(enum timestamp_set_resolution res, struct timespec *ts)
153 switch (res) {
154 case TIMESTAMP_SET_SECONDS:
155 round_timespec_to_sec(ts);
156 break;
157 case TIMESTAMP_SET_MSEC:
158 round_timespec_to_usec(ts);
159 break;
160 case TIMESTAMP_SET_NT_OR_BETTER:
161 /* No rounding needed. */
162 break;
166 /****************************************************************************
167 Take a Unix time and convert to an NTTIME structure and place in buffer
168 pointed to by p, rounded to the correct resolution.
169 ****************************************************************************/
171 void put_long_date_timespec(enum timestamp_set_resolution res, char *p, struct timespec ts)
173 NTTIME nt;
174 round_timespec(res, &ts);
175 unix_timespec_to_nt_time(&nt, ts);
176 SIVAL(p, 0, nt & 0xFFFFFFFF);
177 SIVAL(p, 4, nt >> 32);
180 void put_long_date(char *p, time_t t)
182 struct timespec ts;
183 ts.tv_sec = t;
184 ts.tv_nsec = 0;
185 put_long_date_timespec(TIMESTAMP_SET_SECONDS, p, ts);
188 void dos_filetime_timespec(struct timespec *tsp)
190 tsp->tv_sec &= ~1;
191 tsp->tv_nsec = 0;
194 /*******************************************************************
195 Create a unix date (int GMT) from a dos date (which is actually in
196 localtime).
197 ********************************************************************/
199 time_t make_unix_date(const void *date_ptr, int zone_offset)
201 uint32_t dos_date=0;
202 struct tm t;
203 time_t ret;
205 dos_date = IVAL(date_ptr,0);
207 if (dos_date == 0) {
208 return 0;
211 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
212 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
213 t.tm_isdst = -1;
215 ret = timegm(&t);
217 ret += zone_offset;
219 return(ret);
222 /*******************************************************************
223 Like make_unix_date() but the words are reversed.
224 ********************************************************************/
226 time_t make_unix_date2(const void *date_ptr, int zone_offset)
228 uint32_t x,x2;
230 x = IVAL(date_ptr,0);
231 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
232 SIVAL(&x,0,x2);
234 return(make_unix_date((const void *)&x, zone_offset));
237 /*******************************************************************
238 Create a unix GMT date from a dos date in 32 bit "unix like" format
239 these generally arrive as localtimes, with corresponding DST.
240 ******************************************************************/
242 time_t make_unix_date3(const void *date_ptr, int zone_offset)
244 time_t t = (time_t)IVAL(date_ptr,0);
245 if (!null_time(t)) {
246 t += zone_offset;
248 return(t);
251 time_t srv_make_unix_date(const void *date_ptr)
253 return make_unix_date(date_ptr, server_zone_offset);
256 time_t srv_make_unix_date2(const void *date_ptr)
258 return make_unix_date2(date_ptr, server_zone_offset);
261 time_t srv_make_unix_date3(const void *date_ptr)
263 return make_unix_date3(date_ptr, server_zone_offset);
266 /****************************************************************************
267 Interprets an nt time into a unix struct timespec.
268 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
269 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
270 ****************************************************************************/
272 struct timespec interpret_long_date(const char *p)
274 NTTIME nt;
275 nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);
276 if (nt == (uint64_t)-1) {
277 struct timespec ret;
278 ret.tv_sec = (time_t)-1;
279 ret.tv_nsec = 0;
280 return ret;
282 return nt_time_to_unix_timespec(&nt);
285 /*******************************************************************
286 Re-read the smb serverzone value.
287 ******************************************************************/
289 static struct timeval start_time_hires;
291 void TimeInit(void)
293 set_server_zone_offset(time(NULL));
295 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
297 /* Save the start time of this process. */
298 if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
299 GetTimeOfDay(&start_time_hires);
303 /**********************************************************************
304 Return a timeval struct of the uptime of this process. As TimeInit is
305 done before a daemon fork then this is the start time from the parent
306 daemon start. JRA.
307 ***********************************************************************/
309 void get_process_uptime(struct timeval *ret_time)
311 struct timeval time_now_hires;
313 GetTimeOfDay(&time_now_hires);
314 ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
315 if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
316 ret_time->tv_sec -= 1;
317 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
318 } else {
319 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
324 * @brief Get the startup time of the server.
326 * @param[out] ret_time A pointer to a timveal structure to set the startup
327 * time.
329 void get_startup_time(struct timeval *ret_time)
331 ret_time->tv_sec = start_time_hires.tv_sec;
332 ret_time->tv_usec = start_time_hires.tv_usec;
336 /****************************************************************************
337 Convert a NTTIME structure to a time_t.
338 It's originally in "100ns units".
340 This is an absolute version of the one above.
341 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
342 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
343 ****************************************************************************/
345 time_t nt_time_to_unix_abs(const NTTIME *nt)
347 uint64_t d;
349 if (*nt == 0) {
350 return (time_t)0;
353 if (*nt == (uint64_t)-1) {
354 return (time_t)-1;
357 if (*nt == NTTIME_INFINITY) {
358 return (time_t)-1;
361 /* reverse the time */
362 /* it's a negative value, turn it to positive */
363 d=~*nt;
365 d += 1000*1000*10/2;
366 d /= 1000*1000*10;
368 if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
369 return (time_t)0;
372 return (time_t)d;
375 time_t uint64s_nt_time_to_unix_abs(const uint64_t *src)
377 NTTIME nttime;
378 nttime = *src;
379 return nt_time_to_unix_abs(&nttime);
382 /****************************************************************************
383 Convert a time_t to a NTTIME structure
385 This is an absolute version of the one above.
386 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
387 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
388 ****************************************************************************/
390 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
392 double d;
394 if (t==0) {
395 *nt = 0;
396 return;
399 if (t == TIME_T_MAX) {
400 *nt = 0x7fffffffffffffffLL;
401 return;
404 if (t == (time_t)-1) {
405 /* that's what NT uses for infinite */
406 *nt = NTTIME_INFINITY;
407 return;
410 d = (double)(t);
411 d *= 1.0e7;
413 *nt = (NTTIME)d;
415 /* convert to a negative value */
416 *nt=~*nt;
420 /****************************************************************************
421 Utility function that always returns a const string even if localtime
422 and asctime fail.
423 ****************************************************************************/
425 const char *time_to_asc(const time_t t)
427 const char *asct;
428 struct tm *lt = localtime(&t);
430 if (!lt) {
431 return "unknown time";
434 asct = asctime(lt);
435 if (!asct) {
436 return "unknown time";
438 return asct;
441 const char *display_time(NTTIME nttime)
443 float high;
444 float low;
445 int sec;
446 int days, hours, mins, secs;
448 if (nttime==0)
449 return "Now";
451 if (nttime==NTTIME_INFINITY)
452 return "Never";
454 high = 65536;
455 high = high/10000;
456 high = high*65536;
457 high = high/1000;
458 high = high * (~(nttime >> 32));
460 low = ~(nttime & 0xFFFFFFFF);
461 low = low/(1000*1000*10);
463 sec=(int)(high+low);
465 days=sec/(60*60*24);
466 hours=(sec - (days*60*60*24)) / (60*60);
467 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
468 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
470 return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
471 "%u seconds", days, hours, mins, secs);
474 bool nt_time_is_set(const NTTIME *nt)
476 if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
477 return false;
480 if (*nt == NTTIME_INFINITY) {
481 return false;
484 return true;