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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * @brief time handling functions
33 #define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
34 : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
37 #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
40 #define NTTIME_INFINITY (NTTIME)0x8000000000000000LL
42 /***************************************************************************
43 External access to time_t_min and time_t_max.
44 ****************************************************************************/
46 time_t get_time_t_max(void)
51 /***************************************************************************
52 A gettimeofday wrapper.
53 ****************************************************************************/
55 void GetTimeOfDay(struct timeval
*tval
)
57 #ifdef HAVE_GETTIMEOFDAY_TZ
58 gettimeofday(tval
,NULL
);
64 #if (SIZEOF_LONG == 8)
65 #define TIME_FIXUP_CONSTANT_INT 11644473600L
66 #elif (SIZEOF_LONG_LONG == 8)
67 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
70 /****************************************************************************
71 Interpret an 8 byte "filetime" structure to a time_t
72 It's originally in "100ns units since jan 1st 1601"
74 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
80 ****************************************************************************/
82 time_t nt_time_to_unix(NTTIME nt
)
84 return convert_timespec_to_time_t(nt_time_to_unix_timespec(&nt
));
87 /****************************************************************************
88 Put a 8 byte filetime from a time_t. Uses GMT.
89 ****************************************************************************/
91 void unix_to_nt_time(NTTIME
*nt
, time_t t
)
95 if (t
== (time_t)-1) {
100 if (t
== TIME_T_MAX
) {
101 *nt
= 0x7fffffffffffffffLL
;
111 t2
+= TIME_FIXUP_CONSTANT_INT
;
117 /****************************************************************************
118 Check if it's a null unix time.
119 ****************************************************************************/
121 BOOL
null_time(time_t t
)
124 t
== (time_t)0xFFFFFFFF ||
128 /****************************************************************************
129 Check if it's a null NTTIME.
130 ****************************************************************************/
132 BOOL
null_nttime(NTTIME t
)
134 return t
== 0 || t
== (NTTIME
)-1;
137 /****************************************************************************
138 Check if it's a null timespec.
139 ****************************************************************************/
141 BOOL
null_timespec(struct timespec ts
)
143 return ts
.tv_sec
== 0 ||
144 ts
.tv_sec
== (time_t)0xFFFFFFFF ||
145 ts
.tv_sec
== (time_t)-1;
148 /*******************************************************************
149 create a 16 bit dos packed date
150 ********************************************************************/
151 static uint16_t make_dos_date1(struct tm
*t
)
154 ret
= (((unsigned int)(t
->tm_mon
+1)) >> 3) | ((t
->tm_year
-80) << 1);
155 ret
= ((ret
&0xFF)<<8) | (t
->tm_mday
| (((t
->tm_mon
+1) & 0x7) << 5));
159 /*******************************************************************
160 create a 16 bit dos packed time
161 ********************************************************************/
162 static uint16_t make_dos_time1(struct tm
*t
)
165 ret
= ((((unsigned int)t
->tm_min
>> 3)&0x7) | (((unsigned int)t
->tm_hour
) << 3));
166 ret
= ((ret
&0xFF)<<8) | ((t
->tm_sec
/2) | ((t
->tm_min
& 0x7) << 5));
170 /*******************************************************************
171 create a 32 bit dos packed date/time from some parameters
172 This takes a GMT time and returns a packed localtime structure
173 ********************************************************************/
174 static uint32_t make_dos_date(time_t unixdate
, int zone_offset
)
183 unixdate
-= zone_offset
;
185 t
= gmtime(&unixdate
);
190 ret
= make_dos_date1(t
);
191 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
197 put a dos date into a buffer (time/date format)
198 This takes GMT time and puts local time in the buffer
200 void push_dos_date(uint8_t *buf
, int offset
, time_t unixdate
, int zone_offset
)
202 uint32_t x
= make_dos_date(unixdate
, zone_offset
);
207 put a dos date into a buffer (date/time format)
208 This takes GMT time and puts local time in the buffer
210 void push_dos_date2(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
213 x
= make_dos_date(unixdate
, zone_offset
);
214 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
219 put a dos 32 bit "unix like" date into a buffer. This routine takes
220 GMT and converts it to LOCAL time before putting it (most SMBs assume
221 localtime for this sort of date)
223 void push_dos_date3(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
225 if (!null_time(unixdate
)) {
226 unixdate
-= zone_offset
;
228 SIVAL(buf
,offset
,unixdate
);
231 /*******************************************************************
232 interpret a 32 bit dos packed date/time to some parameters
233 ********************************************************************/
234 static void interpret_dos_date(uint32_t date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
236 uint32_t p0
,p1
,p2
,p3
;
238 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
239 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
241 *second
= 2*(p0
& 0x1F);
242 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
243 *hour
= (p1
>>3)&0xFF;
245 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
246 *year
= ((p3
>>1)&0xFF) + 80;
250 create a unix date (int GMT) from a dos date (which is actually in
253 time_t pull_dos_date(const uint8_t *date_ptr
, int zone_offset
)
259 dos_date
= IVAL(date_ptr
,0);
261 if (dos_date
== 0) return (time_t)0;
263 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
264 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
275 like make_unix_date() but the words are reversed
277 time_t pull_dos_date2(const uint8_t *date_ptr
, int zone_offset
)
281 x
= IVAL(date_ptr
,0);
282 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
285 return pull_dos_date((const uint8_t *)&x
, zone_offset
);
289 create a unix GMT date from a dos date in 32 bit "unix like" format
290 these generally arrive as localtimes, with corresponding DST
292 time_t pull_dos_date3(const uint8_t *date_ptr
, int zone_offset
)
294 time_t t
= (time_t)IVAL(date_ptr
,0);
301 /***************************************************************************
302 Return a HTTP/1.0 time string.
303 ***************************************************************************/
305 char *http_timestring(time_t t
)
308 struct tm
*tm
= localtime(&t
);
310 if (t
== TIME_T_MAX
) {
311 slprintf(buf
,sizeof(buf
)-1,"never");
313 slprintf(buf
,sizeof(buf
)-1,"%ld seconds since the Epoch",(long)t
);
315 #ifndef HAVE_STRFTIME
316 const char *asct
= asctime(tm
);
317 fstrcpy(buf
, asct
? asct
: "unknown");
319 if(buf
[strlen(buf
)-1] == '\n') {
320 buf
[strlen(buf
)-1] = 0;
321 #else /* !HAVE_STRFTIME */
322 strftime(buf
, sizeof(buf
)-1, "%a, %d %b %Y %H:%M:%S %Z", tm
);
323 #endif /* !HAVE_STRFTIME */
330 Return the date and time as a string
332 char *timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
340 return talloc_asprintf(mem_ctx
,
341 "%ld seconds since the Epoch",
346 /* some versions of gcc complain about using %c. This is a bug
347 in the gcc warning, not a bug in this code. See a recent
348 strftime() manual page for details.
350 strftime(tempTime
,sizeof(tempTime
)-1,"%c %Z",tm
);
351 TimeBuf
= talloc_strdup(mem_ctx
, tempTime
);
353 TimeBuf
= talloc_strdup(mem_ctx
, asctime(tm
));
360 return a talloced string representing a NTTIME for human consumption
362 const char *nt_time_string(TALLOC_CTX
*mem_ctx
, NTTIME nt
)
368 t
= nt_time_to_unix(nt
);
369 return timestring(mem_ctx
, t
);
374 parse a nttime as a large integer in a string and return a NTTIME
376 NTTIME
nttime_from_string(const char *s
)
378 return strtoull(s
, NULL
, 0);
382 return (tv1 - tv2) in microseconds
384 int64_t usec_time_diff(struct timeval
*tv1
, struct timeval
*tv2
)
386 int64_t sec_diff
= tv1
->tv_sec
- tv2
->tv_sec
;
387 return (sec_diff
* 1000000) + (int64_t)(tv1
->tv_usec
- tv2
->tv_usec
);
392 return a zero timeval
394 struct timeval
timeval_zero(void)
403 return True if a timeval is zero
405 BOOL
timeval_is_zero(const struct timeval
*tv
)
407 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
411 return a timeval for the current time
413 struct timeval
timeval_current(void)
421 return a timeval struct with the given elements
423 struct timeval
timeval_set(uint32_t secs
, uint32_t usecs
)
433 return a timeval ofs microseconds after tv
435 struct timeval
timeval_add(const struct timeval
*tv
,
436 uint32_t secs
, uint32_t usecs
)
438 struct timeval tv2
= *tv
;
439 const unsigned int million
= 1000000;
441 tv2
.tv_usec
+= usecs
;
442 tv2
.tv_sec
+= tv2
.tv_usec
/ million
;
443 tv2
.tv_usec
= tv2
.tv_usec
% million
;
448 return the sum of two timeval structures
450 struct timeval
timeval_sum(const struct timeval
*tv1
,
451 const struct timeval
*tv2
)
453 return timeval_add(tv1
, tv2
->tv_sec
, tv2
->tv_usec
);
457 return a timeval secs/usecs into the future
459 struct timeval
timeval_current_ofs(uint32_t secs
, uint32_t usecs
)
461 struct timeval tv
= timeval_current();
462 return timeval_add(&tv
, secs
, usecs
);
466 compare two timeval structures.
467 Return -1 if tv1 < tv2
468 Return 0 if tv1 == tv2
469 Return 1 if tv1 > tv2
471 int timeval_compare(const struct timeval
*tv1
, const struct timeval
*tv2
)
473 if (tv1
->tv_sec
> tv2
->tv_sec
) return 1;
474 if (tv1
->tv_sec
< tv2
->tv_sec
) return -1;
475 if (tv1
->tv_usec
> tv2
->tv_usec
) return 1;
476 if (tv1
->tv_usec
< tv2
->tv_usec
) return -1;
481 return True if a timer is in the past
483 BOOL
timeval_expired(const struct timeval
*tv
)
485 struct timeval tv2
= timeval_current();
486 if (tv2
.tv_sec
> tv
->tv_sec
) return True
;
487 if (tv2
.tv_sec
< tv
->tv_sec
) return False
;
488 return (tv2
.tv_usec
>= tv
->tv_usec
);
492 return the number of seconds elapsed between two times
494 double timeval_elapsed2(const struct timeval
*tv1
, const struct timeval
*tv2
)
496 return (tv2
->tv_sec
- tv1
->tv_sec
) +
497 (tv2
->tv_usec
- tv1
->tv_usec
)*1.0e-6;
501 return the number of seconds elapsed since a given time
503 double timeval_elapsed(const struct timeval
*tv
)
505 struct timeval tv2
= timeval_current();
506 return timeval_elapsed2(tv
, &tv2
);
510 return the lesser of two timevals
512 struct timeval
timeval_min(const struct timeval
*tv1
,
513 const struct timeval
*tv2
)
515 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv1
;
516 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv2
;
517 if (tv1
->tv_usec
< tv2
->tv_usec
) return *tv1
;
522 return the greater of two timevals
524 struct timeval
timeval_max(const struct timeval
*tv1
,
525 const struct timeval
*tv2
)
527 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv1
;
528 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv2
;
529 if (tv1
->tv_usec
> tv2
->tv_usec
) return *tv1
;
534 return the difference between two timevals as a timeval
535 if tv1 comes after tv2, then return a zero timeval
536 (this is *tv2 - *tv1)
538 struct timeval
timeval_until(const struct timeval
*tv1
,
539 const struct timeval
*tv2
)
542 if (timeval_compare(tv1
, tv2
) >= 0) {
543 return timeval_zero();
545 t
.tv_sec
= tv2
->tv_sec
- tv1
->tv_sec
;
546 if (tv1
->tv_usec
> tv2
->tv_usec
) {
548 t
.tv_usec
= 1000000 - (tv1
->tv_usec
- tv2
->tv_usec
);
550 t
.tv_usec
= tv2
->tv_usec
- tv1
->tv_usec
;
557 convert a timeval to a NTTIME
559 NTTIME
timeval_to_nttime(const struct timeval
*tv
)
561 return 10*(tv
->tv_usec
+
562 ((TIME_FIXUP_CONSTANT_INT
+ (uint64_t)tv
->tv_sec
) * 1000000));
565 /**************************************************************
566 Handle conversions between time_t and uint32, taking care to
567 preserve the "special" values.
568 **************************************************************/
570 uint32
convert_time_t_to_uint32(time_t t
)
572 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
573 /* time_t is 64-bit. */
574 if (t
== 0x8000000000000000LL
) {
576 } else if (t
== 0x7FFFFFFFFFFFFFFFLL
) {
583 time_t convert_uint32_to_time_t(uint32 u
)
585 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
586 /* time_t is 64-bit. */
587 if (u
== 0x80000000) {
588 return (time_t)0x8000000000000000LL
;
589 } else if (u
== 0x7FFFFFFF) {
590 return (time_t)0x7FFFFFFFFFFFFFFFLL
;
596 /*******************************************************************
597 Yield the difference between *A and *B, in seconds, ignoring leap seconds.
598 ********************************************************************/
600 static int tm_diff(struct tm
*a
, struct tm
*b
)
602 int ay
= a
->tm_year
+ (1900 - 1);
603 int by
= b
->tm_year
+ (1900 - 1);
604 int intervening_leap_days
=
605 (ay
/4 - by
/4) - (ay
/100 - by
/100) + (ay
/400 - by
/400);
607 int days
= 365*years
+ intervening_leap_days
+ (a
->tm_yday
- b
->tm_yday
);
608 int hours
= 24*days
+ (a
->tm_hour
- b
->tm_hour
);
609 int minutes
= 60*hours
+ (a
->tm_min
- b
->tm_min
);
610 int seconds
= 60*minutes
+ (a
->tm_sec
- b
->tm_sec
);
615 int extra_time_offset
=0;
617 /*******************************************************************
618 Return the UTC offset in seconds west of UTC, or 0 if it cannot be determined.
619 ********************************************************************/
621 int get_time_zone(time_t t
)
623 struct tm
*tm
= gmtime(&t
);
631 return tm_diff(&tm_utc
,tm
)+60*extra_time_offset
;
634 /****************************************************************************
635 Check if NTTIME is 0.
636 ****************************************************************************/
638 BOOL
nt_time_is_zero(const NTTIME
*nt
)
643 /****************************************************************************
644 Convert ASN.1 GeneralizedTime string to unix-time.
645 Returns 0 on failure; Currently ignores timezone.
646 ****************************************************************************/
648 time_t generalized_to_unix_time(const char *str
)
654 if (sscanf(str
, "%4d%2d%2d%2d%2d%2d",
655 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
,
656 &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
) != 6) {
665 /*******************************************************************
666 Accessor function for the server time zone offset.
667 set_server_zone_offset() must have been called first.
668 ******************************************************************/
670 static int server_zone_offset
;
672 int get_server_zone_offset(void)
674 return server_zone_offset
;
677 /*******************************************************************
678 Initialize the server time zone offset. Called when a client connects.
679 ******************************************************************/
681 int set_server_zone_offset(time_t t
)
683 server_zone_offset
= get_time_zone(t
);
684 return server_zone_offset
;
687 /****************************************************************************
688 Return the date and time as a string
689 ****************************************************************************/
691 char *current_timestring(BOOL hires
)
693 static fstring TimeBuf
;
700 t
= (time_t)tp
.tv_sec
;
709 "%ld.%06ld seconds since the Epoch",
715 "%ld seconds since the Epoch",
721 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
722 slprintf(TimeBuf
+strlen(TimeBuf
),
723 sizeof(TimeBuf
)-1 - strlen(TimeBuf
),
727 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
731 const char *asct
= asctime(tm
);
735 asct
? asct
: "unknown",
738 const char *asct
= asctime(tm
);
739 fstrcpy(TimeBuf
, asct
? asct
: "unknown");
747 /*******************************************************************
748 Put a dos date into a buffer (time/date format).
749 This takes GMT time and puts local time in the buffer.
750 ********************************************************************/
752 static void put_dos_date(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
754 uint32 x
= make_dos_date(unixdate
, zone_offset
);
758 /*******************************************************************
759 Put a dos date into a buffer (date/time format).
760 This takes GMT time and puts local time in the buffer.
761 ********************************************************************/
763 static void put_dos_date2(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
765 uint32 x
= make_dos_date(unixdate
, zone_offset
);
766 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
770 /*******************************************************************
771 Put a dos 32 bit "unix like" date into a buffer. This routine takes
772 GMT and converts it to LOCAL time before putting it (most SMBs assume
773 localtime for this sort of date)
774 ********************************************************************/
776 static void put_dos_date3(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
778 if (!null_mtime(unixdate
)) {
779 unixdate
-= zone_offset
;
781 SIVAL(buf
,offset
,unixdate
);
785 /***************************************************************************
786 Server versions of the above functions.
787 ***************************************************************************/
789 void srv_put_dos_date(char *buf
,int offset
,time_t unixdate
)
791 put_dos_date(buf
, offset
, unixdate
, server_zone_offset
);
794 void srv_put_dos_date2(char *buf
,int offset
, time_t unixdate
)
796 put_dos_date2(buf
, offset
, unixdate
, server_zone_offset
);
799 void srv_put_dos_date3(char *buf
,int offset
,time_t unixdate
)
801 put_dos_date3(buf
, offset
, unixdate
, server_zone_offset
);
804 /****************************************************************************
805 Take a Unix time and convert to an NTTIME structure and place in buffer
807 ****************************************************************************/
809 void put_long_date_timespec(char *p
, struct timespec ts
)
812 unix_timespec_to_nt_time(&nt
, ts
);
813 SIVAL(p
, 0, nt
& 0xFFFFFFFF);
814 SIVAL(p
, 4, nt
>> 32);
817 void put_long_date(char *p
, time_t t
)
822 put_long_date_timespec(p
, ts
);
825 /****************************************************************************
826 Return the best approximation to a 'create time' under UNIX from a stat
828 ****************************************************************************/
830 time_t get_create_time(const SMB_STRUCT_STAT
*st
,BOOL fake_dirs
)
834 if(S_ISDIR(st
->st_mode
) && fake_dirs
) {
835 return (time_t)315493200L; /* 1/1/1980 */
838 ret
= MIN(st
->st_ctime
, st
->st_mtime
);
839 ret1
= MIN(ret
, st
->st_atime
);
841 if(ret1
!= (time_t)0) {
846 * One of ctime, mtime or atime was zero (probably atime).
847 * Just return MIN(ctime, mtime).
852 struct timespec
get_create_timespec(const SMB_STRUCT_STAT
*st
,BOOL fake_dirs
)
855 ts
.tv_sec
= get_create_time(st
, fake_dirs
);
860 /****************************************************************************
861 Get/Set all the possible time fields from a stat struct as a timespec.
862 ****************************************************************************/
864 struct timespec
get_atimespec(const SMB_STRUCT_STAT
*pst
)
866 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
869 /* Old system - no ns timestamp. */
870 ret
.tv_sec
= pst
->st_atime
;
874 #if defined(HAVE_STAT_ST_ATIM)
876 #elif defined(HAVE_STAT_ST_ATIMENSEC)
878 ret
.tv_sec
= pst
->st_atime
;
879 ret
.tv_nsec
= pst
->st_atimensec
;
882 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
887 void set_atimespec(SMB_STRUCT_STAT
*pst
, struct timespec ts
)
889 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
890 /* Old system - no ns timestamp. */
891 pst
->st_atime
= ts
.tv_sec
;
893 #if defined(HAVE_STAT_ST_ATIM)
895 #elif defined(HAVE_STAT_ST_ATIMENSEC)
896 pst
->st_atime
= ts
.tv_sec
;
897 pst
->st_atimensec
= ts
.tv_nsec
899 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
904 struct timespec
get_mtimespec(const SMB_STRUCT_STAT
*pst
)
906 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
909 /* Old system - no ns timestamp. */
910 ret
.tv_sec
= pst
->st_mtime
;
914 #if defined(HAVE_STAT_ST_MTIM)
916 #elif defined(HAVE_STAT_ST_MTIMENSEC)
918 ret
.tv_sec
= pst
->st_mtime
;
919 ret
.tv_nsec
= pst
->st_mtimensec
;
922 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
927 void set_mtimespec(SMB_STRUCT_STAT
*pst
, struct timespec ts
)
929 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
930 /* Old system - no ns timestamp. */
931 pst
->st_mtime
= ts
.tv_sec
;
933 #if defined(HAVE_STAT_ST_MTIM)
935 #elif defined(HAVE_STAT_ST_MTIMENSEC)
936 pst
->st_mtime
= ts
.tv_sec
;
937 pst
->st_mtimensec
= ts
.tv_nsec
939 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
944 struct timespec
get_ctimespec(const SMB_STRUCT_STAT
*pst
)
946 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
949 /* Old system - no ns timestamp. */
950 ret
.tv_sec
= pst
->st_ctime
;
954 #if defined(HAVE_STAT_ST_CTIM)
956 #elif defined(HAVE_STAT_ST_CTIMENSEC)
958 ret
.tv_sec
= pst
->st_ctime
;
959 ret
.tv_nsec
= pst
->st_ctimensec
;
962 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
967 void set_ctimespec(SMB_STRUCT_STAT
*pst
, struct timespec ts
)
969 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
970 /* Old system - no ns timestamp. */
971 pst
->st_ctime
= ts
.tv_sec
;
973 #if defined(HAVE_STAT_ST_CTIM)
975 #elif defined(HAVE_STAT_ST_CTIMENSEC)
976 pst
->st_ctime
= ts
.tv_sec
;
977 pst
->st_ctimensec
= ts
.tv_nsec
979 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
984 void dos_filetime_timespec(struct timespec
*tsp
)
990 /*******************************************************************
991 Create a unix date (int GMT) from a dos date (which is actually in
993 ********************************************************************/
995 static time_t make_unix_date(const void *date_ptr
, int zone_offset
)
1001 dos_date
= IVAL(date_ptr
,0);
1003 if (dos_date
== 0) {
1007 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
1008 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
1018 /*******************************************************************
1019 Like make_unix_date() but the words are reversed.
1020 ********************************************************************/
1022 static time_t make_unix_date2(const void *date_ptr
, int zone_offset
)
1026 x
= IVAL(date_ptr
,0);
1027 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
1030 return(make_unix_date((const void *)&x
, zone_offset
));
1033 /*******************************************************************
1034 Create a unix GMT date from a dos date in 32 bit "unix like" format
1035 these generally arrive as localtimes, with corresponding DST.
1036 ******************************************************************/
1038 static time_t make_unix_date3(const void *date_ptr
, int zone_offset
)
1040 time_t t
= (time_t)IVAL(date_ptr
,0);
1041 if (!null_mtime(t
)) {
1047 time_t srv_make_unix_date(const void *date_ptr
)
1049 return make_unix_date(date_ptr
, server_zone_offset
);
1052 time_t srv_make_unix_date2(const void *date_ptr
)
1054 return make_unix_date2(date_ptr
, server_zone_offset
);
1057 time_t srv_make_unix_date3(const void *date_ptr
)
1059 return make_unix_date3(date_ptr
, server_zone_offset
);
1062 time_t convert_timespec_to_time_t(struct timespec ts
)
1064 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
1065 increment if it's greater than 500 millionth of a second. */
1066 if (ts
.tv_nsec
> 500000000) {
1067 return ts
.tv_sec
+ 1;
1072 struct timespec
convert_time_t_to_timespec(time_t t
)
1080 /****************************************************************************
1081 Convert a normalized timeval to a timespec.
1082 ****************************************************************************/
1084 struct timespec
convert_timeval_to_timespec(const struct timeval tv
)
1087 ts
.tv_sec
= tv
.tv_sec
;
1088 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1092 /****************************************************************************
1093 Convert a normalized timespec to a timeval.
1094 ****************************************************************************/
1096 struct timeval
convert_timespec_to_timeval(const struct timespec ts
)
1099 tv
.tv_sec
= ts
.tv_sec
;
1100 tv
.tv_usec
= ts
.tv_nsec
/ 1000;
1104 /****************************************************************************
1105 Return a timespec for the current time
1106 ****************************************************************************/
1108 struct timespec
timespec_current(void)
1113 ts
.tv_sec
= tv
.tv_sec
;
1114 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1118 /****************************************************************************
1119 Return the lesser of two timespecs.
1120 ****************************************************************************/
1122 struct timespec
timespec_min(const struct timespec
*ts1
,
1123 const struct timespec
*ts2
)
1125 if (ts1
->tv_sec
< ts2
->tv_sec
) return *ts1
;
1126 if (ts1
->tv_sec
> ts2
->tv_sec
) return *ts2
;
1127 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return *ts1
;
1131 /****************************************************************************
1132 compare two timespec structures.
1133 Return -1 if ts1 < ts2
1134 Return 0 if ts1 == ts2
1135 Return 1 if ts1 > ts2
1136 ****************************************************************************/
1138 int timespec_compare(const struct timespec
*ts1
, const struct timespec
*ts2
)
1140 if (ts1
->tv_sec
> ts2
->tv_sec
) return 1;
1141 if (ts1
->tv_sec
< ts2
->tv_sec
) return -1;
1142 if (ts1
->tv_nsec
> ts2
->tv_nsec
) return 1;
1143 if (ts1
->tv_nsec
< ts2
->tv_nsec
) return -1;
1147 /****************************************************************************
1148 Interprets an nt time into a unix struct timespec.
1149 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
1150 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
1151 ****************************************************************************/
1153 struct timespec
interpret_long_date(const char *p
)
1156 nt
= IVAL(p
,0) + ((uint64_t)IVAL(p
,4) << 32);
1157 if (nt
== (uint64_t)-1) {
1158 struct timespec ret
;
1159 ret
.tv_sec
= (time_t)-1;
1163 return nt_time_to_unix_timespec(&nt
);
1166 /***************************************************************************
1167 Client versions of the above functions.
1168 ***************************************************************************/
1170 void cli_put_dos_date(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
1172 put_dos_date(buf
, offset
, unixdate
, cli
->serverzone
);
1175 void cli_put_dos_date2(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
1177 put_dos_date2(buf
, offset
, unixdate
, cli
->serverzone
);
1180 void cli_put_dos_date3(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
1182 put_dos_date3(buf
, offset
, unixdate
, cli
->serverzone
);
1185 time_t cli_make_unix_date(struct cli_state
*cli
, void *date_ptr
)
1187 return make_unix_date(date_ptr
, cli
->serverzone
);
1190 time_t cli_make_unix_date2(struct cli_state
*cli
, void *date_ptr
)
1192 return make_unix_date2(date_ptr
, cli
->serverzone
);
1195 time_t cli_make_unix_date3(struct cli_state
*cli
, void *date_ptr
)
1197 return make_unix_date3(date_ptr
, cli
->serverzone
);
1200 /* Large integer version. */
1201 struct timespec
nt_time_to_unix_timespec(NTTIME
*nt
)
1204 struct timespec ret
;
1206 if (*nt
== 0 || *nt
== (int64
)-1) {
1213 /* d is now in 100ns units, since jan 1st 1601".
1214 Save off the ns fraction. */
1216 ret
.tv_nsec
= (long) ((d
% 100) * 100);
1218 /* Convert to seconds */
1221 /* Now adjust by 369 years to make the secs since 1970 */
1222 d
-= TIME_FIXUP_CONSTANT_INT
;
1224 if (d
<= (int64
)TIME_T_MIN
) {
1225 ret
.tv_sec
= TIME_T_MIN
;
1230 if (d
>= (int64
)TIME_T_MAX
) {
1231 ret
.tv_sec
= TIME_T_MAX
;
1236 ret
.tv_sec
= (time_t)d
;
1239 /****************************************************************************
1240 Check if two NTTIMEs are the same.
1241 ****************************************************************************/
1243 BOOL
nt_time_equals(const NTTIME
*nt1
, const NTTIME
*nt2
)
1245 return (*nt1
== *nt2
);
1248 /*******************************************************************
1249 Re-read the smb serverzone value.
1250 ******************************************************************/
1252 static struct timeval start_time_hires
;
1256 set_server_zone_offset(time(NULL
));
1258 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset
));
1260 /* Save the start time of this process. */
1261 if (start_time_hires
.tv_sec
== 0 && start_time_hires
.tv_usec
== 0) {
1262 GetTimeOfDay(&start_time_hires
);
1266 /**********************************************************************
1267 Return a timeval struct of the uptime of this process. As TimeInit is
1268 done before a daemon fork then this is the start time from the parent
1270 ***********************************************************************/
1272 void get_process_uptime(struct timeval
*ret_time
)
1274 struct timeval time_now_hires
;
1276 GetTimeOfDay(&time_now_hires
);
1277 ret_time
->tv_sec
= time_now_hires
.tv_sec
- start_time_hires
.tv_sec
;
1278 if (time_now_hires
.tv_usec
< start_time_hires
.tv_usec
) {
1279 ret_time
->tv_sec
-= 1;
1280 ret_time
->tv_usec
= 1000000 + (time_now_hires
.tv_usec
- start_time_hires
.tv_usec
);
1282 ret_time
->tv_usec
= time_now_hires
.tv_usec
- start_time_hires
.tv_usec
;
1286 /****************************************************************************
1287 Convert a NTTIME structure to a time_t.
1288 It's originally in "100ns units".
1290 This is an absolute version of the one above.
1291 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
1292 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
1293 ****************************************************************************/
1295 time_t nt_time_to_unix_abs(const NTTIME
*nt
)
1303 if (*nt
== (uint64
)-1) {
1307 if (*nt
== NTTIME_INFINITY
) {
1311 /* reverse the time */
1312 /* it's a negative value, turn it to positive */
1315 d
+= 1000*1000*10/2;
1318 if (!(TIME_T_MIN
<= ((time_t)d
) && ((time_t)d
) <= TIME_T_MAX
)) {
1325 /****************************************************************************
1326 Put a 8 byte filetime from a struct timespec. Uses GMT.
1327 ****************************************************************************/
1329 void unix_timespec_to_nt_time(NTTIME
*nt
, struct timespec ts
)
1333 if (ts
.tv_sec
==0 && ts
.tv_nsec
== 0) {
1337 if (ts
.tv_sec
== TIME_T_MAX
) {
1338 *nt
= 0x7fffffffffffffffLL
;
1341 if (ts
.tv_sec
== (time_t)-1) {
1347 d
+= TIME_FIXUP_CONSTANT_INT
;
1349 /* d is now in 100ns units. */
1350 d
+= (ts
.tv_nsec
/ 100);
1355 /****************************************************************************
1356 Convert a time_t to a NTTIME structure
1358 This is an absolute version of the one above.
1359 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
1360 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
1361 ****************************************************************************/
1363 void unix_to_nt_time_abs(NTTIME
*nt
, time_t t
)
1372 if (t
== TIME_T_MAX
) {
1373 *nt
= 0x7fffffffffffffffLL
;
1377 if (t
== (time_t)-1) {
1378 /* that's what NT uses for infinite */
1379 *nt
= NTTIME_INFINITY
;
1388 /* convert to a negative value */
1393 /****************************************************************************
1394 Check if it's a null mtime.
1395 ****************************************************************************/
1397 BOOL
null_mtime(time_t mtime
)
1399 if (mtime
== 0 || mtime
== (time_t)0xFFFFFFFF || mtime
== (time_t)-1)
1404 /****************************************************************************
1405 Utility function that always returns a const string even if localtime
1407 ****************************************************************************/
1409 const char *time_to_asc(const time_t t
)
1412 struct tm
*lt
= localtime(&t
);
1415 return "unknown time";
1420 return "unknown time";
1425 const char *display_time(NTTIME nttime
)
1427 static fstring string
;
1432 int days
, hours
, mins
, secs
;
1437 if (nttime
==NTTIME_INFINITY
)
1444 high
= high
* (~(nttime
>> 32));
1446 low
= ~(nttime
& 0xFFFFFFFF);
1447 low
= low
/(1000*1000*10);
1451 days
=sec
/(60*60*24);
1452 hours
=(sec
- (days
*60*60*24)) / (60*60);
1453 mins
=(sec
- (days
*60*60*24) - (hours
*60*60) ) / 60;
1454 secs
=sec
- (days
*60*60*24) - (hours
*60*60) - (mins
*60);
1456 fstr_sprintf(string
, "%u days, %u hours, %u minutes, %u seconds", days
, hours
, mins
, secs
);
1460 BOOL
nt_time_is_set(const NTTIME
*nt
)
1462 if (*nt
== 0x7FFFFFFFFFFFFFFFLL
) {
1466 if (*nt
== NTTIME_INFINITY
) {