param: Rename variable used for lp_server_schannel serverSchannel
[Samba.git] / lib / util / time.c
bloba09490a198634b3e822411d5c59a266b9efbd0a4
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 /****************************************************************************
346 Return the date and time as a string
347 ****************************************************************************/
349 char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
351 time_t t;
352 struct tm *tm;
354 t = (time_t)tp->tv_sec;
355 tm = localtime(&t);
356 if (!tm) {
357 if (hires) {
358 return talloc_asprintf(ctx,
359 "%ld.%06ld seconds since the Epoch",
360 (long)tp->tv_sec,
361 (long)tp->tv_usec);
362 } else {
363 return talloc_asprintf(ctx,
364 "%ld seconds since the Epoch",
365 (long)t);
367 } else {
368 #ifdef HAVE_STRFTIME
369 char TimeBuf[60];
370 if (hires) {
371 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
372 return talloc_asprintf(ctx,
373 "%s.%06ld", TimeBuf,
374 (long)tp->tv_usec);
375 } else {
376 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
377 return talloc_strdup(ctx, TimeBuf);
379 #else
380 if (hires) {
381 const char *asct = asctime(tm);
382 return talloc_asprintf(ctx, "%s.%06ld",
383 asct ? asct : "unknown",
384 (long)tp->tv_usec);
385 } else {
386 const char *asct = asctime(tm);
387 return talloc_asprintf(ctx, asct ? asct : "unknown");
389 #endif
393 char *current_timestring(TALLOC_CTX *ctx, bool hires)
395 struct timeval tv;
397 GetTimeOfDay(&tv);
398 return timeval_string(ctx, &tv, hires);
403 return a HTTP/1.0 time string
405 _PUBLIC_ char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
407 char *buf;
408 char tempTime[60];
409 struct tm *tm = localtime(&t);
411 if (t == TIME_T_MAX) {
412 return talloc_strdup(mem_ctx, "never");
415 if (!tm) {
416 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
419 #ifndef HAVE_STRFTIME
420 buf = talloc_strdup(mem_ctx, asctime(tm));
421 if (buf[strlen(buf)-1] == '\n') {
422 buf[strlen(buf)-1] = 0;
424 #else
425 strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
426 buf = talloc_strdup(mem_ctx, tempTime);
427 #endif /* !HAVE_STRFTIME */
429 return buf;
433 Return the date and time as a string
435 _PUBLIC_ char *timestring(TALLOC_CTX *mem_ctx, time_t t)
437 char *TimeBuf;
438 char tempTime[80];
439 struct tm *tm;
441 tm = localtime(&t);
442 if (!tm) {
443 return talloc_asprintf(mem_ctx,
444 "%ld seconds since the Epoch",
445 (long)t);
448 #ifdef HAVE_STRFTIME
449 /* Some versions of gcc complain about using some special format
450 * specifiers. This is a bug in gcc, not a bug in this code. See a
451 * recent strftime() manual page for details. */
452 strftime(tempTime,sizeof(tempTime)-1,"%a %b %e %X %Y %Z",tm);
453 TimeBuf = talloc_strdup(mem_ctx, tempTime);
454 #else
455 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
456 if (TimeBuf == NULL) {
457 return NULL;
459 if (TimeBuf[0] != '\0') {
460 size_t len = strlen(TimeBuf);
461 if (TimeBuf[len - 1] == '\n') {
462 TimeBuf[len - 1] = '\0';
465 #endif
467 return TimeBuf;
471 return a talloced string representing a NTTIME for human consumption
473 _PUBLIC_ const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
475 time_t t;
476 if (nt == 0) {
477 return "NTTIME(0)";
479 t = nt_time_to_unix(nt);
480 return timestring(mem_ctx, t);
485 put a NTTIME into a packet
487 _PUBLIC_ void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
489 SBVAL(base, offset, t);
493 pull a NTTIME from a packet
495 _PUBLIC_ NTTIME pull_nttime(uint8_t *base, uint16_t offset)
497 NTTIME ret = BVAL(base, offset);
498 return ret;
502 return (tv1 - tv2) in microseconds
504 _PUBLIC_ int64_t usec_time_diff(const struct timeval *tv1, const struct timeval *tv2)
506 int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
507 return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
511 return (tp1 - tp2) in microseconds
513 _PUBLIC_ int64_t nsec_time_diff(const struct timespec *tp1, const struct timespec *tp2)
515 int64_t sec_diff = tp1->tv_sec - tp2->tv_sec;
516 return (sec_diff * 1000000000) + (int64_t)(tp1->tv_nsec - tp2->tv_nsec);
521 return a zero timeval
523 _PUBLIC_ struct timeval timeval_zero(void)
525 struct timeval tv;
526 tv.tv_sec = 0;
527 tv.tv_usec = 0;
528 return tv;
532 return true if a timeval is zero
534 _PUBLIC_ bool timeval_is_zero(const struct timeval *tv)
536 return tv->tv_sec == 0 && tv->tv_usec == 0;
540 return a timeval for the current time
542 _PUBLIC_ struct timeval timeval_current(void)
544 struct timeval tv;
545 GetTimeOfDay(&tv);
546 return tv;
550 return a timeval struct with the given elements
552 _PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs)
554 struct timeval tv;
555 tv.tv_sec = secs;
556 tv.tv_usec = usecs;
557 return tv;
562 return a timeval ofs microseconds after tv
564 _PUBLIC_ struct timeval timeval_add(const struct timeval *tv,
565 uint32_t secs, uint32_t usecs)
567 struct timeval tv2 = *tv;
568 const unsigned int million = 1000000;
569 tv2.tv_sec += secs;
570 tv2.tv_usec += usecs;
571 tv2.tv_sec += tv2.tv_usec / million;
572 tv2.tv_usec = tv2.tv_usec % million;
573 return tv2;
577 return the sum of two timeval structures
579 struct timeval timeval_sum(const struct timeval *tv1,
580 const struct timeval *tv2)
582 return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
586 return a timeval secs/usecs into the future
588 _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
590 struct timeval tv = timeval_current();
591 return timeval_add(&tv, secs, usecs);
595 return a timeval milliseconds into the future
597 _PUBLIC_ struct timeval timeval_current_ofs_msec(uint32_t msecs)
599 struct timeval tv = timeval_current();
600 return timeval_add(&tv, msecs / 1000, (msecs % 1000) * 1000);
604 return a timeval microseconds into the future
606 _PUBLIC_ struct timeval timeval_current_ofs_usec(uint32_t usecs)
608 struct timeval tv = timeval_current();
609 return timeval_add(&tv, usecs / 1000000, usecs % 1000000);
613 compare two timeval structures.
614 Return -1 if tv1 < tv2
615 Return 0 if tv1 == tv2
616 Return 1 if tv1 > tv2
618 _PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
620 if (tv1->tv_sec > tv2->tv_sec) return 1;
621 if (tv1->tv_sec < tv2->tv_sec) return -1;
622 if (tv1->tv_usec > tv2->tv_usec) return 1;
623 if (tv1->tv_usec < tv2->tv_usec) return -1;
624 return 0;
628 return true if a timer is in the past
630 _PUBLIC_ bool timeval_expired(const struct timeval *tv)
632 struct timeval tv2 = timeval_current();
633 if (tv2.tv_sec > tv->tv_sec) return true;
634 if (tv2.tv_sec < tv->tv_sec) return false;
635 return (tv2.tv_usec >= tv->tv_usec);
639 return the number of seconds elapsed between two times
641 _PUBLIC_ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
643 return (tv2->tv_sec - tv1->tv_sec) +
644 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
648 return the number of seconds elapsed since a given time
650 _PUBLIC_ double timeval_elapsed(const struct timeval *tv)
652 struct timeval tv2 = timeval_current();
653 return timeval_elapsed2(tv, &tv2);
656 * return the number of seconds elapsed between two times
658 _PUBLIC_ double timespec_elapsed2(const struct timespec *ts1,
659 const struct timespec *ts2)
661 return (ts2->tv_sec - ts1->tv_sec) +
662 (ts2->tv_nsec - ts1->tv_nsec)*1.0e-9;
666 * return the number of seconds elapsed since a given time
668 _PUBLIC_ double timespec_elapsed(const struct timespec *ts)
670 struct timespec ts2 = timespec_current();
671 return timespec_elapsed2(ts, &ts2);
675 return the lesser of two timevals
677 _PUBLIC_ struct timeval timeval_min(const struct timeval *tv1,
678 const struct timeval *tv2)
680 if (tv1->tv_sec < tv2->tv_sec) return *tv1;
681 if (tv1->tv_sec > tv2->tv_sec) return *tv2;
682 if (tv1->tv_usec < tv2->tv_usec) return *tv1;
683 return *tv2;
687 return the greater of two timevals
689 _PUBLIC_ struct timeval timeval_max(const struct timeval *tv1,
690 const struct timeval *tv2)
692 if (tv1->tv_sec > tv2->tv_sec) return *tv1;
693 if (tv1->tv_sec < tv2->tv_sec) return *tv2;
694 if (tv1->tv_usec > tv2->tv_usec) return *tv1;
695 return *tv2;
699 return the difference between two timevals as a timeval
700 if tv1 comes after tv2, then return a zero timeval
701 (this is *tv2 - *tv1)
703 _PUBLIC_ struct timeval timeval_until(const struct timeval *tv1,
704 const struct timeval *tv2)
706 struct timeval t;
707 if (timeval_compare(tv1, tv2) >= 0) {
708 return timeval_zero();
710 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
711 if (tv1->tv_usec > tv2->tv_usec) {
712 t.tv_sec--;
713 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
714 } else {
715 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
717 return t;
722 convert a timeval to a NTTIME
724 _PUBLIC_ NTTIME timeval_to_nttime(const struct timeval *tv)
726 return 10*(tv->tv_usec +
727 ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
731 convert a NTTIME to a timeval
733 _PUBLIC_ void nttime_to_timeval(struct timeval *tv, NTTIME t)
735 if (tv == NULL) return;
737 t += 10/2;
738 t /= 10;
739 t -= TIME_FIXUP_CONSTANT*1000*1000;
741 tv->tv_sec = t / 1000000;
743 if (TIME_T_MIN > tv->tv_sec || tv->tv_sec > TIME_T_MAX) {
744 tv->tv_sec = 0;
745 tv->tv_usec = 0;
746 return;
749 tv->tv_usec = t - tv->tv_sec*1000000;
752 /*******************************************************************
753 yield the difference between *A and *B, in seconds, ignoring leap seconds
754 ********************************************************************/
755 static int tm_diff(struct tm *a, struct tm *b)
757 int ay = a->tm_year + (1900 - 1);
758 int by = b->tm_year + (1900 - 1);
759 int intervening_leap_days =
760 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
761 int years = ay - by;
762 int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
763 int hours = 24*days + (a->tm_hour - b->tm_hour);
764 int minutes = 60*hours + (a->tm_min - b->tm_min);
765 int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
767 return seconds;
772 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
774 _PUBLIC_ int get_time_zone(time_t t)
776 struct tm *tm = gmtime(&t);
777 struct tm tm_utc;
778 if (!tm)
779 return 0;
780 tm_utc = *tm;
781 tm = localtime(&t);
782 if (!tm)
783 return 0;
784 return tm_diff(&tm_utc,tm);
787 struct timespec nt_time_to_unix_timespec(NTTIME *nt)
789 int64_t d;
790 struct timespec ret;
792 if (*nt == 0 || *nt == (int64_t)-1) {
793 ret.tv_sec = 0;
794 ret.tv_nsec = 0;
795 return ret;
798 d = (int64_t)*nt;
799 /* d is now in 100ns units, since jan 1st 1601".
800 Save off the ns fraction. */
803 * Take the last seven decimal digits and multiply by 100.
804 * to convert from 100ns units to 1ns units.
806 ret.tv_nsec = (long) ((d % (1000 * 1000 * 10)) * 100);
808 /* Convert to seconds */
809 d /= 1000*1000*10;
811 /* Now adjust by 369 years to make the secs since 1970 */
812 d -= TIME_FIXUP_CONSTANT_INT;
814 if (d <= (int64_t)TIME_T_MIN) {
815 ret.tv_sec = TIME_T_MIN;
816 ret.tv_nsec = 0;
817 return ret;
820 if (d >= (int64_t)TIME_T_MAX) {
821 ret.tv_sec = TIME_T_MAX;
822 ret.tv_nsec = 0;
823 return ret;
826 ret.tv_sec = (time_t)d;
827 return ret;
832 check if 2 NTTIMEs are equal.
834 bool nt_time_equal(NTTIME *t1, NTTIME *t2)
836 return *t1 == *t2;
840 Check if it's a null timespec.
843 bool null_timespec(struct timespec ts)
845 return ts.tv_sec == 0 ||
846 ts.tv_sec == (time_t)0xFFFFFFFF ||
847 ts.tv_sec == (time_t)-1;
850 /****************************************************************************
851 Convert a normalized timeval to a timespec.
852 ****************************************************************************/
854 struct timespec convert_timeval_to_timespec(const struct timeval tv)
856 struct timespec ts;
857 ts.tv_sec = tv.tv_sec;
858 ts.tv_nsec = tv.tv_usec * 1000;
859 return ts;
862 /****************************************************************************
863 Convert a normalized timespec to a timeval.
864 ****************************************************************************/
866 struct timeval convert_timespec_to_timeval(const struct timespec ts)
868 struct timeval tv;
869 tv.tv_sec = ts.tv_sec;
870 tv.tv_usec = ts.tv_nsec / 1000;
871 return tv;
874 /****************************************************************************
875 Return a timespec for the current time
876 ****************************************************************************/
878 _PUBLIC_ struct timespec timespec_current(void)
880 struct timespec ts;
881 clock_gettime(CLOCK_REALTIME, &ts);
882 return ts;
885 /****************************************************************************
886 Return the lesser of two timespecs.
887 ****************************************************************************/
889 struct timespec timespec_min(const struct timespec *ts1,
890 const struct timespec *ts2)
892 if (ts1->tv_sec < ts2->tv_sec) return *ts1;
893 if (ts1->tv_sec > ts2->tv_sec) return *ts2;
894 if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
895 return *ts2;
898 /****************************************************************************
899 compare two timespec structures.
900 Return -1 if ts1 < ts2
901 Return 0 if ts1 == ts2
902 Return 1 if ts1 > ts2
903 ****************************************************************************/
905 _PUBLIC_ int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
907 if (ts1->tv_sec > ts2->tv_sec) return 1;
908 if (ts1->tv_sec < ts2->tv_sec) return -1;
909 if (ts1->tv_nsec > ts2->tv_nsec) return 1;
910 if (ts1->tv_nsec < ts2->tv_nsec) return -1;
911 return 0;
914 /****************************************************************************
915 Round up a timespec if nsec > 500000000, round down if lower,
916 then zero nsec.
917 ****************************************************************************/
919 void round_timespec_to_sec(struct timespec *ts)
921 ts->tv_sec = convert_timespec_to_time_t(*ts);
922 ts->tv_nsec = 0;
925 /****************************************************************************
926 Round a timespec to usec value.
927 ****************************************************************************/
929 void round_timespec_to_usec(struct timespec *ts)
931 struct timeval tv = convert_timespec_to_timeval(*ts);
932 *ts = convert_timeval_to_timespec(tv);
933 while (ts->tv_nsec > 1000000000) {
934 ts->tv_sec += 1;
935 ts->tv_nsec -= 1000000000;
939 /****************************************************************************
940 Put a 8 byte filetime from a struct timespec. Uses GMT.
941 ****************************************************************************/
943 _PUBLIC_ void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
945 uint64_t d;
947 if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
948 *nt = 0;
949 return;
951 if (ts.tv_sec == TIME_T_MAX) {
952 *nt = 0x7fffffffffffffffLL;
953 return;
955 if (ts.tv_sec == (time_t)-1) {
956 *nt = (uint64_t)-1;
957 return;
960 d = ts.tv_sec;
961 d += TIME_FIXUP_CONSTANT_INT;
962 d *= 1000*1000*10;
963 /* d is now in 100ns units. */
964 d += (ts.tv_nsec / 100);
966 *nt = d;