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/>.
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
39 /*******************************************************************
40 create a 16 bit dos packed date
41 ********************************************************************/
42 static uint16_t make_dos_date1(struct tm
*t
)
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));
50 /*******************************************************************
51 create a 16 bit dos packed time
52 ********************************************************************/
53 static uint16_t make_dos_time1(struct tm
*t
)
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));
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
)
74 unixdate
-= zone_offset
;
76 t
= gmtime(&unixdate
);
81 ret
= make_dos_date1(t
);
82 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
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
) {
106 } else if (t
== 0x7FFFFFFFFFFFFFFFLL
) {
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
;
126 /****************************************************************************
127 Check if NTTIME is 0.
128 ****************************************************************************/
130 bool nt_time_is_zero(const NTTIME
*nt
)
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
)
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) {
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
)
192 t
= (time_t)tp
.tv_sec
;
201 "%ld.%06ld seconds since the Epoch",
207 "%ld seconds since the Epoch",
213 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
214 slprintf(TimeBuf
+strlen(TimeBuf
),
215 sizeof(TimeBuf
)-1 - strlen(TimeBuf
),
219 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
223 const char *asct
= asctime(tm
);
227 asct
? asct
: "unknown",
230 const char *asct
= asctime(tm
);
231 fstrcpy(TimeBuf
, asct
? asct
: "unknown");
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
);
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);
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
)
299 case TIMESTAMP_SET_SECONDS
:
300 round_timespec_to_sec(ts
);
302 case TIMESTAMP_SET_MSEC
:
303 round_timespec_to_usec(ts
);
305 case TIMESTAMP_SET_NT_OR_BETTER
:
306 /* No rounding needed. */
311 /****************************************************************************
312 Take a Unix time and convert to an NTTIME structure and place in buffer
314 ****************************************************************************/
316 void put_long_date_timespec(enum timestamp_set_resolution res
, char *p
, struct timespec ts
)
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
)
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
336 ****************************************************************************/
338 static time_t calc_create_time(const SMB_STRUCT_STAT
*st
)
342 ret
= MIN(st
->st_ctime
, st
->st_mtime
);
343 ret1
= MIN(ret
, st
->st_atime
);
345 if(ret1
!= (time_t)0) {
350 * One of ctime, mtime or atime was zero (probably atime).
351 * Just return MIN(ctime, mtime).
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
)
365 if(S_ISDIR(pst
->st_mode
) && fake_dirs
) {
366 ret
.tv_sec
= 315493200L; /* 1/1/1980 */
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
;
380 ret
.tv_sec
= calc_create_time(pst
);
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
);
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)
403 /* Old system - no ns timestamp. */
404 ret
.tv_sec
= pst
->st_atime
;
408 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
410 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
412 ret
.tv_sec
= pst
->st_atime
;
413 ret
.tv_nsec
= pst
->st_atimensec
;
415 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
417 ret
.tv_sec
= pst
->st_atime
;
418 ret
.tv_nsec
= pst
->st_atime_n
;
420 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
422 ret
.tv_sec
= pst
->st_atime
;
423 ret
.tv_nsec
= pst
->st_uatime
* 1000;
425 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
426 return pst
->st_atimespec
;
428 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
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
;
439 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
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
;
453 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
458 struct timespec
get_mtimespec(const SMB_STRUCT_STAT
*pst
)
460 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
463 /* Old system - no ns timestamp. */
464 ret
.tv_sec
= pst
->st_mtime
;
468 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
470 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
472 ret
.tv_sec
= pst
->st_mtime
;
473 ret
.tv_nsec
= pst
->st_mtimensec
;
475 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
477 ret
.tv_sec
= pst
->st_mtime
;
478 ret
.tv_nsec
= pst
->st_mtime_n
;
480 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
482 ret
.tv_sec
= pst
->st_mtime
;
483 ret
.tv_nsec
= pst
->st_umtime
* 1000;
485 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
486 return pst
->st_mtimespec
;
488 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
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
;
499 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
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
;
513 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
518 struct timespec
get_ctimespec(const SMB_STRUCT_STAT
*pst
)
520 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
523 /* Old system - no ns timestamp. */
524 ret
.tv_sec
= pst
->st_ctime
;
528 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
530 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
532 ret
.tv_sec
= pst
->st_ctime
;
533 ret
.tv_nsec
= pst
->st_ctimensec
;
535 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
537 ret
.tv_sec
= pst
->st_ctime
;
538 ret
.tv_nsec
= pst
->st_ctime_n
;
540 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
542 ret
.tv_sec
= pst
->st_ctime
;
543 ret
.tv_nsec
= pst
->st_uctime
* 1000;
545 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
546 return pst
->st_ctimespec
;
548 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
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
;
559 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
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
;
573 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
578 void dos_filetime_timespec(struct timespec
*tsp
)
584 /*******************************************************************
585 Create a unix date (int GMT) from a dos date (which is actually in
587 ********************************************************************/
589 static time_t make_unix_date(const void *date_ptr
, int zone_offset
)
595 dos_date
= IVAL(date_ptr
,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
);
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
)
620 x
= IVAL(date_ptr
,0);
621 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
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
)) {
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
)
663 ts
.tv_sec
= tv
.tv_sec
;
664 ts
.tv_nsec
= tv
.tv_usec
* 1000;
668 /****************************************************************************
669 Convert a normalized timespec to a timeval.
670 ****************************************************************************/
672 struct timeval
convert_timespec_to_timeval(const struct timespec ts
)
675 tv
.tv_sec
= ts
.tv_sec
;
676 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
680 /****************************************************************************
681 Return a timespec for the current time
682 ****************************************************************************/
684 struct timespec
timespec_current(void)
689 ts
.tv_sec
= tv
.tv_sec
;
690 ts
.tv_nsec
= tv
.tv_usec
* 1000;
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
;
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;
723 /****************************************************************************
724 Round up a timespec if nsec > 500000000, round down if lower,
726 ****************************************************************************/
728 void round_timespec_to_sec(struct timespec
*ts
)
730 ts
->tv_sec
= convert_timespec_to_time_t(*ts
);
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
)
753 nt
= IVAL(p
,0) + ((uint64_t)IVAL(p
,4) << 32);
754 if (nt
== (uint64_t)-1) {
756 ret
.tv_sec
= (time_t)-1;
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
;
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
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
);
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
)
861 if (*nt
== (uint64_t)-1) {
865 if (*nt
== NTTIME_INFINITY
) {
869 /* reverse the time */
870 /* it's a negative value, turn it to positive */
876 if (!(TIME_T_MIN
<= ((time_t)d
) && ((time_t)d
) <= TIME_T_MAX
)) {
883 time_t uint64s_nt_time_to_unix_abs(const uint64_t *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
)
898 if (ts
.tv_sec
==0 && ts
.tv_nsec
== 0) {
902 if (ts
.tv_sec
== TIME_T_MAX
) {
903 *nt
= 0x7fffffffffffffffLL
;
906 if (ts
.tv_sec
== (time_t)-1) {
912 d
+= TIME_FIXUP_CONSTANT_INT
;
914 /* d is now in 100ns units. */
915 d
+= (ts
.tv_nsec
/ 100);
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
)
937 if (t
== TIME_T_MAX
) {
938 *nt
= 0x7fffffffffffffffLL
;
942 if (t
== (time_t)-1) {
943 /* that's what NT uses for infinite */
944 *nt
= NTTIME_INFINITY
;
953 /* convert to a negative value */
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)
969 /****************************************************************************
970 Utility function that always returns a const string even if localtime
972 ****************************************************************************/
974 const char *time_to_asc(const time_t t
)
977 struct tm
*lt
= localtime(&t
);
980 return "unknown time";
985 return "unknown time";
990 const char *display_time(NTTIME nttime
)
995 int days
, hours
, mins
, secs
;
1000 if (nttime
==NTTIME_INFINITY
)
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
) {
1029 if (*nt
== NTTIME_INFINITY
) {