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