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
32 #define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
33 : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
36 #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
39 #define NTTIME_INFINITY (NTTIME)0x8000000000000000LL
41 /***************************************************************************
42 External access to time_t_min and time_t_max.
43 ****************************************************************************/
45 time_t get_time_t_max(void)
50 /***************************************************************************
51 A gettimeofday wrapper.
52 ****************************************************************************/
54 void GetTimeOfDay(struct timeval
*tval
)
56 #ifdef HAVE_GETTIMEOFDAY_TZ
57 gettimeofday(tval
,NULL
);
63 #if (SIZEOF_LONG == 8)
64 #define TIME_FIXUP_CONSTANT_INT 11644473600L
65 #elif (SIZEOF_LONG_LONG == 8)
66 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
69 /****************************************************************************
70 Interpret an 8 byte "filetime" structure to a time_t
71 It's originally in "100ns units since jan 1st 1601"
73 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
79 ****************************************************************************/
81 time_t nt_time_to_unix(NTTIME nt
)
83 return convert_timespec_to_time_t(nt_time_to_unix_timespec(&nt
));
86 /****************************************************************************
87 Put a 8 byte filetime from a time_t. Uses GMT.
88 ****************************************************************************/
90 void unix_to_nt_time(NTTIME
*nt
, time_t t
)
94 if (t
== (time_t)-1) {
99 if (t
== TIME_T_MAX
) {
100 *nt
= 0x7fffffffffffffffLL
;
110 t2
+= TIME_FIXUP_CONSTANT_INT
;
116 /****************************************************************************
117 Check if it's a null unix time.
118 ****************************************************************************/
120 bool null_time(time_t t
)
123 t
== (time_t)0xFFFFFFFF ||
127 /****************************************************************************
128 Check if it's a null NTTIME.
129 ****************************************************************************/
131 bool null_nttime(NTTIME t
)
133 return t
== 0 || t
== (NTTIME
)-1;
136 /****************************************************************************
137 Check if it's a null timespec.
138 ****************************************************************************/
140 bool null_timespec(struct timespec ts
)
142 return ts
.tv_sec
== 0 ||
143 ts
.tv_sec
== (time_t)0xFFFFFFFF ||
144 ts
.tv_sec
== (time_t)-1;
147 /*******************************************************************
148 create a 16 bit dos packed date
149 ********************************************************************/
150 static uint16_t make_dos_date1(struct tm
*t
)
153 ret
= (((unsigned int)(t
->tm_mon
+1)) >> 3) | ((t
->tm_year
-80) << 1);
154 ret
= ((ret
&0xFF)<<8) | (t
->tm_mday
| (((t
->tm_mon
+1) & 0x7) << 5));
158 /*******************************************************************
159 create a 16 bit dos packed time
160 ********************************************************************/
161 static uint16_t make_dos_time1(struct tm
*t
)
164 ret
= ((((unsigned int)t
->tm_min
>> 3)&0x7) | (((unsigned int)t
->tm_hour
) << 3));
165 ret
= ((ret
&0xFF)<<8) | ((t
->tm_sec
/2) | ((t
->tm_min
& 0x7) << 5));
169 /*******************************************************************
170 create a 32 bit dos packed date/time from some parameters
171 This takes a GMT time and returns a packed localtime structure
172 ********************************************************************/
173 static uint32_t make_dos_date(time_t unixdate
, int zone_offset
)
182 unixdate
-= zone_offset
;
184 t
= gmtime(&unixdate
);
189 ret
= make_dos_date1(t
);
190 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
196 put a dos date into a buffer (time/date format)
197 This takes GMT time and puts local time in the buffer
199 void push_dos_date(uint8_t *buf
, int offset
, time_t unixdate
, int zone_offset
)
201 uint32_t x
= make_dos_date(unixdate
, zone_offset
);
206 put a dos date into a buffer (date/time format)
207 This takes GMT time and puts local time in the buffer
209 void push_dos_date2(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
212 x
= make_dos_date(unixdate
, zone_offset
);
213 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
218 put a dos 32 bit "unix like" date into a buffer. This routine takes
219 GMT and converts it to LOCAL time before putting it (most SMBs assume
220 localtime for this sort of date)
222 void push_dos_date3(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
224 if (!null_time(unixdate
)) {
225 unixdate
-= zone_offset
;
227 SIVAL(buf
,offset
,unixdate
);
230 /*******************************************************************
231 interpret a 32 bit dos packed date/time to some parameters
232 ********************************************************************/
233 static void interpret_dos_date(uint32_t date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
235 uint32_t p0
,p1
,p2
,p3
;
237 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
238 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
240 *second
= 2*(p0
& 0x1F);
241 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
242 *hour
= (p1
>>3)&0xFF;
244 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
245 *year
= ((p3
>>1)&0xFF) + 80;
249 create a unix date (int GMT) from a dos date (which is actually in
252 time_t pull_dos_date(const uint8_t *date_ptr
, int zone_offset
)
258 dos_date
= IVAL(date_ptr
,0);
260 if (dos_date
== 0) return (time_t)0;
262 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
263 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
274 like make_unix_date() but the words are reversed
276 time_t pull_dos_date2(const uint8_t *date_ptr
, int zone_offset
)
280 x
= IVAL(date_ptr
,0);
281 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
284 return pull_dos_date((const uint8_t *)&x
, zone_offset
);
288 create a unix GMT date from a dos date in 32 bit "unix like" format
289 these generally arrive as localtimes, with corresponding DST
291 time_t pull_dos_date3(const uint8_t *date_ptr
, int zone_offset
)
293 time_t t
= (time_t)IVAL(date_ptr
,0);
300 /***************************************************************************
301 Return a HTTP/1.0 time string.
302 ***************************************************************************/
304 char *http_timestring(time_t t
)
307 struct tm
*tm
= localtime(&t
);
309 if (t
== TIME_T_MAX
) {
310 fstrcpy(buf
, "never");
312 fstr_sprintf(buf
, "%ld seconds since the Epoch", (long)t
);
314 #ifndef HAVE_STRFTIME
315 const char *asct
= asctime(tm
);
316 fstrcpy(buf
, asct
? asct
: "unknown");
318 if(buf
[strlen(buf
)-1] == '\n') {
319 buf
[strlen(buf
)-1] = 0;
320 #else /* !HAVE_STRFTIME */
321 strftime(buf
, sizeof(buf
)-1, "%a, %d %b %Y %H:%M:%S %Z", tm
);
322 #endif /* !HAVE_STRFTIME */
324 return talloc_strdup(talloc_tos(), buf
);
329 Return the date and time as a string
331 char *timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
339 return talloc_asprintf(mem_ctx
,
340 "%ld seconds since the Epoch",
345 /* some versions of gcc complain about using %c. This is a bug
346 in the gcc warning, not a bug in this code. See a recent
347 strftime() manual page for details.
349 strftime(tempTime
,sizeof(tempTime
)-1,"%c %Z",tm
);
350 TimeBuf
= talloc_strdup(mem_ctx
, tempTime
);
352 TimeBuf
= talloc_strdup(mem_ctx
, asctime(tm
));
359 return a talloced string representing a NTTIME for human consumption
361 const char *nt_time_string(TALLOC_CTX
*mem_ctx
, NTTIME nt
)
367 t
= nt_time_to_unix(nt
);
368 return timestring(mem_ctx
, t
);
373 parse a nttime as a large integer in a string and return a NTTIME
375 NTTIME
nttime_from_string(const char *s
)
377 return strtoull(s
, NULL
, 0);
381 return (tv1 - tv2) in microseconds
383 int64_t usec_time_diff(struct timeval
*tv1
, struct timeval
*tv2
)
385 int64_t sec_diff
= tv1
->tv_sec
- tv2
->tv_sec
;
386 return (sec_diff
* 1000000) + (int64_t)(tv1
->tv_usec
- tv2
->tv_usec
);
391 return a zero timeval
393 struct timeval
timeval_zero(void)
402 return True if a timeval is zero
404 bool timeval_is_zero(const struct timeval
*tv
)
406 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
410 return a timeval for the current time
412 struct timeval
timeval_current(void)
420 return a timeval struct with the given elements
422 struct timeval
timeval_set(uint32_t secs
, uint32_t usecs
)
432 return a timeval ofs microseconds after tv
434 struct timeval
timeval_add(const struct timeval
*tv
,
435 uint32_t secs
, uint32_t usecs
)
437 struct timeval tv2
= *tv
;
438 const unsigned int million
= 1000000;
440 tv2
.tv_usec
+= usecs
;
441 tv2
.tv_sec
+= tv2
.tv_usec
/ million
;
442 tv2
.tv_usec
= tv2
.tv_usec
% million
;
447 return the sum of two timeval structures
449 struct timeval
timeval_sum(const struct timeval
*tv1
,
450 const struct timeval
*tv2
)
452 return timeval_add(tv1
, tv2
->tv_sec
, tv2
->tv_usec
);
456 return a timeval secs/usecs into the future
458 struct timeval
timeval_current_ofs(uint32_t secs
, uint32_t usecs
)
460 struct timeval tv
= timeval_current();
461 return timeval_add(&tv
, secs
, usecs
);
465 compare two timeval structures.
466 Return -1 if tv1 < tv2
467 Return 0 if tv1 == tv2
468 Return 1 if tv1 > tv2
470 int timeval_compare(const struct timeval
*tv1
, const struct timeval
*tv2
)
472 if (tv1
->tv_sec
> tv2
->tv_sec
) return 1;
473 if (tv1
->tv_sec
< tv2
->tv_sec
) return -1;
474 if (tv1
->tv_usec
> tv2
->tv_usec
) return 1;
475 if (tv1
->tv_usec
< tv2
->tv_usec
) return -1;
480 return True if a timer is in the past
482 bool timeval_expired(const struct timeval
*tv
)
484 struct timeval tv2
= timeval_current();
485 if (tv2
.tv_sec
> tv
->tv_sec
) return True
;
486 if (tv2
.tv_sec
< tv
->tv_sec
) return False
;
487 return (tv2
.tv_usec
>= tv
->tv_usec
);
491 return the number of seconds elapsed between two times
493 double timeval_elapsed2(const struct timeval
*tv1
, const struct timeval
*tv2
)
495 return (tv2
->tv_sec
- tv1
->tv_sec
) +
496 (tv2
->tv_usec
- tv1
->tv_usec
)*1.0e-6;
500 return the number of seconds elapsed since a given time
502 double timeval_elapsed(const struct timeval
*tv
)
504 struct timeval tv2
= timeval_current();
505 return timeval_elapsed2(tv
, &tv2
);
509 return the lesser of two timevals
511 struct timeval
timeval_min(const struct timeval
*tv1
,
512 const struct timeval
*tv2
)
514 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv1
;
515 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv2
;
516 if (tv1
->tv_usec
< tv2
->tv_usec
) return *tv1
;
521 return the greater of two timevals
523 struct timeval
timeval_max(const struct timeval
*tv1
,
524 const struct timeval
*tv2
)
526 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv1
;
527 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv2
;
528 if (tv1
->tv_usec
> tv2
->tv_usec
) return *tv1
;
533 return the difference between two timevals as a timeval
534 if tv1 comes after tv2, then return a zero timeval
535 (this is *tv2 - *tv1)
537 struct timeval
timeval_until(const struct timeval
*tv1
,
538 const struct timeval
*tv2
)
541 if (timeval_compare(tv1
, tv2
) >= 0) {
542 return timeval_zero();
544 t
.tv_sec
= tv2
->tv_sec
- tv1
->tv_sec
;
545 if (tv1
->tv_usec
> tv2
->tv_usec
) {
547 t
.tv_usec
= 1000000 - (tv1
->tv_usec
- tv2
->tv_usec
);
549 t
.tv_usec
= tv2
->tv_usec
- tv1
->tv_usec
;
556 convert a timeval to a NTTIME
558 NTTIME
timeval_to_nttime(const struct timeval
*tv
)
560 return 10*(tv
->tv_usec
+
561 ((TIME_FIXUP_CONSTANT_INT
+ (uint64_t)tv
->tv_sec
) * 1000000));
564 /**************************************************************
565 Handle conversions between time_t and uint32, taking care to
566 preserve the "special" values.
567 **************************************************************/
569 uint32
convert_time_t_to_uint32(time_t t
)
571 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
572 /* time_t is 64-bit. */
573 if (t
== 0x8000000000000000LL
) {
575 } else if (t
== 0x7FFFFFFFFFFFFFFFLL
) {
582 time_t convert_uint32_to_time_t(uint32 u
)
584 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
585 /* time_t is 64-bit. */
586 if (u
== 0x80000000) {
587 return (time_t)0x8000000000000000LL
;
588 } else if (u
== 0x7FFFFFFF) {
589 return (time_t)0x7FFFFFFFFFFFFFFFLL
;
595 /*******************************************************************
596 Yield the difference between *A and *B, in seconds, ignoring leap seconds.
597 ********************************************************************/
599 static int tm_diff(struct tm
*a
, struct tm
*b
)
601 int ay
= a
->tm_year
+ (1900 - 1);
602 int by
= b
->tm_year
+ (1900 - 1);
603 int intervening_leap_days
=
604 (ay
/4 - by
/4) - (ay
/100 - by
/100) + (ay
/400 - by
/400);
606 int days
= 365*years
+ intervening_leap_days
+ (a
->tm_yday
- b
->tm_yday
);
607 int hours
= 24*days
+ (a
->tm_hour
- b
->tm_hour
);
608 int minutes
= 60*hours
+ (a
->tm_min
- b
->tm_min
);
609 int seconds
= 60*minutes
+ (a
->tm_sec
- b
->tm_sec
);
614 int extra_time_offset
=0;
616 /*******************************************************************
617 Return the UTC offset in seconds west of UTC, or 0 if it cannot be determined.
618 ********************************************************************/
620 int get_time_zone(time_t t
)
622 struct tm
*tm
= gmtime(&t
);
630 return tm_diff(&tm_utc
,tm
)+60*extra_time_offset
;
633 /****************************************************************************
634 Check if NTTIME is 0.
635 ****************************************************************************/
637 bool nt_time_is_zero(const NTTIME
*nt
)
642 /****************************************************************************
643 Convert ASN.1 GeneralizedTime string to unix-time.
644 Returns 0 on failure; Currently ignores timezone.
645 ****************************************************************************/
647 time_t generalized_to_unix_time(const char *str
)
653 if (sscanf(str
, "%4d%2d%2d%2d%2d%2d",
654 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
,
655 &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
) != 6) {
664 /*******************************************************************
665 Accessor function for the server time zone offset.
666 set_server_zone_offset() must have been called first.
667 ******************************************************************/
669 static int server_zone_offset
;
671 int get_server_zone_offset(void)
673 return server_zone_offset
;
676 /*******************************************************************
677 Initialize the server time zone offset. Called when a client connects.
678 ******************************************************************/
680 int set_server_zone_offset(time_t t
)
682 server_zone_offset
= get_time_zone(t
);
683 return server_zone_offset
;
686 /****************************************************************************
687 Return the date and time as a string
688 ****************************************************************************/
690 char *current_timestring(TALLOC_CTX
*ctx
, bool hires
)
699 t
= (time_t)tp
.tv_sec
;
708 "%ld.%06ld seconds since the Epoch",
714 "%ld seconds since the Epoch",
720 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
721 slprintf(TimeBuf
+strlen(TimeBuf
),
722 sizeof(TimeBuf
)-1 - strlen(TimeBuf
),
726 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
730 const char *asct
= asctime(tm
);
734 asct
? asct
: "unknown",
737 const char *asct
= asctime(tm
);
738 fstrcpy(TimeBuf
, asct
? asct
: "unknown");
742 return talloc_strdup(ctx
, TimeBuf
);
746 /*******************************************************************
747 Put a dos date into a buffer (time/date format).
748 This takes GMT time and puts local time in the buffer.
749 ********************************************************************/
751 static void put_dos_date(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
753 uint32 x
= make_dos_date(unixdate
, zone_offset
);
757 /*******************************************************************
758 Put a dos date into a buffer (date/time format).
759 This takes GMT time and puts local time in the buffer.
760 ********************************************************************/
762 static void put_dos_date2(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
764 uint32 x
= make_dos_date(unixdate
, zone_offset
);
765 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
769 /*******************************************************************
770 Put a dos 32 bit "unix like" date into a buffer. This routine takes
771 GMT and converts it to LOCAL time before putting it (most SMBs assume
772 localtime for this sort of date)
773 ********************************************************************/
775 static void put_dos_date3(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
777 if (!null_mtime(unixdate
)) {
778 unixdate
-= zone_offset
;
780 SIVAL(buf
,offset
,unixdate
);
784 /***************************************************************************
785 Server versions of the above functions.
786 ***************************************************************************/
788 void srv_put_dos_date(char *buf
,int offset
,time_t unixdate
)
790 put_dos_date(buf
, offset
, unixdate
, server_zone_offset
);
793 void srv_put_dos_date2(char *buf
,int offset
, time_t unixdate
)
795 put_dos_date2(buf
, offset
, unixdate
, server_zone_offset
);
798 void srv_put_dos_date3(char *buf
,int offset
,time_t unixdate
)
800 put_dos_date3(buf
, offset
, unixdate
, server_zone_offset
);
803 /****************************************************************************
804 Take a Unix time and convert to an NTTIME structure and place in buffer
806 ****************************************************************************/
808 void put_long_date_timespec(char *p
, struct timespec ts
)
811 unix_timespec_to_nt_time(&nt
, ts
);
812 SIVAL(p
, 0, nt
& 0xFFFFFFFF);
813 SIVAL(p
, 4, nt
>> 32);
816 void put_long_date(char *p
, time_t t
)
821 put_long_date_timespec(p
, ts
);
824 /****************************************************************************
825 Return the best approximation to a 'create time' under UNIX from a stat
827 ****************************************************************************/
829 static time_t calc_create_time(const SMB_STRUCT_STAT
*st
)
833 ret
= MIN(st
->st_ctime
, st
->st_mtime
);
834 ret1
= MIN(ret
, st
->st_atime
);
836 if(ret1
!= (time_t)0) {
841 * One of ctime, mtime or atime was zero (probably atime).
842 * Just return MIN(ctime, mtime).
847 /****************************************************************************
848 Return the 'create time' from a stat struct if it exists (birthtime) or else
849 use the best approximation.
850 ****************************************************************************/
852 struct timespec
get_create_timespec(const SMB_STRUCT_STAT
*pst
,bool fake_dirs
)
856 if(S_ISDIR(pst
->st_mode
) && fake_dirs
) {
857 ret
.tv_sec
= 315493200L; /* 1/1/1980 */
862 #if defined(HAVE_STAT_ST_BIRTHTIMESPEC)
863 ret
= pst
->st_birthtimespec
;
864 #elif defined(HAVE_STAT_ST_BIRTHTIMENSEC)
865 ret
.tv_sec
= pst
->st_birthtime
;
866 ret
.tv_nsec
= pst
->st_birthtimenspec
;
867 #elif defined(HAVE_STAT_ST_BIRTHTIME)
868 ret
.tv_sec
= pst
->st_birthtime
;
871 ret
.tv_sec
= calc_create_time(pst
);
875 /* Deal with systems that don't initialize birthtime correctly.
876 * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
878 if (null_timespec(ret
)) {
879 ret
.tv_sec
= calc_create_time(pst
);
885 /****************************************************************************
886 Get/Set all the possible time fields from a stat struct as a timespec.
887 ****************************************************************************/
889 struct timespec
get_atimespec(const SMB_STRUCT_STAT
*pst
)
891 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
894 /* Old system - no ns timestamp. */
895 ret
.tv_sec
= pst
->st_atime
;
899 #if defined(HAVE_STAT_ST_ATIM)
901 #elif defined(HAVE_STAT_ST_ATIMENSEC)
903 ret
.tv_sec
= pst
->st_atime
;
904 ret
.tv_nsec
= pst
->st_atimensec
;
906 #elif defined(HAVE_STAT_ST_ATIME_N)
908 ret
.tv_sec
= pst
->st_atime
;
909 ret
.tv_nsec
= pst
->st_atime_n
;
911 #elif defined(HAVE_STAT_ST_ATIMESPEC)
912 return pst
->st_atimespec
;
914 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
919 void set_atimespec(SMB_STRUCT_STAT
*pst
, struct timespec ts
)
921 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
922 /* Old system - no ns timestamp. */
923 pst
->st_atime
= ts
.tv_sec
;
925 #if defined(HAVE_STAT_ST_ATIM)
927 #elif defined(HAVE_STAT_ST_ATIMENSEC)
928 pst
->st_atime
= ts
.tv_sec
;
929 pst
->st_atimensec
= ts
.tv_nsec
;
930 #elif defined(HAVE_STAT_ST_ATIME_N)
931 pst
->st_atime
= ts
.tv_sec
;
932 pst
->st_atime_n
= ts
.tv_nsec
;
933 #elif defined(HAVE_STAT_ST_ATIMESPEC)
934 pst
->st_atimespec
= ts
;
936 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
941 struct timespec
get_mtimespec(const SMB_STRUCT_STAT
*pst
)
943 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
946 /* Old system - no ns timestamp. */
947 ret
.tv_sec
= pst
->st_mtime
;
951 #if defined(HAVE_STAT_ST_MTIM)
953 #elif defined(HAVE_STAT_ST_MTIMENSEC)
955 ret
.tv_sec
= pst
->st_mtime
;
956 ret
.tv_nsec
= pst
->st_mtimensec
;
958 #elif defined(HAVE_STAT_ST_MTIME_N)
960 ret
.tv_sec
= pst
->st_mtime
;
961 ret
.tv_nsec
= pst
->st_mtime_n
;
963 #elif defined(HAVE_STAT_ST_MTIMESPEC)
964 return pst
->st_mtimespec
;
966 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
971 void set_mtimespec(SMB_STRUCT_STAT
*pst
, struct timespec ts
)
973 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
974 /* Old system - no ns timestamp. */
975 pst
->st_mtime
= ts
.tv_sec
;
977 #if defined(HAVE_STAT_ST_MTIM)
979 #elif defined(HAVE_STAT_ST_MTIMENSEC)
980 pst
->st_mtime
= ts
.tv_sec
;
981 pst
->st_mtimensec
= ts
.tv_nsec
;
982 #elif defined(HAVE_STAT_ST_MTIME_N)
983 pst
->st_mtime
= ts
.tv_sec
;
984 pst
->st_mtime_n
= ts
.tv_nsec
;
985 #elif defined(HAVE_STAT_ST_MTIMESPEC)
986 pst
->st_mtimespec
= ts
;
988 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
993 struct timespec
get_ctimespec(const SMB_STRUCT_STAT
*pst
)
995 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
998 /* Old system - no ns timestamp. */
999 ret
.tv_sec
= pst
->st_ctime
;
1003 #if defined(HAVE_STAT_ST_CTIM)
1004 return pst
->st_ctim
;
1005 #elif defined(HAVE_STAT_ST_CTIMENSEC)
1006 struct timespec ret
;
1007 ret
.tv_sec
= pst
->st_ctime
;
1008 ret
.tv_nsec
= pst
->st_ctimensec
;
1010 #elif defined(HAVE_STAT_ST_CTIME_N)
1011 struct timespec ret
;
1012 ret
.tv_sec
= pst
->st_ctime
;
1013 ret
.tv_nsec
= pst
->st_ctime_n
;
1015 #elif defined(HAVE_STAT_ST_CTIMESPEC)
1016 return pst
->st_ctimespec
;
1018 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
1023 void set_ctimespec(SMB_STRUCT_STAT
*pst
, struct timespec ts
)
1025 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
1026 /* Old system - no ns timestamp. */
1027 pst
->st_ctime
= ts
.tv_sec
;
1029 #if defined(HAVE_STAT_ST_CTIM)
1031 #elif defined(HAVE_STAT_ST_CTIMENSEC)
1032 pst
->st_ctime
= ts
.tv_sec
;
1033 pst
->st_ctimensec
= ts
.tv_nsec
;
1034 #elif defined(HAVE_STAT_ST_CTIME_N)
1035 pst
->st_ctime
= ts
.tv_sec
;
1036 pst
->st_ctime_n
= ts
.tv_nsec
;
1037 #elif defined(HAVE_STAT_ST_CTIMESPEC)
1038 pst
->st_ctimespec
= ts
;
1040 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
1045 void dos_filetime_timespec(struct timespec
*tsp
)
1051 /*******************************************************************
1052 Create a unix date (int GMT) from a dos date (which is actually in
1054 ********************************************************************/
1056 static time_t make_unix_date(const void *date_ptr
, int zone_offset
)
1062 dos_date
= IVAL(date_ptr
,0);
1064 if (dos_date
== 0) {
1068 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
1069 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
1079 /*******************************************************************
1080 Like make_unix_date() but the words are reversed.
1081 ********************************************************************/
1083 static time_t make_unix_date2(const void *date_ptr
, int zone_offset
)
1087 x
= IVAL(date_ptr
,0);
1088 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
1091 return(make_unix_date((const void *)&x
, zone_offset
));
1094 /*******************************************************************
1095 Create a unix GMT date from a dos date in 32 bit "unix like" format
1096 these generally arrive as localtimes, with corresponding DST.
1097 ******************************************************************/
1099 static time_t make_unix_date3(const void *date_ptr
, int zone_offset
)
1101 time_t t
= (time_t)IVAL(date_ptr
,0);
1102 if (!null_mtime(t
)) {
1108 time_t srv_make_unix_date(const void *date_ptr
)
1110 return make_unix_date(date_ptr
, server_zone_offset
);
1113 time_t srv_make_unix_date2(const void *date_ptr
)
1115 return make_unix_date2(date_ptr
, server_zone_offset
);
1118 time_t srv_make_unix_date3(const void *date_ptr
)
1120 return make_unix_date3(date_ptr
, server_zone_offset
);
1123 time_t convert_timespec_to_time_t(struct timespec ts
)
1125 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
1126 increment if it's greater than 500 millionth of a second. */
1127 if (ts
.tv_nsec
> 500000000) {
1128 return ts
.tv_sec
+ 1;
1133 struct timespec
convert_time_t_to_timespec(time_t t
)
1141 /****************************************************************************
1142 Convert a normalized timeval to a timespec.
1143 ****************************************************************************/
1145 struct timespec
convert_timeval_to_timespec(const struct timeval tv
)
1148 ts
.tv_sec
= tv
.tv_sec
;
1149 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1153 /****************************************************************************
1154 Convert a normalized timespec to a timeval.
1155 ****************************************************************************/
1157 struct timeval
convert_timespec_to_timeval(const struct timespec ts
)
1160 tv
.tv_sec
= ts
.tv_sec
;
1161 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1165 /****************************************************************************
1166 Return a timespec for the current time
1167 ****************************************************************************/
1169 struct timespec
timespec_current(void)
1174 ts
.tv_sec
= tv
.tv_sec
;
1175 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1179 /****************************************************************************
1180 Return the lesser of two timespecs.
1181 ****************************************************************************/
1183 struct timespec
timespec_min(const struct timespec
*ts1
,
1184 const struct timespec
*ts2
)
1186 if (ts1
->tv_sec
< ts2
->tv_sec
) return *ts1
;
1187 if (ts1
->tv_sec
> ts2
->tv_sec
) return *ts2
;
1188 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return *ts1
;
1192 /****************************************************************************
1193 compare two timespec structures.
1194 Return -1 if ts1 < ts2
1195 Return 0 if ts1 == ts2
1196 Return 1 if ts1 > ts2
1197 ****************************************************************************/
1199 int timespec_compare(const struct timespec
*ts1
, const struct timespec
*ts2
)
1201 if (ts1
->tv_sec
> ts2
->tv_sec
) return 1;
1202 if (ts1
->tv_sec
< ts2
->tv_sec
) return -1;
1203 if (ts1
->tv_nsec
> ts2
->tv_nsec
) return 1;
1204 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return -1;
1208 /****************************************************************************
1209 Interprets an nt time into a unix struct timespec.
1210 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
1211 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
1212 ****************************************************************************/
1214 struct timespec
interpret_long_date(const char *p
)
1217 nt
= IVAL(p
,0) + ((uint64_t)IVAL(p
,4) << 32);
1218 if (nt
== (uint64_t)-1) {
1219 struct timespec ret
;
1220 ret
.tv_sec
= (time_t)-1;
1224 return nt_time_to_unix_timespec(&nt
);
1227 /***************************************************************************
1228 Client versions of the above functions.
1229 ***************************************************************************/
1231 void cli_put_dos_date(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
1233 put_dos_date(buf
, offset
, unixdate
, cli
->serverzone
);
1236 void cli_put_dos_date2(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
1238 put_dos_date2(buf
, offset
, unixdate
, cli
->serverzone
);
1241 void cli_put_dos_date3(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
1243 put_dos_date3(buf
, offset
, unixdate
, cli
->serverzone
);
1246 time_t cli_make_unix_date(struct cli_state
*cli
, const void *date_ptr
)
1248 return make_unix_date(date_ptr
, cli
->serverzone
);
1251 time_t cli_make_unix_date2(struct cli_state
*cli
, const void *date_ptr
)
1253 return make_unix_date2(date_ptr
, cli
->serverzone
);
1256 time_t cli_make_unix_date3(struct cli_state
*cli
, const void *date_ptr
)
1258 return make_unix_date3(date_ptr
, cli
->serverzone
);
1261 /* Large integer version. */
1262 struct timespec
nt_time_to_unix_timespec(NTTIME
*nt
)
1265 struct timespec ret
;
1267 if (*nt
== 0 || *nt
== (int64
)-1) {
1274 /* d is now in 100ns units, since jan 1st 1601".
1275 Save off the ns fraction. */
1278 * Take the last seven decimal digits and multiply by 100.
1279 * to convert from 100ns units to 1ns units.
1281 ret
.tv_nsec
= (long) ((d
% (1000 * 1000 * 10)) * 100);
1283 /* Convert to seconds */
1286 /* Now adjust by 369 years to make the secs since 1970 */
1287 d
-= TIME_FIXUP_CONSTANT_INT
;
1289 if (d
<= (int64
)TIME_T_MIN
) {
1290 ret
.tv_sec
= TIME_T_MIN
;
1295 if (d
>= (int64
)TIME_T_MAX
) {
1296 ret
.tv_sec
= TIME_T_MAX
;
1301 ret
.tv_sec
= (time_t)d
;
1304 /****************************************************************************
1305 Check if two NTTIMEs are the same.
1306 ****************************************************************************/
1308 bool nt_time_equals(const NTTIME
*nt1
, const NTTIME
*nt2
)
1310 return (*nt1
== *nt2
);
1313 /*******************************************************************
1314 Re-read the smb serverzone value.
1315 ******************************************************************/
1317 static struct timeval start_time_hires
;
1321 set_server_zone_offset(time(NULL
));
1323 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset
));
1325 /* Save the start time of this process. */
1326 if (start_time_hires
.tv_sec
== 0 && start_time_hires
.tv_usec
== 0) {
1327 GetTimeOfDay(&start_time_hires
);
1331 /**********************************************************************
1332 Return a timeval struct of the uptime of this process. As TimeInit is
1333 done before a daemon fork then this is the start time from the parent
1335 ***********************************************************************/
1337 void get_process_uptime(struct timeval
*ret_time
)
1339 struct timeval time_now_hires
;
1341 GetTimeOfDay(&time_now_hires
);
1342 ret_time
->tv_sec
= time_now_hires
.tv_sec
- start_time_hires
.tv_sec
;
1343 if (time_now_hires
.tv_usec
< start_time_hires
.tv_usec
) {
1344 ret_time
->tv_sec
-= 1;
1345 ret_time
->tv_usec
= 1000000 + (time_now_hires
.tv_usec
- start_time_hires
.tv_usec
);
1347 ret_time
->tv_usec
= time_now_hires
.tv_usec
- start_time_hires
.tv_usec
;
1351 /****************************************************************************
1352 Convert a NTTIME structure to a time_t.
1353 It's originally in "100ns units".
1355 This is an absolute version of the one above.
1356 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
1357 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
1358 ****************************************************************************/
1360 time_t nt_time_to_unix_abs(const NTTIME
*nt
)
1368 if (*nt
== (uint64
)-1) {
1372 if (*nt
== NTTIME_INFINITY
) {
1376 /* reverse the time */
1377 /* it's a negative value, turn it to positive */
1380 d
+= 1000*1000*10/2;
1383 if (!(TIME_T_MIN
<= ((time_t)d
) && ((time_t)d
) <= TIME_T_MAX
)) {
1390 /****************************************************************************
1391 Put a 8 byte filetime from a struct timespec. Uses GMT.
1392 ****************************************************************************/
1394 void unix_timespec_to_nt_time(NTTIME
*nt
, struct timespec ts
)
1398 if (ts
.tv_sec
==0 && ts
.tv_nsec
== 0) {
1402 if (ts
.tv_sec
== TIME_T_MAX
) {
1403 *nt
= 0x7fffffffffffffffLL
;
1406 if (ts
.tv_sec
== (time_t)-1) {
1412 d
+= TIME_FIXUP_CONSTANT_INT
;
1414 /* d is now in 100ns units. */
1415 d
+= (ts
.tv_nsec
/ 100);
1420 /****************************************************************************
1421 Convert a time_t to a NTTIME structure
1423 This is an absolute version of the one above.
1424 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
1425 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
1426 ****************************************************************************/
1428 void unix_to_nt_time_abs(NTTIME
*nt
, time_t t
)
1437 if (t
== TIME_T_MAX
) {
1438 *nt
= 0x7fffffffffffffffLL
;
1442 if (t
== (time_t)-1) {
1443 /* that's what NT uses for infinite */
1444 *nt
= NTTIME_INFINITY
;
1453 /* convert to a negative value */
1458 /****************************************************************************
1459 Check if it's a null mtime.
1460 ****************************************************************************/
1462 bool null_mtime(time_t mtime
)
1464 if (mtime
== 0 || mtime
== (time_t)0xFFFFFFFF || mtime
== (time_t)-1)
1469 /****************************************************************************
1470 Utility function that always returns a const string even if localtime
1472 ****************************************************************************/
1474 const char *time_to_asc(const time_t t
)
1477 struct tm
*lt
= localtime(&t
);
1480 return "unknown time";
1485 return "unknown time";
1490 const char *display_time(NTTIME nttime
)
1495 int days
, hours
, mins
, secs
;
1500 if (nttime
==NTTIME_INFINITY
)
1507 high
= high
* (~(nttime
>> 32));
1509 low
= ~(nttime
& 0xFFFFFFFF);
1510 low
= low
/(1000*1000*10);
1512 sec
=(int)(high
+low
);
1514 days
=sec
/(60*60*24);
1515 hours
=(sec
- (days
*60*60*24)) / (60*60);
1516 mins
=(sec
- (days
*60*60*24) - (hours
*60*60) ) / 60;
1517 secs
=sec
- (days
*60*60*24) - (hours
*60*60) - (mins
*60);
1519 return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
1520 "%u seconds", days
, hours
, mins
, secs
);
1523 bool nt_time_is_set(const NTTIME
*nt
)
1525 if (*nt
== 0x7FFFFFFFFFFFFFFFLL
) {
1529 if (*nt
== NTTIME_INFINITY
) {