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
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.
12
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.
17
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.
21 */
22
23 #include "includes.h"
24 #include "system/time.h"
25
26 /**
27 * @file
28 * @brief time handling functions
29 */
30
31 #ifndef TIME_T_MIN
32 /* we use 0 here, because (time_t)-1 means error */
33 #define TIME_T_MIN 0
34 #endif
35
36 /*
37 * we use the INT32_MAX here as on 64 bit systems,
38 * gmtime() fails with INT64_MAX
39 */
40
41 #ifndef TIME_T_MAX
42 #define TIME_T_MAX MIN(INT32_MAX,_TYPE_MAXIMUM(time_t))
43 #endif
44
45 /**
46 External access to time_t_min and time_t_max.
47 **/
get_time_t_max(void)48 _PUBLIC_ time_t get_time_t_max(void)
49 {
50 return TIME_T_MAX;
51 }
52
53 /**
54 a gettimeofday wrapper
55 **/
GetTimeOfDay(struct timeval * tval)56 _PUBLIC_ void GetTimeOfDay(struct timeval *tval)
57 {
58 #ifdef HAVE_GETTIMEOFDAY_TZ
59 gettimeofday(tval,NULL);
60 #else
61 gettimeofday(tval);
62 #endif
63 }
64
65
66 #define TIME_FIXUP_CONSTANT 11644473600LL
67
68 /**
69 interpret an 8 byte "filetime" structure to a time_t
70 It's originally in "100ns units since jan 1st 1601"
71 **/
nt_time_to_unix(NTTIME nt)72 _PUBLIC_ time_t nt_time_to_unix(NTTIME nt)
73 {
74 if (nt == 0) {
75 return 0;
76 }
77 if (nt == -1LL) {
78 return (time_t)-1;
79 }
80 nt += 1000*1000*10/2;
81 nt /= 1000*1000*10;
82 nt -= TIME_FIXUP_CONSTANT;
83
84 if (TIME_T_MIN > nt || nt > TIME_T_MAX) {
85 return 0;
86 }
87
88 return (time_t)nt;
89 }
90
91
92 /**
93 put a 8 byte filetime from a time_t
94 This takes GMT as input
95 **/
unix_to_nt_time(NTTIME * nt,time_t t)96 _PUBLIC_ void unix_to_nt_time(NTTIME *nt, time_t t)
97 {
98 uint64_t t2;
99
100 if (t == (time_t)-1) {
101 *nt = (NTTIME)-1LL;
102 return;
103 }
104 if (t == 0) {
105 *nt = 0;
106 return;
107 }
108
109 t2 = t;
110 t2 += TIME_FIXUP_CONSTANT;
111 t2 *= 1000*1000*10;
112
113 *nt = t2;
114 }
115
116
117 /**
118 check if it's a null unix time
119 **/
null_time(time_t t)120 _PUBLIC_ BOOL null_time(time_t t)
121 {
122 return t == 0 ||
123 t == (time_t)0xFFFFFFFF ||
124 t == (time_t)-1;
125 }
126
127
128 /**
129 check if it's a null NTTIME
130 **/
null_nttime(NTTIME t)131 _PUBLIC_ BOOL null_nttime(NTTIME t)
132 {
133 return t == 0 || t == (NTTIME)-1;
134 }
135
136 /*******************************************************************
137 create a 16 bit dos packed date
138 ********************************************************************/
make_dos_date1(struct tm * t)139 static uint16_t make_dos_date1(struct tm *t)
140 {
141 uint16_t ret=0;
142 ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
143 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
144 return ret;
145 }
146
147 /*******************************************************************
148 create a 16 bit dos packed time
149 ********************************************************************/
make_dos_time1(struct tm * t)150 static uint16_t make_dos_time1(struct tm *t)
151 {
152 uint16_t ret=0;
153 ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));
154 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
155 return ret;
156 }
157
158 /*******************************************************************
159 create a 32 bit dos packed date/time from some parameters
160 This takes a GMT time and returns a packed localtime structure
161 ********************************************************************/
make_dos_date(time_t unixdate,int zone_offset)162 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
163 {
164 struct tm *t;
165 uint32_t ret=0;
166
167 if (unixdate == 0) {
168 return 0;
169 }
170
171 unixdate -= zone_offset;
172
173 t = gmtime(&unixdate);
174 if (!t) {
175 return 0xFFFFFFFF;
176 }
177
178 ret = make_dos_date1(t);
179 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
180
181 return ret;
182 }
183
184 /**
185 put a dos date into a buffer (time/date format)
186 This takes GMT time and puts local time in the buffer
187 **/
push_dos_date(uint8_t * buf,int offset,time_t unixdate,int zone_offset)188 _PUBLIC_ void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
189 {
190 uint32_t x = make_dos_date(unixdate, zone_offset);
191 SIVAL(buf,offset,x);
192 }
193
194 /**
195 put a dos date into a buffer (date/time format)
196 This takes GMT time and puts local time in the buffer
197 **/
push_dos_date2(uint8_t * buf,int offset,time_t unixdate,int zone_offset)198 _PUBLIC_ void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
199 {
200 uint32_t x;
201 x = make_dos_date(unixdate, zone_offset);
202 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
203 SIVAL(buf,offset,x);
204 }
205
206 /**
207 put a dos 32 bit "unix like" date into a buffer. This routine takes
208 GMT and converts it to LOCAL time before putting it (most SMBs assume
209 localtime for this sort of date)
210 **/
push_dos_date3(uint8_t * buf,int offset,time_t unixdate,int zone_offset)211 _PUBLIC_ void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
212 {
213 if (!null_time(unixdate)) {
214 unixdate -= zone_offset;
215 }
216 SIVAL(buf,offset,unixdate);
217 }
218
219 /*******************************************************************
220 interpret a 32 bit dos packed date/time to some parameters
221 ********************************************************************/
interpret_dos_date(uint32_t date,int * year,int * month,int * day,int * hour,int * minute,int * second)222 static void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
223 {
224 uint32_t p0,p1,p2,p3;
225
226 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
227 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
228
229 *second = 2*(p0 & 0x1F);
230 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
231 *hour = (p1>>3)&0xFF;
232 *day = (p2&0x1F);
233 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
234 *year = ((p3>>1)&0xFF) + 80;
235 }
236
237 /**
238 create a unix date (int GMT) from a dos date (which is actually in
239 localtime)
240 **/
pull_dos_date(const uint8_t * date_ptr,int zone_offset)241 _PUBLIC_ time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
242 {
243 uint32_t dos_date=0;
244 struct tm t;
245 time_t ret;
246
247 dos_date = IVAL(date_ptr,0);
248
249 if (dos_date == 0) return (time_t)0;
250
251 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
252 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
253 t.tm_isdst = -1;
254
255 ret = timegm(&t);
256
257 ret += zone_offset;
258
259 return ret;
260 }
261
262 /**
263 like make_unix_date() but the words are reversed
264 **/
pull_dos_date2(const uint8_t * date_ptr,int zone_offset)265 _PUBLIC_ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
266 {
267 uint32_t x,x2;
268
269 x = IVAL(date_ptr,0);
270 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
271 SIVAL(&x,0,x2);
272
273 return pull_dos_date((void *)&x, zone_offset);
274 }
275
276 /**
277 create a unix GMT date from a dos date in 32 bit "unix like" format
278 these generally arrive as localtimes, with corresponding DST
279 **/
pull_dos_date3(const uint8_t * date_ptr,int zone_offset)280 _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
281 {
282 time_t t = (time_t)IVAL(date_ptr,0);
283 if (!null_time(t)) {
284 t += zone_offset;
285 }
286 return t;
287 }
288
289
290 /**
291 return a HTTP/1.0 time string
292 **/
http_timestring(TALLOC_CTX * mem_ctx,time_t t)293 _PUBLIC_ char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
294 {
295 char *buf;
296 char tempTime[60];
297 struct tm *tm = localtime(&t);
298
299 if (!tm) {
300 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
301 }
302
303 #ifndef HAVE_STRFTIME
304 buf = talloc_strdup(mem_ctx, asctime(tm));
305 if (buf[strlen(buf)-1] == '\n') {
306 buf[strlen(buf)-1] = 0;
307 }
308 #else
309 strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
310 buf = talloc_strdup(mem_ctx, tempTime);
311 #endif /* !HAVE_STRFTIME */
312
313 return buf;
314 }
315
316 /**
317 Return the date and time as a string
318 **/
timestring(TALLOC_CTX * mem_ctx,time_t t)319 _PUBLIC_ char *timestring(TALLOC_CTX *mem_ctx, time_t t)
320 {
321 char *TimeBuf;
322 char tempTime[80];
323 struct tm *tm;
324
325 tm = localtime(&t);
326 if (!tm) {
327 return talloc_asprintf(mem_ctx,
328 "%ld seconds since the Epoch",
329 (long)t);
330 }
331
332 #ifdef HAVE_STRFTIME
333 /* some versions of gcc complain about using %c. This is a bug
334 in the gcc warning, not a bug in this code. See a recent
335 strftime() manual page for details.
336 */
337 strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
338 TimeBuf = talloc_strdup(mem_ctx, tempTime);
339 #else
340 TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
341 #endif
342
343 return TimeBuf;
344 }
345
346 /**
347 return a talloced string representing a NTTIME for human consumption
348 */
nt_time_string(TALLOC_CTX * mem_ctx,NTTIME nt)349 _PUBLIC_ const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
350 {
351 time_t t;
352 if (nt == 0) {
353 return "NTTIME(0)";
354 }
355 t = nt_time_to_unix(nt);
356 return timestring(mem_ctx, t);
357 }
358
359
360 /**
361 put a NTTIME into a packet
362 */
push_nttime(uint8_t * base,uint16_t offset,NTTIME t)363 _PUBLIC_ void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
364 {
365 SBVAL(base, offset, t);
366 }
367
368 /**
369 pull a NTTIME from a packet
370 */
pull_nttime(uint8_t * base,uint16_t offset)371 _PUBLIC_ NTTIME pull_nttime(uint8_t *base, uint16_t offset)
372 {
373 NTTIME ret = BVAL(base, offset);
374 return ret;
375 }
376
377 /**
378 parse a nttime as a large integer in a string and return a NTTIME
379 */
nttime_from_string(const char * s)380 _PUBLIC_ NTTIME nttime_from_string(const char *s)
381 {
382 return strtoull(s, NULL, 0);
383 }
384
385 /**
386 return (tv1 - tv2) in microseconds
387 */
usec_time_diff(struct timeval * tv1,struct timeval * tv2)388 _PUBLIC_ int64_t usec_time_diff(struct timeval *tv1, struct timeval *tv2)
389 {
390 int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
391 return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
392 }
393
394
395 /**
396 return a zero timeval
397 */
timeval_zero(void)398 _PUBLIC_ struct timeval timeval_zero(void)
399 {
400 struct timeval tv;
401 tv.tv_sec = 0;
402 tv.tv_usec = 0;
403 return tv;
404 }
405
406 /**
407 return True if a timeval is zero
408 */
timeval_is_zero(const struct timeval * tv)409 _PUBLIC_ BOOL timeval_is_zero(const struct timeval *tv)
410 {
411 return tv->tv_sec == 0 && tv->tv_usec == 0;
412 }
413
414 /**
415 return a timeval for the current time
416 */
timeval_current(void)417 _PUBLIC_ struct timeval timeval_current(void)
418 {
419 struct timeval tv;
420 GetTimeOfDay(&tv);
421 return tv;
422 }
423
424 /**
425 return a timeval struct with the given elements
426 */
timeval_set(uint32_t secs,uint32_t usecs)427 _PUBLIC_ struct timeval timeval_set(uint32_t secs, uint32_t usecs)
428 {
429 struct timeval tv;
430 tv.tv_sec = secs;
431 tv.tv_usec = usecs;
432 return tv;
433 }
434
435
436 /**
437 return a timeval ofs microseconds after tv
438 */
timeval_add(const struct timeval * tv,uint32_t secs,uint32_t usecs)439 _PUBLIC_ struct timeval timeval_add(const struct timeval *tv,
440 uint32_t secs, uint32_t usecs)
441 {
442 struct timeval tv2 = *tv;
443 const unsigned int million = 1000000;
444 tv2.tv_sec += secs;
445 tv2.tv_usec += usecs;
446 tv2.tv_sec += tv2.tv_usec / million;
447 tv2.tv_usec = tv2.tv_usec % million;
448 return tv2;
449 }
450
451 /**
452 return the sum of two timeval structures
453 */
timeval_sum(const struct timeval * tv1,const struct timeval * tv2)454 struct timeval timeval_sum(const struct timeval *tv1,
455 const struct timeval *tv2)
456 {
457 return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
458 }
459
460 /**
461 return a timeval secs/usecs into the future
462 */
timeval_current_ofs(uint32_t secs,uint32_t usecs)463 _PUBLIC_ struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
464 {
465 struct timeval tv = timeval_current();
466 return timeval_add(&tv, secs, usecs);
467 }
468
469 /**
470 compare two timeval structures.
471 Return -1 if tv1 < tv2
472 Return 0 if tv1 == tv2
473 Return 1 if tv1 > tv2
474 */
timeval_compare(const struct timeval * tv1,const struct timeval * tv2)475 _PUBLIC_ int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
476 {
477 if (tv1->tv_sec > tv2->tv_sec) return 1;
478 if (tv1->tv_sec < tv2->tv_sec) return -1;
479 if (tv1->tv_usec > tv2->tv_usec) return 1;
480 if (tv1->tv_usec < tv2->tv_usec) return -1;
481 return 0;
482 }
483
484 /**
485 return True if a timer is in the past
486 */
timeval_expired(const struct timeval * tv)487 _PUBLIC_ BOOL timeval_expired(const struct timeval *tv)
488 {
489 struct timeval tv2 = timeval_current();
490 if (tv2.tv_sec > tv->tv_sec) return True;
491 if (tv2.tv_sec < tv->tv_sec) return False;
492 return (tv2.tv_usec >= tv->tv_usec);
493 }
494
495 /**
496 return the number of seconds elapsed between two times
497 */
timeval_elapsed2(const struct timeval * tv1,const struct timeval * tv2)498 _PUBLIC_ double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
499 {
500 return (tv2->tv_sec - tv1->tv_sec) +
501 (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
502 }
503
504 /**
505 return the number of seconds elapsed since a given time
506 */
timeval_elapsed(const struct timeval * tv)507 _PUBLIC_ double timeval_elapsed(const struct timeval *tv)
508 {
509 struct timeval tv2 = timeval_current();
510 return timeval_elapsed2(tv, &tv2);
511 }
512
513 /**
514 return the lesser of two timevals
515 */
timeval_min(const struct timeval * tv1,const struct timeval * tv2)516 _PUBLIC_ struct timeval timeval_min(const struct timeval *tv1,
517 const struct timeval *tv2)
518 {
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;
522 return *tv2;
523 }
524
525 /**
526 return the greater of two timevals
527 */
timeval_max(const struct timeval * tv1,const struct timeval * tv2)528 _PUBLIC_ struct timeval timeval_max(const struct timeval *tv1,
529 const struct timeval *tv2)
530 {
531 if (tv1->tv_sec > tv2->tv_sec) return *tv1;
532 if (tv1->tv_sec < tv2->tv_sec) return *tv2;
533 if (tv1->tv_usec > tv2->tv_usec) return *tv1;
534 return *tv2;
535 }
536
537 /**
538 return the difference between two timevals as a timeval
539 if tv1 comes after tv2, then return a zero timeval
540 (this is *tv2 - *tv1)
541 */
timeval_until(const struct timeval * tv1,const struct timeval * tv2)542 _PUBLIC_ struct timeval timeval_until(const struct timeval *tv1,
543 const struct timeval *tv2)
544 {
545 struct timeval t;
546 if (timeval_compare(tv1, tv2) >= 0) {
547 return timeval_zero();
548 }
549 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
550 if (tv1->tv_usec > tv2->tv_usec) {
551 t.tv_sec--;
552 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
553 } else {
554 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
555 }
556 return t;
557 }
558
559
560 /**
561 convert a timeval to a NTTIME
562 */
timeval_to_nttime(const struct timeval * tv)563 _PUBLIC_ NTTIME timeval_to_nttime(const struct timeval *tv)
564 {
565 return 10*(tv->tv_usec +
566 ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
567 }
568
569 /**
570 convert a NTTIME to a timeval
571 */
nttime_to_timeval(struct timeval * tv,NTTIME t)572 _PUBLIC_ void nttime_to_timeval(struct timeval *tv, NTTIME t)
573 {
574 if (tv == NULL) return;
575
576 t += 10/2;
577 t /= 10;
578 t -= TIME_FIXUP_CONSTANT*1000*1000;
579
580 tv->tv_sec = t / 1000000;
581
582 if (TIME_T_MIN > tv->tv_sec || tv->tv_sec > TIME_T_MAX) {
583 tv->tv_sec = 0;
584 tv->tv_usec = 0;
585 return;
586 }
587
588 tv->tv_usec = t - tv->tv_sec*1000000;
589 }
590
591 /*******************************************************************
592 yield the difference between *A and *B, in seconds, ignoring leap seconds
593 ********************************************************************/
tm_diff(struct tm * a,struct tm * b)594 static int tm_diff(struct tm *a, struct tm *b)
595 {
596 int ay = a->tm_year + (1900 - 1);
597 int by = b->tm_year + (1900 - 1);
598 int intervening_leap_days =
599 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
600 int years = ay - by;
601 int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
602 int hours = 24*days + (a->tm_hour - b->tm_hour);
603 int minutes = 60*hours + (a->tm_min - b->tm_min);
604 int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
605
606 return seconds;
607 }
608
609 /**
610 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
611 */
get_time_zone(time_t t)612 _PUBLIC_ int get_time_zone(time_t t)
613 {
614 struct tm *tm = gmtime(&t);
615 struct tm tm_utc;
616 if (!tm)
617 return 0;
618 tm_utc = *tm;
619 tm = localtime(&t);
620 if (!tm)
621 return 0;
622 return tm_diff(&tm_utc,tm);
623 }
624