tidy up timestamp checks
[Samba/gebeck_regimport.git] / source3 / lib / time.c
blob865456b23b0b488dcf91015186e1ab5cfa55baa4
1 /*
2 Unix SMB/CIFS implementation.
3 time handling functions
5 Copyright (C) Andrew Tridgell 1992-2004
6 Copyright (C) Stefan (metze) Metzmacher 2002
7 Copyright (C) Jeremy Allison 2007
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.
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.
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/>.
23 #include "includes.h"
25 /**
26 * @file
27 * @brief time handling functions
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
39 #define NTTIME_INFINITY (NTTIME)0x8000000000000000LL
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
47 /*******************************************************************
48 create a 16 bit dos packed date
49 ********************************************************************/
50 static uint16_t make_dos_date1(struct tm *t)
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;
58 /*******************************************************************
59 create a 16 bit dos packed time
60 ********************************************************************/
61 static uint16_t make_dos_time1(struct tm *t)
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;
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)
75 struct tm *t;
76 uint32_t ret=0;
78 if (unixdate == 0) {
79 return 0;
82 unixdate -= zone_offset;
84 t = gmtime(&unixdate);
85 if (!t) {
86 return 0xFFFFFFFF;
89 ret = make_dos_date1(t);
90 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
92 return ret;
95 /**
96 parse a nttime as a large integer in a string and return a NTTIME
98 NTTIME nttime_from_string(const char *s)
100 return strtoull(s, NULL, 0);
103 /**************************************************************
104 Handle conversions between time_t and uint32, taking care to
105 preserve the "special" values.
106 **************************************************************/
108 uint32_t convert_time_t_to_uint32(time_t t)
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;
117 #endif
118 return (uint32_t)t;
121 time_t convert_uint32_to_time_t(uint32_t u)
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;
130 #endif
131 return (time_t)u;
134 /****************************************************************************
135 Check if NTTIME is 0.
136 ****************************************************************************/
138 bool nt_time_is_zero(const NTTIME *nt)
140 return (*nt == 0);
143 /****************************************************************************
144 Convert ASN.1 GeneralizedTime string to unix-time.
145 Returns 0 on failure; Currently ignores timezone.
146 ****************************************************************************/
148 time_t generalized_to_unix_time(const char *str)
150 struct tm tm;
152 ZERO_STRUCT(tm);
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;
159 tm.tm_year -= 1900;
160 tm.tm_mon -= 1;
162 return timegm(&tm);
165 /*******************************************************************
166 Accessor function for the server time zone offset.
167 set_server_zone_offset() must have been called first.
168 ******************************************************************/
170 static int server_zone_offset;
172 int get_server_zone_offset(void)
174 return server_zone_offset;
177 /*******************************************************************
178 Initialize the server time zone offset. Called when a client connects.
179 ******************************************************************/
181 int set_server_zone_offset(time_t t)
183 server_zone_offset = get_time_zone(t);
184 return server_zone_offset;
187 /****************************************************************************
188 Return the date and time as a string
189 ****************************************************************************/
191 char *current_timestring(TALLOC_CTX *ctx, bool hires)
193 fstring TimeBuf;
194 struct timeval tp;
195 time_t t;
196 struct tm *tm;
198 if (hires) {
199 GetTimeOfDay(&tp);
200 t = (time_t)tp.tv_sec;
201 } else {
202 t = time(NULL);
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);
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);
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");
241 #endif
243 return talloc_strdup(ctx, TimeBuf);
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 ********************************************************************/
252 static void put_dos_date(char *buf,int offset,time_t unixdate, int zone_offset)
254 uint32_t x = make_dos_date(unixdate, zone_offset);
255 SIVAL(buf,offset,x);
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 ********************************************************************/
263 static void put_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)
265 uint32_t x = make_dos_date(unixdate, zone_offset);
266 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
267 SIVAL(buf,offset,x);
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 ********************************************************************/
276 static void put_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
278 if (!null_mtime(unixdate)) {
279 unixdate -= zone_offset;
281 SIVAL(buf,offset,unixdate);
285 /***************************************************************************
286 Server versions of the above functions.
287 ***************************************************************************/
289 void srv_put_dos_date(char *buf,int offset,time_t unixdate)
291 put_dos_date(buf, offset, unixdate, server_zone_offset);
294 void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
296 put_dos_date2(buf, offset, unixdate, server_zone_offset);
299 void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
301 put_dos_date3(buf, offset, unixdate, server_zone_offset);
304 /****************************************************************************
305 Take a Unix time and convert to an NTTIME structure and place in buffer
306 pointed to by p.
307 ****************************************************************************/
309 void put_long_date_timespec(char *p, struct timespec ts)
311 NTTIME nt;
312 unix_timespec_to_nt_time(&nt, ts);
313 SIVAL(p, 0, nt & 0xFFFFFFFF);
314 SIVAL(p, 4, nt >> 32);
317 void put_long_date(char *p, time_t t)
319 struct timespec ts;
320 ts.tv_sec = t;
321 ts.tv_nsec = 0;
322 put_long_date_timespec(p, ts);
325 /****************************************************************************
326 Return the best approximation to a 'create time' under UNIX from a stat
327 structure.
328 ****************************************************************************/
330 static time_t calc_create_time(const SMB_STRUCT_STAT *st)
332 time_t ret, ret1;
334 ret = MIN(st->st_ctime, st->st_mtime);
335 ret1 = MIN(ret, st->st_atime);
337 if(ret1 != (time_t)0) {
338 return ret1;
342 * One of ctime, mtime or atime was zero (probably atime).
343 * Just return MIN(ctime, mtime).
345 return ret;
348 /****************************************************************************
349 Return the 'create time' from a stat struct if it exists (birthtime) or else
350 use the best approximation.
351 ****************************************************************************/
353 struct timespec get_create_timespec(const SMB_STRUCT_STAT *pst,bool fake_dirs)
355 struct timespec ret;
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;
363 #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC)
364 ret = pst->st_birthtimespec;
365 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
366 ret.tv_sec = pst->st_birthtime;
367 ret.tv_nsec = pst->st_birthtimenspec;
368 #elif defined(HAVE_STRUCT_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
376 /* Deal with systems that don't initialize birthtime correctly.
377 * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
379 if (null_timespec(ret)) {
380 ret.tv_sec = calc_create_time(pst);
381 ret.tv_nsec = 0;
383 return ret;
386 /****************************************************************************
387 Get/Set all the possible time fields from a stat struct as a timespec.
388 ****************************************************************************/
390 struct timespec get_atimespec(const SMB_STRUCT_STAT *pst)
392 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
393 struct timespec ret;
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_STRUCT_STAT_ST_MTIM_TV_NSEC)
401 return pst->st_atim;
402 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
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_STRUCT_STAT_ST_MTIME_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_STRUCT_STAT_ST_UMTIME)
413 struct timespec ret;
414 ret.tv_sec = pst->st_atime;
415 ret.tv_nsec = pst->st_uatime * 1000;
416 return ret;
417 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
418 return pst->st_atimespec;
419 #else
420 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
421 #endif
422 #endif
425 void set_atimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
427 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
428 /* Old system - no ns timestamp. */
429 pst->st_atime = ts.tv_sec;
430 #else
431 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
432 pst->st_atim = ts;
433 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
434 pst->st_atime = ts.tv_sec;
435 pst->st_atimensec = ts.tv_nsec;
436 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
437 pst->st_atime = ts.tv_sec;
438 pst->st_atime_n = ts.tv_nsec;
439 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
440 pst->st_atime = ts.tv_sec;
441 pst->st_uatime = ts.tv_nsec / 1000;
442 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
443 pst->st_atimespec = ts;
444 #else
445 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
446 #endif
447 #endif
450 struct timespec get_mtimespec(const SMB_STRUCT_STAT *pst)
452 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
453 struct timespec ret;
455 /* Old system - no ns timestamp. */
456 ret.tv_sec = pst->st_mtime;
457 ret.tv_nsec = 0;
458 return ret;
459 #else
460 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
461 return pst->st_mtim;
462 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
463 struct timespec ret;
464 ret.tv_sec = pst->st_mtime;
465 ret.tv_nsec = pst->st_mtimensec;
466 return ret;
467 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
468 struct timespec ret;
469 ret.tv_sec = pst->st_mtime;
470 ret.tv_nsec = pst->st_mtime_n;
471 return ret;
472 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
473 struct timespec ret;
474 ret.tv_sec = pst->st_mtime;
475 ret.tv_nsec = pst->st_umtime * 1000;
476 return ret;
477 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
478 return pst->st_mtimespec;
479 #else
480 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
481 #endif
482 #endif
485 void set_mtimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
487 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
488 /* Old system - no ns timestamp. */
489 pst->st_mtime = ts.tv_sec;
490 #else
491 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
492 pst->st_mtim = ts;
493 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
494 pst->st_mtime = ts.tv_sec;
495 pst->st_mtimensec = ts.tv_nsec;
496 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
497 pst->st_mtime = ts.tv_sec;
498 pst->st_mtime_n = ts.tv_nsec;
499 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
500 pst->st_mtime = ts.tv_sec;
501 pst->st_umtime = ts.tv_nsec / 1000;
502 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
503 pst->st_mtimespec = ts;
504 #else
505 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
506 #endif
507 #endif
510 struct timespec get_ctimespec(const SMB_STRUCT_STAT *pst)
512 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
513 struct timespec ret;
515 /* Old system - no ns timestamp. */
516 ret.tv_sec = pst->st_ctime;
517 ret.tv_nsec = 0;
518 return ret;
519 #else
520 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
521 return pst->st_ctim;
522 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
523 struct timespec ret;
524 ret.tv_sec = pst->st_ctime;
525 ret.tv_nsec = pst->st_ctimensec;
526 return ret;
527 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
528 struct timespec ret;
529 ret.tv_sec = pst->st_ctime;
530 ret.tv_nsec = pst->st_ctime_n;
531 return ret;
532 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
533 struct timespec ret;
534 ret.tv_sec = pst->st_ctime;
535 ret.tv_nsec = pst->st_uctime * 1000;
536 return ret;
537 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
538 return pst->st_ctimespec;
539 #else
540 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
541 #endif
542 #endif
545 void set_ctimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
547 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
548 /* Old system - no ns timestamp. */
549 pst->st_ctime = ts.tv_sec;
550 #else
551 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
552 pst->st_ctim = ts;
553 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
554 pst->st_ctime = ts.tv_sec;
555 pst->st_ctimensec = ts.tv_nsec;
556 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
557 pst->st_ctime = ts.tv_sec;
558 pst->st_ctime_n = ts.tv_nsec;
559 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
560 pst->st_ctime = ts.tv_sec;
561 pst->st_uctime = ts.tv_nsec / 1000;
562 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
563 pst->st_ctimespec = ts;
564 #else
565 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
566 #endif
567 #endif
570 void dos_filetime_timespec(struct timespec *tsp)
572 tsp->tv_sec &= ~1;
573 tsp->tv_nsec = 0;
576 /*******************************************************************
577 Create a unix date (int GMT) from a dos date (which is actually in
578 localtime).
579 ********************************************************************/
581 static time_t make_unix_date(const void *date_ptr, int zone_offset)
583 uint32_t dos_date=0;
584 struct tm t;
585 time_t ret;
587 dos_date = IVAL(date_ptr,0);
589 if (dos_date == 0) {
590 return 0;
593 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
594 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
595 t.tm_isdst = -1;
597 ret = timegm(&t);
599 ret += zone_offset;
601 return(ret);
604 /*******************************************************************
605 Like make_unix_date() but the words are reversed.
606 ********************************************************************/
608 static time_t make_unix_date2(const void *date_ptr, int zone_offset)
610 uint32_t x,x2;
612 x = IVAL(date_ptr,0);
613 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
614 SIVAL(&x,0,x2);
616 return(make_unix_date((const void *)&x, zone_offset));
619 /*******************************************************************
620 Create a unix GMT date from a dos date in 32 bit "unix like" format
621 these generally arrive as localtimes, with corresponding DST.
622 ******************************************************************/
624 static time_t make_unix_date3(const void *date_ptr, int zone_offset)
626 time_t t = (time_t)IVAL(date_ptr,0);
627 if (!null_mtime(t)) {
628 t += zone_offset;
630 return(t);
633 time_t srv_make_unix_date(const void *date_ptr)
635 return make_unix_date(date_ptr, server_zone_offset);
638 time_t srv_make_unix_date2(const void *date_ptr)
640 return make_unix_date2(date_ptr, server_zone_offset);
643 time_t srv_make_unix_date3(const void *date_ptr)
645 return make_unix_date3(date_ptr, server_zone_offset);
648 /****************************************************************************
649 Convert a normalized timeval to a timespec.
650 ****************************************************************************/
652 struct timespec convert_timeval_to_timespec(const struct timeval tv)
654 struct timespec ts;
655 ts.tv_sec = tv.tv_sec;
656 ts.tv_nsec = tv.tv_usec * 1000;
657 return ts;
660 /****************************************************************************
661 Convert a normalized timespec to a timeval.
662 ****************************************************************************/
664 struct timeval convert_timespec_to_timeval(const struct timespec ts)
666 struct timeval tv;
667 tv.tv_sec = ts.tv_sec;
668 tv.tv_usec = ts.tv_nsec / 1000;
669 return tv;
672 /****************************************************************************
673 Return a timespec for the current time
674 ****************************************************************************/
676 struct timespec timespec_current(void)
678 struct timeval tv;
679 struct timespec ts;
680 GetTimeOfDay(&tv);
681 ts.tv_sec = tv.tv_sec;
682 ts.tv_nsec = tv.tv_usec * 1000;
683 return ts;
686 /****************************************************************************
687 Return the lesser of two timespecs.
688 ****************************************************************************/
690 struct timespec timespec_min(const struct timespec *ts1,
691 const struct timespec *ts2)
693 if (ts1->tv_sec < ts2->tv_sec) return *ts1;
694 if (ts1->tv_sec > ts2->tv_sec) return *ts2;
695 if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
696 return *ts2;
699 /****************************************************************************
700 compare two timespec structures.
701 Return -1 if ts1 < ts2
702 Return 0 if ts1 == ts2
703 Return 1 if ts1 > ts2
704 ****************************************************************************/
706 int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
708 if (ts1->tv_sec > ts2->tv_sec) return 1;
709 if (ts1->tv_sec < ts2->tv_sec) return -1;
710 if (ts1->tv_nsec > ts2->tv_nsec) return 1;
711 if (ts1->tv_nsec < ts2->tv_nsec) return -1;
712 return 0;
715 /****************************************************************************
716 Interprets an nt time into a unix struct timespec.
717 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
718 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
719 ****************************************************************************/
721 struct timespec interpret_long_date(const char *p)
723 NTTIME nt;
724 nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);
725 if (nt == (uint64_t)-1) {
726 struct timespec ret;
727 ret.tv_sec = (time_t)-1;
728 ret.tv_nsec = 0;
729 return ret;
731 return nt_time_to_unix_timespec(&nt);
734 /***************************************************************************
735 Client versions of the above functions.
736 ***************************************************************************/
738 void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate)
740 put_dos_date(buf, offset, unixdate, cli->serverzone);
743 void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate)
745 put_dos_date2(buf, offset, unixdate, cli->serverzone);
748 void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate)
750 put_dos_date3(buf, offset, unixdate, cli->serverzone);
753 time_t cli_make_unix_date(struct cli_state *cli, const void *date_ptr)
755 return make_unix_date(date_ptr, cli->serverzone);
758 time_t cli_make_unix_date2(struct cli_state *cli, const void *date_ptr)
760 return make_unix_date2(date_ptr, cli->serverzone);
763 time_t cli_make_unix_date3(struct cli_state *cli, const void *date_ptr)
765 return make_unix_date3(date_ptr, cli->serverzone);
768 /****************************************************************************
769 Check if two NTTIMEs are the same.
770 ****************************************************************************/
772 bool nt_time_equals(const NTTIME *nt1, const NTTIME *nt2)
774 return (*nt1 == *nt2);
777 /*******************************************************************
778 Re-read the smb serverzone value.
779 ******************************************************************/
781 static struct timeval start_time_hires;
783 void TimeInit(void)
785 set_server_zone_offset(time(NULL));
787 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
789 /* Save the start time of this process. */
790 if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
791 GetTimeOfDay(&start_time_hires);
795 /**********************************************************************
796 Return a timeval struct of the uptime of this process. As TimeInit is
797 done before a daemon fork then this is the start time from the parent
798 daemon start. JRA.
799 ***********************************************************************/
801 void get_process_uptime(struct timeval *ret_time)
803 struct timeval time_now_hires;
805 GetTimeOfDay(&time_now_hires);
806 ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
807 if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
808 ret_time->tv_sec -= 1;
809 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
810 } else {
811 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
815 /****************************************************************************
816 Convert a NTTIME structure to a time_t.
817 It's originally in "100ns units".
819 This is an absolute version of the one above.
820 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
821 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
822 ****************************************************************************/
824 time_t nt_time_to_unix_abs(const NTTIME *nt)
826 uint64_t d;
828 if (*nt == 0) {
829 return (time_t)0;
832 if (*nt == (uint64_t)-1) {
833 return (time_t)-1;
836 if (*nt == NTTIME_INFINITY) {
837 return (time_t)-1;
840 /* reverse the time */
841 /* it's a negative value, turn it to positive */
842 d=~*nt;
844 d += 1000*1000*10/2;
845 d /= 1000*1000*10;
847 if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
848 return (time_t)0;
851 return (time_t)d;
854 time_t uint64s_nt_time_to_unix_abs(const uint64_t *src)
856 NTTIME nttime;
857 nttime = *src;
858 return nt_time_to_unix_abs(&nttime);
861 /****************************************************************************
862 Put a 8 byte filetime from a struct timespec. Uses GMT.
863 ****************************************************************************/
865 void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
867 uint64_t d;
869 if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
870 *nt = 0;
871 return;
873 if (ts.tv_sec == TIME_T_MAX) {
874 *nt = 0x7fffffffffffffffLL;
875 return;
877 if (ts.tv_sec == (time_t)-1) {
878 *nt = (uint64_t)-1;
879 return;
882 d = ts.tv_sec;
883 d += TIME_FIXUP_CONSTANT_INT;
884 d *= 1000*1000*10;
885 /* d is now in 100ns units. */
886 d += (ts.tv_nsec / 100);
888 *nt = d;
891 /****************************************************************************
892 Convert a time_t to a NTTIME structure
894 This is an absolute version of the one above.
895 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
896 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
897 ****************************************************************************/
899 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
901 double d;
903 if (t==0) {
904 *nt = 0;
905 return;
908 if (t == TIME_T_MAX) {
909 *nt = 0x7fffffffffffffffLL;
910 return;
913 if (t == (time_t)-1) {
914 /* that's what NT uses for infinite */
915 *nt = NTTIME_INFINITY;
916 return;
919 d = (double)(t);
920 d *= 1.0e7;
922 *nt = (NTTIME)d;
924 /* convert to a negative value */
925 *nt=~*nt;
929 /****************************************************************************
930 Check if it's a null mtime.
931 ****************************************************************************/
933 bool null_mtime(time_t mtime)
935 if (mtime == 0 || mtime == (time_t)0xFFFFFFFF || mtime == (time_t)-1)
936 return true;
937 return false;
940 /****************************************************************************
941 Utility function that always returns a const string even if localtime
942 and asctime fail.
943 ****************************************************************************/
945 const char *time_to_asc(const time_t t)
947 const char *asct;
948 struct tm *lt = localtime(&t);
950 if (!lt) {
951 return "unknown time";
954 asct = asctime(lt);
955 if (!asct) {
956 return "unknown time";
958 return asct;
961 const char *display_time(NTTIME nttime)
963 float high;
964 float low;
965 int sec;
966 int days, hours, mins, secs;
968 if (nttime==0)
969 return "Now";
971 if (nttime==NTTIME_INFINITY)
972 return "Never";
974 high = 65536;
975 high = high/10000;
976 high = high*65536;
977 high = high/1000;
978 high = high * (~(nttime >> 32));
980 low = ~(nttime & 0xFFFFFFFF);
981 low = low/(1000*1000*10);
983 sec=(int)(high+low);
985 days=sec/(60*60*24);
986 hours=(sec - (days*60*60*24)) / (60*60);
987 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
988 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
990 return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
991 "%u seconds", days, hours, mins, secs);
994 bool nt_time_is_set(const NTTIME *nt)
996 if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
997 return false;
1000 if (*nt == NTTIME_INFINITY) {
1001 return false;
1004 return true;