504b5d6ac3194c3d3be17cc71c2c0b0bf16a2015
[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   create a 16 bit dos packed date
117 ********************************************************************/
118 static uint16_t make_dos_date1(struct tm *t)
119 {
120         uint16_t ret=0;
121         ret = (((uint_t)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
122         ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
123         return ret;
124 }
125
126 /*******************************************************************
127   create a 16 bit dos packed time
128 ********************************************************************/
129 static uint16_t make_dos_time1(struct tm *t)
130 {
131         uint16_t ret=0;
132         ret = ((((uint_t)t->tm_min >> 3)&0x7) | (((uint_t)t->tm_hour) << 3));
133         ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
134         return ret;
135 }
136
137 /*******************************************************************
138   create a 32 bit dos packed date/time from some parameters
139   This takes a GMT time and returns a packed localtime structure
140 ********************************************************************/
141 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
142 {
143         struct tm *t;
144         uint32_t ret=0;
145
146         if (unixdate == 0) {
147                 return 0;
148         }
149
150         unixdate -= zone_offset;
151
152         t = gmtime(&unixdate);
153         if (!t) {
154                 return 0xFFFFFFFF;
155         }
156
157         ret = make_dos_date1(t);
158         ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
159
160         return ret;
161 }
162
163 /*******************************************************************
164 put a dos date into a buffer (time/date format)
165 This takes GMT time and puts local time in the buffer
166 ********************************************************************/
167 void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
168 {
169         uint32_t x = make_dos_date(unixdate, zone_offset);
170         SIVAL(buf,offset,x);
171 }
172
173 /*******************************************************************
174 put a dos date into a buffer (date/time format)
175 This takes GMT time and puts local time in the buffer
176 ********************************************************************/
177 void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
178 {
179         uint32_t x;
180         x = make_dos_date(unixdate, zone_offset);
181         x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
182         SIVAL(buf,offset,x);
183 }
184
185 /*******************************************************************
186 put a dos 32 bit "unix like" date into a buffer. This routine takes
187 GMT and converts it to LOCAL time before putting it (most SMBs assume
188 localtime for this sort of date)
189 ********************************************************************/
190 void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
191 {
192         if (!null_time(unixdate)) {
193                 unixdate -= zone_offset;
194         }
195         SIVAL(buf,offset,unixdate);
196 }
197
198 /*******************************************************************
199   interpret a 32 bit dos packed date/time to some parameters
200 ********************************************************************/
201 static void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
202 {
203         uint32_t p0,p1,p2,p3;
204
205         p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF; 
206         p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
207
208         *second = 2*(p0 & 0x1F);
209         *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
210         *hour = (p1>>3)&0xFF;
211         *day = (p2&0x1F);
212         *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
213         *year = ((p3>>1)&0xFF) + 80;
214 }
215
216 /*******************************************************************
217   create a unix date (int GMT) from a dos date (which is actually in
218   localtime)
219 ********************************************************************/
220 time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
221 {
222         uint32_t dos_date=0;
223         struct tm t;
224         time_t ret;
225
226         dos_date = IVAL(date_ptr,0);
227
228         if (dos_date == 0) return (time_t)0;
229   
230         interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
231                            &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
232         t.tm_isdst = -1;
233   
234         ret = timegm(&t);
235
236         ret += zone_offset;
237
238         return ret;
239 }
240
241 /*******************************************************************
242 like make_unix_date() but the words are reversed
243 ********************************************************************/
244 time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
245 {
246         uint32_t x,x2;
247
248         x = IVAL(date_ptr,0);
249         x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
250         SIVAL(&x,0,x2);
251
252         return pull_dos_date((void *)&x, zone_offset);
253 }
254
255 /*******************************************************************
256   create a unix GMT date from a dos date in 32 bit "unix like" format
257   these generally arrive as localtimes, with corresponding DST
258   ******************************************************************/
259 time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
260 {
261         time_t t = (time_t)IVAL(date_ptr,0);
262         if (!null_time(t)) {
263                 t += zone_offset;
264         }
265         return t;
266 }
267
268
269 /***************************************************************************
270 return a HTTP/1.0 time string
271   ***************************************************************************/
272 char *http_timestring(TALLOC_CTX *mem_ctx, time_t t)
273 {
274         char *buf;
275         char tempTime[60];
276         struct tm *tm = localtime(&t);
277
278         if (!tm) {
279                 return talloc_asprintf(mem_ctx,"%ld seconds since the Epoch",(long)t);
280         }
281
282 #ifndef HAVE_STRFTIME
283         buf = talloc_strdup(mem_ctx, asctime(tm));
284         if (buf[strlen(buf)-1] == '\n') {
285                 buf[strlen(buf)-1] = 0;
286         }
287 #else
288         strftime(tempTime, sizeof(tempTime)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
289         buf = talloc_strdup(mem_ctx, tempTime);
290 #endif /* !HAVE_STRFTIME */
291
292         return buf;
293 }
294
295 /***************************************************************************
296 return a LDAP time string
297   ***************************************************************************/
298 char *ldap_timestring(TALLOC_CTX *mem_ctx, time_t t)
299 {
300         struct tm *tm = gmtime(&t);
301
302         if (!tm) {
303                 return NULL;
304         }
305
306         /* formatted like: 20040408072012.0Z */
307         return talloc_asprintf(mem_ctx, 
308                                "%04u%02u%02u%02u%02u%02u.0Z",
309                                tm->tm_year+1900, tm->tm_mon+1,
310                                tm->tm_mday, tm->tm_hour, tm->tm_min,
311                                tm->tm_sec);
312 }
313
314 /****************************************************************************
315  Return the date and time as a string
316 ****************************************************************************/
317 char *timestring(TALLOC_CTX *mem_ctx, time_t t)
318 {
319         char *TimeBuf;
320         char tempTime[80];
321         struct tm *tm;
322
323         tm = localtime(&t);
324         if (!tm) {
325                 return talloc_asprintf(mem_ctx,
326                                        "%ld seconds since the Epoch",
327                                        (long)t);
328         }
329
330 #ifdef HAVE_STRFTIME
331         /* some versions of gcc complain about using %c. This is a bug
332            in the gcc warning, not a bug in this code. See a recent
333            strftime() manual page for details.
334          */
335         strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
336         TimeBuf = talloc_strdup(mem_ctx, tempTime);
337 #else
338         TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
339 #endif
340
341         return TimeBuf;
342 }
343
344 /*
345   return a talloced string representing a NTTIME for human consumption
346 */
347 const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
348 {
349         time_t t = nt_time_to_unix(nt);
350         return timestring(mem_ctx, t);
351 }
352
353
354 /*
355   put a NTTIME into a packet
356 */
357 void push_nttime(uint8_t *base, uint16_t offset, NTTIME t)
358 {
359         SBVAL(base, offset,   t);
360 }
361
362 /*
363   pull a NTTIME from a packet
364 */
365 NTTIME pull_nttime(uint8_t *base, uint16_t offset)
366 {
367         NTTIME ret = BVAL(base, offset);
368         return ret;
369 }
370
371 /*
372   parse a nttime as a large integer in a string and return a NTTIME
373 */
374 NTTIME nttime_from_string(const char *s)
375 {
376         return strtoull(s, NULL, 0);
377 }
378
379 /*
380   return (tv1 - tv2) in microseconds
381 */
382 int64_t usec_time_diff(struct timeval *tv1, struct timeval *tv2)
383 {
384         int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
385         return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
386 }
387
388
389 /*
390   return a zero timeval
391 */
392 struct timeval timeval_zero(void)
393 {
394         struct timeval tv;
395         tv.tv_sec = 0;
396         tv.tv_usec = 0;
397         return tv;
398 }
399
400 /*
401   return a timeval for the current time
402 */
403 struct timeval timeval_current(void)
404 {
405         struct timeval tv;
406         GetTimeOfDay(&tv);
407         return tv;
408 }
409
410 /*
411   return a timeval struct with the given elements
412 */
413 struct timeval timeval_set(uint32_t secs, uint32_t usecs)
414 {
415         struct timeval tv;
416         tv.tv_sec = secs;
417         tv.tv_usec = usecs;
418         return tv;
419 }
420
421
422 /*
423   return a timeval ofs microseconds after tv
424 */
425 struct timeval timeval_add(struct timeval *tv, uint32_t secs, uint32_t usecs)
426 {
427         struct timeval tv2 = *tv;
428         const uint_t million = 1000000;
429         tv2.tv_sec += secs;
430         tv2.tv_usec += usecs;
431         tv2.tv_sec += tv2.tv_usec / million;
432         tv2.tv_usec = tv2.tv_usec % million;
433         return tv2;
434 }
435
436 /*
437   return the sum of two timeval structures
438 */
439 struct timeval timeval_sum(struct timeval *tv1, struct timeval *tv2)
440 {
441         return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
442 }
443
444 /*
445   return a timeval secs/usecs into the future
446 */
447 struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
448 {
449         struct timeval tv = timeval_current();
450         return timeval_add(&tv, secs, usecs);
451 }
452
453 /*
454   compare two timeval structures. 
455   Return 1 if tv2 > tv1
456   Return 0 if tv2 == tv1
457   Return -1 if tv2 < tv1
458 */
459 int timeval_compare(struct timeval *tv1, struct timeval *tv2)
460 {
461         if (tv2->tv_sec  > tv1->tv_sec)  return 1;
462         if (tv2->tv_sec  < tv1->tv_sec)  return -1;
463         if (tv2->tv_usec > tv1->tv_usec) return 1;
464         if (tv2->tv_usec < tv1->tv_usec) return -1;
465         return 0;
466 }
467
468 /*
469   return True if a timer is in the past
470 */
471 BOOL timeval_expired(struct timeval *tv)
472 {
473         struct timeval tv2 = timeval_current();
474         if (tv2.tv_sec > tv->tv_sec) return True;
475         if (tv2.tv_sec < tv->tv_sec) return False;
476         return (tv2.tv_usec >= tv->tv_usec);
477 }
478
479 /*
480   return the number of seconds elapsed between two times
481 */
482 double timeval_elapsed2(struct timeval *tv1, struct timeval *tv2)
483 {
484         return (tv2->tv_sec - tv1->tv_sec) + 
485                (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
486 }
487
488 /*
489   return the number of seconds elapsed since a given time
490 */
491 double timeval_elapsed(struct timeval *tv)
492 {
493         struct timeval tv2 = timeval_current();
494         return timeval_elapsed2(tv, &tv2);
495 }
496
497 /*
498   return the lesser of two timevals
499 */
500 struct timeval timeval_min(struct timeval *tv1, struct timeval *tv2)
501 {
502         if (tv1->tv_sec < tv2->tv_sec) return *tv1;
503         if (tv1->tv_sec > tv2->tv_sec) return *tv2;
504         if (tv1->tv_usec < tv2->tv_usec) return *tv1;
505         return *tv2;
506 }
507
508 /*
509   return the greater of two timevals
510 */
511 struct timeval timeval_max(struct timeval *tv1, struct timeval *tv2)
512 {
513         if (tv1->tv_sec > tv2->tv_sec) return *tv1;
514         if (tv1->tv_sec < tv2->tv_sec) return *tv2;
515         if (tv1->tv_usec > tv2->tv_usec) return *tv1;
516         return *tv2;
517 }
518
519 /*
520   return the difference between two timevals as a timeval
521   if tv2 comes after tv1, then return a zero timeval
522   (this is *tv1 - *tv2)
523 */
524 struct timeval timeval_diff(struct timeval *tv1, struct timeval *tv2)
525 {
526         struct timeval t;
527         if (timeval_compare(tv1, tv2) >= 0) {
528                 return timeval_zero();
529         }
530         t.tv_sec = tv1->tv_sec - tv2->tv_sec;
531         if (tv2->tv_usec > tv1->tv_usec) {
532                 t.tv_sec--;
533                 t.tv_usec = 1000000 - (tv2->tv_usec - tv1->tv_usec);
534         } else {
535                 t.tv_usec = tv1->tv_usec - tv2->tv_usec;
536         }
537         return t;
538 }