r9672: Fix build for compilers that don't like undefined-length arrays at the end...
[sfrench/samba-autobuild/.git] / source / lib / 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 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 #ifndef TIME_T_MIN
27 #define TIME_T_MIN 0
28 #endif
29 #ifndef TIME_T_MAX
30 #define TIME_T_MAX (~(time_t)0)
31 #endif
32
33 /*******************************************************************
34  External access to time_t_min and time_t_max.
35 ********************************************************************/
36 time_t get_time_t_max(void)
37 {
38         return TIME_T_MAX;
39 }
40
41 /*******************************************************************
42 a gettimeofday wrapper
43 ********************************************************************/
44 void GetTimeOfDay(struct timeval *tval)
45 {
46 #ifdef HAVE_GETTIMEOFDAY_TZ
47         gettimeofday(tval,NULL);
48 #else
49         gettimeofday(tval);
50 #endif
51 }
52
53
54 #define TIME_FIXUP_CONSTANT 11644473600LL
55
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)
61 {
62         if (nt == 0) {
63                 return 0;
64         }
65         if (nt == -1LL) {
66                 return (time_t)-1;
67         }
68         nt += 1000*1000*10/2;
69         nt /= 1000*1000*10;
70         nt -= TIME_FIXUP_CONSTANT;
71
72         if (TIME_T_MIN >= nt || nt >= TIME_T_MAX) {
73                 return 0;
74         }
75
76         return (time_t)nt;
77 }
78
79
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)
85 {
86         uint64_t t2; 
87
88         if (t == (time_t)-1) {
89                 *nt = (NTTIME)-1LL;
90                 return;
91         }               
92         if (t == 0) {
93                 *nt = 0;
94                 return;
95         }               
96
97         t2 = t;
98         t2 += TIME_FIXUP_CONSTANT;
99         t2 *= 1000*1000*10;
100
101         *nt = t2;
102 }
103
104
105 /****************************************************************************
106 check if it's a null unix time
107 ****************************************************************************/
108 BOOL null_time(time_t t)
109 {
110         return t == 0 || 
111                 t == (time_t)0xFFFFFFFF || 
112                 t == (time_t)-1;
113 }
114
115
116 /****************************************************************************
117 check if it's a null NTTIME
118 ****************************************************************************/
119 BOOL null_nttime(NTTIME t)
120 {
121         return t == 0 || t == (NTTIME)-1;
122 }
123
124 /*******************************************************************
125   create a 16 bit dos packed date
126 ********************************************************************/
127 static uint16_t make_dos_date1(struct tm *t)
128 {
129         uint16_t ret=0;
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));
132         return ret;
133 }
134
135 /*******************************************************************
136   create a 16 bit dos packed time
137 ********************************************************************/
138 static uint16_t make_dos_time1(struct tm *t)
139 {
140         uint16_t ret=0;
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));
143         return ret;
144 }
145
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)
151 {
152         struct tm *t;
153         uint32_t ret=0;
154
155         if (unixdate == 0) {
156                 return 0;
157         }
158
159         unixdate -= zone_offset;
160
161         t = gmtime(&unixdate);
162         if (!t) {
163                 return 0xFFFFFFFF;
164         }
165
166         ret = make_dos_date1(t);
167         ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
168
169         return ret;
170 }
171
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)
177 {
178         uint32_t x = make_dos_date(unixdate, zone_offset);
179         SIVAL(buf,offset,x);
180 }
181
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)
187 {
188         uint32_t x;
189         x = make_dos_date(unixdate, zone_offset);
190         x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
191         SIVAL(buf,offset,x);
192 }
193
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)
200 {
201         if (!null_time(unixdate)) {
202                 unixdate -= zone_offset;
203         }
204         SIVAL(buf,offset,unixdate);
205 }
206
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)
211 {
212         uint32_t p0,p1,p2,p3;
213
214         p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF; 
215         p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
216
217         *second = 2*(p0 & 0x1F);
218         *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
219         *hour = (p1>>3)&0xFF;
220         *day = (p2&0x1F);
221         *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
222         *year = ((p3>>1)&0xFF) + 80;
223 }
224
225 /*******************************************************************
226   create a unix date (int GMT) from a dos date (which is actually in
227   localtime)
228 ********************************************************************/
229 time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
230 {
231         uint32_t dos_date=0;
232         struct tm t;
233         time_t ret;
234
235         dos_date = IVAL(date_ptr,0);
236
237         if (dos_date == 0) return (time_t)0;
238   
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);
241         t.tm_isdst = -1;
242   
243         ret = timegm(&t);
244
245         ret += zone_offset;
246
247         return ret;
248 }
249
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)
254 {
255         uint32_t x,x2;
256
257         x = IVAL(date_ptr,0);
258         x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
259         SIVAL(&x,0,x2);
260
261         return pull_dos_date((void *)&x, zone_offset);
262 }
263
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)
269 {
270         time_t t = (time_t)IVAL(date_ptr,0);
271         if (!null_time(t)) {
272                 t += zone_offset;
273         }
274         return t;
275 }
276
277
278 /***************************************************************************
279 return a HTTP/1.0 time string
280   ***************************************************************************/
281 char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
282 {
283         char *buf;
284         char tempTime[60];
285         struct tm *tm = localtime(&t);
286
287         if (!tm) {
288                 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
289         }
290
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;
295         }
296 #else
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 */
300
301         return buf;
302 }
303
304 /*
305   return a LDAP time string
306 */
307 char *ldap_timestring(TALLOC_CTX *mem_ctx, time_t t)
308 {
309         struct tm *tm = gmtime(&t);
310
311         if (!tm) {
312                 return NULL;
313         }
314
315         /* formatted like: 20040408072012.0Z */
316         return talloc_asprintf(mem_ctx, 
317                                "%04u%02u%02u%02u%02u%02u.0Z",
318                                tm->tm_year+1900, tm->tm_mon+1,
319                                tm->tm_mday, tm->tm_hour, tm->tm_min,
320                                tm->tm_sec);
321 }
322
323
324 /*
325   convert a LDAP time string to a time_t. Return 0 if unable to convert
326 */
327 time_t ldap_string_to_time(const char *s)
328 {
329         struct tm tm;
330         
331         if (s == NULL) return 0;
332         
333         ZERO_STRUCT(tm);
334         if (sscanf(s, "%04u%02u%02u%02u%02u%02u.0Z", 
335                    &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 
336                    &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
337                 return 0;
338         }
339         tm.tm_year -= 1900;
340         tm.tm_mon -= 1;
341         
342         return timegm(&tm);
343 }
344
345 /****************************************************************************
346  Return the date and time as a string
347 ****************************************************************************/
348 char *timestring(TALLOC_CTX *mem_ctx, time_t t)
349 {
350         char *TimeBuf;
351         char tempTime[80];
352         struct tm *tm;
353
354         tm = localtime(&t);
355         if (!tm) {
356                 return talloc_asprintf(mem_ctx,
357                                        "%ld seconds since the Epoch",
358                                        (long)t);
359         }
360
361 #ifdef HAVE_STRFTIME
362         /* some versions of gcc complain about using %c. This is a bug
363            in the gcc warning, not a bug in this code. See a recent
364            strftime() manual page for details.
365          */
366         strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
367         TimeBuf = talloc_strdup(mem_ctx, tempTime);
368 #else
369         TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
370 #endif
371
372         return TimeBuf;
373 }
374
375 /*
376   return a talloced string representing a NTTIME for human consumption
377 */
378 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
379 {
380         time_t t;
381         if (nt == 0) {
382                 return "NTTIME(0)";
383         }
384         t = nt_time_to_unix(nt);
385         return timestring(mem_ctx, t);
386 }
387
388
389 /*
390   put a NTTIME into a packet
391 */
392 void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
393 {
394         SBVAL(base, offset,   t);
395 }
396
397 /*
398   pull a NTTIME from a packet
399 */
400 NTTIME pull_nttime(uint8_t *base, uint16_t offset)
401 {
402         NTTIME ret = BVAL(base, offset);
403         return ret;
404 }
405
406 /*
407   parse a nttime as a large integer in a string and return a NTTIME
408 */
409 NTTIME nttime_from_string(const char *s)
410 {
411         return strtoull(s, NULL, 0);
412 }
413
414 /*
415   return (tv1 - tv2) in microseconds
416 */
417 int64_t usec_time_diff(struct timeval *tv1, struct timeval *tv2)
418 {
419         int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
420         return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
421 }
422
423
424 /*
425   return a zero timeval
426 */
427 struct timeval timeval_zero(void)
428 {
429         struct timeval tv;
430         tv.tv_sec = 0;
431         tv.tv_usec = 0;
432         return tv;
433 }
434
435 /*
436   return True if a timeval is zero
437 */
438 BOOL timeval_is_zero(const struct timeval *tv)
439 {
440         return tv->tv_sec == 0 && tv->tv_usec == 0;
441 }
442
443 /*
444   return a timeval for the current time
445 */
446 struct timeval timeval_current(void)
447 {
448         struct timeval tv;
449         GetTimeOfDay(&tv);
450         return tv;
451 }
452
453 /*
454   return a timeval struct with the given elements
455 */
456 struct timeval timeval_set(uint32_t secs, uint32_t usecs)
457 {
458         struct timeval tv;
459         tv.tv_sec = secs;
460         tv.tv_usec = usecs;
461         return tv;
462 }
463
464
465 /*
466   return a timeval ofs microseconds after tv
467 */
468 struct timeval timeval_add(const struct timeval *tv,
469                            uint32_t secs, uint32_t usecs)
470 {
471         struct timeval tv2 = *tv;
472         const uint_t million = 1000000;
473         tv2.tv_sec += secs;
474         tv2.tv_usec += usecs;
475         tv2.tv_sec += tv2.tv_usec / million;
476         tv2.tv_usec = tv2.tv_usec % million;
477         return tv2;
478 }
479
480 /*
481   return the sum of two timeval structures
482 */
483 struct timeval timeval_sum(const struct timeval *tv1,
484                            const struct timeval *tv2)
485 {
486         return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
487 }
488
489 /*
490   return a timeval secs/usecs into the future
491 */
492 struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
493 {
494         struct timeval tv = timeval_current();
495         return timeval_add(&tv, secs, usecs);
496 }
497
498 /*
499   compare two timeval structures. 
500   Return -1 if tv1 < tv2
501   Return 0 if tv1 == tv2
502   Return 1 if tv1 > tv2
503 */
504 int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
505 {
506         if (tv1->tv_sec  > tv2->tv_sec)  return 1;
507         if (tv1->tv_sec  < tv2->tv_sec)  return -1;
508         if (tv1->tv_usec > tv2->tv_usec) return 1;
509         if (tv1->tv_usec < tv2->tv_usec) return -1;
510         return 0;
511 }
512
513 /*
514   return True if a timer is in the past
515 */
516 BOOL timeval_expired(const struct timeval *tv)
517 {
518         struct timeval tv2 = timeval_current();
519         if (tv2.tv_sec > tv->tv_sec) return True;
520         if (tv2.tv_sec < tv->tv_sec) return False;
521         return (tv2.tv_usec >= tv->tv_usec);
522 }
523
524 /*
525   return the number of seconds elapsed between two times
526 */
527 double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
528 {
529         return (tv2->tv_sec - tv1->tv_sec) + 
530                (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
531 }
532
533 /*
534   return the number of seconds elapsed since a given time
535 */
536 double timeval_elapsed(const struct timeval *tv)
537 {
538         struct timeval tv2 = timeval_current();
539         return timeval_elapsed2(tv, &tv2);
540 }
541
542 /*
543   return the lesser of two timevals
544 */
545 struct timeval timeval_min(const struct timeval *tv1,
546                            const struct timeval *tv2)
547 {
548         if (tv1->tv_sec < tv2->tv_sec) return *tv1;
549         if (tv1->tv_sec > tv2->tv_sec) return *tv2;
550         if (tv1->tv_usec < tv2->tv_usec) return *tv1;
551         return *tv2;
552 }
553
554 /*
555   return the greater of two timevals
556 */
557 struct timeval timeval_max(const struct timeval *tv1,
558                            const struct timeval *tv2)
559 {
560         if (tv1->tv_sec > tv2->tv_sec) return *tv1;
561         if (tv1->tv_sec < tv2->tv_sec) return *tv2;
562         if (tv1->tv_usec > tv2->tv_usec) return *tv1;
563         return *tv2;
564 }
565
566 /*
567   return the difference between two timevals as a timeval
568   if tv1 comes after tv2, then return a zero timeval
569   (this is *tv2 - *tv1)
570 */
571 struct timeval timeval_until(const struct timeval *tv1,
572                              const struct timeval *tv2)
573 {
574         struct timeval t;
575         if (timeval_compare(tv1, tv2) >= 0) {
576                 return timeval_zero();
577         }
578         t.tv_sec = tv2->tv_sec - tv1->tv_sec;
579         if (tv1->tv_usec > tv2->tv_usec) {
580                 t.tv_sec--;
581                 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
582         } else {
583                 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
584         }
585         return t;
586 }
587
588
589 /*
590   convert a timeval to a NTTIME
591 */
592 NTTIME timeval_to_nttime(const struct timeval *tv)
593 {
594         return 10*(tv->tv_usec + 
595                   ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
596 }