b965479957afb3a7580fa6dd18fd4dab943171a7
[abartlet/samba.git/.git] / source3 / 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    Copyright (C) Jeremy Allison                 2007
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24
25 /**
26  * @file
27  * @brief time handling functions
28  */
29
30
31 #define NTTIME_INFINITY (NTTIME)0x8000000000000000LL
32
33 #if (SIZEOF_LONG == 8)
34 #define TIME_FIXUP_CONSTANT_INT 11644473600L
35 #elif (SIZEOF_LONG_LONG == 8)
36 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
37 #endif
38
39
40 /**
41   parse a nttime as a large integer in a string and return a NTTIME
42 */
43 NTTIME nttime_from_string(const char *s)
44 {
45         return strtoull(s, NULL, 0);
46 }
47
48 /**************************************************************
49  Handle conversions between time_t and uint32, taking care to
50  preserve the "special" values.
51 **************************************************************/
52
53 uint32_t convert_time_t_to_uint32(time_t t)
54 {
55 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
56         /* time_t is 64-bit. */
57         if (t == 0x8000000000000000LL) {
58                 return 0x80000000;
59         } else if (t == 0x7FFFFFFFFFFFFFFFLL) {
60                 return 0x7FFFFFFF;
61         }
62 #endif
63         return (uint32_t)t;
64 }
65
66 time_t convert_uint32_to_time_t(uint32_t u)
67 {
68 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
69         /* time_t is 64-bit. */
70         if (u == 0x80000000) {
71                 return (time_t)0x8000000000000000LL;
72         } else if (u == 0x7FFFFFFF) {
73                 return (time_t)0x7FFFFFFFFFFFFFFFLL;
74         }
75 #endif
76         return (time_t)u;
77 }
78
79 /****************************************************************************
80  Check if NTTIME is 0.
81 ****************************************************************************/
82
83 bool nt_time_is_zero(const NTTIME *nt)
84 {
85         return (*nt == 0);
86 }
87
88 /****************************************************************************
89  Convert ASN.1 GeneralizedTime string to unix-time.
90  Returns 0 on failure; Currently ignores timezone. 
91 ****************************************************************************/
92
93 time_t generalized_to_unix_time(const char *str)
94
95         struct tm tm;
96
97         ZERO_STRUCT(tm);
98
99         if (sscanf(str, "%4d%2d%2d%2d%2d%2d", 
100                    &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 
101                    &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
102                 return 0;
103         }
104         tm.tm_year -= 1900;
105         tm.tm_mon -= 1;
106
107         return timegm(&tm);
108 }
109
110 /*******************************************************************
111  Accessor function for the server time zone offset.
112  set_server_zone_offset() must have been called first.
113 ******************************************************************/
114
115 static int server_zone_offset;
116
117 int get_server_zone_offset(void)
118 {
119         return server_zone_offset;
120 }
121
122 /*******************************************************************
123  Initialize the server time zone offset. Called when a client connects.
124 ******************************************************************/
125
126 int set_server_zone_offset(time_t t)
127 {
128         server_zone_offset = get_time_zone(t);
129         return server_zone_offset;
130 }
131
132 /****************************************************************************
133  Return the date and time as a string
134 ****************************************************************************/
135
136 char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
137 {
138         fstring TimeBuf;
139         time_t t;
140         struct tm *tm;
141
142         t = (time_t)tp->tv_sec;
143         tm = localtime(&t);
144         if (!tm) {
145                 if (hires) {
146                         slprintf(TimeBuf,
147                                  sizeof(TimeBuf)-1,
148                                  "%ld.%06ld seconds since the Epoch",
149                                  (long)tp->tv_sec,
150                                  (long)tp->tv_usec);
151                 } else {
152                         slprintf(TimeBuf,
153                                  sizeof(TimeBuf)-1,
154                                  "%ld seconds since the Epoch",
155                                  (long)t);
156                 }
157         } else {
158 #ifdef HAVE_STRFTIME
159                 if (hires) {
160                         strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
161                         slprintf(TimeBuf+strlen(TimeBuf),
162                                  sizeof(TimeBuf)-1 - strlen(TimeBuf), 
163                                  ".%06ld", 
164                                  (long)tp->tv_usec);
165                 } else {
166                         strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
167                 }
168 #else
169                 if (hires) {
170                         const char *asct = asctime(tm);
171                         slprintf(TimeBuf, 
172                                  sizeof(TimeBuf)-1, 
173                                  "%s.%06ld", 
174                                  asct ? asct : "unknown", 
175                                  (long)tp->tv_usec);
176                 } else {
177                         const char *asct = asctime(tm);
178                         fstrcpy(TimeBuf, asct ? asct : "unknown");
179                 }
180 #endif
181         }
182         return talloc_strdup(ctx, TimeBuf);
183 }
184
185 char *current_timestring(TALLOC_CTX *ctx, bool hires)
186 {
187         struct timeval tv;
188
189         GetTimeOfDay(&tv);
190         return timeval_string(ctx, &tv, hires);
191 }
192
193
194
195 /***************************************************************************
196  Server versions of the above functions.
197 ***************************************************************************/
198
199 void srv_put_dos_date(char *buf,int offset,time_t unixdate)
200 {
201         push_dos_date((uint8_t *)buf, offset, unixdate, server_zone_offset);
202 }
203
204 void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
205 {
206         push_dos_date2((uint8_t *)buf, offset, unixdate, server_zone_offset);
207 }
208
209 void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
210 {
211         push_dos_date3((uint8_t *)buf, offset, unixdate, server_zone_offset);
212 }
213
214 void round_timespec(enum timestamp_set_resolution res, struct timespec *ts)
215 {
216         switch (res) {
217                 case TIMESTAMP_SET_SECONDS:
218                         round_timespec_to_sec(ts);
219                         break;
220                 case TIMESTAMP_SET_MSEC:
221                         round_timespec_to_usec(ts);
222                         break;
223                 case TIMESTAMP_SET_NT_OR_BETTER:
224                         /* No rounding needed. */
225                         break;
226         }
227 }
228
229 /****************************************************************************
230  Take a Unix time and convert to an NTTIME structure and place in buffer 
231  pointed to by p, rounded to the correct resolution.
232 ****************************************************************************/
233
234 void put_long_date_timespec(enum timestamp_set_resolution res, char *p, struct timespec ts)
235 {
236         NTTIME nt;
237         round_timespec(res, &ts);
238         unix_timespec_to_nt_time(&nt, ts);
239         SIVAL(p, 0, nt & 0xFFFFFFFF);
240         SIVAL(p, 4, nt >> 32);
241 }
242
243 void put_long_date(char *p, time_t t)
244 {
245         struct timespec ts;
246         ts.tv_sec = t;
247         ts.tv_nsec = 0;
248         put_long_date_timespec(TIMESTAMP_SET_SECONDS, p, ts);
249 }
250
251 void dos_filetime_timespec(struct timespec *tsp)
252 {
253         tsp->tv_sec &= ~1;
254         tsp->tv_nsec = 0;
255 }
256
257 /*******************************************************************
258  Create a unix date (int GMT) from a dos date (which is actually in
259  localtime).
260 ********************************************************************/
261
262 static time_t make_unix_date(const void *date_ptr, int zone_offset)
263 {
264         uint32_t dos_date=0;
265         struct tm t;
266         time_t ret;
267
268         dos_date = IVAL(date_ptr,0);
269
270         if (dos_date == 0) {
271                 return 0;
272         }
273   
274         interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
275                         &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
276         t.tm_isdst = -1;
277   
278         ret = timegm(&t);
279
280         ret += zone_offset;
281
282         return(ret);
283 }
284
285 /*******************************************************************
286  Like make_unix_date() but the words are reversed.
287 ********************************************************************/
288
289 time_t make_unix_date2(const void *date_ptr, int zone_offset)
290 {
291         uint32_t x,x2;
292
293         x = IVAL(date_ptr,0);
294         x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
295         SIVAL(&x,0,x2);
296
297         return(make_unix_date((const void *)&x, zone_offset));
298 }
299
300 /*******************************************************************
301  Create a unix GMT date from a dos date in 32 bit "unix like" format
302  these generally arrive as localtimes, with corresponding DST.
303 ******************************************************************/
304
305 time_t make_unix_date3(const void *date_ptr, int zone_offset)
306 {
307         time_t t = (time_t)IVAL(date_ptr,0);
308         if (!null_time(t)) {
309                 t += zone_offset;
310         }
311         return(t);
312 }
313
314 time_t srv_make_unix_date(const void *date_ptr)
315 {
316         return make_unix_date(date_ptr, server_zone_offset);
317 }
318
319 time_t srv_make_unix_date2(const void *date_ptr)
320 {
321         return make_unix_date2(date_ptr, server_zone_offset);
322 }
323
324 time_t srv_make_unix_date3(const void *date_ptr)
325 {
326         return make_unix_date3(date_ptr, server_zone_offset);
327 }
328
329 /****************************************************************************
330  Convert a normalized timeval to a timespec.
331 ****************************************************************************/
332
333 struct timespec convert_timeval_to_timespec(const struct timeval tv)
334 {
335         struct timespec ts;
336         ts.tv_sec = tv.tv_sec;
337         ts.tv_nsec = tv.tv_usec * 1000;
338         return ts;
339 }
340
341 /****************************************************************************
342  Convert a normalized timespec to a timeval.
343 ****************************************************************************/
344
345 struct timeval convert_timespec_to_timeval(const struct timespec ts)
346 {
347         struct timeval tv;
348         tv.tv_sec = ts.tv_sec;
349         tv.tv_usec = ts.tv_nsec / 1000;
350         return tv;
351 }
352
353 /****************************************************************************
354  Return a timespec for the current time
355 ****************************************************************************/
356
357 struct timespec timespec_current(void)
358 {
359         struct timeval tv;
360         struct timespec ts;
361         GetTimeOfDay(&tv);
362         ts.tv_sec = tv.tv_sec;
363         ts.tv_nsec = tv.tv_usec * 1000;
364         return ts;
365 }
366
367 /****************************************************************************
368  Return the lesser of two timespecs.
369 ****************************************************************************/
370
371 struct timespec timespec_min(const struct timespec *ts1,
372                            const struct timespec *ts2)
373 {
374         if (ts1->tv_sec < ts2->tv_sec) return *ts1;
375         if (ts1->tv_sec > ts2->tv_sec) return *ts2;
376         if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
377         return *ts2;
378 }
379
380 /****************************************************************************
381   compare two timespec structures. 
382   Return -1 if ts1 < ts2
383   Return 0 if ts1 == ts2
384   Return 1 if ts1 > ts2
385 ****************************************************************************/
386
387 int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
388 {
389         if (ts1->tv_sec  > ts2->tv_sec)  return 1;
390         if (ts1->tv_sec  < ts2->tv_sec)  return -1;
391         if (ts1->tv_nsec > ts2->tv_nsec) return 1;
392         if (ts1->tv_nsec < ts2->tv_nsec) return -1;
393         return 0;
394 }
395
396 /****************************************************************************
397  Round up a timespec if nsec > 500000000, round down if lower,
398  then zero nsec.
399 ****************************************************************************/
400
401 void round_timespec_to_sec(struct timespec *ts)
402 {
403         ts->tv_sec = convert_timespec_to_time_t(*ts);
404         ts->tv_nsec = 0;
405 }
406
407 /****************************************************************************
408  Round a timespec to usec value.
409 ****************************************************************************/
410
411 void round_timespec_to_usec(struct timespec *ts)
412 {
413         struct timeval tv = convert_timespec_to_timeval(*ts);
414         *ts = convert_timeval_to_timespec(tv);
415 }
416
417 /****************************************************************************
418  Interprets an nt time into a unix struct timespec.
419  Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
420  will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
421 ****************************************************************************/
422
423 struct timespec interpret_long_date(const char *p)
424 {
425         NTTIME nt;
426         nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);
427         if (nt == (uint64_t)-1) {
428                 struct timespec ret;
429                 ret.tv_sec = (time_t)-1;
430                 ret.tv_nsec = 0;
431                 return ret;
432         }
433         return nt_time_to_unix_timespec(&nt);
434 }
435
436 /***************************************************************************
437  Client versions of the above functions.
438 ***************************************************************************/
439
440 void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate)
441 {
442         push_dos_date((uint8_t *)buf, offset, unixdate, cli->serverzone);
443 }
444
445 void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate)
446 {
447         push_dos_date2((uint8_t *)buf, offset, unixdate, cli->serverzone);
448 }
449
450 void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate)
451 {
452         push_dos_date3((uint8_t *)buf, offset, unixdate, cli->serverzone);
453 }
454
455 time_t cli_make_unix_date(struct cli_state *cli, const void *date_ptr)
456 {
457         return make_unix_date(date_ptr, cli->serverzone);
458 }
459
460 time_t cli_make_unix_date2(struct cli_state *cli, const void *date_ptr)
461 {
462         return make_unix_date2(date_ptr, cli->serverzone);
463 }
464
465 time_t cli_make_unix_date3(struct cli_state *cli, const void *date_ptr)
466 {
467         return make_unix_date3(date_ptr, cli->serverzone);
468 }
469
470
471 /*******************************************************************
472  Re-read the smb serverzone value.
473 ******************************************************************/
474
475 static struct timeval start_time_hires;
476
477 void TimeInit(void)
478 {
479         set_server_zone_offset(time(NULL));
480
481         DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
482
483         /* Save the start time of this process. */
484         if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
485                 GetTimeOfDay(&start_time_hires);
486         }
487 }
488
489 /**********************************************************************
490  Return a timeval struct of the uptime of this process. As TimeInit is
491  done before a daemon fork then this is the start time from the parent
492  daemon start. JRA.
493 ***********************************************************************/
494
495 void get_process_uptime(struct timeval *ret_time)
496 {
497         struct timeval time_now_hires;
498
499         GetTimeOfDay(&time_now_hires);
500         ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
501         if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
502                 ret_time->tv_sec -= 1;
503                 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
504         } else {
505                 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
506         }
507 }
508
509 /**
510  * @brief Get the startup time of the server.
511  *
512  * @param[out] ret_time A pointer to a timveal structure to set the startup
513  *                      time.
514  */
515 void get_startup_time(struct timeval *ret_time)
516 {
517         ret_time->tv_sec = start_time_hires.tv_sec;
518         ret_time->tv_usec = start_time_hires.tv_usec;
519 }
520
521
522 /****************************************************************************
523  Convert a NTTIME structure to a time_t.
524  It's originally in "100ns units".
525
526  This is an absolute version of the one above.
527  By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
528  if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
529 ****************************************************************************/
530
531 time_t nt_time_to_unix_abs(const NTTIME *nt)
532 {
533         uint64_t d;
534
535         if (*nt == 0) {
536                 return (time_t)0;
537         }
538
539         if (*nt == (uint64_t)-1) {
540                 return (time_t)-1;
541         }
542
543         if (*nt == NTTIME_INFINITY) {
544                 return (time_t)-1;
545         }
546
547         /* reverse the time */
548         /* it's a negative value, turn it to positive */
549         d=~*nt;
550
551         d += 1000*1000*10/2;
552         d /= 1000*1000*10;
553
554         if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
555                 return (time_t)0;
556         }
557
558         return (time_t)d;
559 }
560
561 time_t uint64s_nt_time_to_unix_abs(const uint64_t *src)
562 {
563         NTTIME nttime;
564         nttime = *src;
565         return nt_time_to_unix_abs(&nttime);
566 }
567
568 /****************************************************************************
569  Put a 8 byte filetime from a struct timespec. Uses GMT.
570 ****************************************************************************/
571
572 void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
573 {
574         uint64_t d;
575
576         if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
577                 *nt = 0;
578                 return;
579         }
580         if (ts.tv_sec == TIME_T_MAX) {
581                 *nt = 0x7fffffffffffffffLL;
582                 return;
583         }               
584         if (ts.tv_sec == (time_t)-1) {
585                 *nt = (uint64_t)-1;
586                 return;
587         }               
588
589         d = ts.tv_sec;
590         d += TIME_FIXUP_CONSTANT_INT;
591         d *= 1000*1000*10;
592         /* d is now in 100ns units. */
593         d += (ts.tv_nsec / 100);
594
595         *nt = d;
596 }
597
598 #if 0
599 void nt_time_to_unix_timespec(struct timespec *ts, NTTIME t)
600 {
601         if (ts == NULL) {
602                 return;
603         }
604
605         /* t starts in 100 nsec units since 1601-01-01. */
606
607         t *= 100;
608         /* t is now in nsec units since 1601-01-01. */
609
610         t -= TIME_FIXUP_CONSTANT*1000*1000*100;
611         /* t is now in nsec units since the UNIX epoch 1970-01-01. */
612
613         ts->tv_sec  = t / 1000000000LL;
614
615         if (TIME_T_MIN > ts->tv_sec || ts->tv_sec > TIME_T_MAX) {
616                 ts->tv_sec  = 0;
617                 ts->tv_nsec = 0;
618                 return;
619         }
620
621         ts->tv_nsec = t - ts->tv_sec*1000000000LL;
622 }
623 #endif
624
625 /****************************************************************************
626  Convert a time_t to a NTTIME structure
627
628  This is an absolute version of the one above.
629  By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
630  If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
631 ****************************************************************************/
632
633 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
634 {
635         double d;
636
637         if (t==0) {
638                 *nt = 0;
639                 return;
640         }
641
642         if (t == TIME_T_MAX) {
643                 *nt = 0x7fffffffffffffffLL;
644                 return;
645         }
646                 
647         if (t == (time_t)-1) {
648                 /* that's what NT uses for infinite */
649                 *nt = NTTIME_INFINITY;
650                 return;
651         }               
652
653         d = (double)(t);
654         d *= 1.0e7;
655
656         *nt = (NTTIME)d;
657
658         /* convert to a negative value */
659         *nt=~*nt;
660 }
661
662
663 /****************************************************************************
664  Utility function that always returns a const string even if localtime
665  and asctime fail.
666 ****************************************************************************/
667
668 const char *time_to_asc(const time_t t)
669 {
670         const char *asct;
671         struct tm *lt = localtime(&t);
672
673         if (!lt) {
674                 return "unknown time";
675         }
676
677         asct = asctime(lt);
678         if (!asct) {
679                 return "unknown time";
680         }
681         return asct;
682 }
683
684 const char *display_time(NTTIME nttime)
685 {
686         float high;
687         float low;
688         int sec;
689         int days, hours, mins, secs;
690
691         if (nttime==0)
692                 return "Now";
693
694         if (nttime==NTTIME_INFINITY)
695                 return "Never";
696
697         high = 65536;   
698         high = high/10000;
699         high = high*65536;
700         high = high/1000;
701         high = high * (~(nttime >> 32));
702
703         low = ~(nttime & 0xFFFFFFFF);
704         low = low/(1000*1000*10);
705
706         sec=(int)(high+low);
707
708         days=sec/(60*60*24);
709         hours=(sec - (days*60*60*24)) / (60*60);
710         mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
711         secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
712
713         return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
714                                "%u seconds", days, hours, mins, secs);
715 }
716
717 bool nt_time_is_set(const NTTIME *nt)
718 {
719         if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
720                 return false;
721         }
722
723         if (*nt == NTTIME_INFINITY) {
724                 return false;
725         }
726
727         return true;
728 }