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"
27 /* we use 0 here, because (time_t)-1 means error */
32 * we use the INT32_MAX here as on 64 bit systems,
33 * gmtime() fails with INT64_MAX
37 #define TIME_T_MAX MIN(INT32_MAX,_TYPE_MAXIMUM(time_t))
40 /*******************************************************************
41 External access to time_t_min and time_t_max.
42 ********************************************************************/
43 time_t get_time_t_max(void)
48 /*******************************************************************
49 a gettimeofday wrapper
50 ********************************************************************/
51 void GetTimeOfDay(struct timeval *tval)
53 #ifdef HAVE_GETTIMEOFDAY_TZ
54 gettimeofday(tval,NULL);
61 #define TIME_FIXUP_CONSTANT 11644473600LL
63 /****************************************************************************
64 interpret an 8 byte "filetime" structure to a time_t
65 It's originally in "100ns units since jan 1st 1601"
66 ****************************************************************************/
67 time_t nt_time_to_unix(NTTIME nt)
77 nt -= TIME_FIXUP_CONSTANT;
79 if (TIME_T_MIN > nt || nt > TIME_T_MAX) {
87 /****************************************************************************
88 put a 8 byte filetime from a time_t
89 This takes GMT as input
90 ****************************************************************************/
91 void unix_to_nt_time(NTTIME *nt, time_t t)
95 if (t == (time_t)-1) {
105 t2 += TIME_FIXUP_CONSTANT;
112 /****************************************************************************
113 check if it's a null unix time
114 ****************************************************************************/
115 BOOL null_time(time_t t)
118 t == (time_t)0xFFFFFFFF ||
123 /****************************************************************************
124 check if it's a null NTTIME
125 ****************************************************************************/
126 BOOL null_nttime(NTTIME t)
128 return t == 0 || t == (NTTIME)-1;
131 /*******************************************************************
132 create a 16 bit dos packed date
133 ********************************************************************/
134 static uint16_t make_dos_date1(struct tm *t)
137 ret = (((uint_t)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
138 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
142 /*******************************************************************
143 create a 16 bit dos packed time
144 ********************************************************************/
145 static uint16_t make_dos_time1(struct tm *t)
148 ret = ((((uint_t)t->tm_min >> 3)&0x7) | (((uint_t)t->tm_hour) << 3));
149 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
153 /*******************************************************************
154 create a 32 bit dos packed date/time from some parameters
155 This takes a GMT time and returns a packed localtime structure
156 ********************************************************************/
157 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
166 unixdate -= zone_offset;
168 t = gmtime(&unixdate);
173 ret = make_dos_date1(t);
174 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
179 /*******************************************************************
180 put a dos date into a buffer (time/date format)
181 This takes GMT time and puts local time in the buffer
182 ********************************************************************/
183 void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
185 uint32_t x = make_dos_date(unixdate, zone_offset);
189 /*******************************************************************
190 put a dos date into a buffer (date/time format)
191 This takes GMT time and puts local time in the buffer
192 ********************************************************************/
193 void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
196 x = make_dos_date(unixdate, zone_offset);
197 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
201 /*******************************************************************
202 put a dos 32 bit "unix like" date into a buffer. This routine takes
203 GMT and converts it to LOCAL time before putting it (most SMBs assume
204 localtime for this sort of date)
205 ********************************************************************/
206 void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
208 if (!null_time(unixdate)) {
209 unixdate -= zone_offset;
211 SIVAL(buf,offset,unixdate);
214 /*******************************************************************
215 interpret a 32 bit dos packed date/time to some parameters
216 ********************************************************************/
217 static void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
219 uint32_t p0,p1,p2,p3;
221 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
222 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
224 *second = 2*(p0 & 0x1F);
225 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
226 *hour = (p1>>3)&0xFF;
228 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
229 *year = ((p3>>1)&0xFF) + 80;
232 /*******************************************************************
233 create a unix date (int GMT) from a dos date (which is actually in
235 ********************************************************************/
236 time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
242 dos_date = IVAL(date_ptr,0);
244 if (dos_date == 0) return (time_t)0;
246 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
247 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
257 /*******************************************************************
258 like make_unix_date() but the words are reversed
259 ********************************************************************/
260 time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
264 x = IVAL(date_ptr,0);
265 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
268 return pull_dos_date((void *)&x, zone_offset);
271 /*******************************************************************
272 create a unix GMT date from a dos date in 32 bit "unix like" format
273 these generally arrive as localtimes, with corresponding DST
274 ******************************************************************/
275 time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
277 time_t t = (time_t)IVAL(date_ptr,0);
285 /***************************************************************************
286 return a HTTP/1.0 time string
287 ***************************************************************************/
288 char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
292 struct tm *tm = localtime(&t);
295 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
298 #ifndef HAVE_STRFTIME
299 buf = talloc_strdup(mem_ctx, asctime(tm));
300 if (buf[strlen(buf)-1] == '\n') {
301 buf[strlen(buf)-1] = 0;
304 strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
305 buf = talloc_strdup(mem_ctx, tempTime);
306 #endif /* !HAVE_STRFTIME */
311 /****************************************************************************
312 Return the date and time as a string
313 ****************************************************************************/
314 char *timestring(TALLOC_CTX *mem_ctx, time_t t)
322 return talloc_asprintf(mem_ctx,
323 "%ld seconds since the Epoch",
328 /* some versions of gcc complain about using %c. This is a bug
329 in the gcc warning, not a bug in this code. See a recent
330 strftime() manual page for details.
332 strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
333 TimeBuf = talloc_strdup(mem_ctx, tempTime);
335 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
342 return a talloced string representing a NTTIME for human consumption
344 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
350 t = nt_time_to_unix(nt);
351 return timestring(mem_ctx, t);
356 put a NTTIME into a packet
358 void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
360 SBVAL(base, offset, t);
364 pull a NTTIME from a packet
366 NTTIME pull_nttime(uint8_t *base, uint16_t offset)
368 NTTIME ret = BVAL(base, offset);
373 parse a nttime as a large integer in a string and return a NTTIME
375 NTTIME nttime_from_string(const char *s)
377 return strtoull(s, NULL, 0);
381 return (tv1 - tv2) in microseconds
383 int64_t usec_time_diff(struct timeval *tv1, struct timeval *tv2)
385 int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
386 return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
391 return a zero timeval
393 struct timeval timeval_zero(void)
402 return True if a timeval is zero
404 BOOL timeval_is_zero(const struct timeval *tv)
406 return tv->tv_sec == 0 && tv->tv_usec == 0;
410 return a timeval for the current time
412 struct timeval timeval_current(void)
420 return a timeval struct with the given elements
422 struct timeval timeval_set(uint32_t secs, uint32_t usecs)
432 return a timeval ofs microseconds after tv
434 struct timeval timeval_add(const struct timeval *tv,
435 uint32_t secs, uint32_t usecs)
437 struct timeval tv2 = *tv;
438 const uint_t million = 1000000;
440 tv2.tv_usec += usecs;
441 tv2.tv_sec += tv2.tv_usec / million;
442 tv2.tv_usec = tv2.tv_usec % million;
447 return the sum of two timeval structures
449 struct timeval timeval_sum(const struct timeval *tv1,
450 const struct timeval *tv2)
452 return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
456 return a timeval secs/usecs into the future
458 struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
460 struct timeval tv = timeval_current();
461 return timeval_add(&tv, secs, usecs);
465 compare two timeval structures.
466 Return -1 if tv1 < tv2
467 Return 0 if tv1 == tv2
468 Return 1 if tv1 > tv2
470 int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
472 if (tv1->tv_sec > tv2->tv_sec) return 1;
473 if (tv1->tv_sec < tv2->tv_sec) return -1;
474 if (tv1->tv_usec > tv2->tv_usec) return 1;
475 if (tv1->tv_usec < tv2->tv_usec) return -1;
480 return True if a timer is in the past
482 BOOL timeval_expired(const struct timeval *tv)
484 struct timeval tv2 = timeval_current();
485 if (tv2.tv_sec > tv->tv_sec) return True;
486 if (tv2.tv_sec < tv->tv_sec) return False;
487 return (tv2.tv_usec >= tv->tv_usec);
491 return the number of seconds elapsed between two times
493 double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
495 return (tv2->tv_sec - tv1->tv_sec) +
496 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
500 return the number of seconds elapsed since a given time
502 double timeval_elapsed(const struct timeval *tv)
504 struct timeval tv2 = timeval_current();
505 return timeval_elapsed2(tv, &tv2);
509 return the lesser of two timevals
511 struct timeval timeval_min(const struct timeval *tv1,
512 const struct timeval *tv2)
514 if (tv1->tv_sec < tv2->tv_sec) return *tv1;
515 if (tv1->tv_sec > tv2->tv_sec) return *tv2;
516 if (tv1->tv_usec < tv2->tv_usec) return *tv1;
521 return the greater of two timevals
523 struct timeval timeval_max(const struct timeval *tv1,
524 const struct timeval *tv2)
526 if (tv1->tv_sec > tv2->tv_sec) return *tv1;
527 if (tv1->tv_sec < tv2->tv_sec) return *tv2;
528 if (tv1->tv_usec > tv2->tv_usec) return *tv1;
533 return the difference between two timevals as a timeval
534 if tv1 comes after tv2, then return a zero timeval
535 (this is *tv2 - *tv1)
537 struct timeval timeval_until(const struct timeval *tv1,
538 const struct timeval *tv2)
541 if (timeval_compare(tv1, tv2) >= 0) {
542 return timeval_zero();
544 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
545 if (tv1->tv_usec > tv2->tv_usec) {
547 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
549 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
556 convert a timeval to a NTTIME
558 NTTIME timeval_to_nttime(const struct timeval *tv)
560 return 10*(tv->tv_usec +
561 ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
564 /*******************************************************************
565 yield the difference between *A and *B, in seconds, ignoring leap seconds
566 ********************************************************************/
567 static int tm_diff(struct tm *a, struct tm *b)
569 int ay = a->tm_year + (1900 - 1);
570 int by = b->tm_year + (1900 - 1);
571 int intervening_leap_days =
572 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
574 int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
575 int hours = 24*days + (a->tm_hour - b->tm_hour);
576 int minutes = 60*hours + (a->tm_min - b->tm_min);
577 int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
582 /*******************************************************************
583 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
584 ******************************************************************/
585 int get_time_zone(time_t t)
587 struct tm *tm = gmtime(&t);
595 return tm_diff(&tm_utc,tm);