2 Unix SMB/CIFS implementation.
3 time handling functions
5 Copyright (C) Andrew Tridgell 1992-2004
6 Copyright (C) Stefan (metze) Metzmacher 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
42 External access to time_t_min and time_t_max.
44 time_t get_time_t_max(void)
50 a gettimeofday wrapper
52 void GetTimeOfDay(struct timeval
*tval
)
54 #ifdef HAVE_GETTIMEOFDAY_TZ
55 gettimeofday(tval
,NULL
);
61 struct timespec
convert_time_t_to_timespec(time_t t
)
69 #if (SIZEOF_LONG == 8)
70 #define TIME_FIXUP_CONSTANT_INT 11644473600L
71 #elif (SIZEOF_LONG_LONG == 8)
72 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
75 /****************************************************************************
76 Interpret an 8 byte "filetime" structure to a time_t
77 It's originally in "100ns units since jan 1st 1601"
79 An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
85 ****************************************************************************/
87 time_t nt_time_to_unix(NTTIME nt
)
89 return convert_timespec_to_time_t(nt_time_to_unix_timespec(&nt
));
92 /****************************************************************************
93 Put a 8 byte filetime from a time_t. Uses GMT.
94 ****************************************************************************/
96 void unix_to_nt_time(NTTIME
*nt
, time_t t
)
100 if (t
== (time_t)-1) {
110 t2
+= TIME_FIXUP_CONSTANT_INT
;
118 check if it's a null unix time
120 BOOL
null_time(time_t t
)
123 t
== (time_t)0xFFFFFFFF ||
129 check if it's a null NTTIME
131 BOOL
null_nttime(NTTIME t
)
133 return t
== 0 || t
== (NTTIME
)-1;
136 /*******************************************************************
137 create a 16 bit dos packed date
138 ********************************************************************/
139 static uint16_t make_dos_date1(struct tm
*t
)
142 ret
= (((unsigned int)(t
->tm_mon
+1)) >> 3) | ((t
->tm_year
-80) << 1);
143 ret
= ((ret
&0xFF)<<8) | (t
->tm_mday
| (((t
->tm_mon
+1) & 0x7) << 5));
147 /*******************************************************************
148 create a 16 bit dos packed time
149 ********************************************************************/
150 static uint16_t make_dos_time1(struct tm
*t
)
153 ret
= ((((unsigned int)t
->tm_min
>> 3)&0x7) | (((unsigned int)t
->tm_hour
) << 3));
154 ret
= ((ret
&0xFF)<<8) | ((t
->tm_sec
/2) | ((t
->tm_min
& 0x7) << 5));
158 /*******************************************************************
159 create a 32 bit dos packed date/time from some parameters
160 This takes a GMT time and returns a packed localtime structure
161 ********************************************************************/
162 static uint32_t make_dos_date(time_t unixdate
, int zone_offset
)
171 unixdate
-= zone_offset
;
173 t
= gmtime(&unixdate
);
178 ret
= make_dos_date1(t
);
179 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
185 put a dos date into a buffer (time/date format)
186 This takes GMT time and puts local time in the buffer
188 void push_dos_date(uint8_t *buf
, int offset
, time_t unixdate
, int zone_offset
)
190 uint32_t x
= make_dos_date(unixdate
, zone_offset
);
195 put a dos date into a buffer (date/time format)
196 This takes GMT time and puts local time in the buffer
198 void push_dos_date2(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
201 x
= make_dos_date(unixdate
, zone_offset
);
202 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
207 put a dos 32 bit "unix like" date into a buffer. This routine takes
208 GMT and converts it to LOCAL time before putting it (most SMBs assume
209 localtime for this sort of date)
211 void push_dos_date3(uint8_t *buf
,int offset
,time_t unixdate
, int zone_offset
)
213 if (!null_time(unixdate
)) {
214 unixdate
-= zone_offset
;
216 SIVAL(buf
,offset
,unixdate
);
219 /*******************************************************************
220 interpret a 32 bit dos packed date/time to some parameters
221 ********************************************************************/
222 static void interpret_dos_date(uint32_t date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
224 uint32_t p0
,p1
,p2
,p3
;
226 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
227 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
229 *second
= 2*(p0
& 0x1F);
230 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
231 *hour
= (p1
>>3)&0xFF;
233 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
234 *year
= ((p3
>>1)&0xFF) + 80;
238 create a unix date (int GMT) from a dos date (which is actually in
241 time_t pull_dos_date(const uint8_t *date_ptr
, int zone_offset
)
247 dos_date
= IVAL(date_ptr
,0);
249 if (dos_date
== 0) return (time_t)0;
251 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
252 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
263 like make_unix_date() but the words are reversed
265 time_t pull_dos_date2(const uint8_t *date_ptr
, int zone_offset
)
269 x
= IVAL(date_ptr
,0);
270 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
273 return pull_dos_date((const uint8_t *)&x
, zone_offset
);
277 create a unix GMT date from a dos date in 32 bit "unix like" format
278 these generally arrive as localtimes, with corresponding DST
280 time_t pull_dos_date3(const uint8_t *date_ptr
, int zone_offset
)
282 time_t t
= (time_t)IVAL(date_ptr
,0);
289 /***************************************************************************
290 Return a HTTP/1.0 time string.
291 ***************************************************************************/
293 char *http_timestring(time_t t
)
296 struct tm
*tm
= localtime(&t
);
299 slprintf(buf
,sizeof(buf
)-1,"%ld seconds since the Epoch",(long)t
);
301 #ifndef HAVE_STRFTIME
302 const char *asct
= asctime(tm
);
303 fstrcpy(buf
, asct
? asct
: "unknown");
305 if(buf
[strlen(buf
)-1] == '\n') {
306 buf
[strlen(buf
)-1] = 0;
307 #else /* !HAVE_STRFTIME */
308 strftime(buf
, sizeof(buf
)-1, "%a, %d %b %Y %H:%M:%S %Z", tm
);
309 #endif /* !HAVE_STRFTIME */
316 Return the date and time as a string
318 char *timestring(TALLOC_CTX
*mem_ctx
, time_t t
)
326 return talloc_asprintf(mem_ctx
,
327 "%ld seconds since the Epoch",
332 /* some versions of gcc complain about using %c. This is a bug
333 in the gcc warning, not a bug in this code. See a recent
334 strftime() manual page for details.
336 strftime(tempTime
,sizeof(tempTime
)-1,"%c %Z",tm
);
337 TimeBuf
= talloc_strdup(mem_ctx
, tempTime
);
339 TimeBuf
= talloc_strdup(mem_ctx
, asctime(tm
));
346 return a talloced string representing a NTTIME for human consumption
348 const char *nt_time_string(TALLOC_CTX
*mem_ctx
, NTTIME nt
)
354 t
= nt_time_to_unix(nt
);
355 return timestring(mem_ctx
, t
);
360 parse a nttime as a large integer in a string and return a NTTIME
362 NTTIME
nttime_from_string(const char *s
)
364 return strtoull(s
, NULL
, 0);
368 return (tv1 - tv2) in microseconds
370 int64_t usec_time_diff(struct timeval
*tv1
, struct timeval
*tv2
)
372 int64_t sec_diff
= tv1
->tv_sec
- tv2
->tv_sec
;
373 return (sec_diff
* 1000000) + (int64_t)(tv1
->tv_usec
- tv2
->tv_usec
);
378 return a zero timeval
380 struct timeval
timeval_zero(void)
389 return True if a timeval is zero
391 BOOL
timeval_is_zero(const struct timeval
*tv
)
393 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
397 return a timeval for the current time
399 struct timeval
timeval_current(void)
407 return a timeval struct with the given elements
409 struct timeval
timeval_set(uint32_t secs
, uint32_t usecs
)
419 return a timeval ofs microseconds after tv
421 struct timeval
timeval_add(const struct timeval
*tv
,
422 uint32_t secs
, uint32_t usecs
)
424 struct timeval tv2
= *tv
;
425 const unsigned int million
= 1000000;
427 tv2
.tv_usec
+= usecs
;
428 tv2
.tv_sec
+= tv2
.tv_usec
/ million
;
429 tv2
.tv_usec
= tv2
.tv_usec
% million
;
434 return the sum of two timeval structures
436 struct timeval
timeval_sum(const struct timeval
*tv1
,
437 const struct timeval
*tv2
)
439 return timeval_add(tv1
, tv2
->tv_sec
, tv2
->tv_usec
);
443 return a timeval secs/usecs into the future
445 struct timeval
timeval_current_ofs(uint32_t secs
, uint32_t usecs
)
447 struct timeval tv
= timeval_current();
448 return timeval_add(&tv
, secs
, usecs
);
452 compare two timeval structures.
453 Return -1 if tv1 < tv2
454 Return 0 if tv1 == tv2
455 Return 1 if tv1 > tv2
457 int timeval_compare(const struct timeval
*tv1
, const struct timeval
*tv2
)
459 if (tv1
->tv_sec
> tv2
->tv_sec
) return 1;
460 if (tv1
->tv_sec
< tv2
->tv_sec
) return -1;
461 if (tv1
->tv_usec
> tv2
->tv_usec
) return 1;
462 if (tv1
->tv_usec
< tv2
->tv_usec
) return -1;
467 return True if a timer is in the past
469 BOOL
timeval_expired(const struct timeval
*tv
)
471 struct timeval tv2
= timeval_current();
472 if (tv2
.tv_sec
> tv
->tv_sec
) return True
;
473 if (tv2
.tv_sec
< tv
->tv_sec
) return False
;
474 return (tv2
.tv_usec
>= tv
->tv_usec
);
478 return the number of seconds elapsed between two times
480 double timeval_elapsed2(const struct timeval
*tv1
, const struct timeval
*tv2
)
482 return (tv2
->tv_sec
- tv1
->tv_sec
) +
483 (tv2
->tv_usec
- tv1
->tv_usec
)*1.0e-6;
487 return the number of seconds elapsed since a given time
489 double timeval_elapsed(const struct timeval
*tv
)
491 struct timeval tv2
= timeval_current();
492 return timeval_elapsed2(tv
, &tv2
);
496 return the lesser of two timevals
498 struct timeval
timeval_min(const struct timeval
*tv1
,
499 const struct timeval
*tv2
)
501 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv1
;
502 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv2
;
503 if (tv1
->tv_usec
< tv2
->tv_usec
) return *tv1
;
508 return the greater of two timevals
510 struct timeval
timeval_max(const struct timeval
*tv1
,
511 const struct timeval
*tv2
)
513 if (tv1
->tv_sec
> tv2
->tv_sec
) return *tv1
;
514 if (tv1
->tv_sec
< tv2
->tv_sec
) return *tv2
;
515 if (tv1
->tv_usec
> tv2
->tv_usec
) return *tv1
;
520 return the difference between two timevals as a timeval
521 if tv1 comes after tv2, then return a zero timeval
522 (this is *tv2 - *tv1)
524 struct timeval
timeval_until(const struct timeval
*tv1
,
525 const struct timeval
*tv2
)
528 if (timeval_compare(tv1
, tv2
) >= 0) {
529 return timeval_zero();
531 t
.tv_sec
= tv2
->tv_sec
- tv1
->tv_sec
;
532 if (tv1
->tv_usec
> tv2
->tv_usec
) {
534 t
.tv_usec
= 1000000 - (tv1
->tv_usec
- tv2
->tv_usec
);
536 t
.tv_usec
= tv2
->tv_usec
- tv1
->tv_usec
;
543 convert a timeval to a NTTIME
545 NTTIME
timeval_to_nttime(const struct timeval
*tv
)
547 return 10*(tv
->tv_usec
+
548 ((TIME_FIXUP_CONSTANT_INT
+ (uint64_t)tv
->tv_sec
) * 1000000));
551 /*******************************************************************
552 yield the difference between *A and *B, in seconds, ignoring leap seconds
553 ********************************************************************/
554 static int tm_diff(struct tm
*a
, struct tm
*b
)
556 int ay
= a
->tm_year
+ (1900 - 1);
557 int by
= b
->tm_year
+ (1900 - 1);
558 int intervening_leap_days
=
559 (ay
/4 - by
/4) - (ay
/100 - by
/100) + (ay
/400 - by
/400);
561 int days
= 365*years
+ intervening_leap_days
+ (a
->tm_yday
- b
->tm_yday
);
562 int hours
= 24*days
+ (a
->tm_hour
- b
->tm_hour
);
563 int minutes
= 60*hours
+ (a
->tm_min
- b
->tm_min
);
564 int seconds
= 60*minutes
+ (a
->tm_sec
- b
->tm_sec
);
569 int extra_time_offset
=0;
572 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
574 int get_time_zone(time_t t
)
576 struct tm
*tm
= gmtime(&t
);
584 return tm_diff(&tm_utc
,tm
)+60*extra_time_offset
;
587 /****************************************************************************
588 Check if NTTIME is 0.
589 ****************************************************************************/
591 BOOL
nt_time_is_zero(const NTTIME
*nt
)
596 /****************************************************************************
597 Convert ASN.1 GeneralizedTime string to unix-time.
598 Returns 0 on failure; Currently ignores timezone.
599 ****************************************************************************/
601 time_t generalized_to_unix_time(const char *str
)
607 if (sscanf(str
, "%4d%2d%2d%2d%2d%2d",
608 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
,
609 &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
) != 6) {
618 /*******************************************************************
619 Accessor function for the server time zone offset.
620 set_server_zone_offset() must have been called first.
621 ******************************************************************/
623 static int server_zone_offset
;
625 int get_server_zone_offset(void)
627 return server_zone_offset
;
630 /*******************************************************************
631 Initialize the server time zone offset. Called when a client connects.
632 ******************************************************************/
634 int set_server_zone_offset(time_t t
)
636 server_zone_offset
= get_time_zone(t
);
637 return server_zone_offset
;
640 /****************************************************************************
641 Return the date and time as a string
642 ****************************************************************************/
644 char *current_timestring(BOOL hires
)
646 static fstring TimeBuf
;
653 t
= (time_t)tp
.tv_sec
;
662 "%ld.%06ld seconds since the Epoch",
668 "%ld seconds since the Epoch",
674 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
675 slprintf(TimeBuf
+strlen(TimeBuf
),
676 sizeof(TimeBuf
)-1 - strlen(TimeBuf
),
680 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
684 const char *asct
= asctime(tm
);
688 asct
? asct
: "unknown",
691 const char *asct
= asctime(tm
);
692 fstrcpy(TimeBuf
, asct
? asct
: "unknown");
700 /*******************************************************************
701 Put a dos date into a buffer (time/date format).
702 This takes GMT time and puts local time in the buffer.
703 ********************************************************************/
705 static void put_dos_date(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
707 uint32 x
= make_dos_date(unixdate
, zone_offset
);
711 /*******************************************************************
712 Put a dos date into a buffer (date/time format).
713 This takes GMT time and puts local time in the buffer.
714 ********************************************************************/
716 static void put_dos_date2(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
718 uint32 x
= make_dos_date(unixdate
, zone_offset
);
719 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
723 /*******************************************************************
724 Put a dos 32 bit "unix like" date into a buffer. This routine takes
725 GMT and converts it to LOCAL time before putting it (most SMBs assume
726 localtime for this sort of date)
727 ********************************************************************/
729 static void put_dos_date3(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
731 if (!null_mtime(unixdate
)) {
732 unixdate
-= zone_offset
;
734 SIVAL(buf
,offset
,unixdate
);
738 /***************************************************************************
739 Server versions of the above functions.
740 ***************************************************************************/
742 void srv_put_dos_date(char *buf
,int offset
,time_t unixdate
)
744 put_dos_date(buf
, offset
, unixdate
, server_zone_offset
);
747 void srv_put_dos_date2(char *buf
,int offset
, time_t unixdate
)
749 put_dos_date2(buf
, offset
, unixdate
, server_zone_offset
);
752 void srv_put_dos_date3(char *buf
,int offset
,time_t unixdate
)
754 put_dos_date3(buf
, offset
, unixdate
, server_zone_offset
);
757 /****************************************************************************
758 Take a Unix time and convert to an NTTIME structure and place in buffer
760 ****************************************************************************/
762 void put_long_date_timespec(char *p
, struct timespec ts
)
765 unix_timespec_to_nt_time(&nt
, ts
);
766 SIVAL(p
, 0, nt
& 0xFFFFFFFF);
767 SIVAL(p
, 4, nt
>> 32);
770 void put_long_date(char *p
, time_t t
)
775 put_long_date_timespec(p
, ts
);
778 /****************************************************************************
779 Return the best approximation to a 'create time' under UNIX from a stat
781 ****************************************************************************/
783 time_t get_create_time(SMB_STRUCT_STAT
*st
,BOOL fake_dirs
)
787 if(S_ISDIR(st
->st_mode
) && fake_dirs
) {
788 return (time_t)315493200L; /* 1/1/1980 */
791 ret
= MIN(st
->st_ctime
, st
->st_mtime
);
792 ret1
= MIN(ret
, st
->st_atime
);
794 if(ret1
!= (time_t)0) {
799 * One of ctime, mtime or atime was zero (probably atime).
800 * Just return MIN(ctime, mtime).
805 struct timespec
get_create_timespec(SMB_STRUCT_STAT
*st
,BOOL fake_dirs
)
808 ts
.tv_sec
= get_create_time(st
, fake_dirs
);
813 /****************************************************************************
814 Get/Set all the possible time fields from a stat struct as a timespec.
815 ****************************************************************************/
817 struct timespec
get_atimespec(SMB_STRUCT_STAT
*pst
)
819 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
822 /* Old system - no ns timestamp. */
823 ret
.tv_sec
= pst
->st_atime
;
827 #if defined(HAVE_STAT_ST_ATIM)
829 #elif defined(HAVE_STAT_ST_ATIMENSEC)
831 ret
.tv_sec
= pst
->st_atime
;
832 ret
.tv_nsec
= pst
->st_atimensec
;
835 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
840 void set_atimespec(SMB_STRUCT_STAT
*pst
, struct timespec ts
)
842 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
843 /* Old system - no ns timestamp. */
844 pst
->st_atime
= ts
.tv_sec
;
846 #if defined(HAVE_STAT_ST_ATIM)
848 #elif defined(HAVE_STAT_ST_ATIMENSEC)
849 pst
->st_atime
= ts
.tv_sec
;
850 pst
->st_atimensec
= ts
.tv_nsec
852 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
857 struct timespec
get_mtimespec(SMB_STRUCT_STAT
*pst
)
859 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
862 /* Old system - no ns timestamp. */
863 ret
.tv_sec
= pst
->st_mtime
;
867 #if defined(HAVE_STAT_ST_MTIM)
869 #elif defined(HAVE_STAT_ST_MTIMENSEC)
871 ret
.tv_sec
= pst
->st_mtime
;
872 ret
.tv_nsec
= pst
->st_mtimensec
;
875 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
880 void set_mtimespec(SMB_STRUCT_STAT
*pst
, struct timespec ts
)
882 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
883 /* Old system - no ns timestamp. */
884 pst
->st_mtime
= ts
.tv_sec
;
886 #if defined(HAVE_STAT_ST_MTIM)
888 #elif defined(HAVE_STAT_ST_MTIMENSEC)
889 pst
->st_mtime
= ts
.tv_sec
;
890 pst
->st_mtimensec
= ts
.tv_nsec
892 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
897 struct timespec
get_ctimespec(SMB_STRUCT_STAT
*pst
)
899 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
902 /* Old system - no ns timestamp. */
903 ret
.tv_sec
= pst
->st_ctime
;
907 #if defined(HAVE_STAT_ST_CTIM)
909 #elif defined(HAVE_STAT_ST_CTIMENSEC)
911 ret
.tv_sec
= pst
->st_ctime
;
912 ret
.tv_nsec
= pst
->st_ctimensec
;
915 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
920 void set_ctimespec(SMB_STRUCT_STAT
*pst
, struct timespec ts
)
922 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
923 /* Old system - no ns timestamp. */
924 pst
->st_ctime
= ts
.tv_sec
;
926 #if defined(HAVE_STAT_ST_CTIM)
928 #elif defined(HAVE_STAT_ST_CTIMENSEC)
929 pst
->st_ctime
= ts
.tv_sec
;
930 pst
->st_ctimensec
= ts
.tv_nsec
932 #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
937 void dos_filetime_timespec(struct timespec
*tsp
)
943 /*******************************************************************
944 Create a unix date (int GMT) from a dos date (which is actually in
946 ********************************************************************/
948 static time_t make_unix_date(const void *date_ptr
, int zone_offset
)
954 dos_date
= IVAL(date_ptr
,0);
960 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
961 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
971 /*******************************************************************
972 Like make_unix_date() but the words are reversed.
973 ********************************************************************/
975 static time_t make_unix_date2(const void *date_ptr
, int zone_offset
)
979 x
= IVAL(date_ptr
,0);
980 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
983 return(make_unix_date((const void *)&x
, zone_offset
));
986 /*******************************************************************
987 Create a unix GMT date from a dos date in 32 bit "unix like" format
988 these generally arrive as localtimes, with corresponding DST.
989 ******************************************************************/
991 static time_t make_unix_date3(const void *date_ptr
, int zone_offset
)
993 time_t t
= (time_t)IVAL(date_ptr
,0);
994 if (!null_mtime(t
)) {
1000 time_t srv_make_unix_date(const void *date_ptr
)
1002 return make_unix_date(date_ptr
, server_zone_offset
);
1005 time_t srv_make_unix_date2(const void *date_ptr
)
1007 return make_unix_date2(date_ptr
, server_zone_offset
);
1010 time_t srv_make_unix_date3(const void *date_ptr
)
1012 return make_unix_date3(date_ptr
, server_zone_offset
);
1015 time_t convert_timespec_to_time_t(struct timespec ts
)
1017 /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
1018 increment if it's greater than 500 millionth of a second. */
1019 if (ts
.tv_nsec
> 500000000) {
1020 return ts
.tv_sec
+ 1;
1025 /****************************************************************************
1026 Interprets an nt time into a unix struct timespec.
1027 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
1028 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
1029 ****************************************************************************/
1031 struct timespec
interpret_long_date(const char *p
)
1034 nt
= IVAL(p
,0) + ((uint64_t)IVAL(p
,4) << 32);
1035 if (nt
== (uint64_t)-1) {
1036 struct timespec ret
;
1037 ret
.tv_sec
= (time_t)-1;
1041 return nt_time_to_unix_timespec(&nt
);
1044 /***************************************************************************
1045 Client versions of the above functions.
1046 ***************************************************************************/
1048 void cli_put_dos_date(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
1050 put_dos_date(buf
, offset
, unixdate
, cli
->serverzone
);
1053 void cli_put_dos_date2(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
1055 put_dos_date2(buf
, offset
, unixdate
, cli
->serverzone
);
1058 void cli_put_dos_date3(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
1060 put_dos_date3(buf
, offset
, unixdate
, cli
->serverzone
);
1063 time_t cli_make_unix_date(struct cli_state
*cli
, void *date_ptr
)
1065 return make_unix_date(date_ptr
, cli
->serverzone
);
1068 time_t cli_make_unix_date2(struct cli_state
*cli
, void *date_ptr
)
1070 return make_unix_date2(date_ptr
, cli
->serverzone
);
1073 time_t cli_make_unix_date3(struct cli_state
*cli
, void *date_ptr
)
1075 return make_unix_date3(date_ptr
, cli
->serverzone
);
1078 /* Large integer version. */
1079 struct timespec
nt_time_to_unix_timespec(NTTIME
*nt
)
1082 struct timespec ret
;
1084 if (*nt
== 0 || *nt
== (int64
)-1) {
1091 /* d is now in 100ns units, since jan 1st 1601".
1092 Save off the ns fraction. */
1094 ret
.tv_nsec
= (long) ((d
% 100) * 100);
1096 /* Convert to seconds */
1099 /* Now adjust by 369 years to make the secs since 1970 */
1100 d
-= TIME_FIXUP_CONSTANT_INT
;
1102 if (d
<= (int64
)TIME_T_MIN
) {
1103 ret
.tv_sec
= TIME_T_MIN
;
1108 if (d
>= (int64
)TIME_T_MAX
) {
1109 ret
.tv_sec
= TIME_T_MAX
;
1114 ret
.tv_sec
= (time_t)d
;
1117 /****************************************************************************
1118 Check if two NTTIMEs are the same.
1119 ****************************************************************************/
1121 BOOL
nt_time_equals(const NTTIME
*nt1
, const NTTIME
*nt2
)
1123 return (*nt1
== *nt2
);
1126 /*******************************************************************
1127 Re-read the smb serverzone value.
1128 ******************************************************************/
1130 static struct timeval start_time_hires
;
1134 set_server_zone_offset(time(NULL
));
1136 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset
));
1138 /* Save the start time of this process. */
1139 if (start_time_hires
.tv_sec
== 0 && start_time_hires
.tv_usec
== 0) {
1140 GetTimeOfDay(&start_time_hires
);
1144 /**********************************************************************
1145 Return a timeval struct of the uptime of this process. As TimeInit is
1146 done before a daemon fork then this is the start time from the parent
1148 ***********************************************************************/
1150 void get_process_uptime(struct timeval
*ret_time
)
1152 struct timeval time_now_hires
;
1154 GetTimeOfDay(&time_now_hires
);
1155 ret_time
->tv_sec
= time_now_hires
.tv_sec
- start_time_hires
.tv_sec
;
1156 if (time_now_hires
.tv_usec
< start_time_hires
.tv_usec
) {
1157 ret_time
->tv_sec
-= 1;
1158 ret_time
->tv_usec
= 1000000 + (time_now_hires
.tv_usec
- start_time_hires
.tv_usec
);
1160 ret_time
->tv_usec
= time_now_hires
.tv_usec
- start_time_hires
.tv_usec
;
1164 /****************************************************************************
1165 Convert a NTTIME structure to a time_t.
1166 It's originally in "100ns units".
1168 This is an absolute version of the one above.
1169 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
1170 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
1171 ****************************************************************************/
1173 time_t nt_time_to_unix_abs(const NTTIME
*nt
)
1181 if (*nt
== (uint64
)-1) {
1185 if (*nt
== NTTIME_INFINITY
) {
1189 /* reverse the time */
1190 /* it's a negative value, turn it to positive */
1193 d
+= 1000*1000*10/2;
1196 if (!(TIME_T_MIN
<= ((time_t)d
) && ((time_t)d
) <= TIME_T_MAX
)) {
1203 /****************************************************************************
1204 Put a 8 byte filetime from a struct timespec. Uses GMT.
1205 ****************************************************************************/
1207 void unix_timespec_to_nt_time(NTTIME
*nt
, struct timespec ts
)
1211 if (ts
.tv_sec
==0 && ts
.tv_nsec
== 0) {
1215 if (ts
.tv_sec
== TIME_T_MAX
) {
1216 *nt
= 0x7fffffffffffffffLL
;
1219 if (ts
.tv_sec
== (time_t)-1) {
1225 d
+= TIME_FIXUP_CONSTANT_INT
;
1227 /* d is now in 100ns units. */
1228 d
+= (ts
.tv_nsec
/ 100);
1233 /****************************************************************************
1234 Convert a time_t to a NTTIME structure
1236 This is an absolute version of the one above.
1237 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
1238 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
1239 ****************************************************************************/
1241 void unix_to_nt_time_abs(NTTIME
*nt
, time_t t
)
1250 if (t
== TIME_T_MAX
) {
1251 *nt
= 0x7fffffffffffffffLL
;
1255 if (t
== (time_t)-1) {
1256 /* that's what NT uses for infinite */
1257 *nt
= NTTIME_INFINITY
;
1266 /* convert to a negative value */
1271 /****************************************************************************
1272 Check if it's a null mtime.
1273 ****************************************************************************/
1275 BOOL
null_mtime(time_t mtime
)
1277 if (mtime
== 0 || mtime
== (time_t)0xFFFFFFFF || mtime
== (time_t)-1)
1282 /****************************************************************************
1283 Utility function that always returns a const string even if localtime
1285 ****************************************************************************/
1287 const char *time_to_asc(const time_t *t
)
1290 struct tm
*lt
= localtime(t
);
1293 return "unknown time";
1298 return "unknown time";
1303 const char *display_time(NTTIME nttime
)
1305 static fstring string
;
1310 int days
, hours
, mins
, secs
;
1315 if (nttime
==NTTIME_INFINITY
)
1322 high
= high
* (~(nttime
>> 32));
1324 low
= ~(nttime
& 0xFFFFFFFF);
1325 low
= low
/(1000*1000*10);
1329 days
=sec
/(60*60*24);
1330 hours
=(sec
- (days
*60*60*24)) / (60*60);
1331 mins
=(sec
- (days
*60*60*24) - (hours
*60*60) ) / 60;
1332 secs
=sec
- (days
*60*60*24) - (hours
*60*60) - (mins
*60);
1334 fstr_sprintf(string
, "%u days, %u hours, %u minutes, %u seconds", days
, hours
, mins
, secs
);
1338 BOOL
nt_time_is_set(const NTTIME
*nt
)
1340 if (*nt
== 0x7FFFFFFFFFFFFFFFLL
) {
1344 if (*nt
== NTTIME_INFINITY
) {