lib: Use timeval_str_buf in timeval_string
[Samba.git] / lib / util / time.c
blob55baf728fa20ff7dfe9e3bd1ab9cfd021d9542c1
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 "includes.h"
25 #include "system/time.h"
27 /**
28 * @file
29 * @brief time handling functions
32 #if (SIZEOF_LONG == 8)
33 #define TIME_FIXUP_CONSTANT_INT 11644473600L
34 #elif (SIZEOF_LONG_LONG == 8)
35 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
36 #endif
40 /**
41 External access to time_t_min and time_t_max.
42 **/
43 _PUBLIC_ time_t get_time_t_max(void)
45 return TIME_T_MAX;
48 /**
49 a gettimeofday wrapper
50 **/
51 _PUBLIC_ void GetTimeOfDay(struct timeval *tval)
53 #ifdef HAVE_GETTIMEOFDAY_TZ
54 gettimeofday(tval,NULL);
55 #else
56 gettimeofday(tval);
57 #endif
60 /**
61 a wrapper to preferably get the monotonic time
62 **/
63 _PUBLIC_ void clock_gettime_mono(struct timespec *tp)
65 /* prefer a suspend aware monotonic CLOCK_BOOTTIME: */
66 #ifdef CLOCK_BOOTTIME
67 if (clock_gettime(CLOCK_BOOTTIME,tp) == 0) {
68 return;
70 #endif
71 /* then try the monotonic clock: */
72 #if CUSTOM_CLOCK_MONOTONIC != CLOCK_REALTIME
73 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC,tp) == 0) {
74 return;
76 #endif
77 clock_gettime(CLOCK_REALTIME,tp);
80 /**
81 a wrapper to preferably get the monotonic time in seconds
82 **/
83 _PUBLIC_ time_t time_mono(time_t *t)
85 struct timespec tp;
87 clock_gettime_mono(&tp);
88 if (t != NULL) {
89 *t = tp.tv_sec;
91 return tp.tv_sec;
95 #define TIME_FIXUP_CONSTANT 11644473600LL
97 time_t convert_timespec_to_time_t(struct timespec ts)
99 /* Ensure tv_nsec is less than 1sec. */
100 while (ts.tv_nsec > 1000000000) {
101 ts.tv_sec += 1;
102 ts.tv_nsec -= 1000000000;
105 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
106 increment if it's greater than 500 millionth of a second. */
108 if (ts.tv_nsec > 500000000) {
109 return ts.tv_sec + 1;
111 return ts.tv_sec;
114 struct timespec convert_time_t_to_timespec(time_t t)
116 struct timespec ts;
117 ts.tv_sec = t;
118 ts.tv_nsec = 0;
119 return ts;
125 Interpret an 8 byte "filetime" structure to a time_t
126 It's originally in "100ns units since jan 1st 1601"
128 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
130 tv_sec = 0
131 tv_nsec = 0;
133 Returns GMT.
135 time_t nt_time_to_unix(NTTIME nt)
137 return convert_timespec_to_time_t(nt_time_to_unix_timespec(nt));
142 put a 8 byte filetime from a time_t
143 This takes GMT as input
145 _PUBLIC_ void unix_to_nt_time(NTTIME *nt, time_t t)
147 uint64_t t2;
149 if (t == (time_t)-1) {
150 *nt = (NTTIME)-1LL;
151 return;
154 if (t == TIME_T_MAX || t == INT64_MAX) {
155 *nt = 0x7fffffffffffffffLL;
156 return;
159 if (t == 0) {
160 *nt = 0;
161 return;
164 t2 = t;
165 t2 += TIME_FIXUP_CONSTANT_INT;
166 t2 *= 1000*1000*10;
168 *nt = t2;
173 check if it's a null unix time
175 _PUBLIC_ bool null_time(time_t t)
177 return t == 0 ||
178 t == (time_t)0xFFFFFFFF ||
179 t == (time_t)-1;
184 check if it's a null NTTIME
186 _PUBLIC_ bool null_nttime(NTTIME t)
188 return t == 0 || t == (NTTIME)-1;
191 /*******************************************************************
192 create a 16 bit dos packed date
193 ********************************************************************/
194 static uint16_t make_dos_date1(struct tm *t)
196 uint16_t ret=0;
197 ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
198 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
199 return ret;
202 /*******************************************************************
203 create a 16 bit dos packed time
204 ********************************************************************/
205 static uint16_t make_dos_time1(struct tm *t)
207 uint16_t ret=0;
208 ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));
209 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
210 return ret;
213 /*******************************************************************
214 create a 32 bit dos packed date/time from some parameters
215 This takes a GMT time and returns a packed localtime structure
216 ********************************************************************/
217 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
219 struct tm *t;
220 uint32_t ret=0;
222 if (unixdate == 0) {
223 return 0;
226 unixdate -= zone_offset;
228 t = gmtime(&unixdate);
229 if (!t) {
230 return 0xFFFFFFFF;
233 ret = make_dos_date1(t);
234 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
236 return ret;
240 put a dos date into a buffer (time/date format)
241 This takes GMT time and puts local time in the buffer
243 _PUBLIC_ void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
245 uint32_t x = make_dos_date(unixdate, zone_offset);
246 SIVAL(buf,offset,x);
250 put a dos date into a buffer (date/time format)
251 This takes GMT time and puts local time in the buffer
253 _PUBLIC_ void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
255 uint32_t x;
256 x = make_dos_date(unixdate, zone_offset);
257 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
258 SIVAL(buf,offset,x);
262 put a dos 32 bit "unix like" date into a buffer. This routine takes
263 GMT and converts it to LOCAL time before putting it (most SMBs assume
264 localtime for this sort of date)
266 _PUBLIC_ void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
268 if (!null_time(unixdate)) {
269 unixdate -= zone_offset;
271 SIVAL(buf,offset,unixdate);
274 /*******************************************************************
275 interpret a 32 bit dos packed date/time to some parameters
276 ********************************************************************/
277 void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
279 uint32_t p0,p1,p2,p3;
281 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
282 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
284 *second = 2*(p0 & 0x1F);
285 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
286 *hour = (p1>>3)&0xFF;
287 *day = (p2&0x1F);
288 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
289 *year = ((p3>>1)&0xFF) + 80;
293 create a unix date (int GMT) from a dos date (which is actually in
294 localtime)
296 _PUBLIC_ time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
298 uint32_t dos_date=0;
299 struct tm t;
300 time_t ret;
302 dos_date = IVAL(date_ptr,0);
304 if (dos_date == 0) return (time_t)0;
306 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
307 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
308 t.tm_isdst = -1;
310 ret = timegm(&t);
312 ret += zone_offset;
314 return ret;
318 like make_unix_date() but the words are reversed
320 _PUBLIC_ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
322 uint32_t x,x2;
324 x = IVAL(date_ptr,0);
325 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
326 SIVAL(&x,0,x2);
328 return pull_dos_date((const uint8_t *)&x, zone_offset);
332 create a unix GMT date from a dos date in 32 bit "unix like" format
333 these generally arrive as localtimes, with corresponding DST
335 _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
337 time_t t = (time_t)IVAL(date_ptr,0);
338 if (!null_time(t)) {
339 t += zone_offset;
341 return t;
345 char *timeval_str_buf(const struct timeval *tp, bool hires,
346 struct timeval_buf *dst)
348 time_t t;
349 struct tm *tm;
350 size_t len;
352 t = (time_t)tp->tv_sec;
353 tm = localtime(&t);
355 if (tm == NULL) {
356 if (hires) {
357 snprintf(dst->buf, sizeof(dst->buf),
358 "%ld.%06ld seconds since the Epoch",
359 (long)tp->tv_sec, (long)tp->tv_usec);
360 } else {
361 snprintf(dst->buf, sizeof(dst->buf),
362 "%ld seconds since the Epoch", (long)t);
364 return dst->buf;
367 #ifdef HAVE_STRFTIME
368 len = strftime(dst->buf, sizeof(dst->buf), "%Y/%m/%d %H:%M:%S", tm);
369 #else
371 const char *asct = asctime(tm);
372 len = strlcpy(dst->buf, sizeof(dst->buf),
373 asct ? asct : "unknown");
375 #endif
376 if (hires && (len < sizeof(dst->buf))) {
377 snprintf(dst->buf + len, sizeof(dst->buf) - len,
378 ".%06ld", (long)tp->tv_usec);
381 return dst->buf;
384 /****************************************************************************
385 Return the date and time as a string
386 ****************************************************************************/
388 char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
390 struct timeval_buf tmp;
391 char *result;
393 result = talloc_strdup(ctx, timeval_str_buf(tp, hires, &tmp));
394 if (result == NULL) {
395 return NULL;
399 * beautify the talloc_report output
401 * This is not just cosmetics. A C compiler might in theory make the
402 * talloc_strdup call above a tail call with the tail call
403 * optimization. This would render "tmp" invalid while talloc_strdup
404 * tries to duplicate it. The talloc_set_name_const call below puts
405 * the talloc_strdup call into non-tail position.
407 talloc_set_name_const(result, result);
408 return result;
411 char *current_timestring(TALLOC_CTX *ctx, bool hires)
413 struct timeval tv;
415 GetTimeOfDay(&tv);
416 return timeval_string(ctx, &tv, hires);
421 return a HTTP/1.0 time string
423 _PUBLIC_ char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
425 char *buf;
426 char tempTime[60];
427 struct tm *tm = localtime(&t);
429 if (t == TIME_T_MAX) {
430 return talloc_strdup(mem_ctx, "never");
433 if (!tm) {
434 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
437 #ifndef HAVE_STRFTIME
438 buf = talloc_strdup(mem_ctx, asctime(tm));
439 if (buf[strlen(buf)-1] == '\n') {
440 buf[strlen(buf)-1] = 0;
442 #else
443 strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
444 buf = talloc_strdup(mem_ctx, tempTime);
445 #endif /* !HAVE_STRFTIME */
447 return buf;
451 Return the date and time as a string
453 _PUBLIC_ char *timestring(TALLOC_CTX *mem_ctx, time_t t)
455 char *TimeBuf;
456 char tempTime[80];
457 struct tm *tm;
459 tm = localtime(&t);
460 if (!tm) {
461 return talloc_asprintf(mem_ctx,
462 "%ld seconds since the Epoch",
463 (long)t);
466 #ifdef HAVE_STRFTIME
467 /* Some versions of gcc complain about using some special format
468 * specifiers. This is a bug in gcc, not a bug in this code. See a
469 * recent strftime() manual page for details. */
470 strftime(tempTime,sizeof(tempTime)-1,"%a %b %e %X %Y %Z",tm);
471 TimeBuf = talloc_strdup(mem_ctx, tempTime);
472 #else
473 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
474 if (TimeBuf == NULL) {
475 return NULL;
477 if (TimeBuf[0] != '\0') {
478 size_t len = strlen(TimeBuf);
479 if (TimeBuf[len - 1] == '\n') {
480 TimeBuf[len - 1] = '\0';
483 #endif
485 return TimeBuf;
489 return a talloced string representing a NTTIME for human consumption
491 _PUBLIC_ const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
493 time_t t;
494 if (nt == 0) {
495 return "NTTIME(0)";
497 t = nt_time_to_unix(nt);
498 return timestring(mem_ctx, t);
503 put a NTTIME into a packet
505 _PUBLIC_ void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
507 SBVAL(base, offset, t);
511 pull a NTTIME from a packet
513 _PUBLIC_ NTTIME pull_nttime(uint8_t *base, uint16_t offset)
515 NTTIME ret = BVAL(base, offset);
516 return ret;
520 return (tv1 - tv2) in microseconds
522 _PUBLIC_ int64_t usec_time_diff(const struct timeval *tv1, const struct timeval *tv2)
524 int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
525 return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
529 return (tp1 - tp2) in microseconds
531 _PUBLIC_ int64_t nsec_time_diff(const struct timespec *tp1, const struct timespec *tp2)
533 int64_t sec_diff = tp1->tv_sec - tp2->tv_sec;
534 return (sec_diff * 1000000000) + (int64_t)(tp1->tv_nsec - tp2->tv_nsec);
539 return a zero timeval
541 _PUBLIC_ struct timeval timeval_zero(void)
543 struct timeval tv;
544 tv.tv_sec = 0;
545 tv.tv_usec = 0;
546 return tv;
550 return true if a timeval is zero
552 _PUBLIC_ bool timeval_is_zero(const struct timeval *tv)
554 return tv->tv_sec == 0 && tv->tv_usec == 0;
558 return a timeval for the current time
560 _PUBLIC_ struct timeval timeval_current(void)
562 struct timeval tv;
563 GetTimeOfDay(&tv);
564 return tv;
568 return a timeval struct with the given elements
570 _PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs)
572 struct timeval tv;
573 tv.tv_sec = secs;
574 tv.tv_usec = usecs;
575 return tv;
580 return a timeval ofs microseconds after tv
582 _PUBLIC_ struct timeval timeval_add(const struct timeval *tv,
583 uint32_t secs, uint32_t usecs)
585 struct timeval tv2 = *tv;
586 const unsigned int million = 1000000;
587 tv2.tv_sec += secs;
588 tv2.tv_usec += usecs;
589 tv2.tv_sec += tv2.tv_usec / million;
590 tv2.tv_usec = tv2.tv_usec % million;
591 return tv2;
595 return the sum of two timeval structures
597 struct timeval timeval_sum(const struct timeval *tv1,
598 const struct timeval *tv2)
600 return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
604 return a timeval secs/usecs into the future
606 _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
608 struct timeval tv = timeval_current();
609 return timeval_add(&tv, secs, usecs);
613 return a timeval milliseconds into the future
615 _PUBLIC_ struct timeval timeval_current_ofs_msec(uint32_t msecs)
617 struct timeval tv = timeval_current();
618 return timeval_add(&tv, msecs / 1000, (msecs % 1000) * 1000);
622 return a timeval microseconds into the future
624 _PUBLIC_ struct timeval timeval_current_ofs_usec(uint32_t usecs)
626 struct timeval tv = timeval_current();
627 return timeval_add(&tv, usecs / 1000000, usecs % 1000000);
631 compare two timeval structures.
632 Return -1 if tv1 < tv2
633 Return 0 if tv1 == tv2
634 Return 1 if tv1 > tv2
636 _PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
638 if (tv1->tv_sec > tv2->tv_sec) return 1;
639 if (tv1->tv_sec < tv2->tv_sec) return -1;
640 if (tv1->tv_usec > tv2->tv_usec) return 1;
641 if (tv1->tv_usec < tv2->tv_usec) return -1;
642 return 0;
646 return true if a timer is in the past
648 _PUBLIC_ bool timeval_expired(const struct timeval *tv)
650 struct timeval tv2 = timeval_current();
651 if (tv2.tv_sec > tv->tv_sec) return true;
652 if (tv2.tv_sec < tv->tv_sec) return false;
653 return (tv2.tv_usec >= tv->tv_usec);
657 return the number of seconds elapsed between two times
659 _PUBLIC_ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
661 return (tv2->tv_sec - tv1->tv_sec) +
662 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
666 return the number of seconds elapsed since a given time
668 _PUBLIC_ double timeval_elapsed(const struct timeval *tv)
670 struct timeval tv2 = timeval_current();
671 return timeval_elapsed2(tv, &tv2);
674 * return the number of seconds elapsed between two times
676 _PUBLIC_ double timespec_elapsed2(const struct timespec *ts1,
677 const struct timespec *ts2)
679 return (ts2->tv_sec - ts1->tv_sec) +
680 (ts2->tv_nsec - ts1->tv_nsec)*1.0e-9;
684 * return the number of seconds elapsed since a given time
686 _PUBLIC_ double timespec_elapsed(const struct timespec *ts)
688 struct timespec ts2 = timespec_current();
689 return timespec_elapsed2(ts, &ts2);
693 return the lesser of two timevals
695 _PUBLIC_ struct timeval timeval_min(const struct timeval *tv1,
696 const struct timeval *tv2)
698 if (tv1->tv_sec < tv2->tv_sec) return *tv1;
699 if (tv1->tv_sec > tv2->tv_sec) return *tv2;
700 if (tv1->tv_usec < tv2->tv_usec) return *tv1;
701 return *tv2;
705 return the greater of two timevals
707 _PUBLIC_ struct timeval timeval_max(const struct timeval *tv1,
708 const struct timeval *tv2)
710 if (tv1->tv_sec > tv2->tv_sec) return *tv1;
711 if (tv1->tv_sec < tv2->tv_sec) return *tv2;
712 if (tv1->tv_usec > tv2->tv_usec) return *tv1;
713 return *tv2;
717 return the difference between two timevals as a timeval
718 if tv1 comes after tv2, then return a zero timeval
719 (this is *tv2 - *tv1)
721 _PUBLIC_ struct timeval timeval_until(const struct timeval *tv1,
722 const struct timeval *tv2)
724 struct timeval t;
725 if (timeval_compare(tv1, tv2) >= 0) {
726 return timeval_zero();
728 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
729 if (tv1->tv_usec > tv2->tv_usec) {
730 t.tv_sec--;
731 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
732 } else {
733 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
735 return t;
740 convert a timeval to a NTTIME
742 _PUBLIC_ NTTIME timeval_to_nttime(const struct timeval *tv)
744 return 10*(tv->tv_usec +
745 ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
749 convert a NTTIME to a timeval
751 _PUBLIC_ void nttime_to_timeval(struct timeval *tv, NTTIME t)
753 if (tv == NULL) return;
755 t += 10/2;
756 t /= 10;
757 t -= TIME_FIXUP_CONSTANT*1000*1000;
759 tv->tv_sec = t / 1000000;
761 if (TIME_T_MIN > tv->tv_sec || tv->tv_sec > TIME_T_MAX) {
762 tv->tv_sec = 0;
763 tv->tv_usec = 0;
764 return;
767 tv->tv_usec = t - tv->tv_sec*1000000;
770 /*******************************************************************
771 yield the difference between *A and *B, in seconds, ignoring leap seconds
772 ********************************************************************/
773 static int tm_diff(struct tm *a, struct tm *b)
775 int ay = a->tm_year + (1900 - 1);
776 int by = b->tm_year + (1900 - 1);
777 int intervening_leap_days =
778 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
779 int years = ay - by;
780 int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
781 int hours = 24*days + (a->tm_hour - b->tm_hour);
782 int minutes = 60*hours + (a->tm_min - b->tm_min);
783 int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
785 return seconds;
790 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
792 _PUBLIC_ int get_time_zone(time_t t)
794 struct tm *tm = gmtime(&t);
795 struct tm tm_utc;
796 if (!tm)
797 return 0;
798 tm_utc = *tm;
799 tm = localtime(&t);
800 if (!tm)
801 return 0;
802 return tm_diff(&tm_utc,tm);
805 struct timespec nt_time_to_unix_timespec(NTTIME nt)
807 int64_t d;
808 struct timespec ret;
810 if (nt == 0 || nt == (int64_t)-1) {
811 ret.tv_sec = 0;
812 ret.tv_nsec = 0;
813 return ret;
816 d = (int64_t)nt;
817 /* d is now in 100ns units, since jan 1st 1601".
818 Save off the ns fraction. */
821 * Take the last seven decimal digits and multiply by 100.
822 * to convert from 100ns units to 1ns units.
824 ret.tv_nsec = (long) ((d % (1000 * 1000 * 10)) * 100);
826 /* Convert to seconds */
827 d /= 1000*1000*10;
829 /* Now adjust by 369 years to make the secs since 1970 */
830 d -= TIME_FIXUP_CONSTANT_INT;
832 if (d <= (int64_t)TIME_T_MIN) {
833 ret.tv_sec = TIME_T_MIN;
834 ret.tv_nsec = 0;
835 return ret;
838 if (d >= (int64_t)TIME_T_MAX) {
839 ret.tv_sec = TIME_T_MAX;
840 ret.tv_nsec = 0;
841 return ret;
844 ret.tv_sec = (time_t)d;
845 return ret;
850 check if 2 NTTIMEs are equal.
852 bool nt_time_equal(NTTIME *t1, NTTIME *t2)
854 return *t1 == *t2;
858 Check if it's a null timespec.
861 bool null_timespec(struct timespec ts)
863 return ts.tv_sec == 0 ||
864 ts.tv_sec == (time_t)0xFFFFFFFF ||
865 ts.tv_sec == (time_t)-1;
868 /****************************************************************************
869 Convert a normalized timeval to a timespec.
870 ****************************************************************************/
872 struct timespec convert_timeval_to_timespec(const struct timeval tv)
874 struct timespec ts;
875 ts.tv_sec = tv.tv_sec;
876 ts.tv_nsec = tv.tv_usec * 1000;
877 return ts;
880 /****************************************************************************
881 Convert a normalized timespec to a timeval.
882 ****************************************************************************/
884 struct timeval convert_timespec_to_timeval(const struct timespec ts)
886 struct timeval tv;
887 tv.tv_sec = ts.tv_sec;
888 tv.tv_usec = ts.tv_nsec / 1000;
889 return tv;
892 /****************************************************************************
893 Return a timespec for the current time
894 ****************************************************************************/
896 _PUBLIC_ struct timespec timespec_current(void)
898 struct timespec ts;
899 clock_gettime(CLOCK_REALTIME, &ts);
900 return ts;
903 /****************************************************************************
904 Return the lesser of two timespecs.
905 ****************************************************************************/
907 struct timespec timespec_min(const struct timespec *ts1,
908 const struct timespec *ts2)
910 if (ts1->tv_sec < ts2->tv_sec) return *ts1;
911 if (ts1->tv_sec > ts2->tv_sec) return *ts2;
912 if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
913 return *ts2;
916 /****************************************************************************
917 compare two timespec structures.
918 Return -1 if ts1 < ts2
919 Return 0 if ts1 == ts2
920 Return 1 if ts1 > ts2
921 ****************************************************************************/
923 _PUBLIC_ int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
925 if (ts1->tv_sec > ts2->tv_sec) return 1;
926 if (ts1->tv_sec < ts2->tv_sec) return -1;
927 if (ts1->tv_nsec > ts2->tv_nsec) return 1;
928 if (ts1->tv_nsec < ts2->tv_nsec) return -1;
929 return 0;
932 /****************************************************************************
933 Round up a timespec if nsec > 500000000, round down if lower,
934 then zero nsec.
935 ****************************************************************************/
937 void round_timespec_to_sec(struct timespec *ts)
939 ts->tv_sec = convert_timespec_to_time_t(*ts);
940 ts->tv_nsec = 0;
943 /****************************************************************************
944 Round a timespec to usec value.
945 ****************************************************************************/
947 void round_timespec_to_usec(struct timespec *ts)
949 struct timeval tv = convert_timespec_to_timeval(*ts);
950 *ts = convert_timeval_to_timespec(tv);
951 while (ts->tv_nsec > 1000000000) {
952 ts->tv_sec += 1;
953 ts->tv_nsec -= 1000000000;
957 /****************************************************************************
958 Put a 8 byte filetime from a struct timespec. Uses GMT.
959 ****************************************************************************/
961 _PUBLIC_ NTTIME unix_timespec_to_nt_time(struct timespec ts)
963 uint64_t d;
965 if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
966 return 0;
968 if (ts.tv_sec == TIME_T_MAX) {
969 return 0x7fffffffffffffffLL;
971 if (ts.tv_sec == (time_t)-1) {
972 return (uint64_t)-1;
975 d = ts.tv_sec;
976 d += TIME_FIXUP_CONSTANT_INT;
977 d *= 1000*1000*10;
978 /* d is now in 100ns units. */
979 d += (ts.tv_nsec / 100);
981 return d;