source: vendor/current/lib/util/time.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 21.3 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 time handling functions
4
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
9
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.
14
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.
19
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/>.
22*/
23
24#include "replace.h"
25#include "system/time.h"
26#include "byteorder.h"
27#include "time_basic.h"
28#include "lib/util/time.h" /* Avoid /usr/include/time.h */
29
30/**
31 * @file
32 * @brief time handling functions
33 */
34
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
39#endif
40
41
42
43/**
44 External access to time_t_min and time_t_max.
45**/
46_PUBLIC_ time_t get_time_t_max(void)
47{
48 return TIME_T_MAX;
49}
50
51/**
52a wrapper to preferably get the monotonic time
53**/
54_PUBLIC_ void clock_gettime_mono(struct timespec *tp)
55{
56/* prefer a suspend aware monotonic CLOCK_BOOTTIME: */
57#ifdef CLOCK_BOOTTIME
58 if (clock_gettime(CLOCK_BOOTTIME,tp) == 0) {
59 return;
60 }
61#endif
62/* then try the monotonic clock: */
63#if CUSTOM_CLOCK_MONOTONIC != CLOCK_REALTIME
64 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC,tp) == 0) {
65 return;
66 }
67#endif
68 clock_gettime(CLOCK_REALTIME,tp);
69}
70
71/**
72a wrapper to preferably get the monotonic time in seconds
73**/
74_PUBLIC_ time_t time_mono(time_t *t)
75{
76 struct timespec tp;
77
78 clock_gettime_mono(&tp);
79 if (t != NULL) {
80 *t = tp.tv_sec;
81 }
82 return tp.tv_sec;
83}
84
85
86#define TIME_FIXUP_CONSTANT 11644473600LL
87
88time_t convert_timespec_to_time_t(struct timespec ts)
89{
90 /* Ensure tv_nsec is less than 1sec. */
91 while (ts.tv_nsec > 1000000000) {
92 ts.tv_sec += 1;
93 ts.tv_nsec -= 1000000000;
94 }
95
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. */
98
99 if (ts.tv_nsec > 500000000) {
100 return ts.tv_sec + 1;
101 }
102 return ts.tv_sec;
103}
104
105struct timespec convert_time_t_to_timespec(time_t t)
106{
107 struct timespec ts;
108 ts.tv_sec = t;
109 ts.tv_nsec = 0;
110 return ts;
111}
112
113
114
115/**
116 Interpret an 8 byte "filetime" structure to a time_t
117 It's originally in "100ns units since jan 1st 1601"
118
119 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
120
121 tv_sec = 0
122 tv_nsec = 0;
123
124 Returns GMT.
125**/
126time_t nt_time_to_unix(NTTIME nt)
127{
128 return convert_timespec_to_time_t(nt_time_to_unix_timespec(nt));
129}
130
131
132/**
133put a 8 byte filetime from a time_t
134This takes GMT as input
135**/
136_PUBLIC_ void unix_to_nt_time(NTTIME *nt, time_t t)
137{
138 uint64_t t2;
139
140 if (t == (time_t)-1) {
141 *nt = (NTTIME)-1LL;
142 return;
143 }
144
145 if (t == TIME_T_MAX || t == INT64_MAX) {
146 *nt = 0x7fffffffffffffffLL;
147 return;
148 }
149
150 if (t == 0) {
151 *nt = 0;
152 return;
153 }
154
155 t2 = t;
156 t2 += TIME_FIXUP_CONSTANT_INT;
157 t2 *= 1000*1000*10;
158
159 *nt = t2;
160}
161
162
163/**
164check if it's a null unix time
165**/
166_PUBLIC_ bool null_time(time_t t)
167{
168 return t == 0 ||
169 t == (time_t)0xFFFFFFFF ||
170 t == (time_t)-1;
171}
172
173
174/**
175check if it's a null NTTIME
176**/
177_PUBLIC_ bool null_nttime(NTTIME t)
178{
179 return t == 0 || t == (NTTIME)-1;
180}
181
182/*******************************************************************
183 create a 16 bit dos packed date
184********************************************************************/
185static uint16_t make_dos_date1(struct tm *t)
186{
187 uint16_t ret=0;
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));
190 return ret;
191}
192
193/*******************************************************************
194 create a 16 bit dos packed time
195********************************************************************/
196static uint16_t make_dos_time1(struct tm *t)
197{
198 uint16_t ret=0;
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));
201 return ret;
202}
203
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********************************************************************/
208static uint32_t make_dos_date(time_t unixdate, int zone_offset)
209{
210 struct tm *t;
211 uint32_t ret=0;
212
213 if (unixdate == 0) {
214 return 0;
215 }
216
217 unixdate -= zone_offset;
218
219 t = gmtime(&unixdate);
220 if (!t) {
221 return 0xFFFFFFFF;
222 }
223
224 ret = make_dos_date1(t);
225 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
226
227 return ret;
228}
229
230/**
231put a dos date into a buffer (time/date format)
232This takes GMT time and puts local time in the buffer
233**/
234_PUBLIC_ void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
235{
236 uint32_t x = make_dos_date(unixdate, zone_offset);
237 SIVAL(buf,offset,x);
238}
239
240/**
241put a dos date into a buffer (date/time format)
242This takes GMT time and puts local time in the buffer
243**/
244_PUBLIC_ void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
245{
246 uint32_t x;
247 x = make_dos_date(unixdate, zone_offset);
248 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
249 SIVAL(buf,offset,x);
250}
251
252/**
253put a dos 32 bit "unix like" date into a buffer. This routine takes
254GMT and converts it to LOCAL time before putting it (most SMBs assume
255localtime for this sort of date)
256**/
257_PUBLIC_ void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
258{
259 if (!null_time(unixdate)) {
260 unixdate -= zone_offset;
261 }
262 SIVAL(buf,offset,unixdate);
263}
264
265/*******************************************************************
266 interpret a 32 bit dos packed date/time to some parameters
267********************************************************************/
268void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
269{
270 uint32_t p0,p1,p2,p3;
271
272 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
273 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
274
275 *second = 2*(p0 & 0x1F);
276 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
277 *hour = (p1>>3)&0xFF;
278 *day = (p2&0x1F);
279 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
280 *year = ((p3>>1)&0xFF) + 80;
281}
282
283/**
284 create a unix date (int GMT) from a dos date (which is actually in
285 localtime)
286**/
287_PUBLIC_ time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
288{
289 uint32_t dos_date=0;
290 struct tm t;
291 time_t ret;
292
293 dos_date = IVAL(date_ptr,0);
294
295 if (dos_date == 0) return (time_t)0;
296
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);
299 t.tm_isdst = -1;
300
301 ret = timegm(&t);
302
303 ret += zone_offset;
304
305 return ret;
306}
307
308/**
309like make_unix_date() but the words are reversed
310**/
311_PUBLIC_ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
312{
313 uint32_t x,x2;
314
315 x = IVAL(date_ptr,0);
316 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
317 SIVAL(&x,0,x2);
318
319 return pull_dos_date((const uint8_t *)&x, zone_offset);
320}
321
322/**
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
325**/
326_PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
327{
328 time_t t = (time_t)IVAL(date_ptr,0);
329 if (!null_time(t)) {
330 t += zone_offset;
331 }
332 return t;
333}
334
335/****************************************************************************
336 Return the date and time as a string
337****************************************************************************/
338
339char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
340{
341 struct timeval_buf tmp;
342 char *result;
343
344 result = talloc_strdup(ctx, timeval_str_buf(tp, false, hires, &tmp));
345 if (result == NULL) {
346 return NULL;
347 }
348
349 /*
350 * beautify the talloc_report output
351 *
352 * This is not just cosmetics. A C compiler might in theory make the
353 * talloc_strdup call above a tail call with the tail call
354 * optimization. This would render "tmp" invalid while talloc_strdup
355 * tries to duplicate it. The talloc_set_name_const call below puts
356 * the talloc_strdup call into non-tail position.
357 */
358 talloc_set_name_const(result, result);
359 return result;
360}
361
362char *current_timestring(TALLOC_CTX *ctx, bool hires)
363{
364 struct timeval tv;
365
366 GetTimeOfDay(&tv);
367 return timeval_string(ctx, &tv, hires);
368}
369
370
371/**
372return a HTTP/1.0 time string
373**/
374_PUBLIC_ char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
375{
376 char *buf;
377 char tempTime[60];
378 struct tm *tm = localtime(&t);
379
380 if (t == TIME_T_MAX) {
381 return talloc_strdup(mem_ctx, "never");
382 }
383
384 if (!tm) {
385 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
386 }
387
388#ifndef HAVE_STRFTIME
389 buf = talloc_strdup(mem_ctx, asctime(tm));
390 if (buf[strlen(buf)-1] == '\n') {
391 buf[strlen(buf)-1] = 0;
392 }
393#else
394 strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
395 buf = talloc_strdup(mem_ctx, tempTime);
396#endif /* !HAVE_STRFTIME */
397
398 return buf;
399}
400
401/**
402 Return the date and time as a string
403**/
404_PUBLIC_ char *timestring(TALLOC_CTX *mem_ctx, time_t t)
405{
406 char *TimeBuf;
407 char tempTime[80];
408 struct tm *tm;
409
410 tm = localtime(&t);
411 if (!tm) {
412 return talloc_asprintf(mem_ctx,
413 "%ld seconds since the Epoch",
414 (long)t);
415 }
416
417#ifdef HAVE_STRFTIME
418 /* Some versions of gcc complain about using some special format
419 * specifiers. This is a bug in gcc, not a bug in this code. See a
420 * recent strftime() manual page for details. */
421 strftime(tempTime,sizeof(tempTime)-1,"%a %b %e %X %Y %Z",tm);
422 TimeBuf = talloc_strdup(mem_ctx, tempTime);
423#else
424 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
425 if (TimeBuf == NULL) {
426 return NULL;
427 }
428 if (TimeBuf[0] != '\0') {
429 size_t len = strlen(TimeBuf);
430 if (TimeBuf[len - 1] == '\n') {
431 TimeBuf[len - 1] = '\0';
432 }
433 }
434#endif
435
436 return TimeBuf;
437}
438
439/**
440 return a talloced string representing a NTTIME for human consumption
441*/
442_PUBLIC_ const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
443{
444 time_t t;
445 if (nt == 0) {
446 return "NTTIME(0)";
447 }
448 t = nt_time_to_unix(nt);
449 return timestring(mem_ctx, t);
450}
451
452
453/**
454 put a NTTIME into a packet
455*/
456_PUBLIC_ void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
457{
458 SBVAL(base, offset, t);
459}
460
461/**
462 pull a NTTIME from a packet
463*/
464_PUBLIC_ NTTIME pull_nttime(uint8_t *base, uint16_t offset)
465{
466 NTTIME ret = BVAL(base, offset);
467 return ret;
468}
469
470/**
471 return (tv1 - tv2) in microseconds
472*/
473_PUBLIC_ int64_t usec_time_diff(const struct timeval *tv1, const struct timeval *tv2)
474{
475 int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
476 return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
477}
478
479/**
480 return (tp1 - tp2) in microseconds
481*/
482_PUBLIC_ int64_t nsec_time_diff(const struct timespec *tp1, const struct timespec *tp2)
483{
484 int64_t sec_diff = tp1->tv_sec - tp2->tv_sec;
485 return (sec_diff * 1000000000) + (int64_t)(tp1->tv_nsec - tp2->tv_nsec);
486}
487
488
489/**
490 return a zero timeval
491*/
492_PUBLIC_ struct timeval timeval_zero(void)
493{
494 struct timeval tv;
495 tv.tv_sec = 0;
496 tv.tv_usec = 0;
497 return tv;
498}
499
500/**
501 return true if a timeval is zero
502*/
503_PUBLIC_ bool timeval_is_zero(const struct timeval *tv)
504{
505 return tv->tv_sec == 0 && tv->tv_usec == 0;
506}
507
508/**
509 return a timeval for the current time
510*/
511_PUBLIC_ struct timeval timeval_current(void)
512{
513 struct timeval tv;
514 GetTimeOfDay(&tv);
515 return tv;
516}
517
518/**
519 return a timeval struct with the given elements
520*/
521_PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs)
522{
523 struct timeval tv;
524 tv.tv_sec = secs;
525 tv.tv_usec = usecs;
526 return tv;
527}
528
529
530/**
531 return a timeval ofs microseconds after tv
532*/
533_PUBLIC_ struct timeval timeval_add(const struct timeval *tv,
534 uint32_t secs, uint32_t usecs)
535{
536 struct timeval tv2 = *tv;
537 const unsigned int million = 1000000;
538 tv2.tv_sec += secs;
539 tv2.tv_usec += usecs;
540 tv2.tv_sec += tv2.tv_usec / million;
541 tv2.tv_usec = tv2.tv_usec % million;
542 return tv2;
543}
544
545/**
546 return the sum of two timeval structures
547*/
548struct timeval timeval_sum(const struct timeval *tv1,
549 const struct timeval *tv2)
550{
551 return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
552}
553
554/**
555 return a timeval secs/usecs into the future
556*/
557_PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
558{
559 struct timeval tv = timeval_current();
560 return timeval_add(&tv, secs, usecs);
561}
562
563/**
564 return a timeval milliseconds into the future
565*/
566_PUBLIC_ struct timeval timeval_current_ofs_msec(uint32_t msecs)
567{
568 struct timeval tv = timeval_current();
569 return timeval_add(&tv, msecs / 1000, (msecs % 1000) * 1000);
570}
571
572/**
573 return a timeval microseconds into the future
574*/
575_PUBLIC_ struct timeval timeval_current_ofs_usec(uint32_t usecs)
576{
577 struct timeval tv = timeval_current();
578 return timeval_add(&tv, usecs / 1000000, usecs % 1000000);
579}
580
581/**
582 compare two timeval structures.
583 Return -1 if tv1 < tv2
584 Return 0 if tv1 == tv2
585 Return 1 if tv1 > tv2
586*/
587_PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
588{
589 if (tv1->tv_sec > tv2->tv_sec) return 1;
590 if (tv1->tv_sec < tv2->tv_sec) return -1;
591 if (tv1->tv_usec > tv2->tv_usec) return 1;
592 if (tv1->tv_usec < tv2->tv_usec) return -1;
593 return 0;
594}
595
596/**
597 return true if a timer is in the past
598*/
599_PUBLIC_ bool timeval_expired(const struct timeval *tv)
600{
601 struct timeval tv2 = timeval_current();
602 if (tv2.tv_sec > tv->tv_sec) return true;
603 if (tv2.tv_sec < tv->tv_sec) return false;
604 return (tv2.tv_usec >= tv->tv_usec);
605}
606
607/**
608 return the number of seconds elapsed between two times
609*/
610_PUBLIC_ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
611{
612 return (tv2->tv_sec - tv1->tv_sec) +
613 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
614}
615
616/**
617 return the number of seconds elapsed since a given time
618*/
619_PUBLIC_ double timeval_elapsed(const struct timeval *tv)
620{
621 struct timeval tv2 = timeval_current();
622 return timeval_elapsed2(tv, &tv2);
623}
624/**
625 * return the number of seconds elapsed between two times
626 **/
627_PUBLIC_ double timespec_elapsed2(const struct timespec *ts1,
628 const struct timespec *ts2)
629{
630 return (ts2->tv_sec - ts1->tv_sec) +
631 (ts2->tv_nsec - ts1->tv_nsec)*1.0e-9;
632}
633
634/**
635 * return the number of seconds elapsed since a given time
636 */
637_PUBLIC_ double timespec_elapsed(const struct timespec *ts)
638{
639 struct timespec ts2 = timespec_current();
640 return timespec_elapsed2(ts, &ts2);
641}
642
643/**
644 return the lesser of two timevals
645*/
646_PUBLIC_ struct timeval timeval_min(const struct timeval *tv1,
647 const struct timeval *tv2)
648{
649 if (tv1->tv_sec < tv2->tv_sec) return *tv1;
650 if (tv1->tv_sec > tv2->tv_sec) return *tv2;
651 if (tv1->tv_usec < tv2->tv_usec) return *tv1;
652 return *tv2;
653}
654
655/**
656 return the greater of two timevals
657*/
658_PUBLIC_ struct timeval timeval_max(const struct timeval *tv1,
659 const struct timeval *tv2)
660{
661 if (tv1->tv_sec > tv2->tv_sec) return *tv1;
662 if (tv1->tv_sec < tv2->tv_sec) return *tv2;
663 if (tv1->tv_usec > tv2->tv_usec) return *tv1;
664 return *tv2;
665}
666
667/**
668 return the difference between two timevals as a timeval
669 if tv1 comes after tv2, then return a zero timeval
670 (this is *tv2 - *tv1)
671*/
672_PUBLIC_ struct timeval timeval_until(const struct timeval *tv1,
673 const struct timeval *tv2)
674{
675 struct timeval t;
676 if (timeval_compare(tv1, tv2) >= 0) {
677 return timeval_zero();
678 }
679 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
680 if (tv1->tv_usec > tv2->tv_usec) {
681 t.tv_sec--;
682 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
683 } else {
684 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
685 }
686 return t;
687}
688
689
690/**
691 convert a timeval to a NTTIME
692*/
693_PUBLIC_ NTTIME timeval_to_nttime(const struct timeval *tv)
694{
695 return 10*(tv->tv_usec +
696 ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
697}
698
699/**
700 convert a NTTIME to a timeval
701*/
702_PUBLIC_ void nttime_to_timeval(struct timeval *tv, NTTIME t)
703{
704 if (tv == NULL) return;
705
706 t += 10/2;
707 t /= 10;
708 t -= TIME_FIXUP_CONSTANT*1000*1000;
709
710 tv->tv_sec = t / 1000000;
711
712 if (TIME_T_MIN > tv->tv_sec || tv->tv_sec > TIME_T_MAX) {
713 tv->tv_sec = 0;
714 tv->tv_usec = 0;
715 return;
716 }
717
718 tv->tv_usec = t - tv->tv_sec*1000000;
719}
720
721/*******************************************************************
722yield the difference between *A and *B, in seconds, ignoring leap seconds
723********************************************************************/
724static int tm_diff(struct tm *a, struct tm *b)
725{
726 int ay = a->tm_year + (1900 - 1);
727 int by = b->tm_year + (1900 - 1);
728 int intervening_leap_days =
729 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
730 int years = ay - by;
731 int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
732 int hours = 24*days + (a->tm_hour - b->tm_hour);
733 int minutes = 60*hours + (a->tm_min - b->tm_min);
734 int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
735
736 return seconds;
737}
738
739
740/**
741 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
742 */
743_PUBLIC_ int get_time_zone(time_t t)
744{
745 struct tm *tm = gmtime(&t);
746 struct tm tm_utc;
747 if (!tm)
748 return 0;
749 tm_utc = *tm;
750 tm = localtime(&t);
751 if (!tm)
752 return 0;
753 return tm_diff(&tm_utc,tm);
754}
755
756struct timespec nt_time_to_unix_timespec(NTTIME nt)
757{
758 int64_t d;
759 struct timespec ret;
760
761 if (nt == 0 || nt == (int64_t)-1) {
762 ret.tv_sec = 0;
763 ret.tv_nsec = 0;
764 return ret;
765 }
766
767 d = (int64_t)nt;
768 /* d is now in 100ns units, since jan 1st 1601".
769 Save off the ns fraction. */
770
771 /*
772 * Take the last seven decimal digits and multiply by 100.
773 * to convert from 100ns units to 1ns units.
774 */
775 ret.tv_nsec = (long) ((d % (1000 * 1000 * 10)) * 100);
776
777 /* Convert to seconds */
778 d /= 1000*1000*10;
779
780 /* Now adjust by 369 years to make the secs since 1970 */
781 d -= TIME_FIXUP_CONSTANT_INT;
782
783 if (d <= (int64_t)TIME_T_MIN) {
784 ret.tv_sec = TIME_T_MIN;
785 ret.tv_nsec = 0;
786 return ret;
787 }
788
789 if (d >= (int64_t)TIME_T_MAX) {
790 ret.tv_sec = TIME_T_MAX;
791 ret.tv_nsec = 0;
792 return ret;
793 }
794
795 ret.tv_sec = (time_t)d;
796 return ret;
797}
798
799
800/**
801 check if 2 NTTIMEs are equal.
802*/
803bool nt_time_equal(NTTIME *t1, NTTIME *t2)
804{
805 return *t1 == *t2;
806}
807
808/**
809 Check if it's a null timespec.
810**/
811
812bool null_timespec(struct timespec ts)
813{
814 return ts.tv_sec == 0 ||
815 ts.tv_sec == (time_t)0xFFFFFFFF ||
816 ts.tv_sec == (time_t)-1;
817}
818
819/****************************************************************************
820 Convert a normalized timeval to a timespec.
821****************************************************************************/
822
823struct timespec convert_timeval_to_timespec(const struct timeval tv)
824{
825 struct timespec ts;
826 ts.tv_sec = tv.tv_sec;
827 ts.tv_nsec = tv.tv_usec * 1000;
828 return ts;
829}
830
831/****************************************************************************
832 Convert a normalized timespec to a timeval.
833****************************************************************************/
834
835struct timeval convert_timespec_to_timeval(const struct timespec ts)
836{
837 struct timeval tv;
838 tv.tv_sec = ts.tv_sec;
839 tv.tv_usec = ts.tv_nsec / 1000;
840 return tv;
841}
842
843/****************************************************************************
844 Return a timespec for the current time
845****************************************************************************/
846
847_PUBLIC_ struct timespec timespec_current(void)
848{
849 struct timespec ts;
850 clock_gettime(CLOCK_REALTIME, &ts);
851 return ts;
852}
853
854/****************************************************************************
855 Return the lesser of two timespecs.
856****************************************************************************/
857
858struct timespec timespec_min(const struct timespec *ts1,
859 const struct timespec *ts2)
860{
861 if (ts1->tv_sec < ts2->tv_sec) return *ts1;
862 if (ts1->tv_sec > ts2->tv_sec) return *ts2;
863 if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
864 return *ts2;
865}
866
867/****************************************************************************
868 compare two timespec structures.
869 Return -1 if ts1 < ts2
870 Return 0 if ts1 == ts2
871 Return 1 if ts1 > ts2
872****************************************************************************/
873
874_PUBLIC_ int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
875{
876 if (ts1->tv_sec > ts2->tv_sec) return 1;
877 if (ts1->tv_sec < ts2->tv_sec) return -1;
878 if (ts1->tv_nsec > ts2->tv_nsec) return 1;
879 if (ts1->tv_nsec < ts2->tv_nsec) return -1;
880 return 0;
881}
882
883/****************************************************************************
884 Round up a timespec if nsec > 500000000, round down if lower,
885 then zero nsec.
886****************************************************************************/
887
888void round_timespec_to_sec(struct timespec *ts)
889{
890 ts->tv_sec = convert_timespec_to_time_t(*ts);
891 ts->tv_nsec = 0;
892}
893
894/****************************************************************************
895 Round a timespec to usec value.
896****************************************************************************/
897
898void round_timespec_to_usec(struct timespec *ts)
899{
900 struct timeval tv = convert_timespec_to_timeval(*ts);
901 *ts = convert_timeval_to_timespec(tv);
902 while (ts->tv_nsec > 1000000000) {
903 ts->tv_sec += 1;
904 ts->tv_nsec -= 1000000000;
905 }
906}
907
908/****************************************************************************
909 Put a 8 byte filetime from a struct timespec. Uses GMT.
910****************************************************************************/
911
912_PUBLIC_ NTTIME unix_timespec_to_nt_time(struct timespec ts)
913{
914 uint64_t d;
915
916 if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
917 return 0;
918 }
919 if (ts.tv_sec == TIME_T_MAX) {
920 return 0x7fffffffffffffffLL;
921 }
922 if (ts.tv_sec == (time_t)-1) {
923 return (uint64_t)-1;
924 }
925
926 d = ts.tv_sec;
927 d += TIME_FIXUP_CONSTANT_INT;
928 d *= 1000*1000*10;
929 /* d is now in 100ns units. */
930 d += (ts.tv_nsec / 100);
931
932 return d;
933}
Note: See TracBrowser for help on using the repository browser.