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 */
32 * @brief time handling functions
35 #if (SIZEOF_LONG == 8)
36 #define TIME_FIXUP_CONSTANT_INT 11644473600L
37 #elif (SIZEOF_LONG_LONG == 8)
38 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
44 External access to time_t_min and time_t_max.
46 _PUBLIC_
time_t get_time_t_max(void)
52 a wrapper to preferably get the monotonic time
54 _PUBLIC_
void clock_gettime_mono(struct timespec
*tp
)
56 /* prefer a suspend aware monotonic CLOCK_BOOTTIME: */
58 if (clock_gettime(CLOCK_BOOTTIME
,tp
) == 0) {
62 /* then try the monotonic clock: */
63 #ifndef CUSTOM_CLOCK_MONOTONIC_IS_REALTIME
64 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC
,tp
) == 0) {
68 clock_gettime(CLOCK_REALTIME
,tp
);
72 a wrapper to preferably get the monotonic time in seconds
74 _PUBLIC_
time_t time_mono(time_t *t
)
78 clock_gettime_mono(&tp
);
86 #define TIME_FIXUP_CONSTANT 11644473600LL
88 time_t convert_timespec_to_time_t(struct timespec ts
)
90 /* Ensure tv_nsec is less than 1sec. */
91 while (ts
.tv_nsec
> 1000000000) {
93 ts
.tv_nsec
-= 1000000000;
96 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
97 increment if it's greater than 500 millionth of a second. */
99 if (ts
.tv_nsec
> 500000000) {
100 return ts
.tv_sec
+ 1;
105 struct timespec
convert_time_t_to_timespec(time_t t
)
116 Interpret an 8 byte "filetime" structure to a time_t
117 It's originally in "100ns units since jan 1st 1601"
119 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
126 time_t nt_time_to_unix(NTTIME nt
)
128 return convert_timespec_to_time_t(nt_time_to_unix_timespec(nt
));
133 put a 8 byte filetime from a time_t
134 This takes GMT as input
136 _PUBLIC_
void unix_to_nt_time(NTTIME
*nt
, time_t t
)
140 if (t
== (time_t)-1) {
145 if (t
== TIME_T_MAX
|| t
== INT64_MAX
) {
146 *nt
= 0x7fffffffffffffffLL
;
156 t2
+= TIME_FIXUP_CONSTANT_INT
;
164 check if it's a null unix time
166 _PUBLIC_
bool null_time(time_t t
)
169 t
== (time_t)0xFFFFFFFF ||
175 check if it's a null NTTIME
177 _PUBLIC_
bool null_nttime(NTTIME t
)
179 return t
== 0 || t
== (NTTIME
)-1;
182 /*******************************************************************
183 create a 16 bit dos packed date
184 ********************************************************************/
185 static uint16_t make_dos_date1(struct tm
*t
)
188 ret
= (((unsigned int)(t
->tm_mon
+1)) >> 3) | ((t
->tm_year
-80) << 1);
189 ret
= ((ret
&0xFF)<<8) | (t
->tm_mday
| (((t
->tm_mon
+1) & 0x7) << 5));
193 /*******************************************************************
194 create a 16 bit dos packed time
195 ********************************************************************/
196 static uint16_t make_dos_time1(struct tm
*t
)
199 ret
= ((((unsigned int)t
->tm_min
>> 3)&0x7) | (((unsigned int)t
->tm_hour
) << 3));
200 ret
= ((ret
&0xFF)<<8) | ((t
->tm_sec
/2) | ((t
->tm_min
& 0x7) << 5));
204 /*******************************************************************
205 create a 32 bit dos packed date/time from some parameters
206 This takes a GMT time and returns a packed localtime structure
207 ********************************************************************/
208 static uint32_t make_dos_date(time_t unixdate
, int zone_offset
)
217 unixdate
-= zone_offset
;
219 t
= gmtime(&unixdate
);
224 ret
= make_dos_date1(t
);
225 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
231 put a dos date into a buffer (time/date format)
232 This takes GMT time and puts local time in the buffer
234 _PUBLIC_
void push_dos_date(uint8_t *buf
, int offset
, time_t unixdate
, int zone_offset
)
236 uint32_t x
= make_dos_date(unixdate
, zone_offset
);
241 put a dos date into a buffer (date/time format)
242 This takes GMT time and puts local time in the buffer
244 _PUBLIC_
void push_dos_date2(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
247 x
= make_dos_date(unixdate
, zone_offset
);
248 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
253 put a dos 32 bit "unix like" date into a buffer. This routine takes
254 GMT and converts it to LOCAL time before putting it (most SMBs assume
255 localtime for this sort of date)
257 _PUBLIC_
void push_dos_date3(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
259 if (!null_time(unixdate
)) {
260 unixdate
-= zone_offset
;
262 SIVAL(buf
,offset
,unixdate
);
265 /*******************************************************************
266 interpret a 32 bit dos packed date/time to some parameters
267 ********************************************************************/
268 void interpret_dos_date(uint32_t date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
270 uint32_t p0
,p1
,p2
,p3
;
272 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
273 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
275 *second
= 2*(p0
& 0x1F);
276 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
277 *hour
= (p1
>>3)&0xFF;
279 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
280 *year
= ((p3
>>1)&0xFF) + 80;
284 create a unix date (int GMT) from a dos date (which is actually in
287 _PUBLIC_
time_t pull_dos_date(const uint8_t *date_ptr
, int zone_offset
)
293 dos_date
= IVAL(date_ptr
,0);
295 if (dos_date
== 0) return (time_t)0;
297 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
298 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
309 like make_unix_date() but the words are reversed
311 _PUBLIC_
time_t pull_dos_date2(const uint8_t *date_ptr
, int zone_offset
)
315 x
= IVAL(date_ptr
,0);
316 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
319 return pull_dos_date((const uint8_t *)&x
, zone_offset
);
323 create a unix GMT date from a dos date in 32 bit "unix like" format
324 these generally arrive as localtimes, with corresponding DST
326 _PUBLIC_
time_t pull_dos_date3(const uint8_t *date_ptr
, int zone_offset
)
328 time_t t
= (time_t)IVAL(date_ptr
,0);
330 if (t
== (time_t)0xFFFFFFFF) {
340 /****************************************************************************
341 Return the date and time as a string
342 ****************************************************************************/
344 char *timeval_string(TALLOC_CTX
*ctx
, const struct timeval
*tp
, bool hires
)
346 struct timeval_buf tmp
;
349 result
= talloc_strdup(ctx
, timeval_str_buf(tp
, false, hires
, &tmp
));
350 if (result
== NULL
) {
355 * beautify the talloc_report output
357 * This is not just cosmetics. A C compiler might in theory make the
358 * talloc_strdup call above a tail call with the tail call
359 * optimization. This would render "tmp" invalid while talloc_strdup
360 * tries to duplicate it. The talloc_set_name_const call below puts
361 * the talloc_strdup call into non-tail position.
363 talloc_set_name_const(result
, result
);
367 /****************************************************************************
368 Return the date and time as a string
369 ****************************************************************************/
371 const char *timespec_string_buf(const struct timespec
*tp
,
373 struct timeval_buf
*buf
)
376 struct tm
*tm
= NULL
;
379 if (is_omit_timespec(tp
)) {
380 strlcpy(buf
->buf
, "SAMBA_UTIME_OMIT", sizeof(buf
->buf
));
384 t
= (time_t)tp
->tv_sec
;
389 len
= snprintf(buf
->buf
, sizeof(buf
->buf
),
390 "%ld.%09ld seconds since the Epoch",
391 (long)tp
->tv_sec
, (long)tp
->tv_nsec
);
393 len
= snprintf(buf
->buf
, sizeof(buf
->buf
),
394 "%ld seconds since the Epoch", (long)t
);
397 len
= snprintf(buf
->buf
, sizeof(buf
->buf
),
398 "%04d/%02d/%02d %02d:%02d:%02d",
406 len
= snprintf(buf
->buf
, sizeof(buf
->buf
),
407 "%04d/%02d/%02d %02d:%02d:%02d.%09ld",
423 char *current_timestring(TALLOC_CTX
*ctx
, bool hires
)
428 return timeval_string(ctx
, &tv
, hires
);
432 * Return date and time as a minimal string avoiding funny characters
433 * that may cause trouble in file names. We only use digits and
434 * underscore ... or a minus/hyphen if we got negative time.
436 char *minimal_timeval_string(TALLOC_CTX
*ctx
, const struct timeval
*tp
, bool hires
)
441 t
= (time_t)tp
->tv_sec
;
445 return talloc_asprintf(ctx
, "%ld_%06ld",
449 return talloc_asprintf(ctx
, "%ld", (long)t
);
453 return talloc_asprintf(ctx
,
454 "%04d%02d%02d_%02d%02d%02d_%06ld",
463 return talloc_asprintf(ctx
,
464 "%04d%02d%02d_%02d%02d%02d",
475 char *current_minimal_timestring(TALLOC_CTX
*ctx
, bool hires
)
480 return minimal_timeval_string(ctx
, &tv
, hires
);
484 return a HTTP/1.0 time string
486 _PUBLIC_
char *http_timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
490 struct tm
*tm
= localtime(&t
);
492 if (t
== TIME_T_MAX
) {
493 return talloc_strdup(mem_ctx
, "never");
497 return talloc_asprintf(mem_ctx
,"%ld seconds since the Epoch",(long)t
);
500 #ifndef HAVE_STRFTIME
501 buf
= talloc_strdup(mem_ctx
, asctime(tm
));
502 if (buf
[strlen(buf
)-1] == '\n') {
503 buf
[strlen(buf
)-1] = 0;
506 strftime(tempTime
, sizeof(tempTime
)-1, "%a, %d %b %Y %H:%M:%S %Z", tm
);
507 buf
= talloc_strdup(mem_ctx
, tempTime
);
508 #endif /* !HAVE_STRFTIME */
514 Return the date and time as a string
516 _PUBLIC_
char *timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
524 return talloc_asprintf(mem_ctx
,
525 "%ld seconds since the Epoch",
530 /* Some versions of gcc complain about using some special format
531 * specifiers. This is a bug in gcc, not a bug in this code. See a
532 * recent strftime() manual page for details. */
533 strftime(tempTime
,sizeof(tempTime
)-1,"%a %b %e %X %Y %Z",tm
);
534 TimeBuf
= talloc_strdup(mem_ctx
, tempTime
);
536 TimeBuf
= talloc_strdup(mem_ctx
, asctime(tm
));
537 if (TimeBuf
== NULL
) {
540 if (TimeBuf
[0] != '\0') {
541 size_t len
= strlen(TimeBuf
);
542 if (TimeBuf
[len
- 1] == '\n') {
543 TimeBuf
[len
- 1] = '\0';
552 return a talloced string representing a NTTIME for human consumption
554 _PUBLIC_
const char *nt_time_string(TALLOC_CTX
*mem_ctx
, NTTIME nt
)
560 t
= nt_time_to_full_time_t(nt
);
561 return timestring(mem_ctx
, t
);
566 put a NTTIME into a packet
568 _PUBLIC_
void push_nttime(uint8_t *base
, uint16_t offset
, NTTIME t
)
570 SBVAL(base
, offset
, t
);
574 pull a NTTIME from a packet
576 _PUBLIC_ NTTIME
pull_nttime(uint8_t *base
, uint16_t offset
)
578 NTTIME ret
= BVAL(base
, offset
);
583 return (tv1 - tv2) in microseconds
585 _PUBLIC_
int64_t usec_time_diff(const struct timeval
*tv1
, const struct timeval
*tv2
)
587 int64_t sec_diff
= tv1
->tv_sec
- tv2
->tv_sec
;
588 return (sec_diff
* 1000000) + (int64_t)(tv1
->tv_usec
- tv2
->tv_usec
);
592 return (tp1 - tp2) in nanoseconds
594 _PUBLIC_
int64_t nsec_time_diff(const struct timespec
*tp1
, const struct timespec
*tp2
)
596 int64_t sec_diff
= tp1
->tv_sec
- tp2
->tv_sec
;
597 return (sec_diff
* 1000000000) + (int64_t)(tp1
->tv_nsec
- tp2
->tv_nsec
);
602 return a zero timeval
604 _PUBLIC_
struct timeval
timeval_zero(void)
613 return true if a timeval is zero
615 _PUBLIC_
bool timeval_is_zero(const struct timeval
*tv
)
617 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
621 return a timeval for the current time
623 _PUBLIC_
struct timeval
timeval_current(void)
631 return a timeval struct with the given elements
633 _PUBLIC_
struct timeval
timeval_set(uint32_t secs
, uint32_t usecs
)
643 return a timeval ofs microseconds after tv
645 _PUBLIC_
struct timeval
timeval_add(const struct timeval
*tv
,
646 uint32_t secs
, uint32_t usecs
)
648 struct timeval tv2
= *tv
;
649 const unsigned int million
= 1000000;
651 tv2
.tv_usec
+= usecs
;
652 tv2
.tv_sec
+= tv2
.tv_usec
/ million
;
653 tv2
.tv_usec
= tv2
.tv_usec
% million
;
658 return the sum of two timeval structures
660 struct timeval
timeval_sum(const struct timeval
*tv1
,
661 const struct timeval
*tv2
)
663 return timeval_add(tv1
, tv2
->tv_sec
, tv2
->tv_usec
);
667 return a timeval secs/usecs into the future
669 _PUBLIC_
struct timeval
timeval_current_ofs(uint32_t secs
, uint32_t usecs
)
671 struct timeval tv
= timeval_current();
672 return timeval_add(&tv
, secs
, usecs
);
676 return a timeval milliseconds into the future
678 _PUBLIC_
struct timeval
timeval_current_ofs_msec(uint32_t msecs
)
680 struct timeval tv
= timeval_current();
681 return timeval_add(&tv
, msecs
/ 1000, (msecs
% 1000) * 1000);
685 return a timeval microseconds into the future
687 _PUBLIC_
struct timeval
timeval_current_ofs_usec(uint32_t usecs
)
689 struct timeval tv
= timeval_current();
690 return timeval_add(&tv
, usecs
/ 1000000, usecs
% 1000000);
694 compare two timeval structures.
695 Return -1 if tv1 < tv2
696 Return 0 if tv1 == tv2
697 Return 1 if tv1 > tv2
699 _PUBLIC_
int timeval_compare(const struct timeval
*tv1
, const struct timeval
*tv2
)
701 if (tv1
->tv_sec
> tv2
->tv_sec
) return 1;
702 if (tv1
->tv_sec
< tv2
->tv_sec
) return -1;
703 if (tv1
->tv_usec
> tv2
->tv_usec
) return 1;
704 if (tv1
->tv_usec
< tv2
->tv_usec
) return -1;
709 return true if a timer is in the past
711 _PUBLIC_
bool timeval_expired(const struct timeval
*tv
)
713 struct timeval tv2
= timeval_current();
714 if (tv2
.tv_sec
> tv
->tv_sec
) return true;
715 if (tv2
.tv_sec
< tv
->tv_sec
) return false;
716 return (tv2
.tv_usec
>= tv
->tv_usec
);
720 return the number of seconds elapsed between two times
722 _PUBLIC_
double timeval_elapsed2(const struct timeval
*tv1
, const struct timeval
*tv2
)
724 return (tv2
->tv_sec
- tv1
->tv_sec
) +
725 (tv2
->tv_usec
- tv1
->tv_usec
)*1.0e-6;
729 return the number of seconds elapsed since a given time
731 _PUBLIC_
double timeval_elapsed(const struct timeval
*tv
)
733 struct timeval tv2
= timeval_current();
734 return timeval_elapsed2(tv
, &tv2
);
737 * return the number of seconds elapsed between two times
739 _PUBLIC_
double timespec_elapsed2(const struct timespec
*ts1
,
740 const struct timespec
*ts2
)
742 return (ts2
->tv_sec
- ts1
->tv_sec
) +
743 (ts2
->tv_nsec
- ts1
->tv_nsec
)*1.0e-9;
747 * return the number of seconds elapsed since a given time
749 _PUBLIC_
double timespec_elapsed(const struct timespec
*ts
)
751 struct timespec ts2
= timespec_current();
752 return timespec_elapsed2(ts
, &ts2
);
756 return the lesser of two timevals
758 _PUBLIC_
struct timeval
timeval_min(const struct timeval
*tv1
,
759 const struct timeval
*tv2
)
761 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv1
;
762 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv2
;
763 if (tv1
->tv_usec
< tv2
->tv_usec
) return *tv1
;
768 return the greater of two timevals
770 _PUBLIC_
struct timeval
timeval_max(const struct timeval
*tv1
,
771 const struct timeval
*tv2
)
773 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv1
;
774 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv2
;
775 if (tv1
->tv_usec
> tv2
->tv_usec
) return *tv1
;
780 return the difference between two timevals as a timeval
781 if tv1 comes after tv2, then return a zero timeval
782 (this is *tv2 - *tv1)
784 _PUBLIC_
struct timeval
timeval_until(const struct timeval
*tv1
,
785 const struct timeval
*tv2
)
788 if (timeval_compare(tv1
, tv2
) >= 0) {
789 return timeval_zero();
791 t
.tv_sec
= tv2
->tv_sec
- tv1
->tv_sec
;
792 if (tv1
->tv_usec
> tv2
->tv_usec
) {
794 t
.tv_usec
= 1000000 - (tv1
->tv_usec
- tv2
->tv_usec
);
796 t
.tv_usec
= tv2
->tv_usec
- tv1
->tv_usec
;
803 convert a timeval to a NTTIME
805 _PUBLIC_ NTTIME
timeval_to_nttime(const struct timeval
*tv
)
807 return 10*(tv
->tv_usec
+
808 ((TIME_FIXUP_CONSTANT
+ (uint64_t)tv
->tv_sec
) * 1000000));
812 convert a NTTIME to a timeval
814 _PUBLIC_
void nttime_to_timeval(struct timeval
*tv
, NTTIME t
)
816 if (tv
== NULL
) return;
820 t
-= TIME_FIXUP_CONSTANT
*1000*1000;
822 tv
->tv_sec
= t
/ 1000000;
824 if (TIME_T_MIN
> tv
->tv_sec
|| tv
->tv_sec
> TIME_T_MAX
) {
830 tv
->tv_usec
= t
- tv
->tv_sec
*1000000;
833 /*******************************************************************
834 yield the difference between *A and *B, in seconds, ignoring leap seconds
835 ********************************************************************/
836 static int tm_diff(struct tm
*a
, struct tm
*b
)
838 int ay
= a
->tm_year
+ (1900 - 1);
839 int by
= b
->tm_year
+ (1900 - 1);
840 int intervening_leap_days
=
841 (ay
/4 - by
/4) - (ay
/100 - by
/100) + (ay
/400 - by
/400);
843 int days
= 365*years
+ intervening_leap_days
+ (a
->tm_yday
- b
->tm_yday
);
844 int hours
= 24*days
+ (a
->tm_hour
- b
->tm_hour
);
845 int minutes
= 60*hours
+ (a
->tm_min
- b
->tm_min
);
846 int seconds
= 60*minutes
+ (a
->tm_sec
- b
->tm_sec
);
853 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
855 _PUBLIC_
int get_time_zone(time_t t
)
857 struct tm
*tm
= gmtime(&t
);
865 return tm_diff(&tm_utc
,tm
);
868 struct timespec
nt_time_to_unix_timespec(NTTIME nt
)
873 if (nt
== 0 || nt
== (int64_t)-1) {
880 /* d is now in 100ns units, since jan 1st 1601".
881 Save off the ns fraction. */
884 * Take the last seven decimal digits and multiply by 100.
885 * to convert from 100ns units to 1ns units.
887 ret
.tv_nsec
= (long) ((d
% (1000 * 1000 * 10)) * 100);
889 /* Convert to seconds */
892 /* Now adjust by 369 years to make the secs since 1970 */
893 d
-= TIME_FIXUP_CONSTANT_INT
;
895 if (d
<= (int64_t)TIME_T_MIN
) {
896 ret
.tv_sec
= TIME_T_MIN
;
901 if (d
>= (int64_t)TIME_T_MAX
) {
902 ret
.tv_sec
= TIME_T_MAX
;
907 ret
.tv_sec
= (time_t)d
;
913 check if 2 NTTIMEs are equal.
915 bool nt_time_equal(NTTIME
*t1
, NTTIME
*t2
)
921 Check if it's a null timespec.
924 bool null_timespec(struct timespec ts
)
926 return ts
.tv_sec
== 0 ||
927 ts
.tv_sec
== (time_t)0xFFFFFFFF ||
928 ts
.tv_sec
== (time_t)-1;
931 /****************************************************************************
932 Convert a normalized timeval to a timespec.
933 ****************************************************************************/
935 struct timespec
convert_timeval_to_timespec(const struct timeval tv
)
938 ts
.tv_sec
= tv
.tv_sec
;
939 ts
.tv_nsec
= tv
.tv_usec
* 1000;
943 /****************************************************************************
944 Convert a normalized timespec to a timeval.
945 ****************************************************************************/
947 struct timeval
convert_timespec_to_timeval(const struct timespec ts
)
950 tv
.tv_sec
= ts
.tv_sec
;
951 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
955 /****************************************************************************
956 Return a timespec for the current time
957 ****************************************************************************/
959 _PUBLIC_
struct timespec
timespec_current(void)
962 clock_gettime(CLOCK_REALTIME
, &ts
);
966 /****************************************************************************
967 Return the lesser of two timespecs.
968 ****************************************************************************/
970 struct timespec
timespec_min(const struct timespec
*ts1
,
971 const struct timespec
*ts2
)
973 if (ts1
->tv_sec
< ts2
->tv_sec
) return *ts1
;
974 if (ts1
->tv_sec
> ts2
->tv_sec
) return *ts2
;
975 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return *ts1
;
979 /****************************************************************************
980 compare two timespec structures.
981 Return -1 if ts1 < ts2
982 Return 0 if ts1 == ts2
983 Return 1 if ts1 > ts2
984 ****************************************************************************/
986 _PUBLIC_
int timespec_compare(const struct timespec
*ts1
, const struct timespec
*ts2
)
988 if (ts1
->tv_sec
> ts2
->tv_sec
) return 1;
989 if (ts1
->tv_sec
< ts2
->tv_sec
) return -1;
990 if (ts1
->tv_nsec
> ts2
->tv_nsec
) return 1;
991 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return -1;
995 /****************************************************************************
996 Round up a timespec if nsec > 500000000, round down if lower,
998 ****************************************************************************/
1000 void round_timespec_to_sec(struct timespec
*ts
)
1002 ts
->tv_sec
= convert_timespec_to_time_t(*ts
);
1006 /****************************************************************************
1007 Round a timespec to usec value.
1008 ****************************************************************************/
1010 void round_timespec_to_usec(struct timespec
*ts
)
1012 struct timeval tv
= convert_timespec_to_timeval(*ts
);
1013 *ts
= convert_timeval_to_timespec(tv
);
1014 while (ts
->tv_nsec
> 1000000000) {
1016 ts
->tv_nsec
-= 1000000000;
1020 /****************************************************************************
1021 Round a timespec to NTTIME resolution.
1022 ****************************************************************************/
1024 void round_timespec_to_nttime(struct timespec
*ts
)
1026 ts
->tv_nsec
= (ts
->tv_nsec
/ 100) * 100;
1029 /****************************************************************************
1030 Put a 8 byte filetime from a struct timespec. Uses GMT.
1031 ****************************************************************************/
1033 _PUBLIC_ NTTIME
unix_timespec_to_nt_time(struct timespec ts
)
1037 if (ts
.tv_sec
==0 && ts
.tv_nsec
== 0) {
1040 if (ts
.tv_sec
== TIME_T_MAX
) {
1041 return 0x7fffffffffffffffLL
;
1043 if (ts
.tv_sec
== (time_t)-1) {
1044 return (uint64_t)-1;
1048 d
+= TIME_FIXUP_CONSTANT_INT
;
1050 /* d is now in 100ns units. */
1051 d
+= (ts
.tv_nsec
/ 100);
1057 * Functions supporting the full range of time_t and struct timespec values,
1058 * including 0, -1 and all other negative values. These functions don't use 0 or
1059 * -1 values as sentinel to denote "unset" variables, but use the POSIX 2008
1060 * define UTIME_OMIT from utimensat(2).
1064 * Check if it's a to be omitted timespec.
1066 bool is_omit_timespec(const struct timespec
*ts
)
1068 return ts
->tv_nsec
== SAMBA_UTIME_OMIT
;
1072 * Return a to be omitted timespec.
1074 struct timespec
make_omit_timespec(void)
1076 return (struct timespec
){.tv_nsec
= SAMBA_UTIME_OMIT
};
1080 * Like unix_timespec_to_nt_time() but without the special casing of tv_sec=0
1081 * and -1. Also dealing with SAMBA_UTIME_OMIT.
1083 NTTIME
full_timespec_to_nt_time(const struct timespec
*_ts
)
1085 struct timespec ts
= *_ts
;
1088 if (is_omit_timespec(_ts
)) {
1092 /* Ensure tv_nsec is less than 1 sec. */
1093 while (ts
.tv_nsec
> 1000000000) {
1094 if (ts
.tv_sec
> TIME_T_MAX
) {
1098 ts
.tv_nsec
-= 1000000000;
1101 if (ts
.tv_sec
>= TIME_T_MAX
) {
1104 if ((ts
.tv_sec
+ TIME_FIXUP_CONSTANT_INT
) <= 0) {
1108 d
= TIME_FIXUP_CONSTANT_INT
;
1112 /* d is now in 100ns units. */
1113 d
+= (ts
.tv_nsec
/ 100);
1119 * Like nt_time_to_unix_timespec() but allowing negative tv_sec values and
1120 * returning NTTIME=0 and -1 as struct timespec {.tv_nsec = SAMBA_UTIME_OMIT}.
1122 * See also: is_omit_timespec().
1124 struct timespec
nt_time_to_full_timespec(NTTIME nt
)
1127 struct timespec ret
;
1129 if (nt
== NTTIME_OMIT
) {
1130 return make_omit_timespec();
1132 if (nt
== NTTIME_FREEZE
) {
1134 * This should be returned as SAMBA_UTIME_FREEZE in the
1137 return make_omit_timespec();
1139 if (nt
> NTTIME_MAX
) {
1144 /* d is now in 100ns units, since jan 1st 1601".
1145 Save off the ns fraction. */
1148 * Take the last seven decimal digits and multiply by 100.
1149 * to convert from 100ns units to 1ns units.
1151 ret
.tv_nsec
= (long) ((d
% (1000 * 1000 * 10)) * 100);
1153 /* Convert to seconds */
1156 /* Now adjust by 369 years to make the secs since 1970 */
1157 d
-= TIME_FIXUP_CONSTANT_INT
;
1159 if (d
>= (int64_t)TIME_T_MAX
) {
1160 ret
.tv_sec
= TIME_T_MAX
;
1165 ret
.tv_sec
= (time_t)d
;
1170 * Note: this function uses the full time_t range as valid date values including
1171 * (time_t)0 and -1. That means that struct timespec sentinel values (cf
1172 * is_omit_timespec()) can't be converted to sentinel values in a time_t
1173 * representation. Callers should therefor check the NTTIME value with
1174 * null_nttime() before calling this function.
1176 time_t full_timespec_to_time_t(const struct timespec
*_ts
)
1178 struct timespec ts
= *_ts
;
1180 if (is_omit_timespec(_ts
)) {
1182 * Unfortunately there's no sensible sentinel value in the
1183 * time_t range that is not conflicting with a valid time value
1184 * ((time_t)0 and -1 are valid time values). Bite the bullit and
1190 /* Ensure tv_nsec is less than 1sec. */
1191 while (ts
.tv_nsec
> 1000000000) {
1193 ts
.tv_nsec
-= 1000000000;
1196 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
1197 increment if it's greater than 500 millionth of a second. */
1199 if (ts
.tv_nsec
> 500000000) {
1200 return ts
.tv_sec
+ 1;
1206 * Like nt_time_to_unix() but supports negative time_t values.
1208 * Note: this function uses the full time_t range as valid date values including
1209 * (time_t)0 and -1. That means that NTTIME sentinel values of 0 and -1 which
1210 * represent a "not-set" value, can't be converted to sentinel values in a
1211 * time_t representation. Callers should therefor check the NTTIME value with
1212 * null_nttime() before calling this function.
1214 time_t nt_time_to_full_time_t(NTTIME nt
)
1218 ts
= nt_time_to_full_timespec(nt
);
1219 return full_timespec_to_time_t(&ts
);
1223 * Like time_t_to_unix_timespec() but supports negative time_t values.
1225 * This version converts (time_t)0 and -1 to an is_omit_timespec(), so 0 and -1
1226 * can't be used as valid date values. The function supports values < -1 though.
1228 struct timespec
time_t_to_full_timespec(time_t t
)
1231 return (struct timespec
){.tv_nsec
= SAMBA_UTIME_OMIT
};
1233 return (struct timespec
){.tv_sec
= t
};