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"
29 * @brief time handling functions
32 #if (SIZEOF_LONG == 8)
33 #define TIME_FIXUP_CONSTANT_INT 11644473600L
34 #elif (SIZEOF_LONG_LONG == 8)
35 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
41 External access to time_t_min and time_t_max.
43 _PUBLIC_
time_t get_time_t_max(void)
49 a gettimeofday wrapper
51 _PUBLIC_
void GetTimeOfDay(struct timeval
*tval
)
53 #ifdef HAVE_GETTIMEOFDAY_TZ
54 gettimeofday(tval
,NULL
);
61 a wrapper to preferably get the monotonic time
63 _PUBLIC_
void clock_gettime_mono(struct timespec
*tp
)
65 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC
,tp
) != 0) {
66 clock_gettime(CLOCK_REALTIME
,tp
);
71 a wrapper to preferably get the monotonic time in seconds
72 as this is only second resolution we can use the cached
73 (and much faster) COARSE clock variant
75 _PUBLIC_
time_t time_mono(time_t *t
)
79 #ifdef CLOCK_MONOTONIC_COARSE
80 rc
= clock_gettime(CLOCK_MONOTONIC_COARSE
,&tp
);
83 clock_gettime_mono(&tp
);
92 #define TIME_FIXUP_CONSTANT 11644473600LL
94 time_t convert_timespec_to_time_t(struct timespec ts
)
96 /* Ensure tv_nsec is less than 1sec. */
97 while (ts
.tv_nsec
> 1000000000) {
99 ts
.tv_nsec
-= 1000000000;
102 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
103 increment if it's greater than 500 millionth of a second. */
105 if (ts
.tv_nsec
> 500000000) {
106 return ts
.tv_sec
+ 1;
111 struct timespec
convert_time_t_to_timespec(time_t t
)
122 Interpret an 8 byte "filetime" structure to a time_t
123 It's originally in "100ns units since jan 1st 1601"
125 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
132 time_t nt_time_to_unix(NTTIME nt
)
134 return convert_timespec_to_time_t(nt_time_to_unix_timespec(&nt
));
139 put a 8 byte filetime from a time_t
140 This takes GMT as input
142 _PUBLIC_
void unix_to_nt_time(NTTIME
*nt
, time_t t
)
146 if (t
== (time_t)-1) {
151 if (t
== TIME_T_MAX
|| t
== INT64_MAX
) {
152 *nt
= 0x7fffffffffffffffLL
;
162 t2
+= TIME_FIXUP_CONSTANT_INT
;
170 check if it's a null unix time
172 _PUBLIC_
bool null_time(time_t t
)
175 t
== (time_t)0xFFFFFFFF ||
181 check if it's a null NTTIME
183 _PUBLIC_
bool null_nttime(NTTIME t
)
185 return t
== 0 || t
== (NTTIME
)-1;
188 /*******************************************************************
189 create a 16 bit dos packed date
190 ********************************************************************/
191 static uint16_t make_dos_date1(struct tm
*t
)
194 ret
= (((unsigned int)(t
->tm_mon
+1)) >> 3) | ((t
->tm_year
-80) << 1);
195 ret
= ((ret
&0xFF)<<8) | (t
->tm_mday
| (((t
->tm_mon
+1) & 0x7) << 5));
199 /*******************************************************************
200 create a 16 bit dos packed time
201 ********************************************************************/
202 static uint16_t make_dos_time1(struct tm
*t
)
205 ret
= ((((unsigned int)t
->tm_min
>> 3)&0x7) | (((unsigned int)t
->tm_hour
) << 3));
206 ret
= ((ret
&0xFF)<<8) | ((t
->tm_sec
/2) | ((t
->tm_min
& 0x7) << 5));
210 /*******************************************************************
211 create a 32 bit dos packed date/time from some parameters
212 This takes a GMT time and returns a packed localtime structure
213 ********************************************************************/
214 static uint32_t make_dos_date(time_t unixdate
, int zone_offset
)
223 unixdate
-= zone_offset
;
225 t
= gmtime(&unixdate
);
230 ret
= make_dos_date1(t
);
231 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
237 put a dos date into a buffer (time/date format)
238 This takes GMT time and puts local time in the buffer
240 _PUBLIC_
void push_dos_date(uint8_t *buf
, int offset
, time_t unixdate
, int zone_offset
)
242 uint32_t x
= make_dos_date(unixdate
, zone_offset
);
247 put a dos date into a buffer (date/time format)
248 This takes GMT time and puts local time in the buffer
250 _PUBLIC_
void push_dos_date2(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
253 x
= make_dos_date(unixdate
, zone_offset
);
254 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
259 put a dos 32 bit "unix like" date into a buffer. This routine takes
260 GMT and converts it to LOCAL time before putting it (most SMBs assume
261 localtime for this sort of date)
263 _PUBLIC_
void push_dos_date3(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
265 if (!null_time(unixdate
)) {
266 unixdate
-= zone_offset
;
268 SIVAL(buf
,offset
,unixdate
);
271 /*******************************************************************
272 interpret a 32 bit dos packed date/time to some parameters
273 ********************************************************************/
274 void interpret_dos_date(uint32_t date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
276 uint32_t p0
,p1
,p2
,p3
;
278 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
279 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
281 *second
= 2*(p0
& 0x1F);
282 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
283 *hour
= (p1
>>3)&0xFF;
285 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
286 *year
= ((p3
>>1)&0xFF) + 80;
290 create a unix date (int GMT) from a dos date (which is actually in
293 _PUBLIC_
time_t pull_dos_date(const uint8_t *date_ptr
, int zone_offset
)
299 dos_date
= IVAL(date_ptr
,0);
301 if (dos_date
== 0) return (time_t)0;
303 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
304 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
315 like make_unix_date() but the words are reversed
317 _PUBLIC_
time_t pull_dos_date2(const uint8_t *date_ptr
, int zone_offset
)
321 x
= IVAL(date_ptr
,0);
322 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
325 return pull_dos_date((const uint8_t *)&x
, zone_offset
);
329 create a unix GMT date from a dos date in 32 bit "unix like" format
330 these generally arrive as localtimes, with corresponding DST
332 _PUBLIC_
time_t pull_dos_date3(const uint8_t *date_ptr
, int zone_offset
)
334 time_t t
= (time_t)IVAL(date_ptr
,0);
342 /****************************************************************************
343 Return the date and time as a string
344 ****************************************************************************/
346 char *timeval_string(TALLOC_CTX
*ctx
, const struct timeval
*tp
, bool hires
)
351 t
= (time_t)tp
->tv_sec
;
355 return talloc_asprintf(ctx
,
356 "%ld.%06ld seconds since the Epoch",
360 return talloc_asprintf(ctx
,
361 "%ld seconds since the Epoch",
368 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
369 return talloc_asprintf(ctx
,
373 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
374 return talloc_strdup(ctx
, TimeBuf
);
378 const char *asct
= asctime(tm
);
379 return talloc_asprintf(ctx
, "%s.%06ld",
380 asct
? asct
: "unknown",
383 const char *asct
= asctime(tm
);
384 return talloc_asprintf(ctx
, asct
? asct
: "unknown");
390 char *current_timestring(TALLOC_CTX
*ctx
, bool hires
)
395 return timeval_string(ctx
, &tv
, hires
);
400 return a HTTP/1.0 time string
402 _PUBLIC_
char *http_timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
406 struct tm
*tm
= localtime(&t
);
408 if (t
== TIME_T_MAX
) {
409 return talloc_strdup(mem_ctx
, "never");
413 return talloc_asprintf(mem_ctx
,"%ld seconds since the Epoch",(long)t
);
416 #ifndef HAVE_STRFTIME
417 buf
= talloc_strdup(mem_ctx
, asctime(tm
));
418 if (buf
[strlen(buf
)-1] == '\n') {
419 buf
[strlen(buf
)-1] = 0;
422 strftime(tempTime
, sizeof(tempTime
)-1, "%a, %d %b %Y %H:%M:%S %Z", tm
);
423 buf
= talloc_strdup(mem_ctx
, tempTime
);
424 #endif /* !HAVE_STRFTIME */
430 Return the date and time as a string
432 _PUBLIC_
char *timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
440 return talloc_asprintf(mem_ctx
,
441 "%ld seconds since the Epoch",
446 /* Some versions of gcc complain about using some special format
447 * specifiers. This is a bug in gcc, not a bug in this code. See a
448 * recent strftime() manual page for details. */
449 strftime(tempTime
,sizeof(tempTime
)-1,"%a %b %e %X %Y %Z",tm
);
450 TimeBuf
= talloc_strdup(mem_ctx
, tempTime
);
452 TimeBuf
= talloc_strdup(mem_ctx
, asctime(tm
));
453 if (TimeBuf
== NULL
) {
456 if (TimeBuf
[0] != '\0') {
457 size_t len
= strlen(TimeBuf
);
458 if (TimeBuf
[len
- 1] == '\n') {
459 TimeBuf
[len
- 1] = '\0';
468 return a talloced string representing a NTTIME for human consumption
470 _PUBLIC_
const char *nt_time_string(TALLOC_CTX
*mem_ctx
, NTTIME nt
)
476 t
= nt_time_to_unix(nt
);
477 return timestring(mem_ctx
, t
);
482 put a NTTIME into a packet
484 _PUBLIC_
void push_nttime(uint8_t *base
, uint16_t offset
, NTTIME t
)
486 SBVAL(base
, offset
, t
);
490 pull a NTTIME from a packet
492 _PUBLIC_ NTTIME
pull_nttime(uint8_t *base
, uint16_t offset
)
494 NTTIME ret
= BVAL(base
, offset
);
499 return (tv1 - tv2) in microseconds
501 _PUBLIC_
int64_t usec_time_diff(const struct timeval
*tv1
, const struct timeval
*tv2
)
503 int64_t sec_diff
= tv1
->tv_sec
- tv2
->tv_sec
;
504 return (sec_diff
* 1000000) + (int64_t)(tv1
->tv_usec
- tv2
->tv_usec
);
508 return (tp1 - tp2) in microseconds
510 _PUBLIC_
int64_t nsec_time_diff(const struct timespec
*tp1
, const struct timespec
*tp2
)
512 int64_t sec_diff
= tp1
->tv_sec
- tp2
->tv_sec
;
513 return (sec_diff
* 1000000000) + (int64_t)(tp1
->tv_nsec
- tp2
->tv_nsec
);
518 return a zero timeval
520 _PUBLIC_
struct timeval
timeval_zero(void)
529 return true if a timeval is zero
531 _PUBLIC_
bool timeval_is_zero(const struct timeval
*tv
)
533 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
537 return a timeval for the current time
539 _PUBLIC_
struct timeval
timeval_current(void)
547 return a timeval struct with the given elements
549 _PUBLIC_
struct timeval
timeval_set(uint32_t secs
, uint32_t usecs
)
559 return a timeval ofs microseconds after tv
561 _PUBLIC_
struct timeval
timeval_add(const struct timeval
*tv
,
562 uint32_t secs
, uint32_t usecs
)
564 struct timeval tv2
= *tv
;
565 const unsigned int million
= 1000000;
567 tv2
.tv_usec
+= usecs
;
568 tv2
.tv_sec
+= tv2
.tv_usec
/ million
;
569 tv2
.tv_usec
= tv2
.tv_usec
% million
;
574 return the sum of two timeval structures
576 struct timeval
timeval_sum(const struct timeval
*tv1
,
577 const struct timeval
*tv2
)
579 return timeval_add(tv1
, tv2
->tv_sec
, tv2
->tv_usec
);
583 return a timeval secs/usecs into the future
585 _PUBLIC_
struct timeval
timeval_current_ofs(uint32_t secs
, uint32_t usecs
)
587 struct timeval tv
= timeval_current();
588 return timeval_add(&tv
, secs
, usecs
);
592 return a timeval milliseconds into the future
594 _PUBLIC_
struct timeval
timeval_current_ofs_msec(uint32_t msecs
)
596 struct timeval tv
= timeval_current();
597 return timeval_add(&tv
, msecs
/ 1000, (msecs
% 1000) * 1000);
601 return a timeval microseconds into the future
603 _PUBLIC_
struct timeval
timeval_current_ofs_usec(uint32_t usecs
)
605 struct timeval tv
= timeval_current();
606 return timeval_add(&tv
, usecs
/ 1000000, usecs
% 1000000);
610 compare two timeval structures.
611 Return -1 if tv1 < tv2
612 Return 0 if tv1 == tv2
613 Return 1 if tv1 > tv2
615 _PUBLIC_
int timeval_compare(const struct timeval
*tv1
, const struct timeval
*tv2
)
617 if (tv1
->tv_sec
> tv2
->tv_sec
) return 1;
618 if (tv1
->tv_sec
< tv2
->tv_sec
) return -1;
619 if (tv1
->tv_usec
> tv2
->tv_usec
) return 1;
620 if (tv1
->tv_usec
< tv2
->tv_usec
) return -1;
625 return true if a timer is in the past
627 _PUBLIC_
bool timeval_expired(const struct timeval
*tv
)
629 struct timeval tv2
= timeval_current();
630 if (tv2
.tv_sec
> tv
->tv_sec
) return true;
631 if (tv2
.tv_sec
< tv
->tv_sec
) return false;
632 return (tv2
.tv_usec
>= tv
->tv_usec
);
636 return the number of seconds elapsed between two times
638 _PUBLIC_
double timeval_elapsed2(const struct timeval
*tv1
, const struct timeval
*tv2
)
640 return (tv2
->tv_sec
- tv1
->tv_sec
) +
641 (tv2
->tv_usec
- tv1
->tv_usec
)*1.0e-6;
645 return the number of seconds elapsed since a given time
647 _PUBLIC_
double timeval_elapsed(const struct timeval
*tv
)
649 struct timeval tv2
= timeval_current();
650 return timeval_elapsed2(tv
, &tv2
);
654 return the lesser of two timevals
656 _PUBLIC_
struct timeval
timeval_min(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 greater of two timevals
668 _PUBLIC_
struct timeval
timeval_max(const struct timeval
*tv1
,
669 const struct timeval
*tv2
)
671 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv1
;
672 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv2
;
673 if (tv1
->tv_usec
> tv2
->tv_usec
) return *tv1
;
678 return the difference between two timevals as a timeval
679 if tv1 comes after tv2, then return a zero timeval
680 (this is *tv2 - *tv1)
682 _PUBLIC_
struct timeval
timeval_until(const struct timeval
*tv1
,
683 const struct timeval
*tv2
)
686 if (timeval_compare(tv1
, tv2
) >= 0) {
687 return timeval_zero();
689 t
.tv_sec
= tv2
->tv_sec
- tv1
->tv_sec
;
690 if (tv1
->tv_usec
> tv2
->tv_usec
) {
692 t
.tv_usec
= 1000000 - (tv1
->tv_usec
- tv2
->tv_usec
);
694 t
.tv_usec
= tv2
->tv_usec
- tv1
->tv_usec
;
701 convert a timeval to a NTTIME
703 _PUBLIC_ NTTIME
timeval_to_nttime(const struct timeval
*tv
)
705 return 10*(tv
->tv_usec
+
706 ((TIME_FIXUP_CONSTANT
+ (uint64_t)tv
->tv_sec
) * 1000000));
710 convert a NTTIME to a timeval
712 _PUBLIC_
void nttime_to_timeval(struct timeval
*tv
, NTTIME t
)
714 if (tv
== NULL
) return;
718 t
-= TIME_FIXUP_CONSTANT
*1000*1000;
720 tv
->tv_sec
= t
/ 1000000;
722 if (TIME_T_MIN
> tv
->tv_sec
|| tv
->tv_sec
> TIME_T_MAX
) {
728 tv
->tv_usec
= t
- tv
->tv_sec
*1000000;
731 /*******************************************************************
732 yield the difference between *A and *B, in seconds, ignoring leap seconds
733 ********************************************************************/
734 static int tm_diff(struct tm
*a
, struct tm
*b
)
736 int ay
= a
->tm_year
+ (1900 - 1);
737 int by
= b
->tm_year
+ (1900 - 1);
738 int intervening_leap_days
=
739 (ay
/4 - by
/4) - (ay
/100 - by
/100) + (ay
/400 - by
/400);
741 int days
= 365*years
+ intervening_leap_days
+ (a
->tm_yday
- b
->tm_yday
);
742 int hours
= 24*days
+ (a
->tm_hour
- b
->tm_hour
);
743 int minutes
= 60*hours
+ (a
->tm_min
- b
->tm_min
);
744 int seconds
= 60*minutes
+ (a
->tm_sec
- b
->tm_sec
);
751 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
753 _PUBLIC_
int get_time_zone(time_t t
)
755 struct tm
*tm
= gmtime(&t
);
763 return tm_diff(&tm_utc
,tm
);
766 struct timespec
nt_time_to_unix_timespec(NTTIME
*nt
)
771 if (*nt
== 0 || *nt
== (int64_t)-1) {
778 /* d is now in 100ns units, since jan 1st 1601".
779 Save off the ns fraction. */
782 * Take the last seven decimal digits and multiply by 100.
783 * to convert from 100ns units to 1ns units.
785 ret
.tv_nsec
= (long) ((d
% (1000 * 1000 * 10)) * 100);
787 /* Convert to seconds */
790 /* Now adjust by 369 years to make the secs since 1970 */
791 d
-= TIME_FIXUP_CONSTANT_INT
;
793 if (d
<= (int64_t)TIME_T_MIN
) {
794 ret
.tv_sec
= TIME_T_MIN
;
799 if (d
>= (int64_t)TIME_T_MAX
) {
800 ret
.tv_sec
= TIME_T_MAX
;
805 ret
.tv_sec
= (time_t)d
;
811 check if 2 NTTIMEs are equal.
813 bool nt_time_equal(NTTIME
*t1
, NTTIME
*t2
)
819 Check if it's a null timespec.
822 bool null_timespec(struct timespec ts
)
824 return ts
.tv_sec
== 0 ||
825 ts
.tv_sec
== (time_t)0xFFFFFFFF ||
826 ts
.tv_sec
== (time_t)-1;
829 /****************************************************************************
830 Convert a normalized timeval to a timespec.
831 ****************************************************************************/
833 struct timespec
convert_timeval_to_timespec(const struct timeval tv
)
836 ts
.tv_sec
= tv
.tv_sec
;
837 ts
.tv_nsec
= tv
.tv_usec
* 1000;
841 /****************************************************************************
842 Convert a normalized timespec to a timeval.
843 ****************************************************************************/
845 struct timeval
convert_timespec_to_timeval(const struct timespec ts
)
848 tv
.tv_sec
= ts
.tv_sec
;
849 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
853 /****************************************************************************
854 Return a timespec for the current time
855 ****************************************************************************/
857 _PUBLIC_
struct timespec
timespec_current(void)
860 clock_gettime(CLOCK_REALTIME
, &ts
);
864 /****************************************************************************
865 Return the lesser of two timespecs.
866 ****************************************************************************/
868 struct timespec
timespec_min(const struct timespec
*ts1
,
869 const struct timespec
*ts2
)
871 if (ts1
->tv_sec
< ts2
->tv_sec
) return *ts1
;
872 if (ts1
->tv_sec
> ts2
->tv_sec
) return *ts2
;
873 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return *ts1
;
877 /****************************************************************************
878 compare two timespec structures.
879 Return -1 if ts1 < ts2
880 Return 0 if ts1 == ts2
881 Return 1 if ts1 > ts2
882 ****************************************************************************/
884 _PUBLIC_
int timespec_compare(const struct timespec
*ts1
, const struct timespec
*ts2
)
886 if (ts1
->tv_sec
> ts2
->tv_sec
) return 1;
887 if (ts1
->tv_sec
< ts2
->tv_sec
) return -1;
888 if (ts1
->tv_nsec
> ts2
->tv_nsec
) return 1;
889 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return -1;
893 /****************************************************************************
894 Round up a timespec if nsec > 500000000, round down if lower,
896 ****************************************************************************/
898 void round_timespec_to_sec(struct timespec
*ts
)
900 ts
->tv_sec
= convert_timespec_to_time_t(*ts
);
904 /****************************************************************************
905 Round a timespec to usec value.
906 ****************************************************************************/
908 void round_timespec_to_usec(struct timespec
*ts
)
910 struct timeval tv
= convert_timespec_to_timeval(*ts
);
911 *ts
= convert_timeval_to_timespec(tv
);
912 while (ts
->tv_nsec
> 1000000000) {
914 ts
->tv_nsec
-= 1000000000;
918 /****************************************************************************
919 Put a 8 byte filetime from a struct timespec. Uses GMT.
920 ****************************************************************************/
922 _PUBLIC_
void unix_timespec_to_nt_time(NTTIME
*nt
, struct timespec ts
)
926 if (ts
.tv_sec
==0 && ts
.tv_nsec
== 0) {
930 if (ts
.tv_sec
== TIME_T_MAX
) {
931 *nt
= 0x7fffffffffffffffLL
;
934 if (ts
.tv_sec
== (time_t)-1) {
940 d
+= TIME_FIXUP_CONSTANT_INT
;
942 /* d is now in 100ns units. */
943 d
+= (ts
.tv_nsec
/ 100);