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
8 Copyright (C) Andrew Bartlett 2011
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/time.h"
26 #include "lib/util/time_basic.h"
30 * @brief time handling functions
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
42 External access to time_t_min and time_t_max.
44 _PUBLIC_
time_t get_time_t_max(void)
50 a wrapper to preferably get the monotonic time
52 _PUBLIC_
void clock_gettime_mono(struct timespec
*tp
)
54 /* prefer a suspend aware monotonic CLOCK_BOOTTIME: */
56 if (clock_gettime(CLOCK_BOOTTIME
,tp
) == 0) {
60 /* then try the monotonic clock: */
61 #if CUSTOM_CLOCK_MONOTONIC != CLOCK_REALTIME
62 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC
,tp
) == 0) {
66 clock_gettime(CLOCK_REALTIME
,tp
);
70 a wrapper to preferably get the monotonic time in seconds
72 _PUBLIC_
time_t time_mono(time_t *t
)
76 clock_gettime_mono(&tp
);
84 #define TIME_FIXUP_CONSTANT 11644473600LL
86 time_t convert_timespec_to_time_t(struct timespec ts
)
88 /* Ensure tv_nsec is less than 1sec. */
89 while (ts
.tv_nsec
> 1000000000) {
91 ts
.tv_nsec
-= 1000000000;
94 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
95 increment if it's greater than 500 millionth of a second. */
97 if (ts
.tv_nsec
> 500000000) {
103 struct timespec
convert_time_t_to_timespec(time_t t
)
114 Interpret an 8 byte "filetime" structure to a time_t
115 It's originally in "100ns units since jan 1st 1601"
117 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
124 time_t nt_time_to_unix(NTTIME nt
)
126 return convert_timespec_to_time_t(nt_time_to_unix_timespec(nt
));
131 put a 8 byte filetime from a time_t
132 This takes GMT as input
134 _PUBLIC_
void unix_to_nt_time(NTTIME
*nt
, time_t t
)
138 if (t
== (time_t)-1) {
143 if (t
== TIME_T_MAX
|| t
== INT64_MAX
) {
144 *nt
= 0x7fffffffffffffffLL
;
154 t2
+= TIME_FIXUP_CONSTANT_INT
;
162 check if it's a null unix time
164 _PUBLIC_
bool null_time(time_t t
)
167 t
== (time_t)0xFFFFFFFF ||
173 check if it's a null NTTIME
175 _PUBLIC_
bool null_nttime(NTTIME t
)
177 return t
== 0 || t
== (NTTIME
)-1;
180 /*******************************************************************
181 create a 16 bit dos packed date
182 ********************************************************************/
183 static uint16_t make_dos_date1(struct tm
*t
)
186 ret
= (((unsigned int)(t
->tm_mon
+1)) >> 3) | ((t
->tm_year
-80) << 1);
187 ret
= ((ret
&0xFF)<<8) | (t
->tm_mday
| (((t
->tm_mon
+1) & 0x7) << 5));
191 /*******************************************************************
192 create a 16 bit dos packed time
193 ********************************************************************/
194 static uint16_t make_dos_time1(struct tm
*t
)
197 ret
= ((((unsigned int)t
->tm_min
>> 3)&0x7) | (((unsigned int)t
->tm_hour
) << 3));
198 ret
= ((ret
&0xFF)<<8) | ((t
->tm_sec
/2) | ((t
->tm_min
& 0x7) << 5));
202 /*******************************************************************
203 create a 32 bit dos packed date/time from some parameters
204 This takes a GMT time and returns a packed localtime structure
205 ********************************************************************/
206 static uint32_t make_dos_date(time_t unixdate
, int zone_offset
)
215 unixdate
-= zone_offset
;
217 t
= gmtime(&unixdate
);
222 ret
= make_dos_date1(t
);
223 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
229 put a dos date into a buffer (time/date format)
230 This takes GMT time and puts local time in the buffer
232 _PUBLIC_
void push_dos_date(uint8_t *buf
, int offset
, time_t unixdate
, int zone_offset
)
234 uint32_t x
= make_dos_date(unixdate
, zone_offset
);
239 put a dos date into a buffer (date/time format)
240 This takes GMT time and puts local time in the buffer
242 _PUBLIC_
void push_dos_date2(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
245 x
= make_dos_date(unixdate
, zone_offset
);
246 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
251 put a dos 32 bit "unix like" date into a buffer. This routine takes
252 GMT and converts it to LOCAL time before putting it (most SMBs assume
253 localtime for this sort of date)
255 _PUBLIC_
void push_dos_date3(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
257 if (!null_time(unixdate
)) {
258 unixdate
-= zone_offset
;
260 SIVAL(buf
,offset
,unixdate
);
263 /*******************************************************************
264 interpret a 32 bit dos packed date/time to some parameters
265 ********************************************************************/
266 void interpret_dos_date(uint32_t date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
268 uint32_t p0
,p1
,p2
,p3
;
270 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
271 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
273 *second
= 2*(p0
& 0x1F);
274 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
275 *hour
= (p1
>>3)&0xFF;
277 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
278 *year
= ((p3
>>1)&0xFF) + 80;
282 create a unix date (int GMT) from a dos date (which is actually in
285 _PUBLIC_
time_t pull_dos_date(const uint8_t *date_ptr
, int zone_offset
)
291 dos_date
= IVAL(date_ptr
,0);
293 if (dos_date
== 0) return (time_t)0;
295 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
296 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
307 like make_unix_date() but the words are reversed
309 _PUBLIC_
time_t pull_dos_date2(const uint8_t *date_ptr
, int zone_offset
)
313 x
= IVAL(date_ptr
,0);
314 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
317 return pull_dos_date((const uint8_t *)&x
, zone_offset
);
321 create a unix GMT date from a dos date in 32 bit "unix like" format
322 these generally arrive as localtimes, with corresponding DST
324 _PUBLIC_
time_t pull_dos_date3(const uint8_t *date_ptr
, int zone_offset
)
326 time_t t
= (time_t)IVAL(date_ptr
,0);
333 /****************************************************************************
334 Return the date and time as a string
335 ****************************************************************************/
337 char *timeval_string(TALLOC_CTX
*ctx
, const struct timeval
*tp
, bool hires
)
339 struct timeval_buf tmp
;
342 result
= talloc_strdup(ctx
, timeval_str_buf(tp
, hires
, &tmp
));
343 if (result
== NULL
) {
348 * beautify the talloc_report output
350 * This is not just cosmetics. A C compiler might in theory make the
351 * talloc_strdup call above a tail call with the tail call
352 * optimization. This would render "tmp" invalid while talloc_strdup
353 * tries to duplicate it. The talloc_set_name_const call below puts
354 * the talloc_strdup call into non-tail position.
356 talloc_set_name_const(result
, result
);
360 char *current_timestring(TALLOC_CTX
*ctx
, bool hires
)
365 return timeval_string(ctx
, &tv
, hires
);
370 return a HTTP/1.0 time string
372 _PUBLIC_
char *http_timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
376 struct tm
*tm
= localtime(&t
);
378 if (t
== TIME_T_MAX
) {
379 return talloc_strdup(mem_ctx
, "never");
383 return talloc_asprintf(mem_ctx
,"%ld seconds since the Epoch",(long)t
);
386 #ifndef HAVE_STRFTIME
387 buf
= talloc_strdup(mem_ctx
, asctime(tm
));
388 if (buf
[strlen(buf
)-1] == '\n') {
389 buf
[strlen(buf
)-1] = 0;
392 strftime(tempTime
, sizeof(tempTime
)-1, "%a, %d %b %Y %H:%M:%S %Z", tm
);
393 buf
= talloc_strdup(mem_ctx
, tempTime
);
394 #endif /* !HAVE_STRFTIME */
400 Return the date and time as a string
402 _PUBLIC_
char *timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
410 return talloc_asprintf(mem_ctx
,
411 "%ld seconds since the Epoch",
416 /* Some versions of gcc complain about using some special format
417 * specifiers. This is a bug in gcc, not a bug in this code. See a
418 * recent strftime() manual page for details. */
419 strftime(tempTime
,sizeof(tempTime
)-1,"%a %b %e %X %Y %Z",tm
);
420 TimeBuf
= talloc_strdup(mem_ctx
, tempTime
);
422 TimeBuf
= talloc_strdup(mem_ctx
, asctime(tm
));
423 if (TimeBuf
== NULL
) {
426 if (TimeBuf
[0] != '\0') {
427 size_t len
= strlen(TimeBuf
);
428 if (TimeBuf
[len
- 1] == '\n') {
429 TimeBuf
[len
- 1] = '\0';
438 return a talloced string representing a NTTIME for human consumption
440 _PUBLIC_
const char *nt_time_string(TALLOC_CTX
*mem_ctx
, NTTIME nt
)
446 t
= nt_time_to_unix(nt
);
447 return timestring(mem_ctx
, t
);
452 put a NTTIME into a packet
454 _PUBLIC_
void push_nttime(uint8_t *base
, uint16_t offset
, NTTIME t
)
456 SBVAL(base
, offset
, t
);
460 pull a NTTIME from a packet
462 _PUBLIC_ NTTIME
pull_nttime(uint8_t *base
, uint16_t offset
)
464 NTTIME ret
= BVAL(base
, offset
);
469 return (tv1 - tv2) in microseconds
471 _PUBLIC_
int64_t usec_time_diff(const struct timeval
*tv1
, const struct timeval
*tv2
)
473 int64_t sec_diff
= tv1
->tv_sec
- tv2
->tv_sec
;
474 return (sec_diff
* 1000000) + (int64_t)(tv1
->tv_usec
- tv2
->tv_usec
);
478 return (tp1 - tp2) in microseconds
480 _PUBLIC_
int64_t nsec_time_diff(const struct timespec
*tp1
, const struct timespec
*tp2
)
482 int64_t sec_diff
= tp1
->tv_sec
- tp2
->tv_sec
;
483 return (sec_diff
* 1000000000) + (int64_t)(tp1
->tv_nsec
- tp2
->tv_nsec
);
488 return a zero timeval
490 _PUBLIC_
struct timeval
timeval_zero(void)
499 return true if a timeval is zero
501 _PUBLIC_
bool timeval_is_zero(const struct timeval
*tv
)
503 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
507 return a timeval for the current time
509 _PUBLIC_
struct timeval
timeval_current(void)
517 return a timeval struct with the given elements
519 _PUBLIC_
struct timeval
timeval_set(uint32_t secs
, uint32_t usecs
)
529 return a timeval ofs microseconds after tv
531 _PUBLIC_
struct timeval
timeval_add(const struct timeval
*tv
,
532 uint32_t secs
, uint32_t usecs
)
534 struct timeval tv2
= *tv
;
535 const unsigned int million
= 1000000;
537 tv2
.tv_usec
+= usecs
;
538 tv2
.tv_sec
+= tv2
.tv_usec
/ million
;
539 tv2
.tv_usec
= tv2
.tv_usec
% million
;
544 return the sum of two timeval structures
546 struct timeval
timeval_sum(const struct timeval
*tv1
,
547 const struct timeval
*tv2
)
549 return timeval_add(tv1
, tv2
->tv_sec
, tv2
->tv_usec
);
553 return a timeval secs/usecs into the future
555 _PUBLIC_
struct timeval
timeval_current_ofs(uint32_t secs
, uint32_t usecs
)
557 struct timeval tv
= timeval_current();
558 return timeval_add(&tv
, secs
, usecs
);
562 return a timeval milliseconds into the future
564 _PUBLIC_
struct timeval
timeval_current_ofs_msec(uint32_t msecs
)
566 struct timeval tv
= timeval_current();
567 return timeval_add(&tv
, msecs
/ 1000, (msecs
% 1000) * 1000);
571 return a timeval microseconds into the future
573 _PUBLIC_
struct timeval
timeval_current_ofs_usec(uint32_t usecs
)
575 struct timeval tv
= timeval_current();
576 return timeval_add(&tv
, usecs
/ 1000000, usecs
% 1000000);
580 compare two timeval structures.
581 Return -1 if tv1 < tv2
582 Return 0 if tv1 == tv2
583 Return 1 if tv1 > tv2
585 _PUBLIC_
int timeval_compare(const struct timeval
*tv1
, const struct timeval
*tv2
)
587 if (tv1
->tv_sec
> tv2
->tv_sec
) return 1;
588 if (tv1
->tv_sec
< tv2
->tv_sec
) return -1;
589 if (tv1
->tv_usec
> tv2
->tv_usec
) return 1;
590 if (tv1
->tv_usec
< tv2
->tv_usec
) return -1;
595 return true if a timer is in the past
597 _PUBLIC_
bool timeval_expired(const struct timeval
*tv
)
599 struct timeval tv2
= timeval_current();
600 if (tv2
.tv_sec
> tv
->tv_sec
) return true;
601 if (tv2
.tv_sec
< tv
->tv_sec
) return false;
602 return (tv2
.tv_usec
>= tv
->tv_usec
);
606 return the number of seconds elapsed between two times
608 _PUBLIC_
double timeval_elapsed2(const struct timeval
*tv1
, const struct timeval
*tv2
)
610 return (tv2
->tv_sec
- tv1
->tv_sec
) +
611 (tv2
->tv_usec
- tv1
->tv_usec
)*1.0e-6;
615 return the number of seconds elapsed since a given time
617 _PUBLIC_
double timeval_elapsed(const struct timeval
*tv
)
619 struct timeval tv2
= timeval_current();
620 return timeval_elapsed2(tv
, &tv2
);
623 * return the number of seconds elapsed between two times
625 _PUBLIC_
double timespec_elapsed2(const struct timespec
*ts1
,
626 const struct timespec
*ts2
)
628 return (ts2
->tv_sec
- ts1
->tv_sec
) +
629 (ts2
->tv_nsec
- ts1
->tv_nsec
)*1.0e-9;
633 * return the number of seconds elapsed since a given time
635 _PUBLIC_
double timespec_elapsed(const struct timespec
*ts
)
637 struct timespec ts2
= timespec_current();
638 return timespec_elapsed2(ts
, &ts2
);
642 return the lesser of two timevals
644 _PUBLIC_
struct timeval
timeval_min(const struct timeval
*tv1
,
645 const struct timeval
*tv2
)
647 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv1
;
648 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv2
;
649 if (tv1
->tv_usec
< tv2
->tv_usec
) return *tv1
;
654 return the greater of two timevals
656 _PUBLIC_
struct timeval
timeval_max(const struct timeval
*tv1
,
657 const struct timeval
*tv2
)
659 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv1
;
660 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv2
;
661 if (tv1
->tv_usec
> tv2
->tv_usec
) return *tv1
;
666 return the difference between two timevals as a timeval
667 if tv1 comes after tv2, then return a zero timeval
668 (this is *tv2 - *tv1)
670 _PUBLIC_
struct timeval
timeval_until(const struct timeval
*tv1
,
671 const struct timeval
*tv2
)
674 if (timeval_compare(tv1
, tv2
) >= 0) {
675 return timeval_zero();
677 t
.tv_sec
= tv2
->tv_sec
- tv1
->tv_sec
;
678 if (tv1
->tv_usec
> tv2
->tv_usec
) {
680 t
.tv_usec
= 1000000 - (tv1
->tv_usec
- tv2
->tv_usec
);
682 t
.tv_usec
= tv2
->tv_usec
- tv1
->tv_usec
;
689 convert a timeval to a NTTIME
691 _PUBLIC_ NTTIME
timeval_to_nttime(const struct timeval
*tv
)
693 return 10*(tv
->tv_usec
+
694 ((TIME_FIXUP_CONSTANT
+ (uint64_t)tv
->tv_sec
) * 1000000));
698 convert a NTTIME to a timeval
700 _PUBLIC_
void nttime_to_timeval(struct timeval
*tv
, NTTIME t
)
702 if (tv
== NULL
) return;
706 t
-= TIME_FIXUP_CONSTANT
*1000*1000;
708 tv
->tv_sec
= t
/ 1000000;
710 if (TIME_T_MIN
> tv
->tv_sec
|| tv
->tv_sec
> TIME_T_MAX
) {
716 tv
->tv_usec
= t
- tv
->tv_sec
*1000000;
719 /*******************************************************************
720 yield the difference between *A and *B, in seconds, ignoring leap seconds
721 ********************************************************************/
722 static int tm_diff(struct tm
*a
, struct tm
*b
)
724 int ay
= a
->tm_year
+ (1900 - 1);
725 int by
= b
->tm_year
+ (1900 - 1);
726 int intervening_leap_days
=
727 (ay
/4 - by
/4) - (ay
/100 - by
/100) + (ay
/400 - by
/400);
729 int days
= 365*years
+ intervening_leap_days
+ (a
->tm_yday
- b
->tm_yday
);
730 int hours
= 24*days
+ (a
->tm_hour
- b
->tm_hour
);
731 int minutes
= 60*hours
+ (a
->tm_min
- b
->tm_min
);
732 int seconds
= 60*minutes
+ (a
->tm_sec
- b
->tm_sec
);
739 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
741 _PUBLIC_
int get_time_zone(time_t t
)
743 struct tm
*tm
= gmtime(&t
);
751 return tm_diff(&tm_utc
,tm
);
754 struct timespec
nt_time_to_unix_timespec(NTTIME nt
)
759 if (nt
== 0 || nt
== (int64_t)-1) {
766 /* d is now in 100ns units, since jan 1st 1601".
767 Save off the ns fraction. */
770 * Take the last seven decimal digits and multiply by 100.
771 * to convert from 100ns units to 1ns units.
773 ret
.tv_nsec
= (long) ((d
% (1000 * 1000 * 10)) * 100);
775 /* Convert to seconds */
778 /* Now adjust by 369 years to make the secs since 1970 */
779 d
-= TIME_FIXUP_CONSTANT_INT
;
781 if (d
<= (int64_t)TIME_T_MIN
) {
782 ret
.tv_sec
= TIME_T_MIN
;
787 if (d
>= (int64_t)TIME_T_MAX
) {
788 ret
.tv_sec
= TIME_T_MAX
;
793 ret
.tv_sec
= (time_t)d
;
799 check if 2 NTTIMEs are equal.
801 bool nt_time_equal(NTTIME
*t1
, NTTIME
*t2
)
807 Check if it's a null timespec.
810 bool null_timespec(struct timespec ts
)
812 return ts
.tv_sec
== 0 ||
813 ts
.tv_sec
== (time_t)0xFFFFFFFF ||
814 ts
.tv_sec
== (time_t)-1;
817 /****************************************************************************
818 Convert a normalized timeval to a timespec.
819 ****************************************************************************/
821 struct timespec
convert_timeval_to_timespec(const struct timeval tv
)
824 ts
.tv_sec
= tv
.tv_sec
;
825 ts
.tv_nsec
= tv
.tv_usec
* 1000;
829 /****************************************************************************
830 Convert a normalized timespec to a timeval.
831 ****************************************************************************/
833 struct timeval
convert_timespec_to_timeval(const struct timespec ts
)
836 tv
.tv_sec
= ts
.tv_sec
;
837 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
841 /****************************************************************************
842 Return a timespec for the current time
843 ****************************************************************************/
845 _PUBLIC_
struct timespec
timespec_current(void)
848 clock_gettime(CLOCK_REALTIME
, &ts
);
852 /****************************************************************************
853 Return the lesser of two timespecs.
854 ****************************************************************************/
856 struct timespec
timespec_min(const struct timespec
*ts1
,
857 const struct timespec
*ts2
)
859 if (ts1
->tv_sec
< ts2
->tv_sec
) return *ts1
;
860 if (ts1
->tv_sec
> ts2
->tv_sec
) return *ts2
;
861 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return *ts1
;
865 /****************************************************************************
866 compare two timespec structures.
867 Return -1 if ts1 < ts2
868 Return 0 if ts1 == ts2
869 Return 1 if ts1 > ts2
870 ****************************************************************************/
872 _PUBLIC_
int timespec_compare(const struct timespec
*ts1
, const struct timespec
*ts2
)
874 if (ts1
->tv_sec
> ts2
->tv_sec
) return 1;
875 if (ts1
->tv_sec
< ts2
->tv_sec
) return -1;
876 if (ts1
->tv_nsec
> ts2
->tv_nsec
) return 1;
877 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return -1;
881 /****************************************************************************
882 Round up a timespec if nsec > 500000000, round down if lower,
884 ****************************************************************************/
886 void round_timespec_to_sec(struct timespec
*ts
)
888 ts
->tv_sec
= convert_timespec_to_time_t(*ts
);
892 /****************************************************************************
893 Round a timespec to usec value.
894 ****************************************************************************/
896 void round_timespec_to_usec(struct timespec
*ts
)
898 struct timeval tv
= convert_timespec_to_timeval(*ts
);
899 *ts
= convert_timeval_to_timespec(tv
);
900 while (ts
->tv_nsec
> 1000000000) {
902 ts
->tv_nsec
-= 1000000000;
906 /****************************************************************************
907 Put a 8 byte filetime from a struct timespec. Uses GMT.
908 ****************************************************************************/
910 _PUBLIC_ NTTIME
unix_timespec_to_nt_time(struct timespec ts
)
914 if (ts
.tv_sec
==0 && ts
.tv_nsec
== 0) {
917 if (ts
.tv_sec
== TIME_T_MAX
) {
918 return 0x7fffffffffffffffLL
;
920 if (ts
.tv_sec
== (time_t)-1) {
925 d
+= TIME_FIXUP_CONSTANT_INT
;
927 /* d is now in 100ns units. */
928 d
+= (ts
.tv_nsec
/ 100);