2 Unix SMB/CIFS implementation.
3 time handling functions
5 Copyright (C) Andrew Tridgell 1992-2004
6 Copyright (C) Stefan (metze) Metzmacher 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/time.h"
27 * @brief time handling functions
31 /* we use 0 here, because (time_t)-1 means error */
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 * we use the INT32_MAX here as on 64 bit systems,
45 * gmtime() fails with INT64_MAX
49 #define TIME_T_MAX MIN(INT32_MAX,_TYPE_MAXIMUM(time_t))
53 External access to time_t_min and time_t_max.
55 _PUBLIC_
time_t get_time_t_max(void)
61 a gettimeofday wrapper
63 _PUBLIC_
void GetTimeOfDay(struct timeval
*tval
)
65 #ifdef HAVE_GETTIMEOFDAY_TZ
66 gettimeofday(tval
,NULL
);
73 #define TIME_FIXUP_CONSTANT 11644473600LL
75 time_t convert_timespec_to_time_t(struct timespec ts
)
77 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
78 increment if it's greater than 500 millionth of a second. */
79 if (ts
.tv_nsec
> 500000000) {
85 struct timespec
convert_time_t_to_timespec(time_t t
)
96 Interpret an 8 byte "filetime" structure to a time_t
97 It's originally in "100ns units since jan 1st 1601"
99 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
106 time_t nt_time_to_unix(NTTIME nt
)
108 return convert_timespec_to_time_t(nt_time_to_unix_timespec(&nt
));
113 put a 8 byte filetime from a time_t
114 This takes GMT as input
116 _PUBLIC_
void unix_to_nt_time(NTTIME
*nt
, time_t t
)
120 if (t
== (time_t)-1) {
125 if (t
== TIME_T_MAX
) {
126 *nt
= 0x7fffffffffffffffLL
;
136 t2
+= TIME_FIXUP_CONSTANT_INT
;
144 check if it's a null unix time
146 _PUBLIC_
bool null_time(time_t t
)
149 t
== (time_t)0xFFFFFFFF ||
155 check if it's a null NTTIME
157 _PUBLIC_
bool null_nttime(NTTIME t
)
159 return t
== 0 || t
== (NTTIME
)-1;
162 /*******************************************************************
163 create a 16 bit dos packed date
164 ********************************************************************/
165 static uint16_t make_dos_date1(struct tm
*t
)
168 ret
= (((unsigned int)(t
->tm_mon
+1)) >> 3) | ((t
->tm_year
-80) << 1);
169 ret
= ((ret
&0xFF)<<8) | (t
->tm_mday
| (((t
->tm_mon
+1) & 0x7) << 5));
173 /*******************************************************************
174 create a 16 bit dos packed time
175 ********************************************************************/
176 static uint16_t make_dos_time1(struct tm
*t
)
179 ret
= ((((unsigned int)t
->tm_min
>> 3)&0x7) | (((unsigned int)t
->tm_hour
) << 3));
180 ret
= ((ret
&0xFF)<<8) | ((t
->tm_sec
/2) | ((t
->tm_min
& 0x7) << 5));
184 /*******************************************************************
185 create a 32 bit dos packed date/time from some parameters
186 This takes a GMT time and returns a packed localtime structure
187 ********************************************************************/
188 static uint32_t make_dos_date(time_t unixdate
, int zone_offset
)
197 unixdate
-= zone_offset
;
199 t
= gmtime(&unixdate
);
204 ret
= make_dos_date1(t
);
205 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
211 put a dos date into a buffer (time/date format)
212 This takes GMT time and puts local time in the buffer
214 _PUBLIC_
void push_dos_date(uint8_t *buf
, int offset
, time_t unixdate
, int zone_offset
)
216 uint32_t x
= make_dos_date(unixdate
, zone_offset
);
221 put a dos date into a buffer (date/time format)
222 This takes GMT time and puts local time in the buffer
224 _PUBLIC_
void push_dos_date2(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
227 x
= make_dos_date(unixdate
, zone_offset
);
228 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
233 put a dos 32 bit "unix like" date into a buffer. This routine takes
234 GMT and converts it to LOCAL time before putting it (most SMBs assume
235 localtime for this sort of date)
237 _PUBLIC_
void push_dos_date3(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
239 if (!null_time(unixdate
)) {
240 unixdate
-= zone_offset
;
242 SIVAL(buf
,offset
,unixdate
);
245 /*******************************************************************
246 interpret a 32 bit dos packed date/time to some parameters
247 ********************************************************************/
248 void interpret_dos_date(uint32_t date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
250 uint32_t p0
,p1
,p2
,p3
;
252 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
253 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
255 *second
= 2*(p0
& 0x1F);
256 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
257 *hour
= (p1
>>3)&0xFF;
259 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
260 *year
= ((p3
>>1)&0xFF) + 80;
264 create a unix date (int GMT) from a dos date (which is actually in
267 _PUBLIC_
time_t pull_dos_date(const uint8_t *date_ptr
, int zone_offset
)
273 dos_date
= IVAL(date_ptr
,0);
275 if (dos_date
== 0) return (time_t)0;
277 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
278 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
289 like make_unix_date() but the words are reversed
291 _PUBLIC_
time_t pull_dos_date2(const uint8_t *date_ptr
, int zone_offset
)
295 x
= IVAL(date_ptr
,0);
296 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
299 return pull_dos_date((const uint8_t *)&x
, zone_offset
);
303 create a unix GMT date from a dos date in 32 bit "unix like" format
304 these generally arrive as localtimes, with corresponding DST
306 _PUBLIC_
time_t pull_dos_date3(const uint8_t *date_ptr
, int zone_offset
)
308 time_t t
= (time_t)IVAL(date_ptr
,0);
317 return a HTTP/1.0 time string
319 _PUBLIC_
char *http_timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
323 struct tm
*tm
= localtime(&t
);
325 if (t
== TIME_T_MAX
) {
326 return talloc_strdup(mem_ctx
, "never");
330 return talloc_asprintf(mem_ctx
,"%ld seconds since the Epoch",(long)t
);
333 #ifndef HAVE_STRFTIME
334 buf
= talloc_strdup(mem_ctx
, asctime(tm
));
335 if (buf
[strlen(buf
)-1] == '\n') {
336 buf
[strlen(buf
)-1] = 0;
339 strftime(tempTime
, sizeof(tempTime
)-1, "%a, %d %b %Y %H:%M:%S %Z", tm
);
340 buf
= talloc_strdup(mem_ctx
, tempTime
);
341 #endif /* !HAVE_STRFTIME */
347 Return the date and time as a string
349 _PUBLIC_
char *timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
357 return talloc_asprintf(mem_ctx
,
358 "%ld seconds since the Epoch",
363 /* some versions of gcc complain about using %c. This is a bug
364 in the gcc warning, not a bug in this code. See a recent
365 strftime() manual page for details.
367 strftime(tempTime
,sizeof(tempTime
)-1,"%c %Z",tm
);
368 TimeBuf
= talloc_strdup(mem_ctx
, tempTime
);
370 TimeBuf
= talloc_strdup(mem_ctx
, asctime(tm
));
377 return a talloced string representing a NTTIME for human consumption
379 _PUBLIC_
const char *nt_time_string(TALLOC_CTX
*mem_ctx
, NTTIME nt
)
385 t
= nt_time_to_unix(nt
);
386 return timestring(mem_ctx
, t
);
391 put a NTTIME into a packet
393 _PUBLIC_
void push_nttime(uint8_t *base
, uint16_t offset
, NTTIME t
)
395 SBVAL(base
, offset
, t
);
399 pull a NTTIME from a packet
401 _PUBLIC_ NTTIME
pull_nttime(uint8_t *base
, uint16_t offset
)
403 NTTIME ret
= BVAL(base
, offset
);
408 return (tv1 - tv2) in microseconds
410 _PUBLIC_
int64_t usec_time_diff(const struct timeval
*tv1
, const struct timeval
*tv2
)
412 int64_t sec_diff
= tv1
->tv_sec
- tv2
->tv_sec
;
413 return (sec_diff
* 1000000) + (int64_t)(tv1
->tv_usec
- tv2
->tv_usec
);
418 return a zero timeval
420 _PUBLIC_
struct timeval
timeval_zero(void)
429 return true if a timeval is zero
431 _PUBLIC_
bool timeval_is_zero(const struct timeval
*tv
)
433 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
437 return a timeval for the current time
439 _PUBLIC_
struct timeval
timeval_current(void)
447 return a timeval struct with the given elements
449 _PUBLIC_
struct timeval
timeval_set(uint32_t secs
, uint32_t usecs
)
459 return a timeval ofs microseconds after tv
461 _PUBLIC_
struct timeval
timeval_add(const struct timeval
*tv
,
462 uint32_t secs
, uint32_t usecs
)
464 struct timeval tv2
= *tv
;
465 const unsigned int million
= 1000000;
467 tv2
.tv_usec
+= usecs
;
468 tv2
.tv_sec
+= tv2
.tv_usec
/ million
;
469 tv2
.tv_usec
= tv2
.tv_usec
% million
;
474 return the sum of two timeval structures
476 struct timeval
timeval_sum(const struct timeval
*tv1
,
477 const struct timeval
*tv2
)
479 return timeval_add(tv1
, tv2
->tv_sec
, tv2
->tv_usec
);
483 return a timeval secs/usecs into the future
485 _PUBLIC_
struct timeval
timeval_current_ofs(uint32_t secs
, uint32_t usecs
)
487 struct timeval tv
= timeval_current();
488 return timeval_add(&tv
, secs
, usecs
);
492 compare two timeval structures.
493 Return -1 if tv1 < tv2
494 Return 0 if tv1 == tv2
495 Return 1 if tv1 > tv2
497 _PUBLIC_
int timeval_compare(const struct timeval
*tv1
, const struct timeval
*tv2
)
499 if (tv1
->tv_sec
> tv2
->tv_sec
) return 1;
500 if (tv1
->tv_sec
< tv2
->tv_sec
) return -1;
501 if (tv1
->tv_usec
> tv2
->tv_usec
) return 1;
502 if (tv1
->tv_usec
< tv2
->tv_usec
) return -1;
507 return true if a timer is in the past
509 _PUBLIC_
bool timeval_expired(const struct timeval
*tv
)
511 struct timeval tv2
= timeval_current();
512 if (tv2
.tv_sec
> tv
->tv_sec
) return true;
513 if (tv2
.tv_sec
< tv
->tv_sec
) return false;
514 return (tv2
.tv_usec
>= tv
->tv_usec
);
518 return the number of seconds elapsed between two times
520 _PUBLIC_
double timeval_elapsed2(const struct timeval
*tv1
, const struct timeval
*tv2
)
522 return (tv2
->tv_sec
- tv1
->tv_sec
) +
523 (tv2
->tv_usec
- tv1
->tv_usec
)*1.0e-6;
527 return the number of seconds elapsed since a given time
529 _PUBLIC_
double timeval_elapsed(const struct timeval
*tv
)
531 struct timeval tv2
= timeval_current();
532 return timeval_elapsed2(tv
, &tv2
);
536 return the lesser of two timevals
538 _PUBLIC_
struct timeval
timeval_min(const struct timeval
*tv1
,
539 const struct timeval
*tv2
)
541 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv1
;
542 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv2
;
543 if (tv1
->tv_usec
< tv2
->tv_usec
) return *tv1
;
548 return the greater of two timevals
550 _PUBLIC_
struct timeval
timeval_max(const struct timeval
*tv1
,
551 const struct timeval
*tv2
)
553 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv1
;
554 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv2
;
555 if (tv1
->tv_usec
> tv2
->tv_usec
) return *tv1
;
560 return the difference between two timevals as a timeval
561 if tv1 comes after tv2, then return a zero timeval
562 (this is *tv2 - *tv1)
564 _PUBLIC_
struct timeval
timeval_until(const struct timeval
*tv1
,
565 const struct timeval
*tv2
)
568 if (timeval_compare(tv1
, tv2
) >= 0) {
569 return timeval_zero();
571 t
.tv_sec
= tv2
->tv_sec
- tv1
->tv_sec
;
572 if (tv1
->tv_usec
> tv2
->tv_usec
) {
574 t
.tv_usec
= 1000000 - (tv1
->tv_usec
- tv2
->tv_usec
);
576 t
.tv_usec
= tv2
->tv_usec
- tv1
->tv_usec
;
583 convert a timeval to a NTTIME
585 _PUBLIC_ NTTIME
timeval_to_nttime(const struct timeval
*tv
)
587 return 10*(tv
->tv_usec
+
588 ((TIME_FIXUP_CONSTANT
+ (uint64_t)tv
->tv_sec
) * 1000000));
592 convert a NTTIME to a timeval
594 _PUBLIC_
void nttime_to_timeval(struct timeval
*tv
, NTTIME t
)
596 if (tv
== NULL
) return;
600 t
-= TIME_FIXUP_CONSTANT
*1000*1000;
602 tv
->tv_sec
= t
/ 1000000;
604 if (TIME_T_MIN
> tv
->tv_sec
|| tv
->tv_sec
> TIME_T_MAX
) {
610 tv
->tv_usec
= t
- tv
->tv_sec
*1000000;
613 /*******************************************************************
614 yield the difference between *A and *B, in seconds, ignoring leap seconds
615 ********************************************************************/
616 static int tm_diff(struct tm
*a
, struct tm
*b
)
618 int ay
= a
->tm_year
+ (1900 - 1);
619 int by
= b
->tm_year
+ (1900 - 1);
620 int intervening_leap_days
=
621 (ay
/4 - by
/4) - (ay
/100 - by
/100) + (ay
/400 - by
/400);
623 int days
= 365*years
+ intervening_leap_days
+ (a
->tm_yday
- b
->tm_yday
);
624 int hours
= 24*days
+ (a
->tm_hour
- b
->tm_hour
);
625 int minutes
= 60*hours
+ (a
->tm_min
- b
->tm_min
);
626 int seconds
= 60*minutes
+ (a
->tm_sec
- b
->tm_sec
);
632 int extra_time_offset
=0;
635 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
637 _PUBLIC_
int get_time_zone(time_t t
)
639 struct tm
*tm
= gmtime(&t
);
647 return tm_diff(&tm_utc
,tm
)+60*extra_time_offset
;
650 struct timespec
nt_time_to_unix_timespec(NTTIME
*nt
)
655 if (*nt
== 0 || *nt
== (int64_t)-1) {
662 /* d is now in 100ns units, since jan 1st 1601".
663 Save off the ns fraction. */
666 * Take the last seven decimal digits and multiply by 100.
667 * to convert from 100ns units to 1ns units.
669 ret
.tv_nsec
= (long) ((d
% (1000 * 1000 * 10)) * 100);
671 /* Convert to seconds */
674 /* Now adjust by 369 years to make the secs since 1970 */
675 d
-= TIME_FIXUP_CONSTANT_INT
;
677 if (d
<= (int64_t)TIME_T_MIN
) {
678 ret
.tv_sec
= TIME_T_MIN
;
683 if (d
>= (int64_t)TIME_T_MAX
) {
684 ret
.tv_sec
= TIME_T_MAX
;
689 ret
.tv_sec
= (time_t)d
;
695 check if 2 NTTIMEs are equal.
697 bool nt_time_equal(NTTIME
*t1
, NTTIME
*t2
)
703 Check if it's a null timespec.
706 bool null_timespec(struct timespec ts
)
708 return ts
.tv_sec
== 0 ||
709 ts
.tv_sec
== (time_t)0xFFFFFFFF ||
710 ts
.tv_sec
== (time_t)-1;