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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/time.h"
30 #define TIME_T_MAX (~(time_t)0)
33 /*******************************************************************
34 External access to time_t_min and time_t_max.
35 ********************************************************************/
36 time_t get_time_t_max(void)
41 /*******************************************************************
42 a gettimeofday wrapper
43 ********************************************************************/
44 void GetTimeOfDay(struct timeval *tval)
46 #ifdef HAVE_GETTIMEOFDAY_TZ
47 gettimeofday(tval,NULL);
54 #define TIME_FIXUP_CONSTANT 11644473600LL
56 /****************************************************************************
57 interpret an 8 byte "filetime" structure to a time_t
58 It's originally in "100ns units since jan 1st 1601"
59 ****************************************************************************/
60 time_t nt_time_to_unix(NTTIME nt)
70 nt -= TIME_FIXUP_CONSTANT;
72 if (TIME_T_MIN >= nt || nt >= TIME_T_MAX) {
80 /****************************************************************************
81 put a 8 byte filetime from a time_t
82 This takes GMT as input
83 ****************************************************************************/
84 void unix_to_nt_time(NTTIME *nt, time_t t)
88 if (t == (time_t)-1) {
98 t2 += TIME_FIXUP_CONSTANT;
105 /****************************************************************************
106 check if it's a null unix time
107 ****************************************************************************/
108 BOOL null_time(time_t t)
111 t == (time_t)0xFFFFFFFF ||
116 /****************************************************************************
117 check if it's a null NTTIME
118 ****************************************************************************/
119 BOOL null_nttime(NTTIME t)
121 return t == 0 || t == (NTTIME)-1;
124 /*******************************************************************
125 create a 16 bit dos packed date
126 ********************************************************************/
127 static uint16_t make_dos_date1(struct tm *t)
130 ret = (((uint_t)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
131 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
135 /*******************************************************************
136 create a 16 bit dos packed time
137 ********************************************************************/
138 static uint16_t make_dos_time1(struct tm *t)
141 ret = ((((uint_t)t->tm_min >> 3)&0x7) | (((uint_t)t->tm_hour) << 3));
142 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
146 /*******************************************************************
147 create a 32 bit dos packed date/time from some parameters
148 This takes a GMT time and returns a packed localtime structure
149 ********************************************************************/
150 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
159 unixdate -= zone_offset;
161 t = gmtime(&unixdate);
166 ret = make_dos_date1(t);
167 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
172 /*******************************************************************
173 put a dos date into a buffer (time/date format)
174 This takes GMT time and puts local time in the buffer
175 ********************************************************************/
176 void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
178 uint32_t x = make_dos_date(unixdate, zone_offset);
182 /*******************************************************************
183 put a dos date into a buffer (date/time format)
184 This takes GMT time and puts local time in the buffer
185 ********************************************************************/
186 void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
189 x = make_dos_date(unixdate, zone_offset);
190 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
194 /*******************************************************************
195 put a dos 32 bit "unix like" date into a buffer. This routine takes
196 GMT and converts it to LOCAL time before putting it (most SMBs assume
197 localtime for this sort of date)
198 ********************************************************************/
199 void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
201 if (!null_time(unixdate)) {
202 unixdate -= zone_offset;
204 SIVAL(buf,offset,unixdate);
207 /*******************************************************************
208 interpret a 32 bit dos packed date/time to some parameters
209 ********************************************************************/
210 static void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
212 uint32_t p0,p1,p2,p3;
214 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
215 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
217 *second = 2*(p0 & 0x1F);
218 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
219 *hour = (p1>>3)&0xFF;
221 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
222 *year = ((p3>>1)&0xFF) + 80;
225 /*******************************************************************
226 create a unix date (int GMT) from a dos date (which is actually in
228 ********************************************************************/
229 time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
235 dos_date = IVAL(date_ptr,0);
237 if (dos_date == 0) return (time_t)0;
239 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
240 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
250 /*******************************************************************
251 like make_unix_date() but the words are reversed
252 ********************************************************************/
253 time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
257 x = IVAL(date_ptr,0);
258 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
261 return pull_dos_date((void *)&x, zone_offset);
264 /*******************************************************************
265 create a unix GMT date from a dos date in 32 bit "unix like" format
266 these generally arrive as localtimes, with corresponding DST
267 ******************************************************************/
268 time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
270 time_t t = (time_t)IVAL(date_ptr,0);
278 /***************************************************************************
279 return a HTTP/1.0 time string
280 ***************************************************************************/
281 char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
285 struct tm *tm = localtime(&t);
288 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
291 #ifndef HAVE_STRFTIME
292 buf = talloc_strdup(mem_ctx, asctime(tm));
293 if (buf[strlen(buf)-1] == '\n') {
294 buf[strlen(buf)-1] = 0;
297 strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
298 buf = talloc_strdup(mem_ctx, tempTime);
299 #endif /* !HAVE_STRFTIME */
304 /****************************************************************************
305 Return the date and time as a string
306 ****************************************************************************/
307 char *timestring(TALLOC_CTX *mem_ctx, time_t t)
315 return talloc_asprintf(mem_ctx,
316 "%ld seconds since the Epoch",
321 /* some versions of gcc complain about using %c. This is a bug
322 in the gcc warning, not a bug in this code. See a recent
323 strftime() manual page for details.
325 strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
326 TimeBuf = talloc_strdup(mem_ctx, tempTime);
328 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
335 return a talloced string representing a NTTIME for human consumption
337 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
343 t = nt_time_to_unix(nt);
344 return timestring(mem_ctx, t);
349 put a NTTIME into a packet
351 void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
353 SBVAL(base, offset, t);
357 pull a NTTIME from a packet
359 NTTIME pull_nttime(uint8_t *base, uint16_t offset)
361 NTTIME ret = BVAL(base, offset);
366 parse a nttime as a large integer in a string and return a NTTIME
368 NTTIME nttime_from_string(const char *s)
370 return strtoull(s, NULL, 0);
374 return (tv1 - tv2) in microseconds
376 int64_t usec_time_diff(struct timeval *tv1, struct timeval *tv2)
378 int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
379 return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
384 return a zero timeval
386 struct timeval timeval_zero(void)
395 return True if a timeval is zero
397 BOOL timeval_is_zero(const struct timeval *tv)
399 return tv->tv_sec == 0 && tv->tv_usec == 0;
403 return a timeval for the current time
405 struct timeval timeval_current(void)
413 return a timeval struct with the given elements
415 struct timeval timeval_set(uint32_t secs, uint32_t usecs)
425 return a timeval ofs microseconds after tv
427 struct timeval timeval_add(const struct timeval *tv,
428 uint32_t secs, uint32_t usecs)
430 struct timeval tv2 = *tv;
431 const uint_t million = 1000000;
433 tv2.tv_usec += usecs;
434 tv2.tv_sec += tv2.tv_usec / million;
435 tv2.tv_usec = tv2.tv_usec % million;
440 return the sum of two timeval structures
442 struct timeval timeval_sum(const struct timeval *tv1,
443 const struct timeval *tv2)
445 return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
449 return a timeval secs/usecs into the future
451 struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
453 struct timeval tv = timeval_current();
454 return timeval_add(&tv, secs, usecs);
458 compare two timeval structures.
459 Return -1 if tv1 < tv2
460 Return 0 if tv1 == tv2
461 Return 1 if tv1 > tv2
463 int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
465 if (tv1->tv_sec > tv2->tv_sec) return 1;
466 if (tv1->tv_sec < tv2->tv_sec) return -1;
467 if (tv1->tv_usec > tv2->tv_usec) return 1;
468 if (tv1->tv_usec < tv2->tv_usec) return -1;
473 return True if a timer is in the past
475 BOOL timeval_expired(const struct timeval *tv)
477 struct timeval tv2 = timeval_current();
478 if (tv2.tv_sec > tv->tv_sec) return True;
479 if (tv2.tv_sec < tv->tv_sec) return False;
480 return (tv2.tv_usec >= tv->tv_usec);
484 return the number of seconds elapsed between two times
486 double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
488 return (tv2->tv_sec - tv1->tv_sec) +
489 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
493 return the number of seconds elapsed since a given time
495 double timeval_elapsed(const struct timeval *tv)
497 struct timeval tv2 = timeval_current();
498 return timeval_elapsed2(tv, &tv2);
502 return the lesser of two timevals
504 struct timeval timeval_min(const struct timeval *tv1,
505 const struct timeval *tv2)
507 if (tv1->tv_sec < tv2->tv_sec) return *tv1;
508 if (tv1->tv_sec > tv2->tv_sec) return *tv2;
509 if (tv1->tv_usec < tv2->tv_usec) return *tv1;
514 return the greater of two timevals
516 struct timeval timeval_max(const struct timeval *tv1,
517 const struct timeval *tv2)
519 if (tv1->tv_sec > tv2->tv_sec) return *tv1;
520 if (tv1->tv_sec < tv2->tv_sec) return *tv2;
521 if (tv1->tv_usec > tv2->tv_usec) return *tv1;
526 return the difference between two timevals as a timeval
527 if tv1 comes after tv2, then return a zero timeval
528 (this is *tv2 - *tv1)
530 struct timeval timeval_until(const struct timeval *tv1,
531 const struct timeval *tv2)
534 if (timeval_compare(tv1, tv2) >= 0) {
535 return timeval_zero();
537 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
538 if (tv1->tv_usec > tv2->tv_usec) {
540 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
542 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
549 convert a timeval to a NTTIME
551 NTTIME timeval_to_nttime(const struct timeval *tv)
553 return 10*(tv->tv_usec +
554 ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
557 /*******************************************************************
558 yield the difference between *A and *B, in seconds, ignoring leap seconds
559 ********************************************************************/
560 static int tm_diff(struct tm *a, struct tm *b)
562 int ay = a->tm_year + (1900 - 1);
563 int by = b->tm_year + (1900 - 1);
564 int intervening_leap_days =
565 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
567 int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
568 int hours = 24*days + (a->tm_hour - b->tm_hour);
569 int minutes = 60*hours + (a->tm_min - b->tm_min);
570 int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
575 /*******************************************************************
576 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
577 ******************************************************************/
578 int get_time_zone(time_t t)
580 struct tm *tm = gmtime(&t);
588 return tm_diff(&tm_utc,tm);