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/>.
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
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
) {
59 } else if (t
== 0x7FFFFFFFFFFFFFFFLL
) {
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
;
79 /****************************************************************************
81 ****************************************************************************/
83 bool nt_time_is_zero(const NTTIME
*nt
)
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
)
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) {
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 Return the date and time as a string
134 ****************************************************************************/
136 char *timeval_string(TALLOC_CTX
*ctx
, const struct timeval
*tp
, bool hires
)
142 t
= (time_t)tp
->tv_sec
;
148 "%ld.%06ld seconds since the Epoch",
154 "%ld seconds since the Epoch",
160 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
161 slprintf(TimeBuf
+strlen(TimeBuf
),
162 sizeof(TimeBuf
)-1 - strlen(TimeBuf
),
166 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
170 const char *asct
= asctime(tm
);
174 asct
? asct
: "unknown",
177 const char *asct
= asctime(tm
);
178 fstrcpy(TimeBuf
, asct
? asct
: "unknown");
182 return talloc_strdup(ctx
, TimeBuf
);
185 char *current_timestring(TALLOC_CTX
*ctx
, bool hires
)
190 return timeval_string(ctx
, &tv
, hires
);
195 /***************************************************************************
196 Server versions of the above functions.
197 ***************************************************************************/
199 void srv_put_dos_date(char *buf
,int offset
,time_t unixdate
)
201 push_dos_date((uint8_t *)buf
, offset
, unixdate
, server_zone_offset
);
204 void srv_put_dos_date2(char *buf
,int offset
, time_t unixdate
)
206 push_dos_date2((uint8_t *)buf
, offset
, unixdate
, server_zone_offset
);
209 void srv_put_dos_date3(char *buf
,int offset
,time_t unixdate
)
211 push_dos_date3((uint8_t *)buf
, offset
, unixdate
, server_zone_offset
);
214 void round_timespec(enum timestamp_set_resolution res
, struct timespec
*ts
)
217 case TIMESTAMP_SET_SECONDS
:
218 round_timespec_to_sec(ts
);
220 case TIMESTAMP_SET_MSEC
:
221 round_timespec_to_usec(ts
);
223 case TIMESTAMP_SET_NT_OR_BETTER
:
224 /* No rounding needed. */
229 /****************************************************************************
230 Take a Unix time and convert to an NTTIME structure and place in buffer
231 pointed to by p, rounded to the correct resolution.
232 ****************************************************************************/
234 void put_long_date_timespec(enum timestamp_set_resolution res
, char *p
, struct timespec ts
)
237 round_timespec(res
, &ts
);
238 unix_timespec_to_nt_time(&nt
, ts
);
239 SIVAL(p
, 0, nt
& 0xFFFFFFFF);
240 SIVAL(p
, 4, nt
>> 32);
243 void put_long_date(char *p
, time_t t
)
248 put_long_date_timespec(TIMESTAMP_SET_SECONDS
, p
, ts
);
251 void dos_filetime_timespec(struct timespec
*tsp
)
257 /*******************************************************************
258 Create a unix date (int GMT) from a dos date (which is actually in
260 ********************************************************************/
262 time_t make_unix_date(const void *date_ptr
, int zone_offset
)
268 dos_date
= IVAL(date_ptr
,0);
274 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
275 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
285 /*******************************************************************
286 Like make_unix_date() but the words are reversed.
287 ********************************************************************/
289 time_t make_unix_date2(const void *date_ptr
, int zone_offset
)
293 x
= IVAL(date_ptr
,0);
294 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
297 return(make_unix_date((const void *)&x
, zone_offset
));
300 /*******************************************************************
301 Create a unix GMT date from a dos date in 32 bit "unix like" format
302 these generally arrive as localtimes, with corresponding DST.
303 ******************************************************************/
305 time_t make_unix_date3(const void *date_ptr
, int zone_offset
)
307 time_t t
= (time_t)IVAL(date_ptr
,0);
314 time_t srv_make_unix_date(const void *date_ptr
)
316 return make_unix_date(date_ptr
, server_zone_offset
);
319 time_t srv_make_unix_date2(const void *date_ptr
)
321 return make_unix_date2(date_ptr
, server_zone_offset
);
324 time_t srv_make_unix_date3(const void *date_ptr
)
326 return make_unix_date3(date_ptr
, server_zone_offset
);
329 /****************************************************************************
330 Convert a normalized timeval to a timespec.
331 ****************************************************************************/
333 struct timespec
convert_timeval_to_timespec(const struct timeval tv
)
336 ts
.tv_sec
= tv
.tv_sec
;
337 ts
.tv_nsec
= tv
.tv_usec
* 1000;
341 /****************************************************************************
342 Convert a normalized timespec to a timeval.
343 ****************************************************************************/
345 struct timeval
convert_timespec_to_timeval(const struct timespec ts
)
348 tv
.tv_sec
= ts
.tv_sec
;
349 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
353 /****************************************************************************
354 Return a timespec for the current time
355 ****************************************************************************/
357 struct timespec
timespec_current(void)
360 clock_gettime(CLOCK_REALTIME
, &ts
);
364 /****************************************************************************
365 Return the lesser of two timespecs.
366 ****************************************************************************/
368 struct timespec
timespec_min(const struct timespec
*ts1
,
369 const struct timespec
*ts2
)
371 if (ts1
->tv_sec
< ts2
->tv_sec
) return *ts1
;
372 if (ts1
->tv_sec
> ts2
->tv_sec
) return *ts2
;
373 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return *ts1
;
377 /****************************************************************************
378 compare two timespec structures.
379 Return -1 if ts1 < ts2
380 Return 0 if ts1 == ts2
381 Return 1 if ts1 > ts2
382 ****************************************************************************/
384 int timespec_compare(const struct timespec
*ts1
, const struct timespec
*ts2
)
386 if (ts1
->tv_sec
> ts2
->tv_sec
) return 1;
387 if (ts1
->tv_sec
< ts2
->tv_sec
) return -1;
388 if (ts1
->tv_nsec
> ts2
->tv_nsec
) return 1;
389 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return -1;
393 /****************************************************************************
394 Round up a timespec if nsec > 500000000, round down if lower,
396 ****************************************************************************/
398 void round_timespec_to_sec(struct timespec
*ts
)
400 ts
->tv_sec
= convert_timespec_to_time_t(*ts
);
404 /****************************************************************************
405 Round a timespec to usec value.
406 ****************************************************************************/
408 void round_timespec_to_usec(struct timespec
*ts
)
410 struct timeval tv
= convert_timespec_to_timeval(*ts
);
411 *ts
= convert_timeval_to_timespec(tv
);
412 while (ts
->tv_nsec
> 1000000000) {
414 ts
->tv_nsec
-= 1000000000;
418 /****************************************************************************
419 Interprets an nt time into a unix struct timespec.
420 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
421 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
422 ****************************************************************************/
424 struct timespec
interpret_long_date(const char *p
)
427 nt
= IVAL(p
,0) + ((uint64_t)IVAL(p
,4) << 32);
428 if (nt
== (uint64_t)-1) {
430 ret
.tv_sec
= (time_t)-1;
434 return nt_time_to_unix_timespec(&nt
);
437 /*******************************************************************
438 Re-read the smb serverzone value.
439 ******************************************************************/
441 static struct timeval start_time_hires
;
445 set_server_zone_offset(time(NULL
));
447 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset
));
449 /* Save the start time of this process. */
450 if (start_time_hires
.tv_sec
== 0 && start_time_hires
.tv_usec
== 0) {
451 GetTimeOfDay(&start_time_hires
);
455 /**********************************************************************
456 Return a timeval struct of the uptime of this process. As TimeInit is
457 done before a daemon fork then this is the start time from the parent
459 ***********************************************************************/
461 void get_process_uptime(struct timeval
*ret_time
)
463 struct timeval time_now_hires
;
465 GetTimeOfDay(&time_now_hires
);
466 ret_time
->tv_sec
= time_now_hires
.tv_sec
- start_time_hires
.tv_sec
;
467 if (time_now_hires
.tv_usec
< start_time_hires
.tv_usec
) {
468 ret_time
->tv_sec
-= 1;
469 ret_time
->tv_usec
= 1000000 + (time_now_hires
.tv_usec
- start_time_hires
.tv_usec
);
471 ret_time
->tv_usec
= time_now_hires
.tv_usec
- start_time_hires
.tv_usec
;
476 * @brief Get the startup time of the server.
478 * @param[out] ret_time A pointer to a timveal structure to set the startup
481 void get_startup_time(struct timeval
*ret_time
)
483 ret_time
->tv_sec
= start_time_hires
.tv_sec
;
484 ret_time
->tv_usec
= start_time_hires
.tv_usec
;
488 /****************************************************************************
489 Convert a NTTIME structure to a time_t.
490 It's originally in "100ns units".
492 This is an absolute version of the one above.
493 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
494 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
495 ****************************************************************************/
497 time_t nt_time_to_unix_abs(const NTTIME
*nt
)
505 if (*nt
== (uint64_t)-1) {
509 if (*nt
== NTTIME_INFINITY
) {
513 /* reverse the time */
514 /* it's a negative value, turn it to positive */
520 if (!(TIME_T_MIN
<= ((time_t)d
) && ((time_t)d
) <= TIME_T_MAX
)) {
527 time_t uint64s_nt_time_to_unix_abs(const uint64_t *src
)
531 return nt_time_to_unix_abs(&nttime
);
534 /****************************************************************************
535 Put a 8 byte filetime from a struct timespec. Uses GMT.
536 ****************************************************************************/
538 void unix_timespec_to_nt_time(NTTIME
*nt
, struct timespec ts
)
542 if (ts
.tv_sec
==0 && ts
.tv_nsec
== 0) {
546 if (ts
.tv_sec
== TIME_T_MAX
) {
547 *nt
= 0x7fffffffffffffffLL
;
550 if (ts
.tv_sec
== (time_t)-1) {
556 d
+= TIME_FIXUP_CONSTANT_INT
;
558 /* d is now in 100ns units. */
559 d
+= (ts
.tv_nsec
/ 100);
565 void nt_time_to_unix_timespec(struct timespec
*ts
, NTTIME t
)
571 /* t starts in 100 nsec units since 1601-01-01. */
574 /* t is now in nsec units since 1601-01-01. */
576 t
-= TIME_FIXUP_CONSTANT
*1000*1000*100;
577 /* t is now in nsec units since the UNIX epoch 1970-01-01. */
579 ts
->tv_sec
= t
/ 1000000000LL;
581 if (TIME_T_MIN
> ts
->tv_sec
|| ts
->tv_sec
> TIME_T_MAX
) {
587 ts
->tv_nsec
= t
- ts
->tv_sec
*1000000000LL;
591 /****************************************************************************
592 Convert a time_t to a NTTIME structure
594 This is an absolute version of the one above.
595 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
596 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
597 ****************************************************************************/
599 void unix_to_nt_time_abs(NTTIME
*nt
, time_t t
)
608 if (t
== TIME_T_MAX
) {
609 *nt
= 0x7fffffffffffffffLL
;
613 if (t
== (time_t)-1) {
614 /* that's what NT uses for infinite */
615 *nt
= NTTIME_INFINITY
;
624 /* convert to a negative value */
629 /****************************************************************************
630 Utility function that always returns a const string even if localtime
632 ****************************************************************************/
634 const char *time_to_asc(const time_t t
)
637 struct tm
*lt
= localtime(&t
);
640 return "unknown time";
645 return "unknown time";
650 const char *display_time(NTTIME nttime
)
655 int days
, hours
, mins
, secs
;
660 if (nttime
==NTTIME_INFINITY
)
667 high
= high
* (~(nttime
>> 32));
669 low
= ~(nttime
& 0xFFFFFFFF);
670 low
= low
/(1000*1000*10);
675 hours
=(sec
- (days
*60*60*24)) / (60*60);
676 mins
=(sec
- (days
*60*60*24) - (hours
*60*60) ) / 60;
677 secs
=sec
- (days
*60*60*24) - (hours
*60*60) - (mins
*60);
679 return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
680 "%u seconds", days
, hours
, mins
, secs
);
683 bool nt_time_is_set(const NTTIME
*nt
)
685 if (*nt
== 0x7FFFFFFFFFFFFFFFLL
) {
689 if (*nt
== NTTIME_INFINITY
) {