2 Unix SMB/CIFS implementation.
3 time handling functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Stefan (metze) Metzmacher 2002
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 This stuff was largely rewritten by Paul Eggert <eggert@twinsun.com>
28 int extra_time_offset
= 0;
35 #define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
36 : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
39 #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
42 void get_nttime_max(NTTIME
*t
)
44 /* FIXME: This is incorrect */
45 unix_to_nt_time(t
, get_time_t_max());
48 /*******************************************************************
49 External access to time_t_min and time_t_max.
50 ********************************************************************/
52 time_t get_time_t_max(void)
57 /*******************************************************************
58 A gettimeofday wrapper.
59 ********************************************************************/
61 void GetTimeOfDay(struct timeval
*tval
)
63 #ifdef HAVE_GETTIMEOFDAY_TZ
64 gettimeofday(tval
,NULL
);
70 #define TM_YEAR_BASE 1900
72 /*******************************************************************
73 Yield the difference between *A and *B, in seconds, ignoring leap seconds.
74 ********************************************************************/
76 static int tm_diff(struct tm
*a
, struct tm
*b
)
78 int ay
= a
->tm_year
+ (TM_YEAR_BASE
- 1);
79 int by
= b
->tm_year
+ (TM_YEAR_BASE
- 1);
80 int intervening_leap_days
= (ay
/4 - by
/4) - (ay
/100 - by
/100) + (ay
/400 - by
/400);
82 int days
= 365*years
+ intervening_leap_days
+ (a
->tm_yday
- b
->tm_yday
);
83 int hours
= 24*days
+ (a
->tm_hour
- b
->tm_hour
);
84 int minutes
= 60*hours
+ (a
->tm_min
- b
->tm_min
);
85 int seconds
= 60*minutes
+ (a
->tm_sec
- b
->tm_sec
);
90 /*******************************************************************
91 Return the UTC offset in seconds west of UTC, or 0 if it cannot be determined.
92 ******************************************************************/
94 int get_time_zone(time_t t
)
96 struct tm
*tm
= gmtime(&t
);
107 return tm_diff(&tm_utc
,tm
) + 60*extra_time_offset
;
110 /*******************************************************************
111 Accessor function for the server time zone offset.
112 set_server_zone_offset() must have been called first.
113 ******************************************************************/
115 static int server_zone_offset
;
117 int get_server_zone_offset(void)
119 return server_zone_offset
;
122 /*******************************************************************
123 Initialize the server time zone offset. Called when a client connects.
124 ******************************************************************/
126 int set_server_zone_offset(time_t t
)
128 server_zone_offset
= get_time_zone(t
);
129 return server_zone_offset
;
132 /*******************************************************************
133 Re-read the smb serverzone value.
134 ******************************************************************/
136 static struct timeval start_time_hires
;
140 set_server_zone_offset(time(NULL
));
142 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset
));
144 /* Save the start time of this process. */
145 if (start_time_hires
.tv_sec
== 0 && start_time_hires
.tv_usec
== 0) {
146 GetTimeOfDay(&start_time_hires
);
150 /**********************************************************************
151 Return a timeval struct of the uptime of this process. As TimeInit is
152 done before a daemon fork then this is the start time from the parent
154 ***********************************************************************/
156 void get_process_uptime(struct timeval
*ret_time
)
158 struct timeval time_now_hires
;
160 GetTimeOfDay(&time_now_hires
);
161 ret_time
->tv_sec
= time_now_hires
.tv_sec
- start_time_hires
.tv_sec
;
162 ret_time
->tv_usec
= time_now_hires
.tv_usec
- start_time_hires
.tv_usec
;
163 if (time_now_hires
.tv_usec
< start_time_hires
.tv_usec
) {
164 ret_time
->tv_sec
-= 1;
165 ret_time
->tv_usec
= 1000000 + (time_now_hires
.tv_usec
- start_time_hires
.tv_usec
);
167 ret_time
->tv_usec
= time_now_hires
.tv_usec
- start_time_hires
.tv_usec
;
172 /****************************************************************************
173 Return the UTC offset in seconds west of UTC, adjusted for extra time offset.
174 **************************************************************************/
176 int TimeDiff(time_t t
)
178 return get_time_zone(t
);
182 #define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
184 /****************************************************************************
185 Interpret an 8 byte "filetime" structure to a time_t
186 It's originally in "100ns units since jan 1st 1601"
188 An 8 byte value of 0xffffffffffffffff will be returned as (time_t)0.
191 ****************************************************************************/
193 time_t nt_time_to_unix(NTTIME
*nt
)
197 /* The next two lines are a fix needed for the
198 broken SCO compiler. JRA. */
199 time_t l_time_min
= TIME_T_MIN
;
200 time_t l_time_max
= TIME_T_MAX
;
202 if (nt
->high
== 0 || (nt
->high
== 0xffffffff && nt
->low
== 0xffffffff)) {
206 d
= ((double)nt
->high
)*4.0*(double)(1<<30);
207 d
+= (nt
->low
&0xFFF00000);
210 /* now adjust by 369 years to make the secs since 1970 */
211 d
-= TIME_FIXUP_CONSTANT
;
213 if (d
<= l_time_min
) {
217 if (d
>= l_time_max
) {
221 ret
= (time_t)(d
+0.5);
225 /****************************************************************************
226 Convert a NTTIME structure to a time_t.
227 It's originally in "100ns units".
229 This is an absolute version of the one above.
230 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
231 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
232 ****************************************************************************/
234 time_t nt_time_to_unix_abs(NTTIME
*nt
)
238 /* The next two lines are a fix needed for the
239 broken SCO compiler. JRA. */
240 time_t l_time_min
= TIME_T_MIN
;
241 time_t l_time_max
= TIME_T_MAX
;
247 if (nt
->high
==0x80000000 && nt
->low
==0) {
251 /* reverse the time */
252 /* it's a negative value, turn it to positive */
256 d
= ((double)nt
->high
)*4.0*(double)(1<<30);
257 d
+= (nt
->low
&0xFFF00000);
260 if (!(l_time_min
<= d
&& d
<= l_time_max
)) {
264 ret
= (time_t)(d
+0.5);
269 /****************************************************************************
270 Interprets an nt time into a unix time_t.
271 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
272 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
273 ****************************************************************************/
275 time_t interpret_long_date(char *p
)
280 if (nt
.low
== 0xFFFFFFFF && nt
.high
== 0xFFFFFFFF) {
283 return nt_time_to_unix(&nt
);
286 /****************************************************************************
287 Put a 8 byte filetime from a time_t. Uses GMT.
288 ****************************************************************************/
290 void unix_to_nt_time(NTTIME
*nt
, time_t t
)
299 if (t
== TIME_T_MAX
) {
300 nt
->low
= 0xffffffff;
301 nt
->high
= 0x7fffffff;
304 if (t
== (time_t)-1) {
305 nt
->low
= 0xffffffff;
306 nt
->high
= 0xffffffff;
311 d
+= TIME_FIXUP_CONSTANT
;
314 nt
->high
= (uint32
)(d
* (1.0/(4.0*(double)(1<<30))));
315 nt
->low
= (uint32
)(d
- ((double)nt
->high
)*4.0*(double)(1<<30));
318 /****************************************************************************
319 Convert a time_t to a NTTIME structure
321 This is an absolute version of the one above.
322 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
323 If the nttime_t was 5 seconds, the NTTIME is 5 seconds. JFM
324 ****************************************************************************/
326 void unix_to_nt_time_abs(NTTIME
*nt
, time_t t
)
336 if (t
== TIME_T_MAX
) {
337 nt
->low
= 0xffffffff;
338 nt
->high
= 0x7fffffff;
342 if (t
== (time_t)-1) {
343 /* that's what NT uses for infinite */
345 nt
->high
= 0x80000000;
352 nt
->high
= (uint32
)(d
* (1.0/(4.0*(double)(1<<30))));
353 nt
->low
= (uint32
)(d
- ((double)nt
->high
)*4.0*(double)(1<<30));
355 /* convert to a negative value */
360 /****************************************************************************
361 Take a Unix time and convert to an NTTIME structure and place in buffer
363 ****************************************************************************/
365 void put_long_date(char *p
, time_t t
)
368 unix_to_nt_time(&nt
, t
);
370 SIVAL(p
, 4, nt
.high
);
373 /****************************************************************************
374 Check if it's a null mtime.
375 ****************************************************************************/
377 BOOL
null_mtime(time_t mtime
)
379 if (mtime
== 0 || mtime
== (time_t)0xFFFFFFFF || mtime
== (time_t)-1)
384 /*******************************************************************
385 Create a 16 bit dos packed date.
386 ********************************************************************/
388 static uint16
make_dos_date1(struct tm
*t
)
391 ret
= (((unsigned)(t
->tm_mon
+1)) >> 3) | ((t
->tm_year
-80) << 1);
392 ret
= ((ret
&0xFF)<<8) | (t
->tm_mday
| (((t
->tm_mon
+1) & 0x7) << 5));
396 /*******************************************************************
397 Create a 16 bit dos packed time.
398 ********************************************************************/
400 static uint16
make_dos_time1(struct tm
*t
)
403 ret
= ((((unsigned)t
->tm_min
>> 3)&0x7) | (((unsigned)t
->tm_hour
) << 3));
404 ret
= ((ret
&0xFF)<<8) | ((t
->tm_sec
/2) | ((t
->tm_min
& 0x7) << 5));
408 /*******************************************************************
409 Create a 32 bit dos packed date/time from some parameters.
410 This takes a GMT time and returns a packed localtime structure.
411 ********************************************************************/
413 static uint32
make_dos_date(time_t unixdate
, int zone_offset
)
422 unixdate
-= zone_offset
;
423 t
= gmtime(&unixdate
);
428 ret
= make_dos_date1(t
);
429 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
434 /*******************************************************************
435 Put a dos date into a buffer (time/date format).
436 This takes GMT time and puts local time in the buffer.
437 ********************************************************************/
439 static void put_dos_date(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
441 uint32 x
= make_dos_date(unixdate
, zone_offset
);
445 /*******************************************************************
446 Put a dos date into a buffer (date/time format).
447 This takes GMT time and puts local time in the buffer.
448 ********************************************************************/
450 static void put_dos_date2(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
452 uint32 x
= make_dos_date(unixdate
, zone_offset
);
453 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
457 /*******************************************************************
458 Put a dos 32 bit "unix like" date into a buffer. This routine takes
459 GMT and converts it to LOCAL time before putting it (most SMBs assume
460 localtime for this sort of date)
461 ********************************************************************/
463 static void put_dos_date3(char *buf
,int offset
,time_t unixdate
, int zone_offset
)
465 if (!null_mtime(unixdate
)) {
466 unixdate
-= zone_offset
;
468 SIVAL(buf
,offset
,unixdate
);
471 /*******************************************************************
472 Interpret a 32 bit dos packed date/time to some parameters.
473 ********************************************************************/
475 static void interpret_dos_date(uint32 date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
479 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
480 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
482 *second
= 2*(p0
& 0x1F);
483 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
484 *hour
= (p1
>>3)&0xFF;
486 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
487 *year
= ((p3
>>1)&0xFF) + 80;
490 /*******************************************************************
491 Create a unix date (int GMT) from a dos date (which is actually in
493 ********************************************************************/
495 static time_t make_unix_date(void *date_ptr
, int zone_offset
)
501 dos_date
= IVAL(date_ptr
,0);
507 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
508 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
518 /*******************************************************************
519 Like make_unix_date() but the words are reversed.
520 ********************************************************************/
522 static time_t make_unix_date2(void *date_ptr
, int zone_offset
)
526 x
= IVAL(date_ptr
,0);
527 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
530 return(make_unix_date((void *)&x
, zone_offset
));
533 /*******************************************************************
534 Create a unix GMT date from a dos date in 32 bit "unix like" format
535 these generally arrive as localtimes, with corresponding DST.
536 ******************************************************************/
538 static time_t make_unix_date3(void *date_ptr
, int zone_offset
)
540 time_t t
= (time_t)IVAL(date_ptr
,0);
541 if (!null_mtime(t
)) {
547 /***************************************************************************
548 Server versions of the above functions.
549 ***************************************************************************/
551 void srv_put_dos_date(char *buf
,int offset
,time_t unixdate
)
553 put_dos_date(buf
, offset
, unixdate
, server_zone_offset
);
556 void srv_put_dos_date2(char *buf
,int offset
, time_t unixdate
)
558 put_dos_date2(buf
, offset
, unixdate
, server_zone_offset
);
561 void srv_put_dos_date3(char *buf
,int offset
,time_t unixdate
)
563 put_dos_date3(buf
, offset
, unixdate
, server_zone_offset
);
566 time_t srv_make_unix_date(void *date_ptr
)
568 return make_unix_date(date_ptr
, server_zone_offset
);
571 time_t srv_make_unix_date2(void *date_ptr
)
573 return make_unix_date2(date_ptr
, server_zone_offset
);
576 time_t srv_make_unix_date3(void *date_ptr
)
578 return make_unix_date3(date_ptr
, server_zone_offset
);
581 /***************************************************************************
582 Client versions of the above functions.
583 ***************************************************************************/
585 void cli_put_dos_date(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
587 put_dos_date(buf
, offset
, unixdate
, cli
->serverzone
);
590 void cli_put_dos_date2(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
592 put_dos_date2(buf
, offset
, unixdate
, cli
->serverzone
);
595 void cli_put_dos_date3(struct cli_state
*cli
, char *buf
, int offset
, time_t unixdate
)
597 put_dos_date3(buf
, offset
, unixdate
, cli
->serverzone
);
600 time_t cli_make_unix_date(struct cli_state
*cli
, void *date_ptr
)
602 return make_unix_date(date_ptr
, cli
->serverzone
);
605 time_t cli_make_unix_date2(struct cli_state
*cli
, void *date_ptr
)
607 return make_unix_date2(date_ptr
, cli
->serverzone
);
610 time_t cli_make_unix_date3(struct cli_state
*cli
, void *date_ptr
)
612 return make_unix_date3(date_ptr
, cli
->serverzone
);
615 /***************************************************************************
616 Return a HTTP/1.0 time string.
617 ***************************************************************************/
619 char *http_timestring(time_t t
)
622 struct tm
*tm
= localtime(&t
);
625 slprintf(buf
,sizeof(buf
)-1,"%ld seconds since the Epoch",(long)t
);
627 #ifndef HAVE_STRFTIME
628 fstrcpy(buf
, asctime(tm
));
629 if(buf
[strlen(buf
)-1] == '\n')
630 buf
[strlen(buf
)-1] = 0;
631 #else /* !HAVE_STRFTIME */
632 strftime(buf
, sizeof(buf
)-1, "%a, %d %b %Y %H:%M:%S %Z", tm
);
633 #endif /* !HAVE_STRFTIME */
637 /****************************************************************************
638 Return the date and time as a string
639 ****************************************************************************/
641 char *timestring(BOOL hires
)
643 static fstring TimeBuf
;
650 t
= (time_t)tp
.tv_sec
;
659 "%ld.%06ld seconds since the Epoch",
665 "%ld seconds since the Epoch",
671 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
672 slprintf(TimeBuf
+strlen(TimeBuf
),
673 sizeof(TimeBuf
)-1 - strlen(TimeBuf
),
677 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
687 fstrcpy(TimeBuf
, asctime(tm
));
694 /****************************************************************************
695 Return the best approximation to a 'create time' under UNIX from a stat
697 ****************************************************************************/
699 time_t get_create_time(SMB_STRUCT_STAT
*st
,BOOL fake_dirs
)
703 if(S_ISDIR(st
->st_mode
) && fake_dirs
) {
704 return (time_t)315493200L; /* 1/1/1980 */
707 ret
= MIN(st
->st_ctime
, st
->st_mtime
);
708 ret1
= MIN(ret
, st
->st_atime
);
710 if(ret1
!= (time_t)0) {
715 * One of ctime, mtime or atime was zero (probably atime).
716 * Just return MIN(ctime, mtime).
721 /****************************************************************************
722 Initialise an NTTIME to -1, which means "unknown" or "don't expire".
723 ****************************************************************************/
725 void init_nt_time(NTTIME
*nt
)
727 nt
->high
= 0x7FFFFFFF;
728 nt
->low
= 0xFFFFFFFF;
731 /****************************************************************************
732 Check if NTTIME is 0.
733 ****************************************************************************/
735 BOOL
nt_time_is_zero(NTTIME
*nt
)
743 /****************************************************************************
744 Check if two NTTIMEs are the same.
745 ****************************************************************************/
747 BOOL
nt_time_equals(NTTIME
*nt1
, NTTIME
*nt2
)
749 return (nt1
->high
== nt2
->high
&& nt1
->low
== nt2
->low
);
752 /****************************************************************************
753 Return a timeval difference in usec.
754 ****************************************************************************/
756 SMB_BIG_INT
usec_time_diff(const struct timeval
*larget
, const struct timeval
*smallt
)
758 SMB_BIG_INT sec_diff
= larget
->tv_sec
- smallt
->tv_sec
;
759 return (sec_diff
* 1000000) + (SMB_BIG_INT
)(larget
->tv_usec
- smallt
->tv_usec
);
762 /****************************************************************************
763 Return a timeval struct with the given elements.
764 ****************************************************************************/
766 struct timeval
timeval_set(uint32_t secs
, uint32_t usecs
)
774 /****************************************************************************
775 Return a zero timeval.
776 ****************************************************************************/
778 struct timeval
timeval_zero(void)
780 return timeval_set(0,0);
783 /****************************************************************************
784 Return True if a timeval is zero.
785 ****************************************************************************/
787 BOOL
timeval_is_zero(const struct timeval
*tv
)
789 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
792 /****************************************************************************
793 Return a timeval for the current time.
794 ****************************************************************************/
796 struct timeval
timeval_current(void)
803 /****************************************************************************
804 Return a timeval ofs microseconds after tv.
805 ****************************************************************************/
807 struct timeval
timeval_add(const struct timeval
*tv
,
808 uint32_t secs
, uint32_t usecs
)
810 struct timeval tv2
= *tv
;
812 tv2
.tv_usec
+= usecs
;
813 tv2
.tv_sec
+= tv2
.tv_usec
/ 1000000;
814 tv2
.tv_usec
= tv2
.tv_usec
% 1000000;
818 /****************************************************************************
819 Return the sum of two timeval structures.
820 ****************************************************************************/
822 struct timeval
timeval_sum(const struct timeval
*tv1
,
823 const struct timeval
*tv2
)
825 return timeval_add(tv1
, tv2
->tv_sec
, tv2
->tv_usec
);
828 /****************************************************************************
829 Return a timeval secs/usecs into the future.
830 ****************************************************************************/
832 struct timeval
timeval_current_ofs(uint32_t secs
, uint32_t usecs
)
834 struct timeval tv
= timeval_current();
835 return timeval_add(&tv
, secs
, usecs
);
838 /****************************************************************************
839 Compare two timeval structures.
840 Return -1 if tv1 < tv2
841 Return 0 if tv1 == tv2
842 Return 1 if tv1 > tv2
843 ****************************************************************************/
845 int timeval_compare(const struct timeval
*tv1
, const struct timeval
*tv2
)
847 if (tv1
->tv_sec
> tv2
->tv_sec
) {
850 if (tv1
->tv_sec
< tv2
->tv_sec
) {
853 if (tv1
->tv_usec
> tv2
->tv_usec
) {
856 if (tv1
->tv_usec
< tv2
->tv_usec
) {
862 /****************************************************************************
863 Return the difference between two timevals as a timeval.
864 If tv1 comes after tv2, then return a zero timeval
865 (this is *tv2 - *tv1).
866 ****************************************************************************/
868 struct timeval
timeval_until(const struct timeval
*tv1
,
869 const struct timeval
*tv2
)
872 if (timeval_compare(tv1
, tv2
) >= 0) {
873 return timeval_zero();
875 t
.tv_sec
= tv2
->tv_sec
- tv1
->tv_sec
;
876 if (tv1
->tv_usec
> tv2
->tv_usec
) {
878 t
.tv_usec
= 1000000 - (tv1
->tv_usec
- tv2
->tv_usec
);
880 t
.tv_usec
= tv2
->tv_usec
- tv1
->tv_usec
;
885 /****************************************************************************
886 Return the lesser of two timevals.
887 ****************************************************************************/
889 struct timeval
timeval_min(const struct timeval
*tv1
,
890 const struct timeval
*tv2
)
892 if (tv1
->tv_sec
< tv2
->tv_sec
) {
895 if (tv1
->tv_sec
> tv2
->tv_sec
) {
898 if (tv1
->tv_usec
< tv2
->tv_usec
) {
904 /****************************************************************************
905 Return the greater of two timevals.
906 ****************************************************************************/
908 struct timeval
timeval_max(const struct timeval
*tv1
,
909 const struct timeval
*tv2
)
911 if (tv1
->tv_sec
> tv2
->tv_sec
) {
914 if (tv1
->tv_sec
< tv2
->tv_sec
) {
917 if (tv1
->tv_usec
> tv2
->tv_usec
) {
923 /****************************************************************************
924 Convert ASN.1 GeneralizedTime string to unix-time.
925 Returns 0 on failure; Currently ignores timezone.
926 ****************************************************************************/
928 time_t generalized_to_unix_time(const char *str
)
934 if (sscanf(str
, "%4d%2d%2d%2d%2d%2d",
935 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
,
936 &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
) != 6) {