s3:lib/time: remove TIME_T_MIN/MAX defines
[Samba.git] / source3 / lib / time.c
blob0eee0088b7e7675f612a2b16e3968445c46697fb
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 #define NTTIME_INFINITY (NTTIME)0x8000000000000000LL
33 #if (SIZEOF_LONG == 8)
34 #define TIME_FIXUP_CONSTANT_INT 11644473600L
35 #elif (SIZEOF_LONG_LONG == 8)
36 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
37 #endif
39 /*******************************************************************
40 create a 16 bit dos packed date
41 ********************************************************************/
42 static uint16_t make_dos_date1(struct tm *t)
44 uint16_t ret=0;
45 ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
46 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
47 return ret;
50 /*******************************************************************
51 create a 16 bit dos packed time
52 ********************************************************************/
53 static uint16_t make_dos_time1(struct tm *t)
55 uint16_t ret=0;
56 ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));
57 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
58 return ret;
61 /*******************************************************************
62 create a 32 bit dos packed date/time from some parameters
63 This takes a GMT time and returns a packed localtime structure
64 ********************************************************************/
65 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
67 struct tm *t;
68 uint32_t ret=0;
70 if (unixdate == 0) {
71 return 0;
74 unixdate -= zone_offset;
76 t = gmtime(&unixdate);
77 if (!t) {
78 return 0xFFFFFFFF;
81 ret = make_dos_date1(t);
82 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
84 return ret;
87 /**
88 parse a nttime as a large integer in a string and return a NTTIME
90 NTTIME nttime_from_string(const char *s)
92 return strtoull(s, NULL, 0);
95 /**************************************************************
96 Handle conversions between time_t and uint32, taking care to
97 preserve the "special" values.
98 **************************************************************/
100 uint32_t convert_time_t_to_uint32(time_t t)
102 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
103 /* time_t is 64-bit. */
104 if (t == 0x8000000000000000LL) {
105 return 0x80000000;
106 } else if (t == 0x7FFFFFFFFFFFFFFFLL) {
107 return 0x7FFFFFFF;
109 #endif
110 return (uint32_t)t;
113 time_t convert_uint32_to_time_t(uint32_t u)
115 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
116 /* time_t is 64-bit. */
117 if (u == 0x80000000) {
118 return (time_t)0x8000000000000000LL;
119 } else if (u == 0x7FFFFFFF) {
120 return (time_t)0x7FFFFFFFFFFFFFFFLL;
122 #endif
123 return (time_t)u;
126 /****************************************************************************
127 Check if NTTIME is 0.
128 ****************************************************************************/
130 bool nt_time_is_zero(const NTTIME *nt)
132 return (*nt == 0);
135 /****************************************************************************
136 Convert ASN.1 GeneralizedTime string to unix-time.
137 Returns 0 on failure; Currently ignores timezone.
138 ****************************************************************************/
140 time_t generalized_to_unix_time(const char *str)
142 struct tm tm;
144 ZERO_STRUCT(tm);
146 if (sscanf(str, "%4d%2d%2d%2d%2d%2d",
147 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
148 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
149 return 0;
151 tm.tm_year -= 1900;
152 tm.tm_mon -= 1;
154 return timegm(&tm);
157 /*******************************************************************
158 Accessor function for the server time zone offset.
159 set_server_zone_offset() must have been called first.
160 ******************************************************************/
162 static int server_zone_offset;
164 int get_server_zone_offset(void)
166 return server_zone_offset;
169 /*******************************************************************
170 Initialize the server time zone offset. Called when a client connects.
171 ******************************************************************/
173 int set_server_zone_offset(time_t t)
175 server_zone_offset = get_time_zone(t);
176 return server_zone_offset;
179 /****************************************************************************
180 Return the date and time as a string
181 ****************************************************************************/
183 char *current_timestring(TALLOC_CTX *ctx, bool hires)
185 fstring TimeBuf;
186 struct timeval tp;
187 time_t t;
188 struct tm *tm;
190 if (hires) {
191 GetTimeOfDay(&tp);
192 t = (time_t)tp.tv_sec;
193 } else {
194 t = time(NULL);
196 tm = localtime(&t);
197 if (!tm) {
198 if (hires) {
199 slprintf(TimeBuf,
200 sizeof(TimeBuf)-1,
201 "%ld.%06ld seconds since the Epoch",
202 (long)tp.tv_sec,
203 (long)tp.tv_usec);
204 } else {
205 slprintf(TimeBuf,
206 sizeof(TimeBuf)-1,
207 "%ld seconds since the Epoch",
208 (long)t);
210 } else {
211 #ifdef HAVE_STRFTIME
212 if (hires) {
213 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
214 slprintf(TimeBuf+strlen(TimeBuf),
215 sizeof(TimeBuf)-1 - strlen(TimeBuf),
216 ".%06ld",
217 (long)tp.tv_usec);
218 } else {
219 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
221 #else
222 if (hires) {
223 const char *asct = asctime(tm);
224 slprintf(TimeBuf,
225 sizeof(TimeBuf)-1,
226 "%s.%06ld",
227 asct ? asct : "unknown",
228 (long)tp.tv_usec);
229 } else {
230 const char *asct = asctime(tm);
231 fstrcpy(TimeBuf, asct ? asct : "unknown");
233 #endif
235 return talloc_strdup(ctx, TimeBuf);
239 /*******************************************************************
240 Put a dos date into a buffer (time/date format).
241 This takes GMT time and puts local time in the buffer.
242 ********************************************************************/
244 static void put_dos_date(char *buf,int offset,time_t unixdate, int zone_offset)
246 uint32_t x = make_dos_date(unixdate, zone_offset);
247 SIVAL(buf,offset,x);
250 /*******************************************************************
251 Put a dos date into a buffer (date/time format).
252 This takes GMT time and puts local time in the buffer.
253 ********************************************************************/
255 static void put_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)
257 uint32_t x = make_dos_date(unixdate, zone_offset);
258 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
259 SIVAL(buf,offset,x);
262 /*******************************************************************
263 Put a dos 32 bit "unix like" date into a buffer. This routine takes
264 GMT and converts it to LOCAL time before putting it (most SMBs assume
265 localtime for this sort of date)
266 ********************************************************************/
268 static void put_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
270 if (!null_mtime(unixdate)) {
271 unixdate -= zone_offset;
273 SIVAL(buf,offset,unixdate);
277 /***************************************************************************
278 Server versions of the above functions.
279 ***************************************************************************/
281 void srv_put_dos_date(char *buf,int offset,time_t unixdate)
283 put_dos_date(buf, offset, unixdate, server_zone_offset);
286 void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
288 put_dos_date2(buf, offset, unixdate, server_zone_offset);
291 void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
293 put_dos_date3(buf, offset, unixdate, server_zone_offset);
296 void round_timespec(enum timestamp_set_resolution res, struct timespec *ts)
298 switch (res) {
299 case TIMESTAMP_SET_SECONDS:
300 round_timespec_to_sec(ts);
301 break;
302 case TIMESTAMP_SET_MSEC:
303 round_timespec_to_usec(ts);
304 break;
305 case TIMESTAMP_SET_NT_OR_BETTER:
306 /* No rounding needed. */
307 break;
311 /****************************************************************************
312 Take a Unix time and convert to an NTTIME structure and place in buffer
313 pointed to by p.
314 ****************************************************************************/
316 void put_long_date_timespec(enum timestamp_set_resolution res, char *p, struct timespec ts)
318 NTTIME nt;
319 round_timespec(res, &ts);
320 unix_timespec_to_nt_time(&nt, ts);
321 SIVAL(p, 0, nt & 0xFFFFFFFF);
322 SIVAL(p, 4, nt >> 32);
325 void put_long_date(char *p, time_t t)
327 struct timespec ts;
328 ts.tv_sec = t;
329 ts.tv_nsec = 0;
330 put_long_date_timespec(TIMESTAMP_SET_SECONDS, p, ts);
333 /****************************************************************************
334 Return the best approximation to a 'create time' under UNIX from a stat
335 structure.
336 ****************************************************************************/
338 static time_t calc_create_time(const SMB_STRUCT_STAT *st)
340 time_t ret, ret1;
342 ret = MIN(st->st_ctime, st->st_mtime);
343 ret1 = MIN(ret, st->st_atime);
345 if(ret1 != (time_t)0) {
346 return ret1;
350 * One of ctime, mtime or atime was zero (probably atime).
351 * Just return MIN(ctime, mtime).
353 return ret;
356 /****************************************************************************
357 Return the 'create time' from a stat struct if it exists (birthtime) or else
358 use the best approximation.
359 ****************************************************************************/
361 struct timespec get_create_timespec(const SMB_STRUCT_STAT *pst,bool fake_dirs)
363 struct timespec ret;
365 if(S_ISDIR(pst->st_mode) && fake_dirs) {
366 ret.tv_sec = 315493200L; /* 1/1/1980 */
367 ret.tv_nsec = 0;
368 return ret;
371 #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC)
372 ret = pst->st_birthtimespec;
373 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
374 ret.tv_sec = pst->st_birthtime;
375 ret.tv_nsec = pst->st_birthtimenspec;
376 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
377 ret.tv_sec = pst->st_birthtime;
378 ret.tv_nsec = 0;
379 #else
380 ret.tv_sec = calc_create_time(pst);
381 ret.tv_nsec = 0;
382 #endif
384 /* Deal with systems that don't initialize birthtime correctly.
385 * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
387 if (null_timespec(ret)) {
388 ret.tv_sec = calc_create_time(pst);
389 ret.tv_nsec = 0;
391 return ret;
394 /****************************************************************************
395 Get/Set all the possible time fields from a stat struct as a timespec.
396 ****************************************************************************/
398 struct timespec get_atimespec(const SMB_STRUCT_STAT *pst)
400 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
401 struct timespec ret;
403 /* Old system - no ns timestamp. */
404 ret.tv_sec = pst->st_atime;
405 ret.tv_nsec = 0;
406 return ret;
407 #else
408 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
409 return pst->st_atim;
410 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
411 struct timespec ret;
412 ret.tv_sec = pst->st_atime;
413 ret.tv_nsec = pst->st_atimensec;
414 return ret;
415 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
416 struct timespec ret;
417 ret.tv_sec = pst->st_atime;
418 ret.tv_nsec = pst->st_atime_n;
419 return ret;
420 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
421 struct timespec ret;
422 ret.tv_sec = pst->st_atime;
423 ret.tv_nsec = pst->st_uatime * 1000;
424 return ret;
425 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
426 return pst->st_atimespec;
427 #else
428 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
429 #endif
430 #endif
433 void set_atimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
435 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
436 /* Old system - no ns timestamp. */
437 pst->st_atime = ts.tv_sec;
438 #else
439 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
440 pst->st_atim = ts;
441 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
442 pst->st_atime = ts.tv_sec;
443 pst->st_atimensec = ts.tv_nsec;
444 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
445 pst->st_atime = ts.tv_sec;
446 pst->st_atime_n = ts.tv_nsec;
447 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
448 pst->st_atime = ts.tv_sec;
449 pst->st_uatime = ts.tv_nsec / 1000;
450 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
451 pst->st_atimespec = ts;
452 #else
453 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
454 #endif
455 #endif
458 struct timespec get_mtimespec(const SMB_STRUCT_STAT *pst)
460 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
461 struct timespec ret;
463 /* Old system - no ns timestamp. */
464 ret.tv_sec = pst->st_mtime;
465 ret.tv_nsec = 0;
466 return ret;
467 #else
468 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
469 return pst->st_mtim;
470 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
471 struct timespec ret;
472 ret.tv_sec = pst->st_mtime;
473 ret.tv_nsec = pst->st_mtimensec;
474 return ret;
475 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
476 struct timespec ret;
477 ret.tv_sec = pst->st_mtime;
478 ret.tv_nsec = pst->st_mtime_n;
479 return ret;
480 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
481 struct timespec ret;
482 ret.tv_sec = pst->st_mtime;
483 ret.tv_nsec = pst->st_umtime * 1000;
484 return ret;
485 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
486 return pst->st_mtimespec;
487 #else
488 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
489 #endif
490 #endif
493 void set_mtimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
495 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
496 /* Old system - no ns timestamp. */
497 pst->st_mtime = ts.tv_sec;
498 #else
499 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
500 pst->st_mtim = ts;
501 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
502 pst->st_mtime = ts.tv_sec;
503 pst->st_mtimensec = ts.tv_nsec;
504 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
505 pst->st_mtime = ts.tv_sec;
506 pst->st_mtime_n = ts.tv_nsec;
507 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
508 pst->st_mtime = ts.tv_sec;
509 pst->st_umtime = ts.tv_nsec / 1000;
510 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
511 pst->st_mtimespec = ts;
512 #else
513 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
514 #endif
515 #endif
518 struct timespec get_ctimespec(const SMB_STRUCT_STAT *pst)
520 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
521 struct timespec ret;
523 /* Old system - no ns timestamp. */
524 ret.tv_sec = pst->st_ctime;
525 ret.tv_nsec = 0;
526 return ret;
527 #else
528 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
529 return pst->st_ctim;
530 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
531 struct timespec ret;
532 ret.tv_sec = pst->st_ctime;
533 ret.tv_nsec = pst->st_ctimensec;
534 return ret;
535 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
536 struct timespec ret;
537 ret.tv_sec = pst->st_ctime;
538 ret.tv_nsec = pst->st_ctime_n;
539 return ret;
540 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
541 struct timespec ret;
542 ret.tv_sec = pst->st_ctime;
543 ret.tv_nsec = pst->st_uctime * 1000;
544 return ret;
545 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
546 return pst->st_ctimespec;
547 #else
548 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
549 #endif
550 #endif
553 void set_ctimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
555 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
556 /* Old system - no ns timestamp. */
557 pst->st_ctime = ts.tv_sec;
558 #else
559 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
560 pst->st_ctim = ts;
561 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
562 pst->st_ctime = ts.tv_sec;
563 pst->st_ctimensec = ts.tv_nsec;
564 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
565 pst->st_ctime = ts.tv_sec;
566 pst->st_ctime_n = ts.tv_nsec;
567 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
568 pst->st_ctime = ts.tv_sec;
569 pst->st_uctime = ts.tv_nsec / 1000;
570 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
571 pst->st_ctimespec = ts;
572 #else
573 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
574 #endif
575 #endif
578 void dos_filetime_timespec(struct timespec *tsp)
580 tsp->tv_sec &= ~1;
581 tsp->tv_nsec = 0;
584 /*******************************************************************
585 Create a unix date (int GMT) from a dos date (which is actually in
586 localtime).
587 ********************************************************************/
589 static time_t make_unix_date(const void *date_ptr, int zone_offset)
591 uint32_t dos_date=0;
592 struct tm t;
593 time_t ret;
595 dos_date = IVAL(date_ptr,0);
597 if (dos_date == 0) {
598 return 0;
601 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
602 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
603 t.tm_isdst = -1;
605 ret = timegm(&t);
607 ret += zone_offset;
609 return(ret);
612 /*******************************************************************
613 Like make_unix_date() but the words are reversed.
614 ********************************************************************/
616 static time_t make_unix_date2(const void *date_ptr, int zone_offset)
618 uint32_t x,x2;
620 x = IVAL(date_ptr,0);
621 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
622 SIVAL(&x,0,x2);
624 return(make_unix_date((const void *)&x, zone_offset));
627 /*******************************************************************
628 Create a unix GMT date from a dos date in 32 bit "unix like" format
629 these generally arrive as localtimes, with corresponding DST.
630 ******************************************************************/
632 static time_t make_unix_date3(const void *date_ptr, int zone_offset)
634 time_t t = (time_t)IVAL(date_ptr,0);
635 if (!null_mtime(t)) {
636 t += zone_offset;
638 return(t);
641 time_t srv_make_unix_date(const void *date_ptr)
643 return make_unix_date(date_ptr, server_zone_offset);
646 time_t srv_make_unix_date2(const void *date_ptr)
648 return make_unix_date2(date_ptr, server_zone_offset);
651 time_t srv_make_unix_date3(const void *date_ptr)
653 return make_unix_date3(date_ptr, server_zone_offset);
656 /****************************************************************************
657 Convert a normalized timeval to a timespec.
658 ****************************************************************************/
660 struct timespec convert_timeval_to_timespec(const struct timeval tv)
662 struct timespec ts;
663 ts.tv_sec = tv.tv_sec;
664 ts.tv_nsec = tv.tv_usec * 1000;
665 return ts;
668 /****************************************************************************
669 Convert a normalized timespec to a timeval.
670 ****************************************************************************/
672 struct timeval convert_timespec_to_timeval(const struct timespec ts)
674 struct timeval tv;
675 tv.tv_sec = ts.tv_sec;
676 tv.tv_usec = ts.tv_nsec / 1000;
677 return tv;
680 /****************************************************************************
681 Return a timespec for the current time
682 ****************************************************************************/
684 struct timespec timespec_current(void)
686 struct timeval tv;
687 struct timespec ts;
688 GetTimeOfDay(&tv);
689 ts.tv_sec = tv.tv_sec;
690 ts.tv_nsec = tv.tv_usec * 1000;
691 return ts;
694 /****************************************************************************
695 Return the lesser of two timespecs.
696 ****************************************************************************/
698 struct timespec timespec_min(const struct timespec *ts1,
699 const struct timespec *ts2)
701 if (ts1->tv_sec < ts2->tv_sec) return *ts1;
702 if (ts1->tv_sec > ts2->tv_sec) return *ts2;
703 if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
704 return *ts2;
707 /****************************************************************************
708 compare two timespec structures.
709 Return -1 if ts1 < ts2
710 Return 0 if ts1 == ts2
711 Return 1 if ts1 > ts2
712 ****************************************************************************/
714 int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
716 if (ts1->tv_sec > ts2->tv_sec) return 1;
717 if (ts1->tv_sec < ts2->tv_sec) return -1;
718 if (ts1->tv_nsec > ts2->tv_nsec) return 1;
719 if (ts1->tv_nsec < ts2->tv_nsec) return -1;
720 return 0;
723 /****************************************************************************
724 Round up a timespec if nsec > 500000000, round down if lower,
725 then zero nsec.
726 ****************************************************************************/
728 void round_timespec_to_sec(struct timespec *ts)
730 ts->tv_sec = convert_timespec_to_time_t(*ts);
731 ts->tv_nsec = 0;
734 /****************************************************************************
735 Round a timespec to usec value.
736 ****************************************************************************/
738 void round_timespec_to_usec(struct timespec *ts)
740 struct timeval tv = convert_timespec_to_timeval(*ts);
741 *ts = convert_timeval_to_timespec(tv);
744 /****************************************************************************
745 Interprets an nt time into a unix struct timespec.
746 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
747 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
748 ****************************************************************************/
750 struct timespec interpret_long_date(const char *p)
752 NTTIME nt;
753 nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);
754 if (nt == (uint64_t)-1) {
755 struct timespec ret;
756 ret.tv_sec = (time_t)-1;
757 ret.tv_nsec = 0;
758 return ret;
760 return nt_time_to_unix_timespec(&nt);
763 /***************************************************************************
764 Client versions of the above functions.
765 ***************************************************************************/
767 void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate)
769 put_dos_date(buf, offset, unixdate, cli->serverzone);
772 void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate)
774 put_dos_date2(buf, offset, unixdate, cli->serverzone);
777 void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate)
779 put_dos_date3(buf, offset, unixdate, cli->serverzone);
782 time_t cli_make_unix_date(struct cli_state *cli, const void *date_ptr)
784 return make_unix_date(date_ptr, cli->serverzone);
787 time_t cli_make_unix_date2(struct cli_state *cli, const void *date_ptr)
789 return make_unix_date2(date_ptr, cli->serverzone);
792 time_t cli_make_unix_date3(struct cli_state *cli, const void *date_ptr)
794 return make_unix_date3(date_ptr, cli->serverzone);
797 /****************************************************************************
798 Check if two NTTIMEs are the same.
799 ****************************************************************************/
801 bool nt_time_equals(const NTTIME *nt1, const NTTIME *nt2)
803 return (*nt1 == *nt2);
806 /*******************************************************************
807 Re-read the smb serverzone value.
808 ******************************************************************/
810 static struct timeval start_time_hires;
812 void TimeInit(void)
814 set_server_zone_offset(time(NULL));
816 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
818 /* Save the start time of this process. */
819 if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
820 GetTimeOfDay(&start_time_hires);
824 /**********************************************************************
825 Return a timeval struct of the uptime of this process. As TimeInit is
826 done before a daemon fork then this is the start time from the parent
827 daemon start. JRA.
828 ***********************************************************************/
830 void get_process_uptime(struct timeval *ret_time)
832 struct timeval time_now_hires;
834 GetTimeOfDay(&time_now_hires);
835 ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
836 if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
837 ret_time->tv_sec -= 1;
838 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
839 } else {
840 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
844 /****************************************************************************
845 Convert a NTTIME structure to a time_t.
846 It's originally in "100ns units".
848 This is an absolute version of the one above.
849 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
850 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
851 ****************************************************************************/
853 time_t nt_time_to_unix_abs(const NTTIME *nt)
855 uint64_t d;
857 if (*nt == 0) {
858 return (time_t)0;
861 if (*nt == (uint64_t)-1) {
862 return (time_t)-1;
865 if (*nt == NTTIME_INFINITY) {
866 return (time_t)-1;
869 /* reverse the time */
870 /* it's a negative value, turn it to positive */
871 d=~*nt;
873 d += 1000*1000*10/2;
874 d /= 1000*1000*10;
876 if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
877 return (time_t)0;
880 return (time_t)d;
883 time_t uint64s_nt_time_to_unix_abs(const uint64_t *src)
885 NTTIME nttime;
886 nttime = *src;
887 return nt_time_to_unix_abs(&nttime);
890 /****************************************************************************
891 Put a 8 byte filetime from a struct timespec. Uses GMT.
892 ****************************************************************************/
894 void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
896 uint64_t d;
898 if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
899 *nt = 0;
900 return;
902 if (ts.tv_sec == TIME_T_MAX) {
903 *nt = 0x7fffffffffffffffLL;
904 return;
906 if (ts.tv_sec == (time_t)-1) {
907 *nt = (uint64_t)-1;
908 return;
911 d = ts.tv_sec;
912 d += TIME_FIXUP_CONSTANT_INT;
913 d *= 1000*1000*10;
914 /* d is now in 100ns units. */
915 d += (ts.tv_nsec / 100);
917 *nt = d;
920 /****************************************************************************
921 Convert a time_t to a NTTIME structure
923 This is an absolute version of the one above.
924 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
925 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
926 ****************************************************************************/
928 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
930 double d;
932 if (t==0) {
933 *nt = 0;
934 return;
937 if (t == TIME_T_MAX) {
938 *nt = 0x7fffffffffffffffLL;
939 return;
942 if (t == (time_t)-1) {
943 /* that's what NT uses for infinite */
944 *nt = NTTIME_INFINITY;
945 return;
948 d = (double)(t);
949 d *= 1.0e7;
951 *nt = (NTTIME)d;
953 /* convert to a negative value */
954 *nt=~*nt;
958 /****************************************************************************
959 Check if it's a null mtime.
960 ****************************************************************************/
962 bool null_mtime(time_t mtime)
964 if (mtime == 0 || mtime == (time_t)0xFFFFFFFF || mtime == (time_t)-1)
965 return true;
966 return false;
969 /****************************************************************************
970 Utility function that always returns a const string even if localtime
971 and asctime fail.
972 ****************************************************************************/
974 const char *time_to_asc(const time_t t)
976 const char *asct;
977 struct tm *lt = localtime(&t);
979 if (!lt) {
980 return "unknown time";
983 asct = asctime(lt);
984 if (!asct) {
985 return "unknown time";
987 return asct;
990 const char *display_time(NTTIME nttime)
992 float high;
993 float low;
994 int sec;
995 int days, hours, mins, secs;
997 if (nttime==0)
998 return "Now";
1000 if (nttime==NTTIME_INFINITY)
1001 return "Never";
1003 high = 65536;
1004 high = high/10000;
1005 high = high*65536;
1006 high = high/1000;
1007 high = high * (~(nttime >> 32));
1009 low = ~(nttime & 0xFFFFFFFF);
1010 low = low/(1000*1000*10);
1012 sec=(int)(high+low);
1014 days=sec/(60*60*24);
1015 hours=(sec - (days*60*60*24)) / (60*60);
1016 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
1017 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
1019 return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
1020 "%u seconds", days, hours, mins, secs);
1023 bool nt_time_is_set(const NTTIME *nt)
1025 if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
1026 return false;
1029 if (*nt == NTTIME_INFINITY) {
1030 return false;
1033 return true;