s3: smbd: Deliberately currupt an uninitialized pointer.
[Samba.git] / lib / util / time.c
blobc2a77d664d37a21107134cf81950d4389d2ebbda
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
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/>.
24 #include "replace.h"
25 #include "system/time.h"
26 #include "byteorder.h"
27 #include "time_basic.h"
28 #include "lib/util/time.h" /* Avoid /usr/include/time.h */
29 #include <sys/stat.h>
30 #ifndef NO_CONFIG_H
31 #include "config.h"
32 #endif
34 /**
35 * @file
36 * @brief time handling functions
39 #if (SIZEOF_LONG == 8)
40 #define TIME_FIXUP_CONSTANT_INT 11644473600L
41 #elif (SIZEOF_LONG_LONG == 8)
42 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
43 #endif
46 #define NSEC_PER_SEC 1000000000
48 /**
49 External access to time_t_min and time_t_max.
50 **/
51 _PUBLIC_ time_t get_time_t_max(void)
53 return TIME_T_MAX;
56 /**
57 a wrapper to preferably get the monotonic time
58 **/
59 _PUBLIC_ void clock_gettime_mono(struct timespec *tp)
61 /* prefer a suspend aware monotonic CLOCK_BOOTTIME: */
62 #ifdef CLOCK_BOOTTIME
63 if (clock_gettime(CLOCK_BOOTTIME,tp) == 0) {
64 return;
66 #endif
67 /* then try the monotonic clock: */
68 #ifndef CUSTOM_CLOCK_MONOTONIC_IS_REALTIME
69 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC,tp) == 0) {
70 return;
72 #endif
73 clock_gettime(CLOCK_REALTIME,tp);
76 /**
77 a wrapper to preferably get the monotonic time in seconds
78 **/
79 _PUBLIC_ time_t time_mono(time_t *t)
81 struct timespec tp;
83 clock_gettime_mono(&tp);
84 if (t != NULL) {
85 *t = tp.tv_sec;
87 return tp.tv_sec;
91 #define TIME_FIXUP_CONSTANT 11644473600LL
93 time_t convert_timespec_to_time_t(struct timespec ts)
95 /* Ensure tv_nsec is less than 1sec. */
96 normalize_timespec(&ts);
98 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
99 increment if it's greater than 500 millionth of a second. */
101 if (ts.tv_nsec > 500000000) {
102 return ts.tv_sec + 1;
104 return ts.tv_sec;
107 struct timespec convert_time_t_to_timespec(time_t t)
109 struct timespec ts;
110 ts.tv_sec = t;
111 ts.tv_nsec = 0;
112 return ts;
118 Interpret an 8 byte "filetime" structure to a time_t
119 It's originally in "100ns units since jan 1st 1601"
121 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
123 tv_sec = 0
124 tv_nsec = 0;
126 Returns GMT.
128 time_t nt_time_to_unix(NTTIME nt)
130 return convert_timespec_to_time_t(nt_time_to_unix_timespec(nt));
135 put a 8 byte filetime from a time_t
136 This takes GMT as input
138 _PUBLIC_ void unix_to_nt_time(NTTIME *nt, time_t t)
140 uint64_t t2;
142 if (t == (time_t)-1) {
143 *nt = (NTTIME)-1LL;
144 return;
147 if (t == TIME_T_MAX || t == INT64_MAX) {
148 *nt = 0x7fffffffffffffffLL;
149 return;
152 if (t == 0) {
153 *nt = 0;
154 return;
157 t2 = t;
158 t2 += TIME_FIXUP_CONSTANT_INT;
159 t2 *= 1000*1000*10;
161 *nt = t2;
166 check if it's a null unix time
168 _PUBLIC_ bool null_time(time_t t)
170 return t == 0 ||
171 t == (time_t)0xFFFFFFFF ||
172 t == (time_t)-1;
177 check if it's a null NTTIME
179 _PUBLIC_ bool null_nttime(NTTIME t)
181 return t == 0;
184 /*******************************************************************
185 create a 16 bit dos packed date
186 ********************************************************************/
187 static uint16_t make_dos_date1(struct tm *t)
189 uint16_t ret=0;
190 ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
191 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
192 return ret;
195 /*******************************************************************
196 create a 16 bit dos packed time
197 ********************************************************************/
198 static uint16_t make_dos_time1(struct tm *t)
200 uint16_t ret=0;
201 ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));
202 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
203 return ret;
206 /*******************************************************************
207 create a 32 bit dos packed date/time from some parameters
208 This takes a GMT time and returns a packed localtime structure
209 ********************************************************************/
210 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
212 struct tm *t;
213 uint32_t ret=0;
215 if (unixdate == 0) {
216 return 0;
219 unixdate -= zone_offset;
221 t = gmtime(&unixdate);
222 if (!t) {
223 return 0xFFFFFFFF;
226 ret = make_dos_date1(t);
227 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
229 return ret;
233 put a dos date into a buffer (time/date format)
234 This takes GMT time and puts local time in the buffer
236 _PUBLIC_ void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
238 uint32_t x = make_dos_date(unixdate, zone_offset);
239 SIVAL(buf,offset,x);
243 put a dos date into a buffer (date/time format)
244 This takes GMT time and puts local time in the buffer
246 _PUBLIC_ void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
248 uint32_t x;
249 x = make_dos_date(unixdate, zone_offset);
250 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
251 SIVAL(buf,offset,x);
255 put a dos 32 bit "unix like" date into a buffer. This routine takes
256 GMT and converts it to LOCAL time before putting it (most SMBs assume
257 localtime for this sort of date)
259 _PUBLIC_ void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
261 if (!null_time(unixdate)) {
262 unixdate -= zone_offset;
264 SIVAL(buf,offset,unixdate);
267 /*******************************************************************
268 interpret a 32 bit dos packed date/time to some parameters
269 ********************************************************************/
270 void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
272 uint32_t p0,p1,p2,p3;
274 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
275 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
277 *second = 2*(p0 & 0x1F);
278 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
279 *hour = (p1>>3)&0xFF;
280 *day = (p2&0x1F);
281 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
282 *year = ((p3>>1)&0xFF) + 80;
286 create a unix date (int GMT) from a dos date (which is actually in
287 localtime)
289 _PUBLIC_ time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
291 uint32_t dos_date=0;
292 struct tm t;
293 time_t ret;
295 dos_date = IVAL(date_ptr,0);
297 if (dos_date == 0) return (time_t)0;
299 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
300 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
301 t.tm_isdst = -1;
303 ret = timegm(&t);
305 ret += zone_offset;
307 return ret;
311 like make_unix_date() but the words are reversed
313 _PUBLIC_ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
315 uint32_t x,x2;
317 x = IVAL(date_ptr,0);
318 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
319 SIVAL(&x,0,x2);
321 return pull_dos_date((const uint8_t *)&x, zone_offset);
325 create a unix GMT date from a dos date in 32 bit "unix like" format
326 these generally arrive as localtimes, with corresponding DST
328 _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
330 time_t t = (time_t)IVAL(date_ptr,0);
332 if (t == (time_t)0xFFFFFFFF) {
333 t = (time_t)-1;
336 if (!null_time(t)) {
337 t += zone_offset;
339 return t;
342 /****************************************************************************
343 Return the date and time as a string
344 ****************************************************************************/
346 char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
348 struct timeval_buf tmp;
349 char *result;
351 result = talloc_strdup(ctx, timeval_str_buf(tp, false, hires, &tmp));
352 if (result == NULL) {
353 return NULL;
357 * beautify the talloc_report output
359 * This is not just cosmetics. A C compiler might in theory make the
360 * talloc_strdup call above a tail call with the tail call
361 * optimization. This would render "tmp" invalid while talloc_strdup
362 * tries to duplicate it. The talloc_set_name_const call below puts
363 * the talloc_strdup call into non-tail position.
365 talloc_set_name_const(result, result);
366 return result;
369 /****************************************************************************
370 Return the date and time as a string
371 ****************************************************************************/
373 const char *timespec_string_buf(const struct timespec *tp,
374 bool hires,
375 struct timeval_buf *buf)
377 time_t t;
378 struct tm *tm = NULL;
379 int len;
381 if (is_omit_timespec(tp)) {
382 strlcpy(buf->buf, "SAMBA_UTIME_OMIT", sizeof(buf->buf));
383 return buf->buf;
386 t = (time_t)tp->tv_sec;
387 tm = localtime(&t);
389 if (tm == NULL) {
390 if (hires) {
391 len = snprintf(buf->buf, sizeof(buf->buf),
392 "%ld.%09ld seconds since the Epoch",
393 (long)tp->tv_sec, (long)tp->tv_nsec);
394 } else {
395 len = snprintf(buf->buf, sizeof(buf->buf),
396 "%ld seconds since the Epoch", (long)t);
398 } else if (!hires) {
399 len = snprintf(buf->buf, sizeof(buf->buf),
400 "%04d-%02d-%02d %02d:%02d:%02d",
401 1900 + tm->tm_year,
402 tm->tm_mon + 1,
403 tm->tm_mday,
404 tm->tm_hour,
405 tm->tm_min,
406 tm->tm_sec);
407 } else {
408 len = snprintf(buf->buf, sizeof(buf->buf),
409 "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
410 1900 + tm->tm_year,
411 tm->tm_mon + 1,
412 tm->tm_mday,
413 tm->tm_hour,
414 tm->tm_min,
415 tm->tm_sec,
416 (long)tp->tv_nsec);
418 if (len == -1) {
419 return "";
422 return buf->buf;
425 char *current_timestring(TALLOC_CTX *ctx, bool hires)
427 struct timeval tv;
429 GetTimeOfDay(&tv);
430 return timeval_string(ctx, &tv, hires);
434 * Return date and time as a minimal string avoiding funny characters
435 * that may cause trouble in file names. We only use digits and
436 * underscore ... or a minus/hyphen if we got negative time.
438 char *minimal_timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
440 time_t t;
441 struct tm *tm;
443 t = (time_t)tp->tv_sec;
444 tm = localtime(&t);
445 if (!tm) {
446 if (hires) {
447 return talloc_asprintf(ctx, "%ld_%06ld",
448 (long)tp->tv_sec,
449 (long)tp->tv_usec);
450 } else {
451 return talloc_asprintf(ctx, "%ld", (long)t);
453 } else {
454 if (hires) {
455 return talloc_asprintf(ctx,
456 "%04d%02d%02d_%02d%02d%02d_%06ld",
457 tm->tm_year+1900,
458 tm->tm_mon+1,
459 tm->tm_mday,
460 tm->tm_hour,
461 tm->tm_min,
462 tm->tm_sec,
463 (long)tp->tv_usec);
464 } else {
465 return talloc_asprintf(ctx,
466 "%04d%02d%02d_%02d%02d%02d",
467 tm->tm_year+1900,
468 tm->tm_mon+1,
469 tm->tm_mday,
470 tm->tm_hour,
471 tm->tm_min,
472 tm->tm_sec);
477 char *current_minimal_timestring(TALLOC_CTX *ctx, bool hires)
479 struct timeval tv;
481 GetTimeOfDay(&tv);
482 return minimal_timeval_string(ctx, &tv, hires);
486 return a HTTP/1.0 time string
488 _PUBLIC_ char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
490 char *buf;
491 char tempTime[60];
492 struct tm *tm = localtime(&t);
494 if (t == TIME_T_MAX) {
495 return talloc_strdup(mem_ctx, "never");
498 if (!tm) {
499 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
502 #ifndef HAVE_STRFTIME
503 buf = talloc_strdup(mem_ctx, asctime(tm));
504 if (buf[strlen(buf)-1] == '\n') {
505 buf[strlen(buf)-1] = 0;
507 #else
508 strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
509 buf = talloc_strdup(mem_ctx, tempTime);
510 #endif /* !HAVE_STRFTIME */
512 return buf;
516 Return the date and time as a string
518 _PUBLIC_ char *timestring(TALLOC_CTX *mem_ctx, time_t t)
520 char *TimeBuf;
521 char tempTime[80];
522 struct tm *tm;
524 tm = localtime(&t);
525 if (!tm) {
526 return talloc_asprintf(mem_ctx,
527 "%ld seconds since the Epoch",
528 (long)t);
531 #ifdef HAVE_STRFTIME
532 /* Some versions of gcc complain about using some special format
533 * specifiers. This is a bug in gcc, not a bug in this code. See a
534 * recent strftime() manual page for details. */
535 strftime(tempTime,sizeof(tempTime)-1,"%a %b %e %X %Y %Z",tm);
536 TimeBuf = talloc_strdup(mem_ctx, tempTime);
537 #else
538 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
539 if (TimeBuf == NULL) {
540 return NULL;
542 if (TimeBuf[0] != '\0') {
543 size_t len = strlen(TimeBuf);
544 if (TimeBuf[len - 1] == '\n') {
545 TimeBuf[len - 1] = '\0';
548 #endif
550 return TimeBuf;
554 return a talloced string representing a NTTIME for human consumption
556 _PUBLIC_ const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
558 time_t t;
559 if (nt == 0) {
560 return "NTTIME(0)";
562 t = nt_time_to_full_time_t(nt);
563 return timestring(mem_ctx, t);
568 put a NTTIME into a packet
570 _PUBLIC_ void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
572 SBVAL(base, offset, t);
576 pull a NTTIME from a packet
578 _PUBLIC_ NTTIME pull_nttime(uint8_t *base, uint16_t offset)
580 NTTIME ret = BVAL(base, offset);
581 return ret;
585 return (tv1 - tv2) in microseconds
587 _PUBLIC_ int64_t usec_time_diff(const struct timeval *tv1, const struct timeval *tv2)
589 int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
590 return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
594 return (tp1 - tp2) in nanoseconds
596 _PUBLIC_ int64_t nsec_time_diff(const struct timespec *tp1, const struct timespec *tp2)
598 int64_t sec_diff = tp1->tv_sec - tp2->tv_sec;
599 return (sec_diff * 1000000000) + (int64_t)(tp1->tv_nsec - tp2->tv_nsec);
604 return a zero timeval
606 _PUBLIC_ struct timeval timeval_zero(void)
608 struct timeval tv;
609 tv.tv_sec = 0;
610 tv.tv_usec = 0;
611 return tv;
615 return true if a timeval is zero
617 _PUBLIC_ bool timeval_is_zero(const struct timeval *tv)
619 return tv->tv_sec == 0 && tv->tv_usec == 0;
623 return a timeval for the current time
625 _PUBLIC_ struct timeval timeval_current(void)
627 struct timeval tv;
628 GetTimeOfDay(&tv);
629 return tv;
633 return a timeval struct with the given elements
635 _PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs)
637 struct timeval tv;
638 tv.tv_sec = secs;
639 tv.tv_usec = usecs;
640 return tv;
645 return a timeval ofs microseconds after tv
647 _PUBLIC_ struct timeval timeval_add(const struct timeval *tv,
648 uint32_t secs, uint32_t usecs)
650 struct timeval tv2 = *tv;
651 const unsigned int million = 1000000;
652 tv2.tv_sec += secs;
653 tv2.tv_usec += usecs;
654 tv2.tv_sec += tv2.tv_usec / million;
655 tv2.tv_usec = tv2.tv_usec % million;
656 return tv2;
660 return the sum of two timeval structures
662 struct timeval timeval_sum(const struct timeval *tv1,
663 const struct timeval *tv2)
665 return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
669 return a timeval secs/usecs into the future
671 _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
673 struct timeval tv = timeval_current();
674 return timeval_add(&tv, secs, usecs);
678 return a timeval milliseconds into the future
680 _PUBLIC_ struct timeval timeval_current_ofs_msec(uint32_t msecs)
682 struct timeval tv = timeval_current();
683 return timeval_add(&tv, msecs / 1000, (msecs % 1000) * 1000);
687 return a timeval microseconds into the future
689 _PUBLIC_ struct timeval timeval_current_ofs_usec(uint32_t usecs)
691 struct timeval tv = timeval_current();
692 return timeval_add(&tv, usecs / 1000000, usecs % 1000000);
696 compare two timeval structures.
697 Return -1 if tv1 < tv2
698 Return 0 if tv1 == tv2
699 Return 1 if tv1 > tv2
701 _PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
703 if (tv1->tv_sec > tv2->tv_sec) return 1;
704 if (tv1->tv_sec < tv2->tv_sec) return -1;
705 if (tv1->tv_usec > tv2->tv_usec) return 1;
706 if (tv1->tv_usec < tv2->tv_usec) return -1;
707 return 0;
711 return true if a timer is in the past
713 _PUBLIC_ bool timeval_expired(const struct timeval *tv)
715 struct timeval tv2 = timeval_current();
716 if (tv2.tv_sec > tv->tv_sec) return true;
717 if (tv2.tv_sec < tv->tv_sec) return false;
718 return (tv2.tv_usec >= tv->tv_usec);
722 return the number of seconds elapsed between two times
724 _PUBLIC_ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
726 return (tv2->tv_sec - tv1->tv_sec) +
727 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
731 return the number of seconds elapsed since a given time
733 _PUBLIC_ double timeval_elapsed(const struct timeval *tv)
735 struct timeval tv2 = timeval_current();
736 return timeval_elapsed2(tv, &tv2);
739 * return the number of seconds elapsed between two times
741 _PUBLIC_ double timespec_elapsed2(const struct timespec *ts1,
742 const struct timespec *ts2)
744 return (ts2->tv_sec - ts1->tv_sec) +
745 (ts2->tv_nsec - ts1->tv_nsec)*1.0e-9;
749 * return the number of seconds elapsed since a given time
751 _PUBLIC_ double timespec_elapsed(const struct timespec *ts)
753 struct timespec ts2 = timespec_current();
754 return timespec_elapsed2(ts, &ts2);
758 return the lesser of two timevals
760 _PUBLIC_ struct timeval timeval_min(const struct timeval *tv1,
761 const struct timeval *tv2)
763 if (tv1->tv_sec < tv2->tv_sec) return *tv1;
764 if (tv1->tv_sec > tv2->tv_sec) return *tv2;
765 if (tv1->tv_usec < tv2->tv_usec) return *tv1;
766 return *tv2;
770 return the greater of two timevals
772 _PUBLIC_ struct timeval timeval_max(const struct timeval *tv1,
773 const struct timeval *tv2)
775 if (tv1->tv_sec > tv2->tv_sec) return *tv1;
776 if (tv1->tv_sec < tv2->tv_sec) return *tv2;
777 if (tv1->tv_usec > tv2->tv_usec) return *tv1;
778 return *tv2;
782 return the difference between two timevals as a timeval
783 if tv1 comes after tv2, then return a zero timeval
784 (this is *tv2 - *tv1)
786 _PUBLIC_ struct timeval timeval_until(const struct timeval *tv1,
787 const struct timeval *tv2)
789 struct timeval t;
790 if (timeval_compare(tv1, tv2) >= 0) {
791 return timeval_zero();
793 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
794 if (tv1->tv_usec > tv2->tv_usec) {
795 t.tv_sec--;
796 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
797 } else {
798 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
800 return t;
805 convert a timeval to a NTTIME
807 _PUBLIC_ NTTIME timeval_to_nttime(const struct timeval *tv)
809 return 10*(tv->tv_usec +
810 ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
814 convert a NTTIME to a timeval
816 _PUBLIC_ void nttime_to_timeval(struct timeval *tv, NTTIME t)
818 if (tv == NULL) return;
820 t += 10/2;
821 t /= 10;
822 t -= TIME_FIXUP_CONSTANT*1000*1000;
824 tv->tv_sec = t / 1000000;
826 if (TIME_T_MIN > tv->tv_sec || tv->tv_sec > TIME_T_MAX) {
827 tv->tv_sec = 0;
828 tv->tv_usec = 0;
829 return;
832 tv->tv_usec = t - tv->tv_sec*1000000;
835 /*******************************************************************
836 yield the difference between *A and *B, in seconds, ignoring leap seconds
837 ********************************************************************/
838 static int tm_diff(struct tm *a, struct tm *b)
840 int ay = a->tm_year + (1900 - 1);
841 int by = b->tm_year + (1900 - 1);
842 int intervening_leap_days =
843 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
844 int years = ay - by;
845 int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
846 int hours = 24*days + (a->tm_hour - b->tm_hour);
847 int minutes = 60*hours + (a->tm_min - b->tm_min);
848 int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
850 return seconds;
855 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
857 _PUBLIC_ int get_time_zone(time_t t)
859 struct tm *tm = gmtime(&t);
860 struct tm tm_utc;
861 if (!tm)
862 return 0;
863 tm_utc = *tm;
864 tm = localtime(&t);
865 if (!tm)
866 return 0;
867 return tm_diff(&tm_utc,tm);
871 * Raw convert an NTTIME to a unix timespec.
874 struct timespec nt_time_to_unix_timespec_raw(
875 NTTIME nt)
877 int64_t d;
878 struct timespec ret;
880 d = (int64_t)nt;
881 /* d is now in 100ns units, since jan 1st 1601".
882 Save off the ns fraction. */
885 * Take the last seven decimal digits and multiply by 100.
886 * to convert from 100ns units to 1ns units.
888 ret.tv_nsec = (long) ((d % (1000 * 1000 * 10)) * 100);
890 /* Convert to seconds */
891 d /= 1000*1000*10;
893 /* Now adjust by 369 years to make the secs since 1970 */
894 d -= TIME_FIXUP_CONSTANT_INT;
896 ret.tv_sec = (time_t)d;
897 return ret;
900 struct timespec nt_time_to_unix_timespec(NTTIME nt)
902 struct timespec ret;
904 if (nt == 0 || nt == (int64_t)-1) {
905 ret.tv_sec = 0;
906 ret.tv_nsec = 0;
907 return ret;
910 ret = nt_time_to_unix_timespec_raw(nt);
912 if (ret.tv_sec <= TIME_T_MIN) {
913 ret.tv_sec = TIME_T_MIN;
914 ret.tv_nsec = 0;
915 return ret;
918 if (ret.tv_sec >= TIME_T_MAX) {
919 ret.tv_sec = TIME_T_MAX;
920 ret.tv_nsec = 0;
921 return ret;
923 return ret;
928 check if 2 NTTIMEs are equal.
930 bool nt_time_equal(NTTIME *t1, NTTIME *t2)
932 return *t1 == *t2;
936 Check if it's a null timespec.
939 bool null_timespec(struct timespec ts)
941 return ts.tv_sec == 0 ||
942 ts.tv_sec == (time_t)0xFFFFFFFF ||
943 ts.tv_sec == (time_t)-1;
946 /****************************************************************************
947 Convert a normalized timeval to a timespec.
948 ****************************************************************************/
950 struct timespec convert_timeval_to_timespec(const struct timeval tv)
952 struct timespec ts;
953 ts.tv_sec = tv.tv_sec;
954 ts.tv_nsec = tv.tv_usec * 1000;
955 return ts;
958 /****************************************************************************
959 Convert a normalized timespec to a timeval.
960 ****************************************************************************/
962 struct timeval convert_timespec_to_timeval(const struct timespec ts)
964 struct timeval tv;
965 tv.tv_sec = ts.tv_sec;
966 tv.tv_usec = ts.tv_nsec / 1000;
967 return tv;
970 /****************************************************************************
971 Return a timespec for the current time
972 ****************************************************************************/
974 _PUBLIC_ struct timespec timespec_current(void)
976 struct timespec ts;
977 clock_gettime(CLOCK_REALTIME, &ts);
978 return ts;
981 /****************************************************************************
982 Return the lesser of two timespecs.
983 ****************************************************************************/
985 struct timespec timespec_min(const struct timespec *ts1,
986 const struct timespec *ts2)
988 if (ts1->tv_sec < ts2->tv_sec) return *ts1;
989 if (ts1->tv_sec > ts2->tv_sec) return *ts2;
990 if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
991 return *ts2;
994 /****************************************************************************
995 compare two timespec structures.
996 Return -1 if ts1 < ts2
997 Return 0 if ts1 == ts2
998 Return 1 if ts1 > ts2
999 ****************************************************************************/
1001 _PUBLIC_ int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
1003 if (ts1->tv_sec > ts2->tv_sec) return 1;
1004 if (ts1->tv_sec < ts2->tv_sec) return -1;
1005 if (ts1->tv_nsec > ts2->tv_nsec) return 1;
1006 if (ts1->tv_nsec < ts2->tv_nsec) return -1;
1007 return 0;
1010 /****************************************************************************
1011 Round up a timespec if nsec > 500000000, round down if lower,
1012 then zero nsec.
1013 ****************************************************************************/
1015 void round_timespec_to_sec(struct timespec *ts)
1017 ts->tv_sec = convert_timespec_to_time_t(*ts);
1018 ts->tv_nsec = 0;
1021 /****************************************************************************
1022 Round a timespec to usec value.
1023 ****************************************************************************/
1025 void round_timespec_to_usec(struct timespec *ts)
1027 struct timeval tv = convert_timespec_to_timeval(*ts);
1028 *ts = convert_timeval_to_timespec(tv);
1029 normalize_timespec(ts);
1032 /****************************************************************************
1033 Round a timespec to NTTIME resolution.
1034 ****************************************************************************/
1036 void round_timespec_to_nttime(struct timespec *ts)
1038 ts->tv_nsec = (ts->tv_nsec / 100) * 100;
1041 /****************************************************************************
1042 Put a 8 byte filetime from a struct timespec. Uses GMT.
1043 ****************************************************************************/
1045 _PUBLIC_ NTTIME unix_timespec_to_nt_time(struct timespec ts)
1047 uint64_t d;
1049 if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
1050 return 0;
1052 if (ts.tv_sec == TIME_T_MAX) {
1053 return 0x7fffffffffffffffLL;
1055 if (ts.tv_sec == (time_t)-1) {
1056 return (uint64_t)-1;
1059 d = ts.tv_sec;
1060 d += TIME_FIXUP_CONSTANT_INT;
1061 d *= 1000*1000*10;
1062 /* d is now in 100ns units. */
1063 d += (ts.tv_nsec / 100);
1065 return d;
1069 * Functions supporting the full range of time_t and struct timespec values,
1070 * including 0, -1 and all other negative values. These functions don't use 0 or
1071 * -1 values as sentinel to denote "unset" variables, but use the POSIX 2008
1072 * define UTIME_OMIT from utimensat(2).
1076 * Check if it's a to be omitted timespec.
1078 bool is_omit_timespec(const struct timespec *ts)
1080 return ts->tv_nsec == SAMBA_UTIME_OMIT;
1084 * Return a to be omitted timespec.
1086 struct timespec make_omit_timespec(void)
1088 return (struct timespec){.tv_nsec = SAMBA_UTIME_OMIT};
1092 * Like unix_timespec_to_nt_time() but without the special casing of tv_sec=0
1093 * and -1. Also dealing with SAMBA_UTIME_OMIT.
1095 NTTIME full_timespec_to_nt_time(const struct timespec *_ts)
1097 struct timespec ts = *_ts;
1098 uint64_t d;
1100 if (is_omit_timespec(_ts)) {
1101 return NTTIME_OMIT;
1104 /* Ensure tv_nsec is less than 1 sec. */
1105 while (ts.tv_nsec > 1000000000) {
1106 if (ts.tv_sec > TIME_T_MAX) {
1107 return NTTIME_MAX;
1109 ts.tv_sec += 1;
1110 ts.tv_nsec -= 1000000000;
1113 if (ts.tv_sec >= TIME_T_MAX) {
1114 return NTTIME_MAX;
1116 if ((ts.tv_sec + TIME_FIXUP_CONSTANT_INT) <= 0) {
1117 return NTTIME_MIN;
1120 d = TIME_FIXUP_CONSTANT_INT;
1121 d += ts.tv_sec;
1123 d *= 1000*1000*10;
1124 /* d is now in 100ns units. */
1125 d += (ts.tv_nsec / 100);
1127 return d;
1131 * Like nt_time_to_unix_timespec() but allowing negative tv_sec values and
1132 * returning NTTIME=0 and -1 as struct timespec {.tv_nsec = SAMBA_UTIME_OMIT}.
1134 * See also: is_omit_timespec().
1136 struct timespec nt_time_to_full_timespec(NTTIME nt)
1138 struct timespec ret;
1140 if (nt == NTTIME_OMIT) {
1141 return make_omit_timespec();
1143 if (nt == NTTIME_FREEZE || nt == NTTIME_THAW) {
1145 * This should be returned as SAMBA_UTIME_FREEZE or
1146 * SAMBA_UTIME_THAW in the future.
1148 return make_omit_timespec();
1150 if (nt > NTTIME_MAX) {
1151 nt = NTTIME_MAX;
1154 ret = nt_time_to_unix_timespec_raw(nt);
1156 if (ret.tv_sec >= TIME_T_MAX) {
1157 ret.tv_sec = TIME_T_MAX;
1158 ret.tv_nsec = 0;
1159 return ret;
1162 return ret;
1166 * Note: this function uses the full time_t range as valid date values including
1167 * (time_t)0 and -1. That means that struct timespec sentinel values (cf
1168 * is_omit_timespec()) can't be converted to sentinel values in a time_t
1169 * representation. Callers should therefore check the NTTIME value with
1170 * null_nttime() before calling this function.
1172 time_t full_timespec_to_time_t(const struct timespec *_ts)
1174 struct timespec ts = *_ts;
1176 if (is_omit_timespec(_ts)) {
1178 * Unfortunately there's no sensible sentinel value in the
1179 * time_t range that is not conflicting with a valid time value
1180 * ((time_t)0 and -1 are valid time values). Bite the bullit and
1181 * return 0.
1183 return 0;
1186 /* Ensure tv_nsec is less than 1sec. */
1187 while (ts.tv_nsec > 1000000000) {
1188 ts.tv_sec += 1;
1189 ts.tv_nsec -= 1000000000;
1192 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
1193 increment if it's greater than 500 millionth of a second. */
1195 if (ts.tv_nsec > 500000000) {
1196 return ts.tv_sec + 1;
1198 return ts.tv_sec;
1202 * Like nt_time_to_unix() but supports negative time_t values.
1204 * Note: this function uses the full time_t range as valid date values including
1205 * (time_t)0 and -1. That means that NTTIME sentinel values of 0 and -1 which
1206 * represent a "not-set" value, can't be converted to sentinel values in a
1207 * time_t representation. Callers should therefore check the NTTIME value with
1208 * null_nttime() before calling this function.
1210 time_t nt_time_to_full_time_t(NTTIME nt)
1212 struct timespec ts;
1214 ts = nt_time_to_full_timespec(nt);
1215 return full_timespec_to_time_t(&ts);
1219 * Like time_t_to_unix_timespec() but supports negative time_t values.
1221 * This version converts (time_t)0 and -1 to an is_omit_timespec(), so 0 and -1
1222 * can't be used as valid date values. The function supports values < -1 though.
1224 struct timespec time_t_to_full_timespec(time_t t)
1226 if (null_time(t)) {
1227 return (struct timespec){.tv_nsec = SAMBA_UTIME_OMIT};
1229 return (struct timespec){.tv_sec = t};
1232 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
1234 /* Old system - no ns timestamp. */
1235 time_t get_atimensec(const struct stat *st)
1237 return 0;
1240 time_t get_mtimensec(const struct stat *st)
1242 return 0;
1245 time_t get_ctimensec(const struct stat *st)
1247 return 0;
1250 /* Set does nothing with no ns timestamp. */
1251 void set_atimensec(struct stat *st, time_t ns)
1253 return;
1256 void set_mtimensec(struct stat *st, time_t ns)
1258 return;
1261 void set_ctimensec(struct stat *st, time_t ns)
1263 return;
1266 #elif HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1268 time_t get_atimensec(const struct stat *st)
1270 return st->st_atimespec.tv_nsec;
1273 time_t get_mtimensec(const struct stat *st)
1275 return st->st_mtimespec.tv_nsec;
1278 time_t get_ctimensec(const struct stat *st)
1280 return st->st_ctimespec.tv_nsec;
1283 void set_atimensec(struct stat *st, time_t ns)
1285 st->st_atimespec.tv_nsec = ns;
1288 void set_mtimensec(struct stat *st, time_t ns)
1290 st->st_mtimespec.tv_nsec = ns;
1293 void set_ctimensec(struct stat *st, time_t ns)
1295 st->st_ctimespec.tv_nsec = ns;
1298 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1300 time_t get_atimensec(const struct stat *st)
1302 return st->st_atim.tv_nsec;
1305 time_t get_mtimensec(const struct stat *st)
1307 return st->st_mtim.tv_nsec;
1310 time_t get_ctimensec(const struct stat *st)
1312 return st->st_ctim.tv_nsec;
1315 void set_atimensec(struct stat *st, time_t ns)
1317 st->st_atim.tv_nsec = ns;
1320 void set_mtimensec(struct stat *st, time_t ns)
1322 st->st_mtim.tv_nsec = ns;
1324 void set_ctimensec(struct stat *st, time_t ns)
1326 st->st_ctim.tv_nsec = ns;
1329 #elif HAVE_STRUCT_STAT_ST_MTIMENSEC
1331 time_t get_atimensec(const struct stat *st)
1333 return st->st_atimensec;
1336 time_t get_mtimensec(const struct stat *st)
1338 return st->st_mtimensec;
1341 time_t get_ctimensec(const struct stat *st)
1343 return st->st_ctimensec;
1346 void set_atimensec(struct stat *st, time_t ns)
1348 st->st_atimensec = ns;
1351 void set_mtimensec(struct stat *st, time_t ns)
1353 st->st_mtimensec = ns;
1356 void set_ctimensec(struct stat *st, time_t ns)
1358 st->st_ctimensec = ns;
1361 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1363 time_t get_atimensec(const struct stat *st)
1365 return st->st_atime_n;
1368 time_t get_mtimensec(const struct stat *st)
1370 return st->st_mtime_n;
1373 time_t get_ctimensec(const struct stat *st)
1375 return st->st_ctime_n;
1378 void set_atimensec(struct stat *st, time_t ns)
1380 st->st_atime_n = ns;
1383 void set_mtimensec(struct stat *st, time_t ns)
1385 st->st_mtime_n = ns;
1388 void set_ctimensec(struct stat *st, time_t ns)
1390 st->st_ctime_n = ns;
1393 #elif HAVE_STRUCT_STAT_ST_UMTIME
1395 /* Only usec timestamps available. Convert to/from nsec. */
1397 time_t get_atimensec(const struct stat *st)
1399 return st->st_uatime * 1000;
1402 time_t get_mtimensec(const struct stat *st)
1404 return st->st_umtime * 1000;
1407 time_t get_ctimensec(const struct stat *st)
1409 return st->st_uctime * 1000;
1412 void set_atimensec(struct stat *st, time_t ns)
1414 st->st_uatime = ns / 1000;
1417 void set_mtimensec(struct stat *st, time_t ns)
1419 st->st_umtime = ns / 1000;
1422 void set_ctimensec(struct stat *st, time_t ns)
1424 st->st_uctime = ns / 1000;
1427 #else
1428 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
1429 #endif
1431 struct timespec get_atimespec(const struct stat *pst)
1433 struct timespec ret;
1435 ret.tv_sec = pst->st_atime;
1436 ret.tv_nsec = get_atimensec(pst);
1437 return ret;
1440 struct timespec get_mtimespec(const struct stat *pst)
1442 struct timespec ret;
1444 ret.tv_sec = pst->st_mtime;
1445 ret.tv_nsec = get_mtimensec(pst);
1446 return ret;
1449 struct timespec get_ctimespec(const struct stat *pst)
1451 struct timespec ret;
1453 ret.tv_sec = pst->st_mtime;
1454 ret.tv_nsec = get_ctimensec(pst);
1455 return ret;
1458 /****************************************************************************
1459 Deal with nanoseconds overflow.
1460 ****************************************************************************/
1462 void normalize_timespec(struct timespec *ts)
1464 lldiv_t dres;
1466 /* most likely case: nsec is valid */
1467 if ((unsigned long)ts->tv_nsec < NSEC_PER_SEC) {
1468 return;
1471 dres = lldiv(ts->tv_nsec, NSEC_PER_SEC);
1473 /* if the operation would result in overflow, max out values and bail */
1474 if (dres.quot > 0) {
1475 if ((int64_t)LONG_MAX - dres.quot < ts->tv_sec) {
1476 ts->tv_sec = LONG_MAX;
1477 ts->tv_nsec = NSEC_PER_SEC - 1;
1478 return;
1480 } else {
1481 if ((int64_t)LONG_MIN - dres.quot > ts->tv_sec) {
1482 ts->tv_sec = LONG_MIN;
1483 ts->tv_nsec = 0;
1484 return;
1488 ts->tv_nsec = dres.rem;
1489 ts->tv_sec += dres.quot;
1491 /* if the ns part was positive or a multiple of -1000000000, we're done */
1492 if (ts->tv_nsec > 0 || dres.rem == 0) {
1493 return;
1496 ts->tv_nsec += NSEC_PER_SEC;
1497 --ts->tv_sec;