Document default of the printing config variable.
[Samba/nascimento.git] / source3 / lib / time.c
blob7dd0da8fa8ec43f90b9611b7ba42a0a82cc71ddb
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_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
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_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_ATIMESPEC)
408 return pst->st_atimespec;
409 #else
410 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
411 #endif
412 #endif
415 void set_atimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
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 #elif defined(HAVE_STAT_ST_ATIMESPEC)
427 pst->st_atimespec = ts;
428 #else
429 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
430 #endif
431 #endif
434 struct timespec get_mtimespec(const SMB_STRUCT_STAT *pst)
436 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
437 struct timespec ret;
439 /* Old system - no ns timestamp. */
440 ret.tv_sec = pst->st_mtime;
441 ret.tv_nsec = 0;
442 return ret;
443 #else
444 #if defined(HAVE_STAT_ST_MTIM)
445 return pst->st_mtim;
446 #elif defined(HAVE_STAT_ST_MTIMENSEC)
447 struct timespec ret;
448 ret.tv_sec = pst->st_mtime;
449 ret.tv_nsec = pst->st_mtimensec;
450 return ret;
451 #elif defined(HAVE_STAT_ST_MTIMESPEC)
452 return pst->st_mtimespec;
453 #else
454 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
455 #endif
456 #endif
459 void set_mtimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
461 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
462 /* Old system - no ns timestamp. */
463 pst->st_mtime = ts.tv_sec;
464 #else
465 #if defined(HAVE_STAT_ST_MTIM)
466 pst->st_mtim = ts;
467 #elif defined(HAVE_STAT_ST_MTIMENSEC)
468 pst->st_mtime = ts.tv_sec;
469 pst->st_mtimensec = ts.tv_nsec
470 #elif defined(HAVE_STAT_ST_ATIMESPEC)
471 pst->st_atimespec = ts;
472 #else
473 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
474 #endif
475 #endif
478 struct timespec get_ctimespec(const SMB_STRUCT_STAT *pst)
480 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
481 struct timespec ret;
483 /* Old system - no ns timestamp. */
484 ret.tv_sec = pst->st_ctime;
485 ret.tv_nsec = 0;
486 return ret;
487 #else
488 #if defined(HAVE_STAT_ST_CTIM)
489 return pst->st_ctim;
490 #elif defined(HAVE_STAT_ST_CTIMENSEC)
491 struct timespec ret;
492 ret.tv_sec = pst->st_ctime;
493 ret.tv_nsec = pst->st_ctimensec;
494 return ret;
495 #elif defined(HAVE_STAT_ST_CTIMESPEC)
496 return pst->st_ctimespec;
497 #else
498 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
499 #endif
500 #endif
503 void set_ctimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
505 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
506 /* Old system - no ns timestamp. */
507 pst->st_ctime = ts.tv_sec;
508 #else
509 #if defined(HAVE_STAT_ST_CTIM)
510 pst->st_ctim = ts;
511 #elif defined(HAVE_STAT_ST_CTIMENSEC)
512 pst->st_ctime = ts.tv_sec;
513 pst->st_ctimensec = ts.tv_nsec
514 #elif defined(HAVE_STAT_ST_CTIMESPEC)
515 pst->st_ctimespec = ts;
516 #else
517 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
518 #endif
519 #endif
522 void dos_filetime_timespec(struct timespec *tsp)
524 tsp->tv_sec &= ~1;
525 tsp->tv_nsec = 0;
528 /*******************************************************************
529 Create a unix date (int GMT) from a dos date (which is actually in
530 localtime).
531 ********************************************************************/
533 static time_t make_unix_date(const void *date_ptr, int zone_offset)
535 uint32_t dos_date=0;
536 struct tm t;
537 time_t ret;
539 dos_date = IVAL(date_ptr,0);
541 if (dos_date == 0) {
542 return 0;
545 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
546 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
547 t.tm_isdst = -1;
549 ret = timegm(&t);
551 ret += zone_offset;
553 return(ret);
556 /*******************************************************************
557 Like make_unix_date() but the words are reversed.
558 ********************************************************************/
560 static time_t make_unix_date2(const void *date_ptr, int zone_offset)
562 uint32_t x,x2;
564 x = IVAL(date_ptr,0);
565 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
566 SIVAL(&x,0,x2);
568 return(make_unix_date((const void *)&x, zone_offset));
571 /*******************************************************************
572 Create a unix GMT date from a dos date in 32 bit "unix like" format
573 these generally arrive as localtimes, with corresponding DST.
574 ******************************************************************/
576 static time_t make_unix_date3(const void *date_ptr, int zone_offset)
578 time_t t = (time_t)IVAL(date_ptr,0);
579 if (!null_mtime(t)) {
580 t += zone_offset;
582 return(t);
585 time_t srv_make_unix_date(const void *date_ptr)
587 return make_unix_date(date_ptr, server_zone_offset);
590 time_t srv_make_unix_date2(const void *date_ptr)
592 return make_unix_date2(date_ptr, server_zone_offset);
595 time_t srv_make_unix_date3(const void *date_ptr)
597 return make_unix_date3(date_ptr, server_zone_offset);
600 /****************************************************************************
601 Convert a normalized timeval to a timespec.
602 ****************************************************************************/
604 struct timespec convert_timeval_to_timespec(const struct timeval tv)
606 struct timespec ts;
607 ts.tv_sec = tv.tv_sec;
608 ts.tv_nsec = tv.tv_usec * 1000;
609 return ts;
612 /****************************************************************************
613 Convert a normalized timespec to a timeval.
614 ****************************************************************************/
616 struct timeval convert_timespec_to_timeval(const struct timespec ts)
618 struct timeval tv;
619 tv.tv_sec = ts.tv_sec;
620 tv.tv_usec = ts.tv_nsec / 1000;
621 return tv;
624 /****************************************************************************
625 Return a timespec for the current time
626 ****************************************************************************/
628 struct timespec timespec_current(void)
630 struct timeval tv;
631 struct timespec ts;
632 GetTimeOfDay(&tv);
633 ts.tv_sec = tv.tv_sec;
634 ts.tv_nsec = tv.tv_usec * 1000;
635 return ts;
638 /****************************************************************************
639 Return the lesser of two timespecs.
640 ****************************************************************************/
642 struct timespec timespec_min(const struct timespec *ts1,
643 const struct timespec *ts2)
645 if (ts1->tv_sec < ts2->tv_sec) return *ts1;
646 if (ts1->tv_sec > ts2->tv_sec) return *ts2;
647 if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
648 return *ts2;
651 /****************************************************************************
652 compare two timespec structures.
653 Return -1 if ts1 < ts2
654 Return 0 if ts1 == ts2
655 Return 1 if ts1 > ts2
656 ****************************************************************************/
658 int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
660 if (ts1->tv_sec > ts2->tv_sec) return 1;
661 if (ts1->tv_sec < ts2->tv_sec) return -1;
662 if (ts1->tv_nsec > ts2->tv_nsec) return 1;
663 if (ts1->tv_nsec < ts2->tv_nsec) return -1;
664 return 0;
667 /****************************************************************************
668 Interprets an nt time into a unix struct timespec.
669 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
670 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
671 ****************************************************************************/
673 struct timespec interpret_long_date(const char *p)
675 NTTIME nt;
676 nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);
677 if (nt == (uint64_t)-1) {
678 struct timespec ret;
679 ret.tv_sec = (time_t)-1;
680 ret.tv_nsec = 0;
681 return ret;
683 return nt_time_to_unix_timespec(&nt);
686 /***************************************************************************
687 Client versions of the above functions.
688 ***************************************************************************/
690 void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate)
692 put_dos_date(buf, offset, unixdate, cli->serverzone);
695 void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate)
697 put_dos_date2(buf, offset, unixdate, cli->serverzone);
700 void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate)
702 put_dos_date3(buf, offset, unixdate, cli->serverzone);
705 time_t cli_make_unix_date(struct cli_state *cli, const void *date_ptr)
707 return make_unix_date(date_ptr, cli->serverzone);
710 time_t cli_make_unix_date2(struct cli_state *cli, const void *date_ptr)
712 return make_unix_date2(date_ptr, cli->serverzone);
715 time_t cli_make_unix_date3(struct cli_state *cli, const void *date_ptr)
717 return make_unix_date3(date_ptr, cli->serverzone);
720 /****************************************************************************
721 Check if two NTTIMEs are the same.
722 ****************************************************************************/
724 bool nt_time_equals(const NTTIME *nt1, const NTTIME *nt2)
726 return (*nt1 == *nt2);
729 /*******************************************************************
730 Re-read the smb serverzone value.
731 ******************************************************************/
733 static struct timeval start_time_hires;
735 void TimeInit(void)
737 set_server_zone_offset(time(NULL));
739 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
741 /* Save the start time of this process. */
742 if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
743 GetTimeOfDay(&start_time_hires);
747 /**********************************************************************
748 Return a timeval struct of the uptime of this process. As TimeInit is
749 done before a daemon fork then this is the start time from the parent
750 daemon start. JRA.
751 ***********************************************************************/
753 void get_process_uptime(struct timeval *ret_time)
755 struct timeval time_now_hires;
757 GetTimeOfDay(&time_now_hires);
758 ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
759 if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
760 ret_time->tv_sec -= 1;
761 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
762 } else {
763 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
767 /****************************************************************************
768 Convert a NTTIME structure to a time_t.
769 It's originally in "100ns units".
771 This is an absolute version of the one above.
772 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
773 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
774 ****************************************************************************/
776 time_t nt_time_to_unix_abs(const NTTIME *nt)
778 uint64_t d;
780 if (*nt == 0) {
781 return (time_t)0;
784 if (*nt == (uint64_t)-1) {
785 return (time_t)-1;
788 if (*nt == NTTIME_INFINITY) {
789 return (time_t)-1;
792 /* reverse the time */
793 /* it's a negative value, turn it to positive */
794 d=~*nt;
796 d += 1000*1000*10/2;
797 d /= 1000*1000*10;
799 if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
800 return (time_t)0;
803 return (time_t)d;
806 time_t uint64s_nt_time_to_unix_abs(const uint64_t *src)
808 NTTIME nttime;
809 nttime = *src;
810 return nt_time_to_unix_abs(&nttime);
813 /****************************************************************************
814 Put a 8 byte filetime from a struct timespec. Uses GMT.
815 ****************************************************************************/
817 void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
819 uint64_t d;
821 if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
822 *nt = 0;
823 return;
825 if (ts.tv_sec == TIME_T_MAX) {
826 *nt = 0x7fffffffffffffffLL;
827 return;
829 if (ts.tv_sec == (time_t)-1) {
830 *nt = (uint64_t)-1;
831 return;
834 d = ts.tv_sec;
835 d += TIME_FIXUP_CONSTANT_INT;
836 d *= 1000*1000*10;
837 /* d is now in 100ns units. */
838 d += (ts.tv_nsec / 100);
840 *nt = d;
843 /****************************************************************************
844 Convert a time_t to a NTTIME structure
846 This is an absolute version of the one above.
847 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
848 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
849 ****************************************************************************/
851 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
853 double d;
855 if (t==0) {
856 *nt = 0;
857 return;
860 if (t == TIME_T_MAX) {
861 *nt = 0x7fffffffffffffffLL;
862 return;
865 if (t == (time_t)-1) {
866 /* that's what NT uses for infinite */
867 *nt = NTTIME_INFINITY;
868 return;
871 d = (double)(t);
872 d *= 1.0e7;
874 *nt = (NTTIME)d;
876 /* convert to a negative value */
877 *nt=~*nt;
881 /****************************************************************************
882 Check if it's a null mtime.
883 ****************************************************************************/
885 bool null_mtime(time_t mtime)
887 if (mtime == 0 || mtime == (time_t)0xFFFFFFFF || mtime == (time_t)-1)
888 return true;
889 return false;
892 /****************************************************************************
893 Utility function that always returns a const string even if localtime
894 and asctime fail.
895 ****************************************************************************/
897 const char *time_to_asc(const time_t t)
899 const char *asct;
900 struct tm *lt = localtime(&t);
902 if (!lt) {
903 return "unknown time";
906 asct = asctime(lt);
907 if (!asct) {
908 return "unknown time";
910 return asct;
913 const char *display_time(NTTIME nttime)
915 float high;
916 float low;
917 int sec;
918 int days, hours, mins, secs;
920 if (nttime==0)
921 return "Now";
923 if (nttime==NTTIME_INFINITY)
924 return "Never";
926 high = 65536;
927 high = high/10000;
928 high = high*65536;
929 high = high/1000;
930 high = high * (~(nttime >> 32));
932 low = ~(nttime & 0xFFFFFFFF);
933 low = low/(1000*1000*10);
935 sec=(int)(high+low);
937 days=sec/(60*60*24);
938 hours=(sec - (days*60*60*24)) / (60*60);
939 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
940 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
942 return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
943 "%u seconds", days, hours, mins, secs);
946 bool nt_time_is_set(const NTTIME *nt)
948 if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
949 return false;
952 if (*nt == NTTIME_INFINITY) {
953 return false;
956 return true;