2 Unix SMB/CIFS implementation.
3 time handling functions
5 Copyright (C) Andrew Tridgell 1992-2004
6 Copyright (C) Stefan (metze) Metzmacher 2002
7 Copyright (C) Jeremy Allison 2007
8 Copyright (C) Andrew Bartlett 2011
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/time.h"
26 #include "byteorder.h"
27 #include "time_basic.h"
28 #include "lib/util/time.h" /* Avoid /usr/include/time.h */
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
46 #define NSEC_PER_SEC 1000000000
49 External access to time_t_min and time_t_max.
51 _PUBLIC_
time_t get_time_t_max(void)
57 a wrapper to preferably get the monotonic time
59 _PUBLIC_
void clock_gettime_mono(struct timespec
*tp
)
61 /* prefer a suspend aware monotonic CLOCK_BOOTTIME: */
63 if (clock_gettime(CLOCK_BOOTTIME
,tp
) == 0) {
67 /* then try the monotonic clock: */
68 #ifndef CUSTOM_CLOCK_MONOTONIC_IS_REALTIME
69 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC
,tp
) == 0) {
73 clock_gettime(CLOCK_REALTIME
,tp
);
77 a wrapper to preferably get the monotonic time in seconds
79 _PUBLIC_
time_t time_mono(time_t *t
)
83 clock_gettime_mono(&tp
);
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;
107 struct timespec
convert_time_t_to_timespec(time_t t
)
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
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
)
142 if (t
== (time_t)-1) {
147 if (t
== TIME_T_MAX
|| t
== INT64_MAX
) {
148 *nt
= 0x7fffffffffffffffLL
;
158 t2
+= TIME_FIXUP_CONSTANT_INT
;
166 check if it's a null unix time
168 _PUBLIC_
bool null_time(time_t t
)
171 t
== (time_t)0xFFFFFFFF ||
177 check if it's a null NTTIME
179 _PUBLIC_
bool null_nttime(NTTIME t
)
184 /*******************************************************************
185 create a 16 bit dos packed date
186 ********************************************************************/
187 static uint16_t make_dos_date1(struct tm
*t
)
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));
195 /*******************************************************************
196 create a 16 bit dos packed time
197 ********************************************************************/
198 static uint16_t make_dos_time1(struct tm
*t
)
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));
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
)
219 unixdate
-= zone_offset
;
221 t
= gmtime(&unixdate
);
226 ret
= make_dos_date1(t
);
227 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
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
);
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
)
249 x
= make_dos_date(unixdate
, zone_offset
);
250 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
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;
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
289 _PUBLIC_
time_t pull_dos_date(const uint8_t *date_ptr
, int zone_offset
)
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
);
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
)
317 x
= IVAL(date_ptr
,0);
318 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
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) {
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
;
351 result
= talloc_strdup(ctx
, timeval_str_buf(tp
, false, hires
, &tmp
));
352 if (result
== 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
);
369 /****************************************************************************
370 Return the date and time as a string
371 ****************************************************************************/
373 const char *timespec_string_buf(const struct timespec
*tp
,
375 struct timeval_buf
*buf
)
378 struct tm
*tm
= NULL
;
381 if (is_omit_timespec(tp
)) {
382 strlcpy(buf
->buf
, "SAMBA_UTIME_OMIT", sizeof(buf
->buf
));
386 t
= (time_t)tp
->tv_sec
;
391 len
= snprintf(buf
->buf
, sizeof(buf
->buf
),
392 "%ld.%09ld seconds since the Epoch",
393 (long)tp
->tv_sec
, (long)tp
->tv_nsec
);
395 len
= snprintf(buf
->buf
, sizeof(buf
->buf
),
396 "%ld seconds since the Epoch", (long)t
);
399 len
= snprintf(buf
->buf
, sizeof(buf
->buf
),
400 "%04d/%02d/%02d %02d:%02d:%02d",
408 len
= snprintf(buf
->buf
, sizeof(buf
->buf
),
409 "%04d/%02d/%02d %02d:%02d:%02d.%09ld",
425 char *current_timestring(TALLOC_CTX
*ctx
, bool hires
)
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
)
443 t
= (time_t)tp
->tv_sec
;
447 return talloc_asprintf(ctx
, "%ld_%06ld",
451 return talloc_asprintf(ctx
, "%ld", (long)t
);
455 return talloc_asprintf(ctx
,
456 "%04d%02d%02d_%02d%02d%02d_%06ld",
465 return talloc_asprintf(ctx
,
466 "%04d%02d%02d_%02d%02d%02d",
477 char *current_minimal_timestring(TALLOC_CTX
*ctx
, bool hires
)
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
)
492 struct tm
*tm
= localtime(&t
);
494 if (t
== TIME_T_MAX
) {
495 return talloc_strdup(mem_ctx
, "never");
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;
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 */
516 Return the date and time as a string
518 _PUBLIC_
char *timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
526 return talloc_asprintf(mem_ctx
,
527 "%ld seconds since the Epoch",
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
);
538 TimeBuf
= talloc_strdup(mem_ctx
, asctime(tm
));
539 if (TimeBuf
== NULL
) {
542 if (TimeBuf
[0] != '\0') {
543 size_t len
= strlen(TimeBuf
);
544 if (TimeBuf
[len
- 1] == '\n') {
545 TimeBuf
[len
- 1] = '\0';
554 return a talloced string representing a NTTIME for human consumption
556 _PUBLIC_
const char *nt_time_string(TALLOC_CTX
*mem_ctx
, NTTIME nt
)
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
);
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)
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)
633 return a timeval struct with the given elements
635 _PUBLIC_
struct timeval
timeval_set(uint32_t secs
, uint32_t usecs
)
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;
653 tv2
.tv_usec
+= usecs
;
654 tv2
.tv_sec
+= tv2
.tv_usec
/ million
;
655 tv2
.tv_usec
= tv2
.tv_usec
% million
;
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;
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
;
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
;
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
)
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
) {
796 t
.tv_usec
= 1000000 - (tv1
->tv_usec
- tv2
->tv_usec
);
798 t
.tv_usec
= tv2
->tv_usec
- tv1
->tv_usec
;
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;
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
) {
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);
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
);
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
);
867 return tm_diff(&tm_utc
,tm
);
870 struct timespec
nt_time_to_unix_timespec(NTTIME nt
)
875 if (nt
== 0 || nt
== (int64_t)-1) {
882 /* d is now in 100ns units, since jan 1st 1601".
883 Save off the ns fraction. */
886 * Take the last seven decimal digits and multiply by 100.
887 * to convert from 100ns units to 1ns units.
889 ret
.tv_nsec
= (long) ((d
% (1000 * 1000 * 10)) * 100);
891 /* Convert to seconds */
894 /* Now adjust by 369 years to make the secs since 1970 */
895 d
-= TIME_FIXUP_CONSTANT_INT
;
897 if (d
<= (int64_t)TIME_T_MIN
) {
898 ret
.tv_sec
= TIME_T_MIN
;
903 if (d
>= (int64_t)TIME_T_MAX
) {
904 ret
.tv_sec
= TIME_T_MAX
;
909 ret
.tv_sec
= (time_t)d
;
915 check if 2 NTTIMEs are equal.
917 bool nt_time_equal(NTTIME
*t1
, NTTIME
*t2
)
923 Check if it's a null timespec.
926 bool null_timespec(struct timespec ts
)
928 return ts
.tv_sec
== 0 ||
929 ts
.tv_sec
== (time_t)0xFFFFFFFF ||
930 ts
.tv_sec
== (time_t)-1;
933 /****************************************************************************
934 Convert a normalized timeval to a timespec.
935 ****************************************************************************/
937 struct timespec
convert_timeval_to_timespec(const struct timeval tv
)
940 ts
.tv_sec
= tv
.tv_sec
;
941 ts
.tv_nsec
= tv
.tv_usec
* 1000;
945 /****************************************************************************
946 Convert a normalized timespec to a timeval.
947 ****************************************************************************/
949 struct timeval
convert_timespec_to_timeval(const struct timespec ts
)
952 tv
.tv_sec
= ts
.tv_sec
;
953 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
957 /****************************************************************************
958 Return a timespec for the current time
959 ****************************************************************************/
961 _PUBLIC_
struct timespec
timespec_current(void)
964 clock_gettime(CLOCK_REALTIME
, &ts
);
968 /****************************************************************************
969 Return the lesser of two timespecs.
970 ****************************************************************************/
972 struct timespec
timespec_min(const struct timespec
*ts1
,
973 const struct timespec
*ts2
)
975 if (ts1
->tv_sec
< ts2
->tv_sec
) return *ts1
;
976 if (ts1
->tv_sec
> ts2
->tv_sec
) return *ts2
;
977 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return *ts1
;
981 /****************************************************************************
982 compare two timespec structures.
983 Return -1 if ts1 < ts2
984 Return 0 if ts1 == ts2
985 Return 1 if ts1 > ts2
986 ****************************************************************************/
988 _PUBLIC_
int timespec_compare(const struct timespec
*ts1
, const struct timespec
*ts2
)
990 if (ts1
->tv_sec
> ts2
->tv_sec
) return 1;
991 if (ts1
->tv_sec
< ts2
->tv_sec
) return -1;
992 if (ts1
->tv_nsec
> ts2
->tv_nsec
) return 1;
993 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return -1;
997 /****************************************************************************
998 Round up a timespec if nsec > 500000000, round down if lower,
1000 ****************************************************************************/
1002 void round_timespec_to_sec(struct timespec
*ts
)
1004 ts
->tv_sec
= convert_timespec_to_time_t(*ts
);
1008 /****************************************************************************
1009 Round a timespec to usec value.
1010 ****************************************************************************/
1012 void round_timespec_to_usec(struct timespec
*ts
)
1014 struct timeval tv
= convert_timespec_to_timeval(*ts
);
1015 *ts
= convert_timeval_to_timespec(tv
);
1016 normalize_timespec(ts
);
1019 /****************************************************************************
1020 Round a timespec to NTTIME resolution.
1021 ****************************************************************************/
1023 void round_timespec_to_nttime(struct timespec
*ts
)
1025 ts
->tv_nsec
= (ts
->tv_nsec
/ 100) * 100;
1028 /****************************************************************************
1029 Put a 8 byte filetime from a struct timespec. Uses GMT.
1030 ****************************************************************************/
1032 _PUBLIC_ NTTIME
unix_timespec_to_nt_time(struct timespec ts
)
1036 if (ts
.tv_sec
==0 && ts
.tv_nsec
== 0) {
1039 if (ts
.tv_sec
== TIME_T_MAX
) {
1040 return 0x7fffffffffffffffLL
;
1042 if (ts
.tv_sec
== (time_t)-1) {
1043 return (uint64_t)-1;
1047 d
+= TIME_FIXUP_CONSTANT_INT
;
1049 /* d is now in 100ns units. */
1050 d
+= (ts
.tv_nsec
/ 100);
1056 * Functions supporting the full range of time_t and struct timespec values,
1057 * including 0, -1 and all other negative values. These functions don't use 0 or
1058 * -1 values as sentinel to denote "unset" variables, but use the POSIX 2008
1059 * define UTIME_OMIT from utimensat(2).
1063 * Check if it's a to be omitted timespec.
1065 bool is_omit_timespec(const struct timespec
*ts
)
1067 return ts
->tv_nsec
== SAMBA_UTIME_OMIT
;
1071 * Return a to be omitted timespec.
1073 struct timespec
make_omit_timespec(void)
1075 return (struct timespec
){.tv_nsec
= SAMBA_UTIME_OMIT
};
1079 * Like unix_timespec_to_nt_time() but without the special casing of tv_sec=0
1080 * and -1. Also dealing with SAMBA_UTIME_OMIT.
1082 NTTIME
full_timespec_to_nt_time(const struct timespec
*_ts
)
1084 struct timespec ts
= *_ts
;
1087 if (is_omit_timespec(_ts
)) {
1091 /* Ensure tv_nsec is less than 1 sec. */
1092 while (ts
.tv_nsec
> 1000000000) {
1093 if (ts
.tv_sec
> TIME_T_MAX
) {
1097 ts
.tv_nsec
-= 1000000000;
1100 if (ts
.tv_sec
>= TIME_T_MAX
) {
1103 if ((ts
.tv_sec
+ TIME_FIXUP_CONSTANT_INT
) <= 0) {
1107 d
= TIME_FIXUP_CONSTANT_INT
;
1111 /* d is now in 100ns units. */
1112 d
+= (ts
.tv_nsec
/ 100);
1118 * Like nt_time_to_unix_timespec() but allowing negative tv_sec values and
1119 * returning NTTIME=0 and -1 as struct timespec {.tv_nsec = SAMBA_UTIME_OMIT}.
1121 * See also: is_omit_timespec().
1123 struct timespec
nt_time_to_full_timespec(NTTIME nt
)
1126 struct timespec ret
;
1128 if (nt
== NTTIME_OMIT
) {
1129 return make_omit_timespec();
1131 if (nt
== NTTIME_FREEZE
|| nt
== NTTIME_THAW
) {
1133 * This should be returned as SAMBA_UTIME_FREEZE or
1134 * SAMBA_UTIME_THAW in the future.
1136 return make_omit_timespec();
1138 if (nt
> NTTIME_MAX
) {
1143 /* d is now in 100ns units, since jan 1st 1601".
1144 Save off the ns fraction. */
1147 * Take the last seven decimal digits and multiply by 100.
1148 * to convert from 100ns units to 1ns units.
1150 ret
.tv_nsec
= (long) ((d
% (1000 * 1000 * 10)) * 100);
1152 /* Convert to seconds */
1155 /* Now adjust by 369 years to make the secs since 1970 */
1156 d
-= TIME_FIXUP_CONSTANT_INT
;
1158 if (d
>= (int64_t)TIME_T_MAX
) {
1159 ret
.tv_sec
= TIME_T_MAX
;
1164 ret
.tv_sec
= (time_t)d
;
1169 * Note: this function uses the full time_t range as valid date values including
1170 * (time_t)0 and -1. That means that struct timespec sentinel values (cf
1171 * is_omit_timespec()) can't be converted to sentinel values in a time_t
1172 * representation. Callers should therefor check the NTTIME value with
1173 * null_nttime() before calling this function.
1175 time_t full_timespec_to_time_t(const struct timespec
*_ts
)
1177 struct timespec ts
= *_ts
;
1179 if (is_omit_timespec(_ts
)) {
1181 * Unfortunately there's no sensible sentinel value in the
1182 * time_t range that is not conflicting with a valid time value
1183 * ((time_t)0 and -1 are valid time values). Bite the bullit and
1189 /* Ensure tv_nsec is less than 1sec. */
1190 while (ts
.tv_nsec
> 1000000000) {
1192 ts
.tv_nsec
-= 1000000000;
1195 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
1196 increment if it's greater than 500 millionth of a second. */
1198 if (ts
.tv_nsec
> 500000000) {
1199 return ts
.tv_sec
+ 1;
1205 * Like nt_time_to_unix() but supports negative time_t values.
1207 * Note: this function uses the full time_t range as valid date values including
1208 * (time_t)0 and -1. That means that NTTIME sentinel values of 0 and -1 which
1209 * represent a "not-set" value, can't be converted to sentinel values in a
1210 * time_t representation. Callers should therefor check the NTTIME value with
1211 * null_nttime() before calling this function.
1213 time_t nt_time_to_full_time_t(NTTIME nt
)
1217 ts
= nt_time_to_full_timespec(nt
);
1218 return full_timespec_to_time_t(&ts
);
1222 * Like time_t_to_unix_timespec() but supports negative time_t values.
1224 * This version converts (time_t)0 and -1 to an is_omit_timespec(), so 0 and -1
1225 * can't be used as valid date values. The function supports values < -1 though.
1227 struct timespec
time_t_to_full_timespec(time_t t
)
1230 return (struct timespec
){.tv_nsec
= SAMBA_UTIME_OMIT
};
1232 return (struct timespec
){.tv_sec
= t
};
1235 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
1237 /* Old system - no ns timestamp. */
1238 time_t get_atimensec(const struct stat
*st
)
1243 time_t get_mtimensec(const struct stat
*st
)
1248 time_t get_ctimensec(const struct stat
*st
)
1253 /* Set does nothing with no ns timestamp. */
1254 void set_atimensec(struct stat
*st
, time_t ns
)
1259 void set_mtimensec(struct stat
*st
, time_t ns
)
1264 void set_ctimensec(struct stat
*st
, time_t ns
)
1269 #elif HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1271 time_t get_atimensec(const struct stat
*st
)
1273 return st
->st_atimespec
.tv_nsec
;
1276 time_t get_mtimensec(const struct stat
*st
)
1278 return st
->st_mtimespec
.tv_nsec
;
1281 time_t get_ctimensec(const struct stat
*st
)
1283 return st
->st_ctimespec
.tv_nsec
;
1286 void set_atimensec(struct stat
*st
, time_t ns
)
1288 st
->st_atimespec
.tv_nsec
= ns
;
1291 void set_mtimensec(struct stat
*st
, time_t ns
)
1293 st
->st_mtimespec
.tv_nsec
= ns
;
1296 void set_ctimensec(struct stat
*st
, time_t ns
)
1298 st
->st_ctimespec
.tv_nsec
= ns
;
1301 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1303 time_t get_atimensec(const struct stat
*st
)
1305 return st
->st_atim
.tv_nsec
;
1308 time_t get_mtimensec(const struct stat
*st
)
1310 return st
->st_mtim
.tv_nsec
;
1313 time_t get_ctimensec(const struct stat
*st
)
1315 return st
->st_ctim
.tv_nsec
;
1318 void set_atimensec(struct stat
*st
, time_t ns
)
1320 st
->st_atim
.tv_nsec
= ns
;
1323 void set_mtimensec(struct stat
*st
, time_t ns
)
1325 st
->st_mtim
.tv_nsec
= ns
;
1327 void set_ctimensec(struct stat
*st
, time_t ns
)
1329 st
->st_ctim
.tv_nsec
= ns
;
1332 #elif HAVE_STRUCT_STAT_ST_MTIMENSEC
1334 time_t get_atimensec(const struct stat
*st
)
1336 return st
->st_atimensec
;
1339 time_t get_mtimensec(const struct stat
*st
)
1341 return st
->st_mtimensec
;
1344 time_t get_ctimensec(const struct stat
*st
)
1346 return st
->st_ctimensec
;
1349 void set_atimensec(struct stat
*st
, time_t ns
)
1351 st
->st_atimensec
= ns
;
1354 void set_mtimensec(struct stat
*st
, time_t ns
)
1356 st
->st_mtimensec
= ns
;
1359 void set_ctimensec(struct stat
*st
, time_t ns
)
1361 st
->st_ctimensec
= ns
;
1364 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1366 time_t get_atimensec(const struct stat
*st
)
1368 return st
->st_atime_n
;
1371 time_t get_mtimensec(const struct stat
*st
)
1373 return st
->st_mtime_n
;
1376 time_t get_ctimensec(const struct stat
*st
)
1378 return st
->st_ctime_n
;
1381 void set_atimensec(struct stat
*st
, time_t ns
)
1383 st
->st_atime_n
= ns
;
1386 void set_mtimensec(struct stat
*st
, time_t ns
)
1388 st
->st_mtime_n
= ns
;
1391 void set_ctimensec(struct stat
*st
, time_t ns
)
1393 st
->st_ctime_n
= ns
;
1396 #elif HAVE_STRUCT_STAT_ST_UMTIME
1398 /* Only usec timestamps available. Convert to/from nsec. */
1400 time_t get_atimensec(const struct stat
*st
)
1402 return st
->st_uatime
* 1000;
1405 time_t get_mtimensec(const struct stat
*st
)
1407 return st
->st_umtime
* 1000;
1410 time_t get_ctimensec(const struct stat
*st
)
1412 return st
->st_uctime
* 1000;
1415 void set_atimensec(struct stat
*st
, time_t ns
)
1417 st
->st_uatime
= ns
/ 1000;
1420 void set_mtimensec(struct stat
*st
, time_t ns
)
1422 st
->st_umtime
= ns
/ 1000;
1425 void set_ctimensec(struct stat
*st
, time_t ns
)
1427 st
->st_uctime
= ns
/ 1000;
1431 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
1434 struct timespec
get_atimespec(const struct stat
*pst
)
1436 struct timespec ret
;
1438 ret
.tv_sec
= pst
->st_atime
;
1439 ret
.tv_nsec
= get_atimensec(pst
);
1443 struct timespec
get_mtimespec(const struct stat
*pst
)
1445 struct timespec ret
;
1447 ret
.tv_sec
= pst
->st_mtime
;
1448 ret
.tv_nsec
= get_mtimensec(pst
);
1452 struct timespec
get_ctimespec(const struct stat
*pst
)
1454 struct timespec ret
;
1456 ret
.tv_sec
= pst
->st_mtime
;
1457 ret
.tv_nsec
= get_ctimensec(pst
);
1461 /****************************************************************************
1462 Deal with nanoseconds overflow.
1463 ****************************************************************************/
1465 void normalize_timespec(struct timespec
*ts
)
1469 /* most likely case: nsec is valid */
1470 if ((unsigned long)ts
->tv_nsec
< NSEC_PER_SEC
) {
1474 dres
= lldiv(ts
->tv_nsec
, NSEC_PER_SEC
);
1476 /* if the operation would result in overflow, max out values and bail */
1477 if (dres
.quot
> 0) {
1478 if ((int64_t)LONG_MAX
- dres
.quot
< ts
->tv_sec
) {
1479 ts
->tv_sec
= LONG_MAX
;
1480 ts
->tv_nsec
= NSEC_PER_SEC
- 1;
1484 if ((int64_t)LONG_MIN
- dres
.quot
> ts
->tv_sec
) {
1485 ts
->tv_sec
= LONG_MIN
;
1491 ts
->tv_nsec
= dres
.rem
;
1492 ts
->tv_sec
+= dres
.quot
;
1494 /* if the ns part was positive or a multiple of -1000000000, we're done */
1495 if (ts
->tv_nsec
> 0 || dres
.rem
== 0) {
1499 ts
->tv_nsec
+= NSEC_PER_SEC
;