2 Unix SMB/CIFS implementation.
3 time handling functions
5 Copyright (C) Andrew Tridgell 1992-2004
6 Copyright (C) Stefan (metze) Metzmacher 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/time.h"
27 * @brief time handling functions
30 #if (SIZEOF_LONG == 8)
31 #define TIME_FIXUP_CONSTANT_INT 11644473600L
32 #elif (SIZEOF_LONG_LONG == 8)
33 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
39 External access to time_t_min and time_t_max.
41 _PUBLIC_
time_t get_time_t_max(void)
47 a gettimeofday wrapper
49 _PUBLIC_
void GetTimeOfDay(struct timeval
*tval
)
51 #ifdef HAVE_GETTIMEOFDAY_TZ
52 gettimeofday(tval
,NULL
);
59 a wrapper to preferably get the monotonic time
61 _PUBLIC_
void clock_gettime_mono(struct timespec
*tp
)
63 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC
,tp
) != 0) {
64 clock_gettime(CLOCK_REALTIME
,tp
);
69 a wrapper to preferably get the monotonic time in seconds
70 as this is only second resolution we can use the cached
71 (and much faster) COARSE clock variant
73 _PUBLIC_
time_t time_mono(time_t *t
)
77 #ifdef CLOCK_MONOTONIC_COARSE
78 rc
= clock_gettime(CLOCK_MONOTONIC_COARSE
,&tp
);
81 clock_gettime_mono(&tp
);
90 #define TIME_FIXUP_CONSTANT 11644473600LL
92 time_t convert_timespec_to_time_t(struct timespec ts
)
94 /* Ensure tv_nsec is less than 1sec. */
95 while (ts
.tv_nsec
> 1000000000) {
97 ts
.tv_nsec
-= 1000000000;
100 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
101 increment if it's greater than 500 millionth of a second. */
103 if (ts
.tv_nsec
> 500000000) {
104 return ts
.tv_sec
+ 1;
109 struct timespec
convert_time_t_to_timespec(time_t t
)
120 Interpret an 8 byte "filetime" structure to a time_t
121 It's originally in "100ns units since jan 1st 1601"
123 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
130 time_t nt_time_to_unix(NTTIME nt
)
132 return convert_timespec_to_time_t(nt_time_to_unix_timespec(&nt
));
137 put a 8 byte filetime from a time_t
138 This takes GMT as input
140 _PUBLIC_
void unix_to_nt_time(NTTIME
*nt
, time_t t
)
144 if (t
== (time_t)-1) {
149 if (t
== TIME_T_MAX
) {
150 *nt
= 0x7fffffffffffffffLL
;
160 t2
+= TIME_FIXUP_CONSTANT_INT
;
168 check if it's a null unix time
170 _PUBLIC_
bool null_time(time_t t
)
173 t
== (time_t)0xFFFFFFFF ||
179 check if it's a null NTTIME
181 _PUBLIC_
bool null_nttime(NTTIME t
)
183 return t
== 0 || t
== (NTTIME
)-1;
186 /*******************************************************************
187 create a 16 bit dos packed date
188 ********************************************************************/
189 static uint16_t make_dos_date1(struct tm
*t
)
192 ret
= (((unsigned int)(t
->tm_mon
+1)) >> 3) | ((t
->tm_year
-80) << 1);
193 ret
= ((ret
&0xFF)<<8) | (t
->tm_mday
| (((t
->tm_mon
+1) & 0x7) << 5));
197 /*******************************************************************
198 create a 16 bit dos packed time
199 ********************************************************************/
200 static uint16_t make_dos_time1(struct tm
*t
)
203 ret
= ((((unsigned int)t
->tm_min
>> 3)&0x7) | (((unsigned int)t
->tm_hour
) << 3));
204 ret
= ((ret
&0xFF)<<8) | ((t
->tm_sec
/2) | ((t
->tm_min
& 0x7) << 5));
208 /*******************************************************************
209 create a 32 bit dos packed date/time from some parameters
210 This takes a GMT time and returns a packed localtime structure
211 ********************************************************************/
212 static uint32_t make_dos_date(time_t unixdate
, int zone_offset
)
221 unixdate
-= zone_offset
;
223 t
= gmtime(&unixdate
);
228 ret
= make_dos_date1(t
);
229 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
235 put a dos date into a buffer (time/date format)
236 This takes GMT time and puts local time in the buffer
238 _PUBLIC_
void push_dos_date(uint8_t *buf
, int offset
, time_t unixdate
, int zone_offset
)
240 uint32_t x
= make_dos_date(unixdate
, zone_offset
);
245 put a dos date into a buffer (date/time format)
246 This takes GMT time and puts local time in the buffer
248 _PUBLIC_
void push_dos_date2(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
251 x
= make_dos_date(unixdate
, zone_offset
);
252 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
257 put a dos 32 bit "unix like" date into a buffer. This routine takes
258 GMT and converts it to LOCAL time before putting it (most SMBs assume
259 localtime for this sort of date)
261 _PUBLIC_
void push_dos_date3(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
263 if (!null_time(unixdate
)) {
264 unixdate
-= zone_offset
;
266 SIVAL(buf
,offset
,unixdate
);
269 /*******************************************************************
270 interpret a 32 bit dos packed date/time to some parameters
271 ********************************************************************/
272 void interpret_dos_date(uint32_t date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
274 uint32_t p0
,p1
,p2
,p3
;
276 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
277 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
279 *second
= 2*(p0
& 0x1F);
280 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
281 *hour
= (p1
>>3)&0xFF;
283 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
284 *year
= ((p3
>>1)&0xFF) + 80;
288 create a unix date (int GMT) from a dos date (which is actually in
291 _PUBLIC_
time_t pull_dos_date(const uint8_t *date_ptr
, int zone_offset
)
297 dos_date
= IVAL(date_ptr
,0);
299 if (dos_date
== 0) return (time_t)0;
301 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
302 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
313 like make_unix_date() but the words are reversed
315 _PUBLIC_
time_t pull_dos_date2(const uint8_t *date_ptr
, int zone_offset
)
319 x
= IVAL(date_ptr
,0);
320 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
323 return pull_dos_date((const uint8_t *)&x
, zone_offset
);
327 create a unix GMT date from a dos date in 32 bit "unix like" format
328 these generally arrive as localtimes, with corresponding DST
330 _PUBLIC_
time_t pull_dos_date3(const uint8_t *date_ptr
, int zone_offset
)
332 time_t t
= (time_t)IVAL(date_ptr
,0);
341 return a HTTP/1.0 time string
343 _PUBLIC_
char *http_timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
347 struct tm
*tm
= localtime(&t
);
349 if (t
== TIME_T_MAX
) {
350 return talloc_strdup(mem_ctx
, "never");
354 return talloc_asprintf(mem_ctx
,"%ld seconds since the Epoch",(long)t
);
357 #ifndef HAVE_STRFTIME
358 buf
= talloc_strdup(mem_ctx
, asctime(tm
));
359 if (buf
[strlen(buf
)-1] == '\n') {
360 buf
[strlen(buf
)-1] = 0;
363 strftime(tempTime
, sizeof(tempTime
)-1, "%a, %d %b %Y %H:%M:%S %Z", tm
);
364 buf
= talloc_strdup(mem_ctx
, tempTime
);
365 #endif /* !HAVE_STRFTIME */
371 Return the date and time as a string
373 _PUBLIC_
char *timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
381 return talloc_asprintf(mem_ctx
,
382 "%ld seconds since the Epoch",
387 /* Some versions of gcc complain about using some special format
388 * specifiers. This is a bug in gcc, not a bug in this code. See a
389 * recent strftime() manual page for details. */
390 strftime(tempTime
,sizeof(tempTime
)-1,"%a %b %e %X %Y %Z",tm
);
391 TimeBuf
= talloc_strdup(mem_ctx
, tempTime
);
393 TimeBuf
= talloc_strdup(mem_ctx
, asctime(tm
));
400 return a talloced string representing a NTTIME for human consumption
402 _PUBLIC_
const char *nt_time_string(TALLOC_CTX
*mem_ctx
, NTTIME nt
)
408 t
= nt_time_to_unix(nt
);
409 return timestring(mem_ctx
, t
);
414 put a NTTIME into a packet
416 _PUBLIC_
void push_nttime(uint8_t *base
, uint16_t offset
, NTTIME t
)
418 SBVAL(base
, offset
, t
);
422 pull a NTTIME from a packet
424 _PUBLIC_ NTTIME
pull_nttime(uint8_t *base
, uint16_t offset
)
426 NTTIME ret
= BVAL(base
, offset
);
431 return (tv1 - tv2) in microseconds
433 _PUBLIC_
int64_t usec_time_diff(const struct timeval
*tv1
, const struct timeval
*tv2
)
435 int64_t sec_diff
= tv1
->tv_sec
- tv2
->tv_sec
;
436 return (sec_diff
* 1000000) + (int64_t)(tv1
->tv_usec
- tv2
->tv_usec
);
440 return (tp1 - tp2) in microseconds
442 _PUBLIC_
int64_t nsec_time_diff(const struct timespec
*tp1
, const struct timespec
*tp2
)
444 int64_t sec_diff
= tp1
->tv_sec
- tp2
->tv_sec
;
445 return (sec_diff
* 1000000000) + (int64_t)(tp1
->tv_nsec
- tp2
->tv_nsec
);
450 return a zero timeval
452 _PUBLIC_
struct timeval
timeval_zero(void)
461 return true if a timeval is zero
463 _PUBLIC_
bool timeval_is_zero(const struct timeval
*tv
)
465 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
469 return a timeval for the current time
471 _PUBLIC_
struct timeval
timeval_current(void)
479 return a timeval struct with the given elements
481 _PUBLIC_
struct timeval
timeval_set(uint32_t secs
, uint32_t usecs
)
491 return a timeval ofs microseconds after tv
493 _PUBLIC_
struct timeval
timeval_add(const struct timeval
*tv
,
494 uint32_t secs
, uint32_t usecs
)
496 struct timeval tv2
= *tv
;
497 const unsigned int million
= 1000000;
499 tv2
.tv_usec
+= usecs
;
500 tv2
.tv_sec
+= tv2
.tv_usec
/ million
;
501 tv2
.tv_usec
= tv2
.tv_usec
% million
;
506 return the sum of two timeval structures
508 struct timeval
timeval_sum(const struct timeval
*tv1
,
509 const struct timeval
*tv2
)
511 return timeval_add(tv1
, tv2
->tv_sec
, tv2
->tv_usec
);
515 return a timeval secs/usecs into the future
517 _PUBLIC_
struct timeval
timeval_current_ofs(uint32_t secs
, uint32_t usecs
)
519 struct timeval tv
= timeval_current();
520 return timeval_add(&tv
, secs
, usecs
);
524 compare two timeval structures.
525 Return -1 if tv1 < tv2
526 Return 0 if tv1 == tv2
527 Return 1 if tv1 > tv2
529 _PUBLIC_
int timeval_compare(const struct timeval
*tv1
, const struct timeval
*tv2
)
531 if (tv1
->tv_sec
> tv2
->tv_sec
) return 1;
532 if (tv1
->tv_sec
< tv2
->tv_sec
) return -1;
533 if (tv1
->tv_usec
> tv2
->tv_usec
) return 1;
534 if (tv1
->tv_usec
< tv2
->tv_usec
) return -1;
539 return true if a timer is in the past
541 _PUBLIC_
bool timeval_expired(const struct timeval
*tv
)
543 struct timeval tv2
= timeval_current();
544 if (tv2
.tv_sec
> tv
->tv_sec
) return true;
545 if (tv2
.tv_sec
< tv
->tv_sec
) return false;
546 return (tv2
.tv_usec
>= tv
->tv_usec
);
550 return the number of seconds elapsed between two times
552 _PUBLIC_
double timeval_elapsed2(const struct timeval
*tv1
, const struct timeval
*tv2
)
554 return (tv2
->tv_sec
- tv1
->tv_sec
) +
555 (tv2
->tv_usec
- tv1
->tv_usec
)*1.0e-6;
559 return the number of seconds elapsed since a given time
561 _PUBLIC_
double timeval_elapsed(const struct timeval
*tv
)
563 struct timeval tv2
= timeval_current();
564 return timeval_elapsed2(tv
, &tv2
);
568 return the lesser of two timevals
570 _PUBLIC_
struct timeval
timeval_min(const struct timeval
*tv1
,
571 const struct timeval
*tv2
)
573 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv1
;
574 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv2
;
575 if (tv1
->tv_usec
< tv2
->tv_usec
) return *tv1
;
580 return the greater of two timevals
582 _PUBLIC_
struct timeval
timeval_max(const struct timeval
*tv1
,
583 const struct timeval
*tv2
)
585 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv1
;
586 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv2
;
587 if (tv1
->tv_usec
> tv2
->tv_usec
) return *tv1
;
592 return the difference between two timevals as a timeval
593 if tv1 comes after tv2, then return a zero timeval
594 (this is *tv2 - *tv1)
596 _PUBLIC_
struct timeval
timeval_until(const struct timeval
*tv1
,
597 const struct timeval
*tv2
)
600 if (timeval_compare(tv1
, tv2
) >= 0) {
601 return timeval_zero();
603 t
.tv_sec
= tv2
->tv_sec
- tv1
->tv_sec
;
604 if (tv1
->tv_usec
> tv2
->tv_usec
) {
606 t
.tv_usec
= 1000000 - (tv1
->tv_usec
- tv2
->tv_usec
);
608 t
.tv_usec
= tv2
->tv_usec
- tv1
->tv_usec
;
615 convert a timeval to a NTTIME
617 _PUBLIC_ NTTIME
timeval_to_nttime(const struct timeval
*tv
)
619 return 10*(tv
->tv_usec
+
620 ((TIME_FIXUP_CONSTANT
+ (uint64_t)tv
->tv_sec
) * 1000000));
624 convert a NTTIME to a timeval
626 _PUBLIC_
void nttime_to_timeval(struct timeval
*tv
, NTTIME t
)
628 if (tv
== NULL
) return;
632 t
-= TIME_FIXUP_CONSTANT
*1000*1000;
634 tv
->tv_sec
= t
/ 1000000;
636 if (TIME_T_MIN
> tv
->tv_sec
|| tv
->tv_sec
> TIME_T_MAX
) {
642 tv
->tv_usec
= t
- tv
->tv_sec
*1000000;
645 /*******************************************************************
646 yield the difference between *A and *B, in seconds, ignoring leap seconds
647 ********************************************************************/
648 static int tm_diff(struct tm
*a
, struct tm
*b
)
650 int ay
= a
->tm_year
+ (1900 - 1);
651 int by
= b
->tm_year
+ (1900 - 1);
652 int intervening_leap_days
=
653 (ay
/4 - by
/4) - (ay
/100 - by
/100) + (ay
/400 - by
/400);
655 int days
= 365*years
+ intervening_leap_days
+ (a
->tm_yday
- b
->tm_yday
);
656 int hours
= 24*days
+ (a
->tm_hour
- b
->tm_hour
);
657 int minutes
= 60*hours
+ (a
->tm_min
- b
->tm_min
);
658 int seconds
= 60*minutes
+ (a
->tm_sec
- b
->tm_sec
);
664 int extra_time_offset
=0;
667 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
669 _PUBLIC_
int get_time_zone(time_t t
)
671 struct tm
*tm
= gmtime(&t
);
679 return tm_diff(&tm_utc
,tm
)+60*extra_time_offset
;
682 struct timespec
nt_time_to_unix_timespec(NTTIME
*nt
)
687 if (*nt
== 0 || *nt
== (int64_t)-1) {
694 /* d is now in 100ns units, since jan 1st 1601".
695 Save off the ns fraction. */
698 * Take the last seven decimal digits and multiply by 100.
699 * to convert from 100ns units to 1ns units.
701 ret
.tv_nsec
= (long) ((d
% (1000 * 1000 * 10)) * 100);
703 /* Convert to seconds */
706 /* Now adjust by 369 years to make the secs since 1970 */
707 d
-= TIME_FIXUP_CONSTANT_INT
;
709 if (d
<= (int64_t)TIME_T_MIN
) {
710 ret
.tv_sec
= TIME_T_MIN
;
715 if (d
>= (int64_t)TIME_T_MAX
) {
716 ret
.tv_sec
= TIME_T_MAX
;
721 ret
.tv_sec
= (time_t)d
;
727 check if 2 NTTIMEs are equal.
729 bool nt_time_equal(NTTIME
*t1
, NTTIME
*t2
)
735 Check if it's a null timespec.
738 bool null_timespec(struct timespec ts
)
740 return ts
.tv_sec
== 0 ||
741 ts
.tv_sec
== (time_t)0xFFFFFFFF ||
742 ts
.tv_sec
== (time_t)-1;