r12178: Make ldb_ildap work against localhost again, by setting the event
[samba.git] / source4 / 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 the date and time as a string
306 ****************************************************************************/
307 char *timestring(TALLOC_CTX *mem_ctx, time_t t)
308 {
309         char *TimeBuf;
310         char tempTime[80];
311         struct tm *tm;
312
313         tm = localtime(&t);
314         if (!tm) {
315                 return talloc_asprintf(mem_ctx,
316                                        "%ld seconds since the Epoch",
317                                        (long)t);
318         }
319
320 #ifdef HAVE_STRFTIME
321         /* some versions of gcc complain about using %c. This is a bug
322            in the gcc warning, not a bug in this code. See a recent
323            strftime() manual page for details.
324          */
325         strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
326         TimeBuf = talloc_strdup(mem_ctx, tempTime);
327 #else
328         TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
329 #endif
330
331         return TimeBuf;
332 }
333
334 /*
335   return a talloced string representing a NTTIME for human consumption
336 */
337 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
338 {
339         time_t t;
340         if (nt == 0) {
341                 return "NTTIME(0)";
342         }
343         t = nt_time_to_unix(nt);
344         return timestring(mem_ctx, t);
345 }
346
347
348 /*
349   put a NTTIME into a packet
350 */
351 void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
352 {
353         SBVAL(base, offset,   t);
354 }
355
356 /*
357   pull a NTTIME from a packet
358 */
359 NTTIME pull_nttime(uint8_t *base, uint16_t offset)
360 {
361         NTTIME ret = BVAL(base, offset);
362         return ret;
363 }
364
365 /*
366   parse a nttime as a large integer in a string and return a NTTIME
367 */
368 NTTIME nttime_from_string(const char *s)
369 {
370         return strtoull(s, NULL, 0);
371 }
372
373 /*
374   return (tv1 - tv2) in microseconds
375 */
376 int64_t usec_time_diff(struct timeval *tv1, struct timeval *tv2)
377 {
378         int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
379         return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
380 }
381
382
383 /*
384   return a zero timeval
385 */
386 struct timeval timeval_zero(void)
387 {
388         struct timeval tv;
389         tv.tv_sec = 0;
390         tv.tv_usec = 0;
391         return tv;
392 }
393
394 /*
395   return True if a timeval is zero
396 */
397 BOOL timeval_is_zero(const struct timeval *tv)
398 {
399         return tv->tv_sec == 0 && tv->tv_usec == 0;
400 }
401
402 /*
403   return a timeval for the current time
404 */
405 struct timeval timeval_current(void)
406 {
407         struct timeval tv;
408         GetTimeOfDay(&tv);
409         return tv;
410 }
411
412 /*
413   return a timeval struct with the given elements
414 */
415 struct timeval timeval_set(uint32_t secs, uint32_t usecs)
416 {
417         struct timeval tv;
418         tv.tv_sec = secs;
419         tv.tv_usec = usecs;
420         return tv;
421 }
422
423
424 /*
425   return a timeval ofs microseconds after tv
426 */
427 struct timeval timeval_add(const struct timeval *tv,
428                            uint32_t secs, uint32_t usecs)
429 {
430         struct timeval tv2 = *tv;
431         const uint_t million = 1000000;
432         tv2.tv_sec += secs;
433         tv2.tv_usec += usecs;
434         tv2.tv_sec += tv2.tv_usec / million;
435         tv2.tv_usec = tv2.tv_usec % million;
436         return tv2;
437 }
438
439 /*
440   return the sum of two timeval structures
441 */
442 struct timeval timeval_sum(const struct timeval *tv1,
443                            const struct timeval *tv2)
444 {
445         return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
446 }
447
448 /*
449   return a timeval secs/usecs into the future
450 */
451 struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
452 {
453         struct timeval tv = timeval_current();
454         return timeval_add(&tv, secs, usecs);
455 }
456
457 /*
458   compare two timeval structures. 
459   Return -1 if tv1 < tv2
460   Return 0 if tv1 == tv2
461   Return 1 if tv1 > tv2
462 */
463 int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
464 {
465         if (tv1->tv_sec  > tv2->tv_sec)  return 1;
466         if (tv1->tv_sec  < tv2->tv_sec)  return -1;
467         if (tv1->tv_usec > tv2->tv_usec) return 1;
468         if (tv1->tv_usec < tv2->tv_usec) return -1;
469         return 0;
470 }
471
472 /*
473   return True if a timer is in the past
474 */
475 BOOL timeval_expired(const struct timeval *tv)
476 {
477         struct timeval tv2 = timeval_current();
478         if (tv2.tv_sec > tv->tv_sec) return True;
479         if (tv2.tv_sec < tv->tv_sec) return False;
480         return (tv2.tv_usec >= tv->tv_usec);
481 }
482
483 /*
484   return the number of seconds elapsed between two times
485 */
486 double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
487 {
488         return (tv2->tv_sec - tv1->tv_sec) + 
489                (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
490 }
491
492 /*
493   return the number of seconds elapsed since a given time
494 */
495 double timeval_elapsed(const struct timeval *tv)
496 {
497         struct timeval tv2 = timeval_current();
498         return timeval_elapsed2(tv, &tv2);
499 }
500
501 /*
502   return the lesser of two timevals
503 */
504 struct timeval timeval_min(const struct timeval *tv1,
505                            const struct timeval *tv2)
506 {
507         if (tv1->tv_sec < tv2->tv_sec) return *tv1;
508         if (tv1->tv_sec > tv2->tv_sec) return *tv2;
509         if (tv1->tv_usec < tv2->tv_usec) return *tv1;
510         return *tv2;
511 }
512
513 /*
514   return the greater of two timevals
515 */
516 struct timeval timeval_max(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 difference between two timevals as a timeval
527   if tv1 comes after tv2, then return a zero timeval
528   (this is *tv2 - *tv1)
529 */
530 struct timeval timeval_until(const struct timeval *tv1,
531                              const struct timeval *tv2)
532 {
533         struct timeval t;
534         if (timeval_compare(tv1, tv2) >= 0) {
535                 return timeval_zero();
536         }
537         t.tv_sec = tv2->tv_sec - tv1->tv_sec;
538         if (tv1->tv_usec > tv2->tv_usec) {
539                 t.tv_sec--;
540                 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
541         } else {
542                 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
543         }
544         return t;
545 }
546
547
548 /*
549   convert a timeval to a NTTIME
550 */
551 NTTIME timeval_to_nttime(const struct timeval *tv)
552 {
553         return 10*(tv->tv_usec + 
554                   ((TIME_FIXUP_CONSTANT + (uint64_t)tv->tv_sec) * 1000000));
555 }
556
557 /*******************************************************************
558 yield the difference between *A and *B, in seconds, ignoring leap seconds
559 ********************************************************************/
560 static int tm_diff(struct tm *a, struct tm *b)
561 {
562         int ay = a->tm_year + (1900 - 1);
563         int by = b->tm_year + (1900 - 1);
564         int intervening_leap_days =
565                 (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
566         int years = ay - by;
567         int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
568         int hours = 24*days + (a->tm_hour - b->tm_hour);
569         int minutes = 60*hours + (a->tm_min - b->tm_min);
570         int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
571
572         return seconds;
573 }
574
575 /*******************************************************************
576   return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
577   ******************************************************************/
578 int get_time_zone(time_t t)
579 {
580         struct tm *tm = gmtime(&t);
581         struct tm tm_utc;
582         if (!tm)
583                 return 0;
584         tm_utc = *tm;
585         tm = localtime(&t);
586         if (!tm)
587                 return 0;
588         return tm_diff(&tm_utc,tm);
589 }