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