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 */
36 * we use the INT32_MAX here as on 64 bit systems,
37 * gmtime() fails with INT64_MAX
41 #define TIME_T_MAX MIN(INT32_MAX,_TYPE_MAXIMUM(time_t))
45 External access to time_t_min and time_t_max.
47 _PUBLIC_ time_t get_time_t_max(void)
53 a gettimeofday wrapper
55 _PUBLIC_ void GetTimeOfDay(struct timeval *tval)
57 #ifdef HAVE_GETTIMEOFDAY_TZ
58 gettimeofday(tval,NULL);
65 #define TIME_FIXUP_CONSTANT 11644473600LL
68 interpret an 8 byte "filetime" structure to a time_t
69 It's originally in "100ns units since jan 1st 1601"
71 _PUBLIC_ time_t nt_time_to_unix(NTTIME nt)
81 nt -= TIME_FIXUP_CONSTANT;
83 if (TIME_T_MIN > nt || nt > TIME_T_MAX) {
92 put a 8 byte filetime from a time_t
93 This takes GMT as input
95 _PUBLIC_ void unix_to_nt_time(NTTIME *nt, time_t t)
99 if (t == (time_t)-1) {
109 t2 += TIME_FIXUP_CONSTANT;
117 check if it's a null unix time
119 _PUBLIC_ bool null_time(time_t t)
122 t == (time_t)0xFFFFFFFF ||
128 check if it's a null NTTIME
130 _PUBLIC_ bool null_nttime(NTTIME t)
132 return t == 0 || t == (NTTIME)-1;
135 /*******************************************************************
136 create a 16 bit dos packed date
137 ********************************************************************/
138 static uint16_t make_dos_date1(struct tm *t)
141 ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
142 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
146 /*******************************************************************
147 create a 16 bit dos packed time
148 ********************************************************************/
149 static uint16_t make_dos_time1(struct tm *t)
152 ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));
153 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
157 /*******************************************************************
158 create a 32 bit dos packed date/time from some parameters
159 This takes a GMT time and returns a packed localtime structure
160 ********************************************************************/
161 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
170 unixdate -= zone_offset;
172 t = gmtime(&unixdate);
177 ret = make_dos_date1(t);
178 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
184 put a dos date into a buffer (time/date format)
185 This takes GMT time and puts local time in the buffer
187 _PUBLIC_ void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
189 uint32_t x = make_dos_date(unixdate, zone_offset);
194 put a dos date into a buffer (date/time format)
195 This takes GMT time and puts local time in the buffer
197 _PUBLIC_ void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
200 x = make_dos_date(unixdate, zone_offset);
201 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
206 put a dos 32 bit "unix like" date into a buffer. This routine takes
207 GMT and converts it to LOCAL time before putting it (most SMBs assume
208 localtime for this sort of date)
210 _PUBLIC_ void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
212 if (!null_time(unixdate)) {
213 unixdate -= zone_offset;
215 SIVAL(buf,offset,unixdate);
218 /*******************************************************************
219 interpret a 32 bit dos packed date/time to some parameters
220 ********************************************************************/
221 static void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
223 uint32_t p0,p1,p2,p3;
225 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
226 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
228 *second = 2*(p0 & 0x1F);
229 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
230 *hour = (p1>>3)&0xFF;
232 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
233 *year = ((p3>>1)&0xFF) + 80;
237 create a unix date (int GMT) from a dos date (which is actually in
240 _PUBLIC_ time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
246 dos_date = IVAL(date_ptr,0);
248 if (dos_date == 0) return (time_t)0;
250 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
251 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
262 like make_unix_date() but the words are reversed
264 _PUBLIC_ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
268 x = IVAL(date_ptr,0);
269 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
272 return pull_dos_date((void *)&x, zone_offset);
276 create a unix GMT date from a dos date in 32 bit "unix like" format
277 these generally arrive as localtimes, with corresponding DST
279 _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
281 time_t t = (time_t)IVAL(date_ptr,0);
290 return a HTTP/1.0 time string
292 _PUBLIC_ char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
296 struct tm *tm = localtime(&t);
299 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
302 #ifndef HAVE_STRFTIME
303 buf = talloc_strdup(mem_ctx, asctime(tm));
304 if (buf[strlen(buf)-1] == '\n') {
305 buf[strlen(buf)-1] = 0;
308 strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
309 buf = talloc_strdup(mem_ctx, tempTime);
310 #endif /* !HAVE_STRFTIME */
316 Return the date and time as a string
318 _PUBLIC_ char *timestring(TALLOC_CTX *mem_ctx, time_t t)
326 return talloc_asprintf(mem_ctx,
327 "%ld seconds since the Epoch",
332 /* some versions of gcc complain about using %c. This is a bug
333 in the gcc warning, not a bug in this code. See a recent
334 strftime() manual page for details.
336 strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
337 TimeBuf = talloc_strdup(mem_ctx, tempTime);
339 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
346 return a talloced string representing a NTTIME for human consumption
348 _PUBLIC_ const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
354 t = nt_time_to_unix(nt);
355 return timestring(mem_ctx, t);
360 put a NTTIME into a packet
362 _PUBLIC_ void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
364 SBVAL(base, offset, t);
368 pull a NTTIME from a packet
370 _PUBLIC_ NTTIME pull_nttime(uint8_t *base, uint16_t offset)
372 NTTIME ret = BVAL(base, offset);
377 return (tv1 - tv2) in microseconds
379 _PUBLIC_ int64_t usec_time_diff(struct timeval *tv1, struct timeval *tv2)
381 int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
382 return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
387 return a zero timeval
389 _PUBLIC_ struct timeval timeval_zero(void)
398 return true if a timeval is zero
400 _PUBLIC_ bool timeval_is_zero(const struct timeval *tv)
402 return tv->tv_sec == 0 && tv->tv_usec == 0;
406 return a timeval for the current time
408 _PUBLIC_ struct timeval timeval_current(void)
416 return a timeval struct with the given elements
418 _PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs)
428 return a timeval ofs microseconds after tv
430 _PUBLIC_ struct timeval timeval_add(const struct timeval *tv,
431 uint32_t secs, uint32_t usecs)
433 struct timeval tv2 = *tv;
434 const unsigned int million = 1000000;
436 tv2.tv_usec += usecs;
437 tv2.tv_sec += tv2.tv_usec / million;
438 tv2.tv_usec = tv2.tv_usec % million;
443 return the sum of two timeval structures
445 struct timeval timeval_sum(const struct timeval *tv1,
446 const struct timeval *tv2)
448 return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
452 return a timeval secs/usecs into the future
454 _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
456 struct timeval tv = timeval_current();
457 return timeval_add(&tv, secs, usecs);
461 compare two timeval structures.
462 Return -1 if tv1 < tv2
463 Return 0 if tv1 == tv2
464 Return 1 if tv1 > tv2
466 _PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
468 if (tv1->tv_sec > tv2->tv_sec) return 1;
469 if (tv1->tv_sec < tv2->tv_sec) return -1;
470 if (tv1->tv_usec > tv2->tv_usec) return 1;
471 if (tv1->tv_usec < tv2->tv_usec) return -1;
476 return true if a timer is in the past
478 _PUBLIC_ bool timeval_expired(const struct timeval *tv)
480 struct timeval tv2 = timeval_current();
481 if (tv2.tv_sec > tv->tv_sec) return true;
482 if (tv2.tv_sec < tv->tv_sec) return false;
483 return (tv2.tv_usec >= tv->tv_usec);
487 return the number of seconds elapsed between two times
489 _PUBLIC_ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
491 return (tv2->tv_sec - tv1->tv_sec) +
492 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
496 return the number of seconds elapsed since a given time
498 _PUBLIC_ double timeval_elapsed(const struct timeval *tv)
500 struct timeval tv2 = timeval_current();
501 return timeval_elapsed2(tv, &tv2);
505 return the lesser of two timevals
507 _PUBLIC_ struct timeval timeval_min(const struct timeval *tv1,
508 const struct timeval *tv2)
510 if (tv1->tv_sec < tv2->tv_sec) return *tv1;
511 if (tv1->tv_sec > tv2->tv_sec) return *tv2;
512 if (tv1->tv_usec < tv2->tv_usec) return *tv1;
517 return the greater of two timevals
519 _PUBLIC_ struct timeval timeval_max(const struct timeval *tv1,
520 const struct timeval *tv2)
522 if (tv1->tv_sec > tv2->tv_sec) return *tv1;
523 if (tv1->tv_sec < tv2->tv_sec) return *tv2;
524 if (tv1->tv_usec > tv2->tv_usec) return *tv1;
529 return the difference between two timevals as a timeval
530 if tv1 comes after tv2, then return a zero timeval
531 (this is *tv2 - *tv1)
533 _PUBLIC_ struct timeval timeval_until(const struct timeval *tv1,
534 const struct timeval *tv2)
537 if (timeval_compare(tv1, tv2) >= 0) {
538 return timeval_zero();
540 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
541 if (tv1->tv_usec > tv2->tv_usec) {
543 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
545 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
552 convert a timeval to a NTTIME
554 _PUBLIC_ NTTIME timeval_to_nttime(const struct timeval *tv)
556 return 10*(tv->tv_usec +
557 ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
561 convert a NTTIME to a timeval
563 _PUBLIC_ void nttime_to_timeval(struct timeval *tv, NTTIME t)
565 if (tv == NULL) return;
569 t -= TIME_FIXUP_CONSTANT*1000*1000;
571 tv->tv_sec = t / 1000000;
573 if (TIME_T_MIN > tv->tv_sec || tv->tv_sec > TIME_T_MAX) {
579 tv->tv_usec = t - tv->tv_sec*1000000;
582 /*******************************************************************
583 yield the difference between *A and *B, in seconds, ignoring leap seconds
584 ********************************************************************/
585 static int tm_diff(struct tm *a, struct tm *b)
587 int ay = a->tm_year + (1900 - 1);
588 int by = b->tm_year + (1900 - 1);
589 int intervening_leap_days =
590 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
592 int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
593 int hours = 24*days + (a->tm_hour - b->tm_hour);
594 int minutes = 60*hours + (a->tm_min - b->tm_min);
595 int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
601 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
603 _PUBLIC_ int get_time_zone(time_t t)
605 struct tm *tm = gmtime(&t);
613 return tm_diff(&tm_utc,tm);