Merge branch 'master' of ssh://git.samba.org/data/git/samba into regsrv
[metze/samba/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 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;
119 }
120
121 time_t convert_uint32_to_time_t(uint32 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 int extra_time_offset=0;
135
136 /****************************************************************************
137  Check if NTTIME is 0.
138 ****************************************************************************/
139
140 bool nt_time_is_zero(const NTTIME *nt)
141 {
142         return (*nt == 0);
143 }
144
145 /****************************************************************************
146  Convert ASN.1 GeneralizedTime string to unix-time.
147  Returns 0 on failure; Currently ignores timezone. 
148 ****************************************************************************/
149
150 time_t generalized_to_unix_time(const char *str)
151
152         struct tm tm;
153
154         ZERO_STRUCT(tm);
155
156         if (sscanf(str, "%4d%2d%2d%2d%2d%2d", 
157                    &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 
158                    &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
159                 return 0;
160         }
161         tm.tm_year -= 1900;
162         tm.tm_mon -= 1;
163
164         return timegm(&tm);
165 }
166
167 /*******************************************************************
168  Accessor function for the server time zone offset.
169  set_server_zone_offset() must have been called first.
170 ******************************************************************/
171
172 static int server_zone_offset;
173
174 int get_server_zone_offset(void)
175 {
176         return server_zone_offset;
177 }
178
179 /*******************************************************************
180  Initialize the server time zone offset. Called when a client connects.
181 ******************************************************************/
182
183 int set_server_zone_offset(time_t t)
184 {
185         server_zone_offset = get_time_zone(t);
186         return server_zone_offset;
187 }
188
189 /****************************************************************************
190  Return the date and time as a string
191 ****************************************************************************/
192
193 char *current_timestring(TALLOC_CTX *ctx, bool hires)
194 {
195         fstring TimeBuf;
196         struct timeval tp;
197         time_t t;
198         struct tm *tm;
199
200         if (hires) {
201                 GetTimeOfDay(&tp);
202                 t = (time_t)tp.tv_sec;
203         } else {
204                 t = time(NULL);
205         }
206         tm = localtime(&t);
207         if (!tm) {
208                 if (hires) {
209                         slprintf(TimeBuf,
210                                  sizeof(TimeBuf)-1,
211                                  "%ld.%06ld seconds since the Epoch",
212                                  (long)tp.tv_sec, 
213                                  (long)tp.tv_usec);
214                 } else {
215                         slprintf(TimeBuf,
216                                  sizeof(TimeBuf)-1,
217                                  "%ld seconds since the Epoch",
218                                  (long)t);
219                 }
220         } else {
221 #ifdef HAVE_STRFTIME
222                 if (hires) {
223                         strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
224                         slprintf(TimeBuf+strlen(TimeBuf),
225                                  sizeof(TimeBuf)-1 - strlen(TimeBuf), 
226                                  ".%06ld", 
227                                  (long)tp.tv_usec);
228                 } else {
229                         strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
230                 }
231 #else
232                 if (hires) {
233                         const char *asct = asctime(tm);
234                         slprintf(TimeBuf, 
235                                  sizeof(TimeBuf)-1, 
236                                  "%s.%06ld", 
237                                  asct ? asct : "unknown", 
238                                  (long)tp.tv_usec);
239                 } else {
240                         const char *asct = asctime(tm);
241                         fstrcpy(TimeBuf, asct ? asct : "unknown");
242                 }
243 #endif
244         }
245         return talloc_strdup(ctx, TimeBuf);
246 }
247
248
249 /*******************************************************************
250  Put a dos date into a buffer (time/date format).
251  This takes GMT time and puts local time in the buffer.
252 ********************************************************************/
253
254 static void put_dos_date(char *buf,int offset,time_t unixdate, int zone_offset)
255 {
256         uint32 x = make_dos_date(unixdate, zone_offset);
257         SIVAL(buf,offset,x);
258 }
259
260 /*******************************************************************
261  Put a dos date into a buffer (date/time format).
262  This takes GMT time and puts local time in the buffer.
263 ********************************************************************/
264
265 static void put_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)
266 {
267         uint32 x = make_dos_date(unixdate, zone_offset);
268         x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
269         SIVAL(buf,offset,x);
270 }
271
272 /*******************************************************************
273  Put a dos 32 bit "unix like" date into a buffer. This routine takes
274  GMT and converts it to LOCAL time before putting it (most SMBs assume
275  localtime for this sort of date)
276 ********************************************************************/
277
278 static void put_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
279 {
280         if (!null_mtime(unixdate)) {
281                 unixdate -= zone_offset;
282         }
283         SIVAL(buf,offset,unixdate);
284 }
285
286
287 /***************************************************************************
288  Server versions of the above functions.
289 ***************************************************************************/
290
291 void srv_put_dos_date(char *buf,int offset,time_t unixdate)
292 {
293         put_dos_date(buf, offset, unixdate, server_zone_offset);
294 }
295
296 void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
297 {
298         put_dos_date2(buf, offset, unixdate, server_zone_offset);
299 }
300
301 void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
302 {
303         put_dos_date3(buf, offset, unixdate, server_zone_offset);
304 }
305
306 /****************************************************************************
307  Take a Unix time and convert to an NTTIME structure and place in buffer 
308  pointed to by p.
309 ****************************************************************************/
310
311 void put_long_date_timespec(char *p, struct timespec ts)
312 {
313         NTTIME nt;
314         unix_timespec_to_nt_time(&nt, ts);
315         SIVAL(p, 0, nt & 0xFFFFFFFF);
316         SIVAL(p, 4, nt >> 32);
317 }
318
319 void put_long_date(char *p, time_t t)
320 {
321         struct timespec ts;
322         ts.tv_sec = t;
323         ts.tv_nsec = 0;
324         put_long_date_timespec(p, ts);
325 }
326
327 /****************************************************************************
328  Return the best approximation to a 'create time' under UNIX from a stat
329  structure.
330 ****************************************************************************/
331
332 static time_t calc_create_time(const SMB_STRUCT_STAT *st)
333 {
334         time_t ret, ret1;
335
336         ret = MIN(st->st_ctime, st->st_mtime);
337         ret1 = MIN(ret, st->st_atime);
338
339         if(ret1 != (time_t)0) {
340                 return ret1;
341         }
342
343         /*
344          * One of ctime, mtime or atime was zero (probably atime).
345          * Just return MIN(ctime, mtime).
346          */
347         return ret;
348 }
349
350 /****************************************************************************
351  Return the 'create time' from a stat struct if it exists (birthtime) or else
352  use the best approximation.
353 ****************************************************************************/
354
355 struct timespec get_create_timespec(const SMB_STRUCT_STAT *pst,bool fake_dirs)
356 {
357         struct timespec ret;
358
359         if(S_ISDIR(pst->st_mode) && fake_dirs) {
360                 ret.tv_sec = 315493200L;          /* 1/1/1980 */
361                 ret.tv_nsec = 0;
362                 return ret;
363         }
364
365 #if defined(HAVE_STAT_ST_BIRTHTIMESPEC)
366         ret = pst->st_birthtimespec;
367 #elif defined(HAVE_STAT_ST_BIRTHTIMENSEC)
368         ret.tv_sec = pst->st_birthtime;
369         ret.tv_nsec = pst->st_birthtimenspec;
370 #elif defined(HAVE_STAT_ST_BIRTHTIME)
371         ret.tv_sec = pst->st_birthtime;
372         ret.tv_nsec = 0;
373 #else
374         ret.tv_sec = calc_create_time(pst);
375         ret.tv_nsec = 0;
376 #endif
377
378         /* Deal with systems that don't initialize birthtime correctly.
379          * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
380          */
381         if (null_timespec(ret)) {
382                 ret.tv_sec = calc_create_time(pst);
383                 ret.tv_nsec = 0;
384         }
385         return ret;
386 }
387
388 /****************************************************************************
389  Get/Set all the possible time fields from a stat struct as a timespec.
390 ****************************************************************************/
391
392 struct timespec get_atimespec(const SMB_STRUCT_STAT *pst)
393 {
394 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
395         struct timespec ret;
396
397         /* Old system - no ns timestamp. */
398         ret.tv_sec = pst->st_atime;
399         ret.tv_nsec = 0;
400         return ret;
401 #else
402 #if defined(HAVE_STAT_ST_ATIM)
403         return pst->st_atim;
404 #elif defined(HAVE_STAT_ST_ATIMENSEC)
405         struct timespec ret;
406         ret.tv_sec = pst->st_atime;
407         ret.tv_nsec = pst->st_atimensec;
408         return ret;
409 #else
410 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
411 #endif
412 #endif
413 }
414
415 void set_atimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
416 {
417 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
418         /* Old system - no ns timestamp. */
419         pst->st_atime = ts.tv_sec;
420 #else
421 #if defined(HAVE_STAT_ST_ATIM)
422         pst->st_atim = ts;
423 #elif defined(HAVE_STAT_ST_ATIMENSEC)
424         pst->st_atime = ts.tv_sec;
425         pst->st_atimensec = ts.tv_nsec
426 #else
427 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
428 #endif
429 #endif
430 }
431
432 struct timespec get_mtimespec(const SMB_STRUCT_STAT *pst)
433 {
434 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
435         struct timespec ret;
436
437         /* Old system - no ns timestamp. */
438         ret.tv_sec = pst->st_mtime;
439         ret.tv_nsec = 0;
440         return ret;
441 #else
442 #if defined(HAVE_STAT_ST_MTIM)
443         return pst->st_mtim;
444 #elif defined(HAVE_STAT_ST_MTIMENSEC)
445         struct timespec ret;
446         ret.tv_sec = pst->st_mtime;
447         ret.tv_nsec = pst->st_mtimensec;
448         return ret;
449 #else
450 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
451 #endif
452 #endif
453 }
454
455 void set_mtimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
456 {
457 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
458         /* Old system - no ns timestamp. */
459         pst->st_mtime = ts.tv_sec;
460 #else
461 #if defined(HAVE_STAT_ST_MTIM)
462         pst->st_mtim = ts;
463 #elif defined(HAVE_STAT_ST_MTIMENSEC)
464         pst->st_mtime = ts.tv_sec;
465         pst->st_mtimensec = ts.tv_nsec
466 #else
467 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
468 #endif
469 #endif
470 }
471
472 struct timespec get_ctimespec(const SMB_STRUCT_STAT *pst)
473 {
474 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
475         struct timespec ret;
476
477         /* Old system - no ns timestamp. */
478         ret.tv_sec = pst->st_ctime;
479         ret.tv_nsec = 0;
480         return ret;
481 #else
482 #if defined(HAVE_STAT_ST_CTIM)
483         return pst->st_ctim;
484 #elif defined(HAVE_STAT_ST_CTIMENSEC)
485         struct timespec ret;
486         ret.tv_sec = pst->st_ctime;
487         ret.tv_nsec = pst->st_ctimensec;
488         return ret;
489 #else
490 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
491 #endif
492 #endif
493 }
494
495 void set_ctimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
496 {
497 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
498         /* Old system - no ns timestamp. */
499         pst->st_ctime = ts.tv_sec;
500 #else
501 #if defined(HAVE_STAT_ST_CTIM)
502         pst->st_ctim = ts;
503 #elif defined(HAVE_STAT_ST_CTIMENSEC)
504         pst->st_ctime = ts.tv_sec;
505         pst->st_ctimensec = ts.tv_nsec
506 #else
507 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
508 #endif
509 #endif
510 }
511
512 void dos_filetime_timespec(struct timespec *tsp)
513 {
514         tsp->tv_sec &= ~1;
515         tsp->tv_nsec = 0;
516 }
517
518 /*******************************************************************
519  Create a unix date (int GMT) from a dos date (which is actually in
520  localtime).
521 ********************************************************************/
522
523 static time_t make_unix_date(const void *date_ptr, int zone_offset)
524 {
525         uint32 dos_date=0;
526         struct tm t;
527         time_t ret;
528
529         dos_date = IVAL(date_ptr,0);
530
531         if (dos_date == 0) {
532                 return 0;
533         }
534   
535         interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
536                         &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
537         t.tm_isdst = -1;
538   
539         ret = timegm(&t);
540
541         ret += zone_offset;
542
543         return(ret);
544 }
545
546 /*******************************************************************
547  Like make_unix_date() but the words are reversed.
548 ********************************************************************/
549
550 static time_t make_unix_date2(const void *date_ptr, int zone_offset)
551 {
552         uint32 x,x2;
553
554         x = IVAL(date_ptr,0);
555         x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
556         SIVAL(&x,0,x2);
557
558         return(make_unix_date((const void *)&x, zone_offset));
559 }
560
561 /*******************************************************************
562  Create a unix GMT date from a dos date in 32 bit "unix like" format
563  these generally arrive as localtimes, with corresponding DST.
564 ******************************************************************/
565
566 static time_t make_unix_date3(const void *date_ptr, int zone_offset)
567 {
568         time_t t = (time_t)IVAL(date_ptr,0);
569         if (!null_mtime(t)) {
570                 t += zone_offset;
571         }
572         return(t);
573 }
574
575 time_t srv_make_unix_date(const void *date_ptr)
576 {
577         return make_unix_date(date_ptr, server_zone_offset);
578 }
579
580 time_t srv_make_unix_date2(const void *date_ptr)
581 {
582         return make_unix_date2(date_ptr, server_zone_offset);
583 }
584
585 time_t srv_make_unix_date3(const void *date_ptr)
586 {
587         return make_unix_date3(date_ptr, server_zone_offset);
588 }
589
590 /****************************************************************************
591  Convert a normalized timeval to a timespec.
592 ****************************************************************************/
593
594 struct timespec convert_timeval_to_timespec(const struct timeval tv)
595 {
596         struct timespec ts;
597         ts.tv_sec = tv.tv_sec;
598         ts.tv_nsec = tv.tv_usec * 1000;
599         return ts;
600 }
601
602 /****************************************************************************
603  Convert a normalized timespec to a timeval.
604 ****************************************************************************/
605
606 struct timeval convert_timespec_to_timeval(const struct timespec ts)
607 {
608         struct timeval tv;
609         tv.tv_sec = ts.tv_sec;
610         tv.tv_usec = ts.tv_nsec / 1000;
611         return tv;
612 }
613
614 /****************************************************************************
615  Return a timespec for the current time
616 ****************************************************************************/
617
618 struct timespec timespec_current(void)
619 {
620         struct timeval tv;
621         struct timespec ts;
622         GetTimeOfDay(&tv);
623         ts.tv_sec = tv.tv_sec;
624         ts.tv_nsec = tv.tv_usec * 1000;
625         return ts;
626 }
627
628 /****************************************************************************
629  Return the lesser of two timespecs.
630 ****************************************************************************/
631
632 struct timespec timespec_min(const struct timespec *ts1,
633                            const struct timespec *ts2)
634 {
635         if (ts1->tv_sec < ts2->tv_sec) return *ts1;
636         if (ts1->tv_sec > ts2->tv_sec) return *ts2;
637         if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
638         return *ts2;
639 }
640
641 /****************************************************************************
642   compare two timespec structures. 
643   Return -1 if ts1 < ts2
644   Return 0 if ts1 == ts2
645   Return 1 if ts1 > ts2
646 ****************************************************************************/
647
648 int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
649 {
650         if (ts1->tv_sec  > ts2->tv_sec)  return 1;
651         if (ts1->tv_sec  < ts2->tv_sec)  return -1;
652         if (ts1->tv_nsec > ts2->tv_nsec) return 1;
653         if (ts1->tv_nsec < ts2->tv_nsec) return -1;
654         return 0;
655 }
656
657 /****************************************************************************
658  Interprets an nt time into a unix struct timespec.
659  Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
660  will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
661 ****************************************************************************/
662
663 struct timespec interpret_long_date(const char *p)
664 {
665         NTTIME nt;
666         nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);
667         if (nt == (uint64_t)-1) {
668                 struct timespec ret;
669                 ret.tv_sec = (time_t)-1;
670                 ret.tv_nsec = 0;
671                 return ret;
672         }
673         return nt_time_to_unix_timespec(&nt);
674 }
675
676 /***************************************************************************
677  Client versions of the above functions.
678 ***************************************************************************/
679
680 void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate)
681 {
682         put_dos_date(buf, offset, unixdate, cli->serverzone);
683 }
684
685 void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate)
686 {
687         put_dos_date2(buf, offset, unixdate, cli->serverzone);
688 }
689
690 void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate)
691 {
692         put_dos_date3(buf, offset, unixdate, cli->serverzone);
693 }
694
695 time_t cli_make_unix_date(struct cli_state *cli, const void *date_ptr)
696 {
697         return make_unix_date(date_ptr, cli->serverzone);
698 }
699
700 time_t cli_make_unix_date2(struct cli_state *cli, const void *date_ptr)
701 {
702         return make_unix_date2(date_ptr, cli->serverzone);
703 }
704
705 time_t cli_make_unix_date3(struct cli_state *cli, const void *date_ptr)
706 {
707         return make_unix_date3(date_ptr, cli->serverzone);
708 }
709
710 /****************************************************************************
711  Check if two NTTIMEs are the same.
712 ****************************************************************************/
713
714 bool nt_time_equals(const NTTIME *nt1, const NTTIME *nt2)
715 {
716         return (*nt1 == *nt2);
717 }
718
719 /*******************************************************************
720  Re-read the smb serverzone value.
721 ******************************************************************/
722
723 static struct timeval start_time_hires;
724
725 void TimeInit(void)
726 {
727         set_server_zone_offset(time(NULL));
728
729         DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
730
731         /* Save the start time of this process. */
732         if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
733                 GetTimeOfDay(&start_time_hires);
734         }
735 }
736
737 /**********************************************************************
738  Return a timeval struct of the uptime of this process. As TimeInit is
739  done before a daemon fork then this is the start time from the parent
740  daemon start. JRA.
741 ***********************************************************************/
742
743 void get_process_uptime(struct timeval *ret_time)
744 {
745         struct timeval time_now_hires;
746
747         GetTimeOfDay(&time_now_hires);
748         ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
749         if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
750                 ret_time->tv_sec -= 1;
751                 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
752         } else {
753                 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
754         }
755 }
756
757 /****************************************************************************
758  Convert a NTTIME structure to a time_t.
759  It's originally in "100ns units".
760
761  This is an absolute version of the one above.
762  By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
763  if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
764 ****************************************************************************/
765
766 time_t nt_time_to_unix_abs(const NTTIME *nt)
767 {
768         uint64 d;
769
770         if (*nt == 0) {
771                 return (time_t)0;
772         }
773
774         if (*nt == (uint64)-1) {
775                 return (time_t)-1;
776         }
777
778         if (*nt == NTTIME_INFINITY) {
779                 return (time_t)-1;
780         }
781
782         /* reverse the time */
783         /* it's a negative value, turn it to positive */
784         d=~*nt;
785
786         d += 1000*1000*10/2;
787         d /= 1000*1000*10;
788
789         if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
790                 return (time_t)0;
791         }
792
793         return (time_t)d;
794 }
795
796 time_t uint64s_nt_time_to_unix_abs(const uint64_t *src)
797 {
798         NTTIME nttime;
799         nttime = *src;
800         return nt_time_to_unix_abs(&nttime);
801 }
802
803 /****************************************************************************
804  Put a 8 byte filetime from a struct timespec. Uses GMT.
805 ****************************************************************************/
806
807 void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
808 {
809         uint64 d;
810
811         if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
812                 *nt = 0;
813                 return;
814         }
815         if (ts.tv_sec == TIME_T_MAX) {
816                 *nt = 0x7fffffffffffffffLL;
817                 return;
818         }               
819         if (ts.tv_sec == (time_t)-1) {
820                 *nt = (uint64)-1;
821                 return;
822         }               
823
824         d = ts.tv_sec;
825         d += TIME_FIXUP_CONSTANT_INT;
826         d *= 1000*1000*10;
827         /* d is now in 100ns units. */
828         d += (ts.tv_nsec / 100);
829
830         *nt = d;
831 }
832
833 /****************************************************************************
834  Convert a time_t to a NTTIME structure
835
836  This is an absolute version of the one above.
837  By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
838  If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
839 ****************************************************************************/
840
841 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
842 {
843         double d;
844
845         if (t==0) {
846                 *nt = 0;
847                 return;
848         }
849
850         if (t == TIME_T_MAX) {
851                 *nt = 0x7fffffffffffffffLL;
852                 return;
853         }
854                 
855         if (t == (time_t)-1) {
856                 /* that's what NT uses for infinite */
857                 *nt = NTTIME_INFINITY;
858                 return;
859         }               
860
861         d = (double)(t);
862         d *= 1.0e7;
863
864         *nt = (NTTIME)d;
865
866         /* convert to a negative value */
867         *nt=~*nt;
868 }
869
870
871 /****************************************************************************
872  Check if it's a null mtime.
873 ****************************************************************************/
874
875 bool null_mtime(time_t mtime)
876 {
877         if (mtime == 0 || mtime == (time_t)0xFFFFFFFF || mtime == (time_t)-1)
878                 return(True);
879         return(False);
880 }
881
882 /****************************************************************************
883  Utility function that always returns a const string even if localtime
884  and asctime fail.
885 ****************************************************************************/
886
887 const char *time_to_asc(const time_t t)
888 {
889         const char *asct;
890         struct tm *lt = localtime(&t);
891
892         if (!lt) {
893                 return "unknown time";
894         }
895
896         asct = asctime(lt);
897         if (!asct) {
898                 return "unknown time";
899         }
900         return asct;
901 }
902
903 const char *display_time(NTTIME nttime)
904 {
905         float high;
906         float low;
907         int sec;
908         int days, hours, mins, secs;
909
910         if (nttime==0)
911                 return "Now";
912
913         if (nttime==NTTIME_INFINITY)
914                 return "Never";
915
916         high = 65536;   
917         high = high/10000;
918         high = high*65536;
919         high = high/1000;
920         high = high * (~(nttime >> 32));
921
922         low = ~(nttime & 0xFFFFFFFF);
923         low = low/(1000*1000*10);
924
925         sec=(int)(high+low);
926
927         days=sec/(60*60*24);
928         hours=(sec - (days*60*60*24)) / (60*60);
929         mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
930         secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
931
932         return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
933                                "%u seconds", days, hours, mins, secs);
934 }
935
936 bool nt_time_is_set(const NTTIME *nt)
937 {
938         if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
939                 return False;
940         }
941
942         if (*nt == NTTIME_INFINITY) {
943                 return False;
944         }
945
946         return True;
947 }