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