WHATSNEW.txt: document "veto files" and "hide files"
[Samba.git] / lib / util / time.c
blob7ed8fb00447c8252f252f98cfcf46030be34d123
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 #define TIME_FIXUP_CONSTANT_INT INT64_C(11644473600)
42 #define NSEC_PER_SEC 1000000000
44 /**
45 External access to time_t_min and time_t_max.
46 **/
47 _PUBLIC_ time_t get_time_t_max(void)
49 return TIME_T_MAX;
52 /**
53 a wrapper to preferably get the monotonic time
54 **/
55 _PUBLIC_ void clock_gettime_mono(struct timespec *tp)
57 /* prefer a suspend aware monotonic CLOCK_BOOTTIME: */
58 #ifdef CLOCK_BOOTTIME
59 if (clock_gettime(CLOCK_BOOTTIME,tp) == 0) {
60 return;
62 #endif
63 /* then try the monotonic clock: */
64 #ifndef CUSTOM_CLOCK_MONOTONIC_IS_REALTIME
65 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC,tp) == 0) {
66 return;
68 #endif
69 clock_gettime(CLOCK_REALTIME,tp);
72 /**
73 a wrapper to preferably get the monotonic time in seconds
74 **/
75 _PUBLIC_ time_t time_mono(time_t *t)
77 struct timespec tp;
79 clock_gettime_mono(&tp);
80 if (t != NULL) {
81 *t = tp.tv_sec;
83 return tp.tv_sec;
87 #define TIME_FIXUP_CONSTANT 11644473600LL
89 time_t convert_timespec_to_time_t(struct timespec ts)
91 /* Ensure tv_nsec is less than 1sec. */
92 normalize_timespec(&ts);
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) {
98 return ts.tv_sec + 1;
100 return ts.tv_sec;
103 struct timespec convert_time_t_to_timespec(time_t t)
105 struct timespec ts = {.tv_sec = t};
106 return ts;
112 Interpret an 8 byte "filetime" structure to a time_t
113 It's originally in "100ns units since jan 1st 1601"
115 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
117 tv_sec = 0
118 tv_nsec = 0;
120 Returns GMT.
122 time_t nt_time_to_unix(NTTIME nt)
124 return convert_timespec_to_time_t(nt_time_to_unix_timespec(nt));
129 put a 8 byte filetime from a time_t
130 This takes GMT as input
132 _PUBLIC_ void unix_to_nt_time(NTTIME *nt, time_t t)
134 uint64_t t2;
136 if (t == (time_t)-1) {
137 *nt = UINT64_MAX;
138 return;
141 if (t == TIME_T_MAX || t == INT64_MAX) {
142 *nt = 0x7fffffffffffffffLL;
143 return;
146 if (t == 0) {
147 *nt = 0;
148 return;
151 t2 = t;
152 t2 += TIME_FIXUP_CONSTANT_INT;
153 t2 *= 1000*1000*10;
155 *nt = t2;
160 check if it's a null unix time
162 _PUBLIC_ bool null_time(time_t t)
164 return t == 0 ||
165 t == (time_t)0xFFFFFFFF ||
166 t == (time_t)-1;
171 check if it's a null NTTIME
173 _PUBLIC_ bool null_nttime(NTTIME t)
175 return t == 0;
178 /*******************************************************************
179 create a 16 bit dos packed date
180 ********************************************************************/
181 static uint16_t make_dos_date1(struct tm *t)
183 uint16_t ret=0;
184 ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
185 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
186 return ret;
189 /*******************************************************************
190 create a 16 bit dos packed time
191 ********************************************************************/
192 static uint16_t make_dos_time1(struct tm *t)
194 uint16_t ret=0;
195 ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));
196 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
197 return ret;
200 /*******************************************************************
201 create a 32 bit dos packed date/time from some parameters
202 This takes a GMT time and returns a packed localtime structure
203 ********************************************************************/
204 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
206 struct tm *t;
207 uint32_t ret=0;
209 if (unixdate == 0) {
210 return 0;
213 unixdate -= zone_offset;
215 t = gmtime(&unixdate);
216 if (!t) {
217 return 0xFFFFFFFF;
220 ret = make_dos_date1(t);
221 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
223 return ret;
227 put a dos date into a buffer (time/date format)
228 This takes GMT time and puts local time in the buffer
230 _PUBLIC_ void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
232 uint32_t x = make_dos_date(unixdate, zone_offset);
233 SIVAL(buf,offset,x);
237 put a dos date into a buffer (date/time format)
238 This takes GMT time and puts local time in the buffer
240 _PUBLIC_ void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
242 uint32_t x;
243 x = make_dos_date(unixdate, zone_offset);
244 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
245 SIVAL(buf,offset,x);
249 put a dos 32 bit "unix like" date into a buffer. This routine takes
250 GMT and converts it to LOCAL time before putting it (most SMBs assume
251 localtime for this sort of date)
253 _PUBLIC_ void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
255 if (!null_time(unixdate)) {
256 unixdate -= zone_offset;
258 SIVAL(buf,offset,unixdate);
261 /*******************************************************************
262 interpret a 32 bit dos packed date/time to some parameters
263 ********************************************************************/
264 void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
266 uint32_t p0,p1,p2,p3;
268 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
269 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
271 *second = 2*(p0 & 0x1F);
272 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
273 *hour = (p1>>3)&0xFF;
274 *day = (p2&0x1F);
275 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
276 *year = ((p3>>1)&0xFF) + 80;
280 create a unix date (int GMT) from a dos date (which is actually in
281 localtime)
283 _PUBLIC_ time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
285 uint32_t dos_date=0;
286 struct tm t;
287 time_t ret;
289 dos_date = IVAL(date_ptr,0);
291 if (dos_date == 0) return (time_t)0;
293 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
294 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
295 t.tm_isdst = -1;
297 ret = timegm(&t);
299 ret += zone_offset;
301 return ret;
305 like make_unix_date() but the words are reversed
307 _PUBLIC_ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
309 uint32_t x,x2;
311 x = IVAL(date_ptr,0);
312 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
313 SIVAL(&x,0,x2);
315 return pull_dos_date((const uint8_t *)&x, zone_offset);
319 create a unix GMT date from a dos date in 32 bit "unix like" format
320 these generally arrive as localtimes, with corresponding DST
322 _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
324 time_t t = (time_t)IVAL(date_ptr,0);
326 if (t == (time_t)0xFFFFFFFF) {
327 t = (time_t)-1;
330 if (!null_time(t)) {
331 t += zone_offset;
333 return t;
336 /****************************************************************************
337 Return the date and time as a string
338 ****************************************************************************/
340 char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
342 struct timeval_buf tmp;
343 char *result;
345 result = talloc_strdup(ctx, timeval_str_buf(tp, false, hires, &tmp));
346 if (result == NULL) {
347 return NULL;
350 /* beautify the talloc_report output */
351 talloc_set_name_const(result, result);
352 return result;
355 /****************************************************************************
356 Return the date and time as a string
357 ****************************************************************************/
359 const char *timespec_string_buf(const struct timespec *tp,
360 bool hires,
361 struct timeval_buf *buf)
363 time_t t;
364 struct tm *tm = NULL;
365 int len;
367 if (is_omit_timespec(tp)) {
368 strlcpy(buf->buf, "SAMBA_UTIME_OMIT", sizeof(buf->buf));
369 return buf->buf;
372 t = (time_t)tp->tv_sec;
373 tm = localtime(&t);
375 if (tm == NULL) {
376 if (hires) {
377 len = snprintf(buf->buf, sizeof(buf->buf),
378 "%ld.%09ld seconds since the Epoch",
379 (long)tp->tv_sec, (long)tp->tv_nsec);
380 } else {
381 len = snprintf(buf->buf, sizeof(buf->buf),
382 "%ld seconds since the Epoch", (long)t);
384 } else if (!hires) {
385 len = snprintf(buf->buf, sizeof(buf->buf),
386 "%04d-%02d-%02d %02d:%02d:%02d",
387 1900 + tm->tm_year,
388 tm->tm_mon + 1,
389 tm->tm_mday,
390 tm->tm_hour,
391 tm->tm_min,
392 tm->tm_sec);
393 } else {
394 len = snprintf(buf->buf, sizeof(buf->buf),
395 "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
396 1900 + tm->tm_year,
397 tm->tm_mon + 1,
398 tm->tm_mday,
399 tm->tm_hour,
400 tm->tm_min,
401 tm->tm_sec,
402 (long)tp->tv_nsec);
404 if (len == -1) {
405 return "";
408 return buf->buf;
411 char *current_timestring(TALLOC_CTX *ctx, bool hires)
413 struct timeval tv;
415 GetTimeOfDay(&tv);
416 return timeval_string(ctx, &tv, hires);
420 * Return date and time as a minimal string avoiding funny characters
421 * that may cause trouble in file names. We only use digits and
422 * underscore ... or a minus/hyphen if we got negative time.
424 char *minimal_timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
426 time_t t;
427 struct tm *tm;
429 t = (time_t)tp->tv_sec;
430 tm = localtime(&t);
431 if (!tm) {
432 if (hires) {
433 return talloc_asprintf(ctx, "%ld_%06ld",
434 (long)tp->tv_sec,
435 (long)tp->tv_usec);
436 } else {
437 return talloc_asprintf(ctx, "%ld", (long)t);
439 } else {
440 if (hires) {
441 return talloc_asprintf(ctx,
442 "%04d%02d%02d_%02d%02d%02d_%06ld",
443 tm->tm_year+1900,
444 tm->tm_mon+1,
445 tm->tm_mday,
446 tm->tm_hour,
447 tm->tm_min,
448 tm->tm_sec,
449 (long)tp->tv_usec);
450 } else {
451 return talloc_asprintf(ctx,
452 "%04d%02d%02d_%02d%02d%02d",
453 tm->tm_year+1900,
454 tm->tm_mon+1,
455 tm->tm_mday,
456 tm->tm_hour,
457 tm->tm_min,
458 tm->tm_sec);
463 char *current_minimal_timestring(TALLOC_CTX *ctx, bool hires)
465 struct timeval tv;
467 GetTimeOfDay(&tv);
468 return minimal_timeval_string(ctx, &tv, hires);
472 return a HTTP/1.0 time string
474 _PUBLIC_ char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
476 char *buf;
477 char tempTime[60];
478 struct tm *tm = localtime(&t);
480 if (t == TIME_T_MAX) {
481 return talloc_strdup(mem_ctx, "never");
484 if (!tm) {
485 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
488 #ifndef HAVE_STRFTIME
489 buf = talloc_strdup(mem_ctx, asctime(tm));
490 if (buf[strlen(buf)-1] == '\n') {
491 buf[strlen(buf)-1] = 0;
493 #else
494 strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
495 buf = talloc_strdup(mem_ctx, tempTime);
496 #endif /* !HAVE_STRFTIME */
498 return buf;
502 Return the date and time as a string
504 _PUBLIC_ char *timestring(TALLOC_CTX *mem_ctx, time_t t)
506 char *TimeBuf;
507 char tempTime[80];
508 struct tm *tm;
510 tm = localtime(&t);
511 if (!tm) {
512 return talloc_asprintf(mem_ctx,
513 "%ld seconds since the Epoch",
514 (long)t);
517 #ifdef HAVE_STRFTIME
518 /* Some versions of gcc complain about using some special format
519 * specifiers. This is a bug in gcc, not a bug in this code. See a
520 * recent strftime() manual page for details. */
521 strftime(tempTime,sizeof(tempTime)-1,"%a %b %e %X %Y %Z",tm);
522 TimeBuf = talloc_strdup(mem_ctx, tempTime);
523 #else
524 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
525 if (TimeBuf == NULL) {
526 return NULL;
528 if (TimeBuf[0] != '\0') {
529 size_t len = strlen(TimeBuf);
530 if (TimeBuf[len - 1] == '\n') {
531 TimeBuf[len - 1] = '\0';
534 #endif
536 return TimeBuf;
540 return a talloced string representing a NTTIME for human consumption
542 _PUBLIC_ const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
544 time_t t;
545 if (nt == 0) {
546 return "NTTIME(0)";
548 t = nt_time_to_full_time_t(nt);
549 return timestring(mem_ctx, t);
554 put a NTTIME into a packet
556 _PUBLIC_ void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
558 SBVAL(base, offset, t);
562 pull a NTTIME from a packet
564 _PUBLIC_ NTTIME pull_nttime(uint8_t *base, uint16_t offset)
566 NTTIME ret = BVAL(base, offset);
567 return ret;
571 return (tv1 - tv2) in microseconds
573 _PUBLIC_ int64_t usec_time_diff(const struct timeval *tv1, const struct timeval *tv2)
575 int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
576 return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
580 return (tp1 - tp2) in nanoseconds
582 _PUBLIC_ int64_t nsec_time_diff(const struct timespec *tp1, const struct timespec *tp2)
584 int64_t sec_diff = tp1->tv_sec - tp2->tv_sec;
585 return (sec_diff * 1000000000) + (int64_t)(tp1->tv_nsec - tp2->tv_nsec);
590 return a zero timeval
592 _PUBLIC_ struct timeval timeval_zero(void)
594 struct timeval tv;
595 tv.tv_sec = 0;
596 tv.tv_usec = 0;
597 return tv;
601 return true if a timeval is zero
603 _PUBLIC_ bool timeval_is_zero(const struct timeval *tv)
605 return tv->tv_sec == 0 && tv->tv_usec == 0;
609 return a timeval for the current time
611 _PUBLIC_ struct timeval timeval_current(void)
613 struct timeval tv;
614 GetTimeOfDay(&tv);
615 return tv;
619 return a timeval ofs microseconds after tv
621 _PUBLIC_ struct timeval timeval_add(const struct timeval *tv,
622 uint32_t secs, uint32_t usecs)
624 struct timeval tv2 = *tv;
625 const unsigned int million = 1000000;
626 tv2.tv_sec += secs;
627 tv2.tv_usec += usecs;
628 tv2.tv_sec += tv2.tv_usec / million;
629 tv2.tv_usec = tv2.tv_usec % million;
630 return tv2;
634 return the sum of two timeval structures
636 struct timeval timeval_sum(const struct timeval *tv1,
637 const struct timeval *tv2)
639 return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
643 return a timeval secs/usecs into the future
645 _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
647 struct timeval tv = timeval_current();
648 return timeval_add(&tv, secs, usecs);
652 return a timeval milliseconds into the future
654 _PUBLIC_ struct timeval timeval_current_ofs_msec(uint32_t msecs)
656 struct timeval tv = timeval_current();
657 return timeval_add(&tv, msecs / 1000, (msecs % 1000) * 1000);
661 return a timeval microseconds into the future
663 _PUBLIC_ struct timeval timeval_current_ofs_usec(uint32_t usecs)
665 struct timeval tv = timeval_current();
666 return timeval_add(&tv, usecs / 1000000, usecs % 1000000);
670 compare two timeval structures.
671 Return -1 if tv1 < tv2
672 Return 0 if tv1 == tv2
673 Return 1 if tv1 > tv2
675 _PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
677 if (tv1->tv_sec > tv2->tv_sec) return 1;
678 if (tv1->tv_sec < tv2->tv_sec) return -1;
679 if (tv1->tv_usec > tv2->tv_usec) return 1;
680 if (tv1->tv_usec < tv2->tv_usec) return -1;
681 return 0;
685 return true if a timer is in the past
687 _PUBLIC_ bool timeval_expired(const struct timeval *tv)
689 struct timeval tv2 = timeval_current();
690 if (tv2.tv_sec > tv->tv_sec) return true;
691 if (tv2.tv_sec < tv->tv_sec) return false;
692 return (tv2.tv_usec >= tv->tv_usec);
696 return the number of seconds elapsed between two times
698 _PUBLIC_ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
700 return (tv2->tv_sec - tv1->tv_sec) +
701 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
705 return the number of seconds elapsed since a given time
707 _PUBLIC_ double timeval_elapsed(const struct timeval *tv)
709 struct timeval tv2 = timeval_current();
710 return timeval_elapsed2(tv, &tv2);
713 * return the number of seconds elapsed between two times
715 _PUBLIC_ double timespec_elapsed2(const struct timespec *ts1,
716 const struct timespec *ts2)
718 return (ts2->tv_sec - ts1->tv_sec) +
719 (ts2->tv_nsec - ts1->tv_nsec)*1.0e-9;
723 * return the number of seconds elapsed since a given time
725 _PUBLIC_ double timespec_elapsed(const struct timespec *ts)
727 struct timespec ts2 = timespec_current();
728 return timespec_elapsed2(ts, &ts2);
732 return the lesser of two timevals
734 _PUBLIC_ struct timeval timeval_min(const struct timeval *tv1,
735 const struct timeval *tv2)
737 if (tv1->tv_sec < tv2->tv_sec) return *tv1;
738 if (tv1->tv_sec > tv2->tv_sec) return *tv2;
739 if (tv1->tv_usec < tv2->tv_usec) return *tv1;
740 return *tv2;
744 return the greater of two timevals
746 _PUBLIC_ struct timeval timeval_max(const struct timeval *tv1,
747 const struct timeval *tv2)
749 if (tv1->tv_sec > tv2->tv_sec) return *tv1;
750 if (tv1->tv_sec < tv2->tv_sec) return *tv2;
751 if (tv1->tv_usec > tv2->tv_usec) return *tv1;
752 return *tv2;
756 convert a timeval to a NTTIME
758 _PUBLIC_ NTTIME timeval_to_nttime(const struct timeval *tv)
760 return 10*(tv->tv_usec +
761 ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
765 convert a NTTIME to a timeval
767 _PUBLIC_ void nttime_to_timeval(struct timeval *tv, NTTIME t)
769 if (tv == NULL) return;
771 t += 10/2;
772 t /= 10;
773 t -= TIME_FIXUP_CONSTANT*1000*1000;
775 tv->tv_sec = t / 1000000;
777 if (TIME_T_MIN > tv->tv_sec || tv->tv_sec > TIME_T_MAX) {
778 tv->tv_sec = 0;
779 tv->tv_usec = 0;
780 return;
783 tv->tv_usec = t - tv->tv_sec*1000000;
786 /*******************************************************************
787 yield the difference between *A and *B, in seconds, ignoring leap seconds
788 ********************************************************************/
789 static int tm_diff(struct tm *a, struct tm *b)
791 int ay = a->tm_year + (1900 - 1);
792 int by = b->tm_year + (1900 - 1);
793 int intervening_leap_days =
794 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
795 int years = ay - by;
796 int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
797 int hours = 24*days + (a->tm_hour - b->tm_hour);
798 int minutes = 60*hours + (a->tm_min - b->tm_min);
799 int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
801 return seconds;
806 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
808 _PUBLIC_ int get_time_zone(time_t t)
810 struct tm *tm = gmtime(&t);
811 struct tm tm_utc;
812 if (!tm)
813 return 0;
814 tm_utc = *tm;
815 tm = localtime(&t);
816 if (!tm)
817 return 0;
818 return tm_diff(&tm_utc,tm);
822 * Raw convert an NTTIME to a unix timespec.
825 struct timespec nt_time_to_unix_timespec_raw(
826 NTTIME nt)
828 int64_t d;
829 struct timespec ret;
831 d = (int64_t)nt;
832 /* d is now in 100ns units, since jan 1st 1601".
833 Save off the ns fraction. */
836 * Take the last seven decimal digits and multiply by 100.
837 * to convert from 100ns units to 1ns units.
839 ret.tv_nsec = (long) ((d % (1000 * 1000 * 10)) * 100);
841 /* Convert to seconds */
842 d /= 1000*1000*10;
844 /* Now adjust by 369 years to make the secs since 1970 */
845 d -= TIME_FIXUP_CONSTANT_INT;
847 ret.tv_sec = (time_t)d;
848 return ret;
851 struct timespec nt_time_to_unix_timespec(NTTIME nt)
853 struct timespec ret;
855 if (nt == 0 || nt == UINT64_MAX) {
856 ret.tv_sec = 0;
857 ret.tv_nsec = 0;
858 return ret;
861 ret = nt_time_to_unix_timespec_raw(nt);
863 if (ret.tv_sec <= TIME_T_MIN) {
864 ret.tv_sec = TIME_T_MIN;
865 ret.tv_nsec = 0;
866 return ret;
869 if (ret.tv_sec >= TIME_T_MAX) {
870 ret.tv_sec = TIME_T_MAX;
871 ret.tv_nsec = 0;
872 return ret;
874 return ret;
879 check if 2 NTTIMEs are equal.
881 bool nt_time_equal(NTTIME *t1, NTTIME *t2)
883 return *t1 == *t2;
887 Check if it's a null timespec.
890 bool null_timespec(struct timespec ts)
892 return ts.tv_sec == 0 ||
893 ts.tv_sec == (time_t)0xFFFFFFFF ||
894 ts.tv_sec == (time_t)-1;
897 /****************************************************************************
898 Convert a normalized timeval to a timespec.
899 ****************************************************************************/
901 struct timespec convert_timeval_to_timespec(const struct timeval tv)
903 struct timespec ts;
904 ts.tv_sec = tv.tv_sec;
905 ts.tv_nsec = tv.tv_usec * 1000;
906 return ts;
909 /****************************************************************************
910 Convert a normalized timespec to a timeval.
911 ****************************************************************************/
913 struct timeval convert_timespec_to_timeval(const struct timespec ts)
915 struct timeval tv;
916 tv.tv_sec = ts.tv_sec;
917 tv.tv_usec = ts.tv_nsec / 1000;
918 return tv;
921 /****************************************************************************
922 Return a timespec for the current time
923 ****************************************************************************/
925 _PUBLIC_ struct timespec timespec_current(void)
927 struct timespec ts;
928 clock_gettime(CLOCK_REALTIME, &ts);
929 return ts;
932 /****************************************************************************
933 Return the lesser of two timespecs.
934 ****************************************************************************/
936 struct timespec timespec_min(const struct timespec *ts1,
937 const struct timespec *ts2)
939 if (ts1->tv_sec < ts2->tv_sec) return *ts1;
940 if (ts1->tv_sec > ts2->tv_sec) return *ts2;
941 if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
942 return *ts2;
945 /****************************************************************************
946 compare two timespec structures.
947 Return -1 if ts1 < ts2
948 Return 0 if ts1 == ts2
949 Return 1 if ts1 > ts2
950 ****************************************************************************/
952 _PUBLIC_ int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
954 if (ts1->tv_sec > ts2->tv_sec) return 1;
955 if (ts1->tv_sec < ts2->tv_sec) return -1;
956 if (ts1->tv_nsec > ts2->tv_nsec) return 1;
957 if (ts1->tv_nsec < ts2->tv_nsec) return -1;
958 return 0;
961 /****************************************************************************
962 Round up a timespec if nsec > 500000000, round down if lower,
963 then zero nsec.
964 ****************************************************************************/
966 void round_timespec_to_sec(struct timespec *ts)
968 ts->tv_sec = convert_timespec_to_time_t(*ts);
969 ts->tv_nsec = 0;
972 /****************************************************************************
973 Round a timespec to usec value.
974 ****************************************************************************/
976 void round_timespec_to_usec(struct timespec *ts)
978 struct timeval tv = convert_timespec_to_timeval(*ts);
979 *ts = convert_timeval_to_timespec(tv);
980 normalize_timespec(ts);
983 /****************************************************************************
984 Round a timespec to NTTIME resolution.
985 ****************************************************************************/
987 void round_timespec_to_nttime(struct timespec *ts)
989 ts->tv_nsec = (ts->tv_nsec / 100) * 100;
992 /****************************************************************************
993 Put a 8 byte filetime from a struct timespec. Uses GMT.
994 ****************************************************************************/
996 _PUBLIC_ NTTIME unix_timespec_to_nt_time(struct timespec ts)
998 uint64_t d;
1000 if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
1001 return 0;
1003 if (ts.tv_sec == TIME_T_MAX) {
1004 return 0x7fffffffffffffffLL;
1006 if (ts.tv_sec == (time_t)-1) {
1007 return UINT64_MAX;
1010 d = ts.tv_sec;
1011 d += TIME_FIXUP_CONSTANT_INT;
1012 d *= 1000*1000*10;
1013 /* d is now in 100ns units. */
1014 d += (ts.tv_nsec / 100);
1016 return d;
1020 * Functions supporting the full range of time_t and struct timespec values,
1021 * including 0, -1 and all other negative values. These functions don't use 0 or
1022 * -1 values as sentinel to denote "unset" variables, but use the POSIX 2008
1023 * define UTIME_OMIT from utimensat(2).
1027 * Check if it's a to be omitted timespec.
1029 bool is_omit_timespec(const struct timespec *ts)
1031 return ts->tv_nsec == SAMBA_UTIME_OMIT;
1035 * Return a to be omitted timespec.
1037 struct timespec make_omit_timespec(void)
1039 return (struct timespec){.tv_nsec = SAMBA_UTIME_OMIT};
1043 * Like unix_timespec_to_nt_time() but without the special casing of tv_sec=0
1044 * and -1. Also dealing with SAMBA_UTIME_OMIT.
1046 NTTIME full_timespec_to_nt_time(const struct timespec *_ts)
1048 struct timespec ts = *_ts;
1049 uint64_t d;
1051 if (is_omit_timespec(_ts)) {
1052 return NTTIME_OMIT;
1055 /* Ensure tv_nsec is less than 1 sec. */
1056 while (ts.tv_nsec > 1000000000) {
1057 if (ts.tv_sec > TIME_T_MAX) {
1058 return NTTIME_MAX;
1060 ts.tv_sec += 1;
1061 ts.tv_nsec -= 1000000000;
1064 if (ts.tv_sec >= TIME_T_MAX) {
1065 return NTTIME_MAX;
1067 if ((ts.tv_sec + TIME_FIXUP_CONSTANT_INT) <= 0) {
1068 return NTTIME_MIN;
1071 d = TIME_FIXUP_CONSTANT_INT;
1072 d += ts.tv_sec;
1074 d *= 1000*1000*10;
1075 /* d is now in 100ns units. */
1076 d += (ts.tv_nsec / 100);
1078 return d;
1082 * Like nt_time_to_unix_timespec() but allowing negative tv_sec values and
1083 * returning NTTIME=0 and -1 as struct timespec {.tv_nsec = SAMBA_UTIME_OMIT}.
1085 * See also: is_omit_timespec().
1087 struct timespec nt_time_to_full_timespec(NTTIME nt)
1089 struct timespec ret;
1091 if (nt == NTTIME_OMIT) {
1092 return make_omit_timespec();
1094 if (nt == NTTIME_FREEZE || nt == NTTIME_THAW) {
1096 * This should be returned as SAMBA_UTIME_FREEZE or
1097 * SAMBA_UTIME_THAW in the future.
1099 return make_omit_timespec();
1101 if (nt > NTTIME_MAX) {
1102 nt = NTTIME_MAX;
1105 ret = nt_time_to_unix_timespec_raw(nt);
1107 if (ret.tv_sec >= TIME_T_MAX) {
1108 ret.tv_sec = TIME_T_MAX;
1109 ret.tv_nsec = 0;
1110 return ret;
1113 return ret;
1117 * Note: this function uses the full time_t range as valid date values including
1118 * (time_t)0 and -1. That means that struct timespec sentinel values (cf
1119 * is_omit_timespec()) can't be converted to sentinel values in a time_t
1120 * representation. Callers should therefore check the NTTIME value with
1121 * null_nttime() before calling this function.
1123 time_t full_timespec_to_time_t(const struct timespec *_ts)
1125 struct timespec ts = *_ts;
1127 if (is_omit_timespec(_ts)) {
1129 * Unfortunately there's no sensible sentinel value in the
1130 * time_t range that is not conflicting with a valid time value
1131 * ((time_t)0 and -1 are valid time values). Bite the bullit and
1132 * return 0.
1134 return 0;
1137 /* Ensure tv_nsec is less than 1sec. */
1138 while (ts.tv_nsec > 1000000000) {
1139 ts.tv_sec += 1;
1140 ts.tv_nsec -= 1000000000;
1143 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
1144 increment if it's greater than 500 millionth of a second. */
1146 if (ts.tv_nsec > 500000000) {
1147 return ts.tv_sec + 1;
1149 return ts.tv_sec;
1153 * Like nt_time_to_unix() but supports negative time_t values.
1155 * Note: this function uses the full time_t range as valid date values including
1156 * (time_t)0 and -1. That means that NTTIME sentinel values of 0 and -1 which
1157 * represent a "not-set" value, can't be converted to sentinel values in a
1158 * time_t representation. Callers should therefore check the NTTIME value with
1159 * null_nttime() before calling this function.
1161 time_t nt_time_to_full_time_t(NTTIME nt)
1163 struct timespec ts;
1165 ts = nt_time_to_full_timespec(nt);
1166 return full_timespec_to_time_t(&ts);
1170 * Like time_t_to_unix_timespec() but supports negative time_t values.
1172 * This version converts (time_t)0 and -1 to an is_omit_timespec(), so 0 and -1
1173 * can't be used as valid date values. The function supports values < -1 though.
1175 struct timespec time_t_to_full_timespec(time_t t)
1177 if (null_time(t)) {
1178 return (struct timespec){.tv_nsec = SAMBA_UTIME_OMIT};
1180 return (struct timespec){.tv_sec = t};
1183 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
1185 /* Old system - no ns timestamp. */
1186 time_t get_atimensec(const struct stat *st)
1188 return 0;
1191 time_t get_mtimensec(const struct stat *st)
1193 return 0;
1196 time_t get_ctimensec(const struct stat *st)
1198 return 0;
1201 /* Set does nothing with no ns timestamp. */
1202 void set_atimensec(struct stat *st, time_t ns)
1204 return;
1207 void set_mtimensec(struct stat *st, time_t ns)
1209 return;
1212 void set_ctimensec(struct stat *st, time_t ns)
1214 return;
1217 #elif HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1219 time_t get_atimensec(const struct stat *st)
1221 return st->st_atimespec.tv_nsec;
1224 time_t get_mtimensec(const struct stat *st)
1226 return st->st_mtimespec.tv_nsec;
1229 time_t get_ctimensec(const struct stat *st)
1231 return st->st_ctimespec.tv_nsec;
1234 void set_atimensec(struct stat *st, time_t ns)
1236 st->st_atimespec.tv_nsec = ns;
1239 void set_mtimensec(struct stat *st, time_t ns)
1241 st->st_mtimespec.tv_nsec = ns;
1244 void set_ctimensec(struct stat *st, time_t ns)
1246 st->st_ctimespec.tv_nsec = ns;
1249 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1251 time_t get_atimensec(const struct stat *st)
1253 return st->st_atim.tv_nsec;
1256 time_t get_mtimensec(const struct stat *st)
1258 return st->st_mtim.tv_nsec;
1261 time_t get_ctimensec(const struct stat *st)
1263 return st->st_ctim.tv_nsec;
1266 void set_atimensec(struct stat *st, time_t ns)
1268 st->st_atim.tv_nsec = ns;
1271 void set_mtimensec(struct stat *st, time_t ns)
1273 st->st_mtim.tv_nsec = ns;
1275 void set_ctimensec(struct stat *st, time_t ns)
1277 st->st_ctim.tv_nsec = ns;
1280 #elif HAVE_STRUCT_STAT_ST_MTIMENSEC
1282 time_t get_atimensec(const struct stat *st)
1284 return st->st_atimensec;
1287 time_t get_mtimensec(const struct stat *st)
1289 return st->st_mtimensec;
1292 time_t get_ctimensec(const struct stat *st)
1294 return st->st_ctimensec;
1297 void set_atimensec(struct stat *st, time_t ns)
1299 st->st_atimensec = ns;
1302 void set_mtimensec(struct stat *st, time_t ns)
1304 st->st_mtimensec = ns;
1307 void set_ctimensec(struct stat *st, time_t ns)
1309 st->st_ctimensec = ns;
1312 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1314 time_t get_atimensec(const struct stat *st)
1316 return st->st_atime_n;
1319 time_t get_mtimensec(const struct stat *st)
1321 return st->st_mtime_n;
1324 time_t get_ctimensec(const struct stat *st)
1326 return st->st_ctime_n;
1329 void set_atimensec(struct stat *st, time_t ns)
1331 st->st_atime_n = ns;
1334 void set_mtimensec(struct stat *st, time_t ns)
1336 st->st_mtime_n = ns;
1339 void set_ctimensec(struct stat *st, time_t ns)
1341 st->st_ctime_n = ns;
1344 #elif HAVE_STRUCT_STAT_ST_UMTIME
1346 /* Only usec timestamps available. Convert to/from nsec. */
1348 time_t get_atimensec(const struct stat *st)
1350 return st->st_uatime * 1000;
1353 time_t get_mtimensec(const struct stat *st)
1355 return st->st_umtime * 1000;
1358 time_t get_ctimensec(const struct stat *st)
1360 return st->st_uctime * 1000;
1363 void set_atimensec(struct stat *st, time_t ns)
1365 st->st_uatime = ns / 1000;
1368 void set_mtimensec(struct stat *st, time_t ns)
1370 st->st_umtime = ns / 1000;
1373 void set_ctimensec(struct stat *st, time_t ns)
1375 st->st_uctime = ns / 1000;
1378 #else
1379 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
1380 #endif
1382 struct timespec get_atimespec(const struct stat *pst)
1384 struct timespec ret;
1386 ret.tv_sec = pst->st_atime;
1387 ret.tv_nsec = get_atimensec(pst);
1388 return ret;
1391 struct timespec get_mtimespec(const struct stat *pst)
1393 struct timespec ret;
1395 ret.tv_sec = pst->st_mtime;
1396 ret.tv_nsec = get_mtimensec(pst);
1397 return ret;
1400 struct timespec get_ctimespec(const struct stat *pst)
1402 struct timespec ret;
1404 ret.tv_sec = pst->st_ctime;
1405 ret.tv_nsec = get_ctimensec(pst);
1406 return ret;
1409 /****************************************************************************
1410 Deal with nanoseconds overflow.
1411 ****************************************************************************/
1413 void normalize_timespec(struct timespec *ts)
1415 lldiv_t dres;
1417 /* most likely case: nsec is valid */
1418 if ((unsigned long)ts->tv_nsec < NSEC_PER_SEC) {
1419 return;
1422 dres = lldiv(ts->tv_nsec, NSEC_PER_SEC);
1424 /* if the operation would result in overflow, max out values and bail */
1425 if (dres.quot > 0) {
1426 if ((int64_t)LONG_MAX - dres.quot < ts->tv_sec) {
1427 ts->tv_sec = LONG_MAX;
1428 ts->tv_nsec = NSEC_PER_SEC - 1;
1429 return;
1431 } else {
1432 if ((int64_t)LONG_MIN - dres.quot > ts->tv_sec) {
1433 ts->tv_sec = LONG_MIN;
1434 ts->tv_nsec = 0;
1435 return;
1439 ts->tv_nsec = dres.rem;
1440 ts->tv_sec += dres.quot;
1442 /* if the ns part was positive or a multiple of -1000000000, we're done */
1443 if (ts->tv_nsec > 0 || dres.rem == 0) {
1444 return;
1447 ts->tv_nsec += NSEC_PER_SEC;
1448 --ts->tv_sec;