682d96c7dbf60493b701cf493fc984a87fca80f2
[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_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 /****************************************************************************
305  Take a Unix time and convert to an NTTIME structure and place in buffer 
306  pointed to by p.
307 ****************************************************************************/
308
309 void put_long_date_timespec(char *p, struct timespec ts)
310 {
311         NTTIME nt;
312         unix_timespec_to_nt_time(&nt, ts);
313         SIVAL(p, 0, nt & 0xFFFFFFFF);
314         SIVAL(p, 4, nt >> 32);
315 }
316
317 void put_long_date(char *p, time_t t)
318 {
319         struct timespec ts;
320         ts.tv_sec = t;
321         ts.tv_nsec = 0;
322         put_long_date_timespec(p, ts);
323 }
324
325 /****************************************************************************
326  Return the best approximation to a 'create time' under UNIX from a stat
327  structure.
328 ****************************************************************************/
329
330 static time_t calc_create_time(const SMB_STRUCT_STAT *st)
331 {
332         time_t ret, ret1;
333
334         ret = MIN(st->st_ctime, st->st_mtime);
335         ret1 = MIN(ret, st->st_atime);
336
337         if(ret1 != (time_t)0) {
338                 return ret1;
339         }
340
341         /*
342          * One of ctime, mtime or atime was zero (probably atime).
343          * Just return MIN(ctime, mtime).
344          */
345         return ret;
346 }
347
348 /****************************************************************************
349  Return the 'create time' from a stat struct if it exists (birthtime) or else
350  use the best approximation.
351 ****************************************************************************/
352
353 struct timespec get_create_timespec(const SMB_STRUCT_STAT *pst,bool fake_dirs)
354 {
355         struct timespec ret;
356
357         if(S_ISDIR(pst->st_mode) && fake_dirs) {
358                 ret.tv_sec = 315493200L;          /* 1/1/1980 */
359                 ret.tv_nsec = 0;
360                 return ret;
361         }
362
363 #if defined(HAVE_STAT_ST_BIRTHTIMESPEC)
364         ret = pst->st_birthtimespec;
365 #elif defined(HAVE_STAT_ST_BIRTHTIMENSEC)
366         ret.tv_sec = pst->st_birthtime;
367         ret.tv_nsec = pst->st_birthtimenspec;
368 #elif defined(HAVE_STAT_ST_BIRTHTIME)
369         ret.tv_sec = pst->st_birthtime;
370         ret.tv_nsec = 0;
371 #else
372         ret.tv_sec = calc_create_time(pst);
373         ret.tv_nsec = 0;
374 #endif
375
376         /* Deal with systems that don't initialize birthtime correctly.
377          * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
378          */
379         if (null_timespec(ret)) {
380                 ret.tv_sec = calc_create_time(pst);
381                 ret.tv_nsec = 0;
382         }
383         return ret;
384 }
385
386 /****************************************************************************
387  Get/Set all the possible time fields from a stat struct as a timespec.
388 ****************************************************************************/
389
390 struct timespec get_atimespec(const SMB_STRUCT_STAT *pst)
391 {
392 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
393         struct timespec ret;
394
395         /* Old system - no ns timestamp. */
396         ret.tv_sec = pst->st_atime;
397         ret.tv_nsec = 0;
398         return ret;
399 #else
400 #if defined(HAVE_STAT_ST_ATIM)
401         return pst->st_atim;
402 #elif defined(HAVE_STAT_ST_ATIMENSEC)
403         struct timespec ret;
404         ret.tv_sec = pst->st_atime;
405         ret.tv_nsec = pst->st_atimensec;
406         return ret;
407 #elif defined(HAVE_STAT_ST_ATIME_N)
408         struct timespec ret;
409         ret.tv_sec = pst->st_atime;
410         ret.tv_nsec = pst->st_atime_n;
411         return ret;
412 #elif defined(HAVE_STAT_ST_ATIMESPEC)
413         return pst->st_atimespec;
414 #else
415 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
416 #endif
417 #endif
418 }
419
420 void set_atimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
421 {
422 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
423         /* Old system - no ns timestamp. */
424         pst->st_atime = ts.tv_sec;
425 #else
426 #if defined(HAVE_STAT_ST_ATIM)
427         pst->st_atim = ts;
428 #elif defined(HAVE_STAT_ST_ATIMENSEC)
429         pst->st_atime = ts.tv_sec;
430         pst->st_atimensec = ts.tv_nsec;
431 #elif defined(HAVE_STAT_ST_ATIME_N)
432         pst->st_atime = ts.tv_sec;
433         pst->st_atime_n = ts.tv_nsec;
434 #elif defined(HAVE_STAT_ST_ATIMESPEC)
435         pst->st_atimespec = ts;
436 #else
437 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
438 #endif
439 #endif
440 }
441
442 struct timespec get_mtimespec(const SMB_STRUCT_STAT *pst)
443 {
444 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
445         struct timespec ret;
446
447         /* Old system - no ns timestamp. */
448         ret.tv_sec = pst->st_mtime;
449         ret.tv_nsec = 0;
450         return ret;
451 #else
452 #if defined(HAVE_STAT_ST_MTIM)
453         return pst->st_mtim;
454 #elif defined(HAVE_STAT_ST_MTIMENSEC)
455         struct timespec ret;
456         ret.tv_sec = pst->st_mtime;
457         ret.tv_nsec = pst->st_mtimensec;
458         return ret;
459 #elif defined(HAVE_STAT_ST_MTIME_N)
460         struct timespec ret;
461         ret.tv_sec = pst->st_mtime;
462         ret.tv_nsec = pst->st_mtime_n;
463         return ret;
464 #elif defined(HAVE_STAT_ST_MTIMESPEC)
465         return pst->st_mtimespec;
466 #else
467 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
468 #endif
469 #endif
470 }
471
472 void set_mtimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
473 {
474 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
475         /* Old system - no ns timestamp. */
476         pst->st_mtime = ts.tv_sec;
477 #else
478 #if defined(HAVE_STAT_ST_MTIM)
479         pst->st_mtim = ts;
480 #elif defined(HAVE_STAT_ST_MTIMENSEC)
481         pst->st_mtime = ts.tv_sec;
482         pst->st_mtimensec = ts.tv_nsec;
483 #elif defined(HAVE_STAT_ST_MTIME_N)
484         pst->st_mtime = ts.tv_sec;
485         pst->st_mtime_n = ts.tv_nsec;
486 #elif defined(HAVE_STAT_ST_MTIMESPEC)
487         pst->st_mtimespec = ts;
488 #else
489 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
490 #endif
491 #endif
492 }
493
494 struct timespec get_ctimespec(const SMB_STRUCT_STAT *pst)
495 {
496 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
497         struct timespec ret;
498
499         /* Old system - no ns timestamp. */
500         ret.tv_sec = pst->st_ctime;
501         ret.tv_nsec = 0;
502         return ret;
503 #else
504 #if defined(HAVE_STAT_ST_CTIM)
505         return pst->st_ctim;
506 #elif defined(HAVE_STAT_ST_CTIMENSEC)
507         struct timespec ret;
508         ret.tv_sec = pst->st_ctime;
509         ret.tv_nsec = pst->st_ctimensec;
510         return ret;
511 #elif defined(HAVE_STAT_ST_CTIME_N)
512         struct timespec ret;
513         ret.tv_sec = pst->st_ctime;
514         ret.tv_nsec = pst->st_ctime_n;
515         return ret;
516 #elif defined(HAVE_STAT_ST_CTIMESPEC)
517         return pst->st_ctimespec;
518 #else
519 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
520 #endif
521 #endif
522 }
523
524 void set_ctimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
525 {
526 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
527         /* Old system - no ns timestamp. */
528         pst->st_ctime = ts.tv_sec;
529 #else
530 #if defined(HAVE_STAT_ST_CTIM)
531         pst->st_ctim = ts;
532 #elif defined(HAVE_STAT_ST_CTIMENSEC)
533         pst->st_ctime = ts.tv_sec;
534         pst->st_ctimensec = ts.tv_nsec;
535 #elif defined(HAVE_STAT_ST_CTIME_N)
536         pst->st_ctime = ts.tv_sec;
537         pst->st_ctime_n = ts.tv_nsec;
538 #elif defined(HAVE_STAT_ST_CTIMESPEC)
539         pst->st_ctimespec = ts;
540 #else
541 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT 
542 #endif
543 #endif
544 }
545
546 void dos_filetime_timespec(struct timespec *tsp)
547 {
548         tsp->tv_sec &= ~1;
549         tsp->tv_nsec = 0;
550 }
551
552 /*******************************************************************
553  Create a unix date (int GMT) from a dos date (which is actually in
554  localtime).
555 ********************************************************************/
556
557 static time_t make_unix_date(const void *date_ptr, int zone_offset)
558 {
559         uint32_t dos_date=0;
560         struct tm t;
561         time_t ret;
562
563         dos_date = IVAL(date_ptr,0);
564
565         if (dos_date == 0) {
566                 return 0;
567         }
568   
569         interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
570                         &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
571         t.tm_isdst = -1;
572   
573         ret = timegm(&t);
574
575         ret += zone_offset;
576
577         return(ret);
578 }
579
580 /*******************************************************************
581  Like make_unix_date() but the words are reversed.
582 ********************************************************************/
583
584 static time_t make_unix_date2(const void *date_ptr, int zone_offset)
585 {
586         uint32_t x,x2;
587
588         x = IVAL(date_ptr,0);
589         x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
590         SIVAL(&x,0,x2);
591
592         return(make_unix_date((const void *)&x, zone_offset));
593 }
594
595 /*******************************************************************
596  Create a unix GMT date from a dos date in 32 bit "unix like" format
597  these generally arrive as localtimes, with corresponding DST.
598 ******************************************************************/
599
600 static time_t make_unix_date3(const void *date_ptr, int zone_offset)
601 {
602         time_t t = (time_t)IVAL(date_ptr,0);
603         if (!null_mtime(t)) {
604                 t += zone_offset;
605         }
606         return(t);
607 }
608
609 time_t srv_make_unix_date(const void *date_ptr)
610 {
611         return make_unix_date(date_ptr, server_zone_offset);
612 }
613
614 time_t srv_make_unix_date2(const void *date_ptr)
615 {
616         return make_unix_date2(date_ptr, server_zone_offset);
617 }
618
619 time_t srv_make_unix_date3(const void *date_ptr)
620 {
621         return make_unix_date3(date_ptr, server_zone_offset);
622 }
623
624 /****************************************************************************
625  Convert a normalized timeval to a timespec.
626 ****************************************************************************/
627
628 struct timespec convert_timeval_to_timespec(const struct timeval tv)
629 {
630         struct timespec ts;
631         ts.tv_sec = tv.tv_sec;
632         ts.tv_nsec = tv.tv_usec * 1000;
633         return ts;
634 }
635
636 /****************************************************************************
637  Convert a normalized timespec to a timeval.
638 ****************************************************************************/
639
640 struct timeval convert_timespec_to_timeval(const struct timespec ts)
641 {
642         struct timeval tv;
643         tv.tv_sec = ts.tv_sec;
644         tv.tv_usec = ts.tv_nsec / 1000;
645         return tv;
646 }
647
648 /****************************************************************************
649  Return a timespec for the current time
650 ****************************************************************************/
651
652 struct timespec timespec_current(void)
653 {
654         struct timeval tv;
655         struct timespec ts;
656         GetTimeOfDay(&tv);
657         ts.tv_sec = tv.tv_sec;
658         ts.tv_nsec = tv.tv_usec * 1000;
659         return ts;
660 }
661
662 /****************************************************************************
663  Return the lesser of two timespecs.
664 ****************************************************************************/
665
666 struct timespec timespec_min(const struct timespec *ts1,
667                            const struct timespec *ts2)
668 {
669         if (ts1->tv_sec < ts2->tv_sec) return *ts1;
670         if (ts1->tv_sec > ts2->tv_sec) return *ts2;
671         if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
672         return *ts2;
673 }
674
675 /****************************************************************************
676   compare two timespec structures. 
677   Return -1 if ts1 < ts2
678   Return 0 if ts1 == ts2
679   Return 1 if ts1 > ts2
680 ****************************************************************************/
681
682 int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
683 {
684         if (ts1->tv_sec  > ts2->tv_sec)  return 1;
685         if (ts1->tv_sec  < ts2->tv_sec)  return -1;
686         if (ts1->tv_nsec > ts2->tv_nsec) return 1;
687         if (ts1->tv_nsec < ts2->tv_nsec) return -1;
688         return 0;
689 }
690
691 /****************************************************************************
692  Interprets an nt time into a unix struct timespec.
693  Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
694  will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
695 ****************************************************************************/
696
697 struct timespec interpret_long_date(const char *p)
698 {
699         NTTIME nt;
700         nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);
701         if (nt == (uint64_t)-1) {
702                 struct timespec ret;
703                 ret.tv_sec = (time_t)-1;
704                 ret.tv_nsec = 0;
705                 return ret;
706         }
707         return nt_time_to_unix_timespec(&nt);
708 }
709
710 /***************************************************************************
711  Client versions of the above functions.
712 ***************************************************************************/
713
714 void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate)
715 {
716         put_dos_date(buf, offset, unixdate, cli->serverzone);
717 }
718
719 void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate)
720 {
721         put_dos_date2(buf, offset, unixdate, cli->serverzone);
722 }
723
724 void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate)
725 {
726         put_dos_date3(buf, offset, unixdate, cli->serverzone);
727 }
728
729 time_t cli_make_unix_date(struct cli_state *cli, const void *date_ptr)
730 {
731         return make_unix_date(date_ptr, cli->serverzone);
732 }
733
734 time_t cli_make_unix_date2(struct cli_state *cli, const void *date_ptr)
735 {
736         return make_unix_date2(date_ptr, cli->serverzone);
737 }
738
739 time_t cli_make_unix_date3(struct cli_state *cli, const void *date_ptr)
740 {
741         return make_unix_date3(date_ptr, cli->serverzone);
742 }
743
744 /****************************************************************************
745  Check if two NTTIMEs are the same.
746 ****************************************************************************/
747
748 bool nt_time_equals(const NTTIME *nt1, const NTTIME *nt2)
749 {
750         return (*nt1 == *nt2);
751 }
752
753 /*******************************************************************
754  Re-read the smb serverzone value.
755 ******************************************************************/
756
757 static struct timeval start_time_hires;
758
759 void TimeInit(void)
760 {
761         set_server_zone_offset(time(NULL));
762
763         DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
764
765         /* Save the start time of this process. */
766         if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
767                 GetTimeOfDay(&start_time_hires);
768         }
769 }
770
771 /**********************************************************************
772  Return a timeval struct of the uptime of this process. As TimeInit is
773  done before a daemon fork then this is the start time from the parent
774  daemon start. JRA.
775 ***********************************************************************/
776
777 void get_process_uptime(struct timeval *ret_time)
778 {
779         struct timeval time_now_hires;
780
781         GetTimeOfDay(&time_now_hires);
782         ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
783         if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
784                 ret_time->tv_sec -= 1;
785                 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
786         } else {
787                 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
788         }
789 }
790
791 /****************************************************************************
792  Convert a NTTIME structure to a time_t.
793  It's originally in "100ns units".
794
795  This is an absolute version of the one above.
796  By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
797  if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
798 ****************************************************************************/
799
800 time_t nt_time_to_unix_abs(const NTTIME *nt)
801 {
802         uint64_t d;
803
804         if (*nt == 0) {
805                 return (time_t)0;
806         }
807
808         if (*nt == (uint64_t)-1) {
809                 return (time_t)-1;
810         }
811
812         if (*nt == NTTIME_INFINITY) {
813                 return (time_t)-1;
814         }
815
816         /* reverse the time */
817         /* it's a negative value, turn it to positive */
818         d=~*nt;
819
820         d += 1000*1000*10/2;
821         d /= 1000*1000*10;
822
823         if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
824                 return (time_t)0;
825         }
826
827         return (time_t)d;
828 }
829
830 time_t uint64s_nt_time_to_unix_abs(const uint64_t *src)
831 {
832         NTTIME nttime;
833         nttime = *src;
834         return nt_time_to_unix_abs(&nttime);
835 }
836
837 /****************************************************************************
838  Put a 8 byte filetime from a struct timespec. Uses GMT.
839 ****************************************************************************/
840
841 void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
842 {
843         uint64_t d;
844
845         if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
846                 *nt = 0;
847                 return;
848         }
849         if (ts.tv_sec == TIME_T_MAX) {
850                 *nt = 0x7fffffffffffffffLL;
851                 return;
852         }               
853         if (ts.tv_sec == (time_t)-1) {
854                 *nt = (uint64_t)-1;
855                 return;
856         }               
857
858         d = ts.tv_sec;
859         d += TIME_FIXUP_CONSTANT_INT;
860         d *= 1000*1000*10;
861         /* d is now in 100ns units. */
862         d += (ts.tv_nsec / 100);
863
864         *nt = d;
865 }
866
867 /****************************************************************************
868  Convert a time_t to a NTTIME structure
869
870  This is an absolute version of the one above.
871  By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
872  If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
873 ****************************************************************************/
874
875 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
876 {
877         double d;
878
879         if (t==0) {
880                 *nt = 0;
881                 return;
882         }
883
884         if (t == TIME_T_MAX) {
885                 *nt = 0x7fffffffffffffffLL;
886                 return;
887         }
888                 
889         if (t == (time_t)-1) {
890                 /* that's what NT uses for infinite */
891                 *nt = NTTIME_INFINITY;
892                 return;
893         }               
894
895         d = (double)(t);
896         d *= 1.0e7;
897
898         *nt = (NTTIME)d;
899
900         /* convert to a negative value */
901         *nt=~*nt;
902 }
903
904
905 /****************************************************************************
906  Check if it's a null mtime.
907 ****************************************************************************/
908
909 bool null_mtime(time_t mtime)
910 {
911         if (mtime == 0 || mtime == (time_t)0xFFFFFFFF || mtime == (time_t)-1)
912                 return true;
913         return false;
914 }
915
916 /****************************************************************************
917  Utility function that always returns a const string even if localtime
918  and asctime fail.
919 ****************************************************************************/
920
921 const char *time_to_asc(const time_t t)
922 {
923         const char *asct;
924         struct tm *lt = localtime(&t);
925
926         if (!lt) {
927                 return "unknown time";
928         }
929
930         asct = asctime(lt);
931         if (!asct) {
932                 return "unknown time";
933         }
934         return asct;
935 }
936
937 const char *display_time(NTTIME nttime)
938 {
939         float high;
940         float low;
941         int sec;
942         int days, hours, mins, secs;
943
944         if (nttime==0)
945                 return "Now";
946
947         if (nttime==NTTIME_INFINITY)
948                 return "Never";
949
950         high = 65536;   
951         high = high/10000;
952         high = high*65536;
953         high = high/1000;
954         high = high * (~(nttime >> 32));
955
956         low = ~(nttime & 0xFFFFFFFF);
957         low = low/(1000*1000*10);
958
959         sec=(int)(high+low);
960
961         days=sec/(60*60*24);
962         hours=(sec - (days*60*60*24)) / (60*60);
963         mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
964         secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
965
966         return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
967                                "%u seconds", days, hours, mins, secs);
968 }
969
970 bool nt_time_is_set(const NTTIME *nt)
971 {
972         if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
973                 return false;
974         }
975
976         if (*nt == NTTIME_INFINITY) {
977                 return false;
978         }
979
980         return true;
981 }