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>
29 int extra_time_offset
= 0;
36 #define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
37 : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
40 #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
43 void get_nttime_max(NTTIME
*t
)
45 /* FIXME: This is incorrect */
46 unix_to_nt_time(t
, get_time_t_max());
49 /*******************************************************************
50 External access to time_t_min and time_t_max.
51 ********************************************************************/
53 time_t get_time_t_max(void)
58 /*******************************************************************
59 a gettimeofday wrapper
60 ********************************************************************/
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 ********************************************************************/
75 static int tm_diff(struct tm
*a
, struct tm
*b
)
77 int ay
= a
->tm_year
+ (TM_YEAR_BASE
- 1);
78 int by
= b
->tm_year
+ (TM_YEAR_BASE
- 1);
79 int intervening_leap_days
=
80 (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 ******************************************************************/
93 static int TimeZone(time_t t
)
95 struct tm
*tm
= gmtime(&t
);
103 return tm_diff(&tm_utc
,tm
);
107 static BOOL done_serverzone_init
;
109 /* Return the smb serverzone value */
111 static int get_serverzone(void)
113 static int serverzone
;
115 if (!done_serverzone_init
) {
116 serverzone
= TimeZone(time(NULL
));
118 if ((serverzone
% 60) != 0) {
119 DEBUG(1,("WARNING: Your timezone is not a multiple of 1 minute.\n"));
122 DEBUG(4,("Serverzone is %d\n",serverzone
));
124 done_serverzone_init
= True
;
130 /* Re-read the smb serverzone value */
132 static struct timeval start_time_hires
;
136 done_serverzone_init
= False
;
138 /* Save the start time of this process. */
139 if (start_time_hires
.tv_sec
== 0 && start_time_hires
.tv_usec
== 0)
140 GetTimeOfDay(&start_time_hires
);
143 /**********************************************************************
144 Return a timeval struct of the uptime of this process. As TimeInit is
145 done before a daemon fork then this is the start time from the parent
147 ***********************************************************************/
149 void get_process_uptime(struct timeval
*ret_time
)
151 struct timeval time_now_hires
;
153 GetTimeOfDay(&time_now_hires
);
154 ret_time
->tv_sec
= time_now_hires
.tv_sec
- start_time_hires
.tv_sec
;
155 ret_time
->tv_usec
= time_now_hires
.tv_usec
- start_time_hires
.tv_usec
;
156 if (time_now_hires
.tv_usec
< start_time_hires
.tv_usec
) {
157 ret_time
->tv_sec
-= 1;
158 ret_time
->tv_usec
= 1000000 + (time_now_hires
.tv_usec
- start_time_hires
.tv_usec
);
160 ret_time
->tv_usec
= time_now_hires
.tv_usec
- start_time_hires
.tv_usec
;
163 /*******************************************************************
164 return the same value as TimeZone, but it should be more efficient.
166 We keep a table of DST offsets to prevent calling localtime() on each
167 call of this function. This saves a LOT of time on many unixes.
169 Updated by Paul Eggert <eggert@twinsun.com>
170 ********************************************************************/
171 static int TimeZoneFaster(time_t t
)
173 static struct dst_table
{time_t start
,end
; int zone
;} *tdt
, *dst_table
= NULL
;
174 static int table_size
= 0;
178 if (t
== 0) t
= time(NULL
);
180 /* Tunis has a 8 day DST region, we need to be careful ... */
181 #define MAX_DST_WIDTH (365*24*60*60)
182 #define MAX_DST_SKIP (7*24*60*60)
184 for (i
=0;i
<table_size
;i
++)
185 if (t
>= dst_table
[i
].start
&& t
<= dst_table
[i
].end
) break;
188 zone
= dst_table
[i
].zone
;
193 tdt
= (struct dst_table
*)Realloc(dst_table
,
194 sizeof(dst_table
[0])*(i
+1));
196 DEBUG(0,("TimeZoneFaster: out of memory!\n"));
197 SAFE_FREE(dst_table
);
203 dst_table
[i
].zone
= zone
;
204 dst_table
[i
].start
= dst_table
[i
].end
= t
;
206 /* no entry will cover more than 6 months */
207 low
= t
- MAX_DST_WIDTH
/2;
211 high
= t
+ MAX_DST_WIDTH
/2;
215 /* widen the new entry using two bisection searches */
216 while (low
+60*60 < dst_table
[i
].start
) {
217 if (dst_table
[i
].start
- low
> MAX_DST_SKIP
*2)
218 t
= dst_table
[i
].start
- MAX_DST_SKIP
;
220 t
= low
+ (dst_table
[i
].start
-low
)/2;
221 if (TimeZone(t
) == zone
)
222 dst_table
[i
].start
= t
;
227 while (high
-60*60 > dst_table
[i
].end
) {
228 if (high
- dst_table
[i
].end
> MAX_DST_SKIP
*2)
229 t
= dst_table
[i
].end
+ MAX_DST_SKIP
;
231 t
= high
- (high
-dst_table
[i
].end
)/2;
232 if (TimeZone(t
) == zone
)
233 dst_table
[i
].end
= t
;
238 DEBUG(1,("Added DST entry from %s ",
239 asctime(localtime(&dst_table
[i
].start
))));
240 DEBUG(1,("to %s (%d)\n",asctime(localtime(&dst_table
[i
].end
)),
248 /****************************************************************************
249 return the UTC offset in seconds west of UTC, adjusted for extra time offset
250 **************************************************************************/
251 int TimeDiff(time_t t
)
253 return TimeZoneFaster(t
) + 60*extra_time_offset
;
257 /****************************************************************************
258 return the UTC offset in seconds west of UTC, adjusted for extra time
259 offset, for a local time value. If ut = lt + LocTimeDiff(lt), then
260 lt = ut - TimeDiff(ut), but the converse does not necessarily hold near
261 daylight savings transitions because some local times are ambiguous.
262 LocTimeDiff(t) equals TimeDiff(t) except near daylight savings transitions.
263 +**************************************************************************/
264 static int LocTimeDiff(time_t lte
)
266 time_t lt
= lte
- 60*extra_time_offset
;
267 int d
= TimeZoneFaster(lt
);
270 /* if overflow occurred, ignore all the adjustments so far */
271 if (((lte
< lt
) ^ (extra_time_offset
< 0)) | ((t
< lt
) ^ (d
< 0)))
274 /* now t should be close enough to the true UTC to yield the right answer */
279 /****************************************************************************
280 try to optimise the localtime call, it can be quite expensive on some machines
281 ****************************************************************************/
282 struct tm
*LocalTime(time_t *t
)
291 #define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
293 /****************************************************************************
294 interpret an 8 byte "filetime" structure to a time_t
295 It's originally in "100ns units since jan 1st 1601"
297 It appears to be kludge-GMT (at least for file listings). This means
298 its the GMT you get by taking a localtime and adding the
299 serverzone. This is NOT the same as GMT in some cases. This routine
300 converts this to real GMT.
301 ****************************************************************************/
302 time_t nt_time_to_unix(NTTIME
*nt
)
306 /* The next two lines are a fix needed for the
307 broken SCO compiler. JRA. */
308 time_t l_time_min
= TIME_T_MIN
;
309 time_t l_time_max
= TIME_T_MAX
;
311 if (nt
->high
== 0) return(0);
313 d
= ((double)nt
->high
)*4.0*(double)(1<<30);
314 d
+= (nt
->low
&0xFFF00000);
317 /* now adjust by 369 years to make the secs since 1970 */
318 d
-= TIME_FIXUP_CONSTANT
;
320 if (!(l_time_min
<= d
&& d
<= l_time_max
))
323 ret
= (time_t)(d
+0.5);
325 /* this takes us from kludge-GMT to real GMT */
326 ret
-= get_serverzone();
327 ret
+= LocTimeDiff(ret
);
332 /****************************************************************************
333 Convert a NTTIME structure to a time_t.
334 It's originally in "100ns units".
336 This is an absolute version of the one above.
337 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
338 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
339 ****************************************************************************/
341 time_t nt_time_to_unix_abs(NTTIME
*nt
)
345 /* The next two lines are a fix needed for the
346 broken SCO compiler. JRA. */
347 time_t l_time_min
= TIME_T_MIN
;
348 time_t l_time_max
= TIME_T_MAX
;
353 if (nt
->high
==0x80000000 && nt
->low
==0)
356 /* reverse the time */
357 /* it's a negative value, turn it to positive */
361 d
= ((double)nt
->high
)*4.0*(double)(1<<30);
362 d
+= (nt
->low
&0xFFF00000);
365 if (!(l_time_min
<= d
&& d
<= l_time_max
))
368 ret
= (time_t)(d
+0.5);
373 /****************************************************************************
374 interprets an nt time into a unix time_t
375 ****************************************************************************/
376 time_t interpret_long_date(char *p
)
381 return nt_time_to_unix(&nt
);
384 /****************************************************************************
385 put a 8 byte filetime from a time_t
386 This takes real GMT as input and converts to kludge-GMT
387 ****************************************************************************/
388 void unix_to_nt_time(NTTIME
*nt
, time_t t
)
400 nt
->low
= 0xffffffff;
401 nt
->high
= 0x7fffffff;
406 nt
->low
= 0xffffffff;
407 nt
->high
= 0xffffffff;
411 /* this converts GMT to kludge-GMT */
412 t
-= TimeDiff(t
) - get_serverzone();
415 d
+= TIME_FIXUP_CONSTANT
;
418 nt
->high
= (uint32
)(d
* (1.0/(4.0*(double)(1<<30))));
419 nt
->low
= (uint32
)(d
- ((double)nt
->high
)*4.0*(double)(1<<30));
422 /****************************************************************************
423 Convert a time_t to a NTTIME structure
425 This is an absolute version of the one above.
426 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
427 If the nttime_t was 5 seconds, the NTTIME is 5 seconds. JFM
428 ****************************************************************************/
430 void unix_to_nt_time_abs(NTTIME
*nt
, time_t t
)
440 if (t
== TIME_T_MAX
) {
441 nt
->low
= 0xffffffff;
442 nt
->high
= 0x7fffffff;
447 /* that's what NT uses for infinite */
449 nt
->high
= 0x80000000;
456 nt
->high
= (uint32
)(d
* (1.0/(4.0*(double)(1<<30))));
457 nt
->low
= (uint32
)(d
- ((double)nt
->high
)*4.0*(double)(1<<30));
459 /* convert to a negative value */
465 /****************************************************************************
466 take an NTTIME structure, containing high / low time. convert to unix time.
467 lkclXXXX this may need 2 SIVALs not a memcpy. we'll see...
468 ****************************************************************************/
469 void put_long_date(char *p
,time_t t
)
472 unix_to_nt_time(&nt
, t
);
474 SIVAL(p
, 4, nt
.high
);
477 /****************************************************************************
478 check if it's a null mtime
479 ****************************************************************************/
480 BOOL
null_mtime(time_t mtime
)
482 if (mtime
== 0 || mtime
== (time_t)0xFFFFFFFF || mtime
== (time_t)-1)
487 /*******************************************************************
488 create a 16 bit dos packed date
489 ********************************************************************/
490 static uint16
make_dos_date1(struct tm
*t
)
493 ret
= (((unsigned)(t
->tm_mon
+1)) >> 3) | ((t
->tm_year
-80) << 1);
494 ret
= ((ret
&0xFF)<<8) | (t
->tm_mday
| (((t
->tm_mon
+1) & 0x7) << 5));
498 /*******************************************************************
499 create a 16 bit dos packed time
500 ********************************************************************/
501 static uint16
make_dos_time1(struct tm
*t
)
504 ret
= ((((unsigned)t
->tm_min
>> 3)&0x7) | (((unsigned)t
->tm_hour
) << 3));
505 ret
= ((ret
&0xFF)<<8) | ((t
->tm_sec
/2) | ((t
->tm_min
& 0x7) << 5));
509 /*******************************************************************
510 create a 32 bit dos packed date/time from some parameters
511 This takes a GMT time and returns a packed localtime structure
512 ********************************************************************/
513 static uint32
make_dos_date(time_t unixdate
)
518 t
= LocalTime(&unixdate
);
522 ret
= make_dos_date1(t
);
523 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
528 /*******************************************************************
529 put a dos date into a buffer (time/date format)
530 This takes GMT time and puts local time in the buffer
531 ********************************************************************/
532 void put_dos_date(char *buf
,int offset
,time_t unixdate
)
534 uint32 x
= make_dos_date(unixdate
);
538 /*******************************************************************
539 put a dos date into a buffer (date/time format)
540 This takes GMT time and puts local time in the buffer
541 ********************************************************************/
542 void put_dos_date2(char *buf
,int offset
,time_t unixdate
)
544 uint32 x
= make_dos_date(unixdate
);
545 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
549 /*******************************************************************
550 put a dos 32 bit "unix like" date into a buffer. This routine takes
551 GMT and converts it to LOCAL time before putting it (most SMBs assume
552 localtime for this sort of date)
553 ********************************************************************/
554 void put_dos_date3(char *buf
,int offset
,time_t unixdate
)
556 if (!null_mtime(unixdate
))
557 unixdate
-= TimeDiff(unixdate
);
558 SIVAL(buf
,offset
,unixdate
);
561 /*******************************************************************
562 interpret a 32 bit dos packed date/time to some parameters
563 ********************************************************************/
564 static void interpret_dos_date(uint32 date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
568 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
569 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
571 *second
= 2*(p0
& 0x1F);
572 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
573 *hour
= (p1
>>3)&0xFF;
575 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
576 *year
= ((p3
>>1)&0xFF) + 80;
579 /*******************************************************************
580 create a unix date (int GMT) from a dos date (which is actually in
582 ********************************************************************/
583 time_t make_unix_date(void *date_ptr
)
589 dos_date
= IVAL(date_ptr
,0);
591 if (dos_date
== 0) return(0);
593 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
594 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
597 /* mktime() also does the local to GMT time conversion for us */
603 /*******************************************************************
604 like make_unix_date() but the words are reversed
605 ********************************************************************/
606 time_t make_unix_date2(void *date_ptr
)
610 x
= IVAL(date_ptr
,0);
611 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
614 return(make_unix_date((void *)&x
));
617 /*******************************************************************
618 create a unix GMT date from a dos date in 32 bit "unix like" format
619 these generally arrive as localtimes, with corresponding DST
620 ******************************************************************/
621 time_t make_unix_date3(void *date_ptr
)
623 time_t t
= (time_t)IVAL(date_ptr
,0);
630 /***************************************************************************
631 return a HTTP/1.0 time string
632 ***************************************************************************/
633 char *http_timestring(time_t t
)
636 struct tm
*tm
= LocalTime(&t
);
639 slprintf(buf
,sizeof(buf
)-1,"%ld seconds since the Epoch",(long)t
);
641 #ifndef HAVE_STRFTIME
642 fstrcpy(buf
, asctime(tm
));
643 if(buf
[strlen(buf
)-1] == '\n')
644 buf
[strlen(buf
)-1] = 0;
645 #else /* !HAVE_STRFTIME */
646 strftime(buf
, sizeof(buf
)-1, "%a, %d %b %Y %H:%M:%S %Z", tm
);
647 #endif /* !HAVE_STRFTIME */
653 /****************************************************************************
654 Return the date and time as a string
655 ****************************************************************************/
657 char *timestring(BOOL hires
)
659 static fstring TimeBuf
;
666 t
= (time_t)tp
.tv_sec
;
675 "%ld.%06ld seconds since the Epoch",
681 "%ld seconds since the Epoch",
687 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
688 slprintf(TimeBuf
+strlen(TimeBuf
),
689 sizeof(TimeBuf
)-1 - strlen(TimeBuf
),
693 strftime(TimeBuf
,100,"%Y/%m/%d %H:%M:%S",tm
);
703 fstrcpy(TimeBuf
, asctime(tm
));
710 /****************************************************************************
711 return the best approximation to a 'create time' under UNIX from a stat
713 ****************************************************************************/
715 time_t get_create_time(SMB_STRUCT_STAT
*st
,BOOL fake_dirs
)
719 if(S_ISDIR(st
->st_mode
) && fake_dirs
)
720 return (time_t)315493200L; /* 1/1/1980 */
722 ret
= MIN(st
->st_ctime
, st
->st_mtime
);
723 ret1
= MIN(ret
, st
->st_atime
);
725 if(ret1
!= (time_t)0)
729 * One of ctime, mtime or atime was zero (probably atime).
730 * Just return MIN(ctime, mtime).
735 /****************************************************************************
736 initialise an NTTIME to -1, which means "unknown" or "don't expire"
737 ****************************************************************************/
739 void init_nt_time(NTTIME
*nt
)
741 nt
->high
= 0x7FFFFFFF;
742 nt
->low
= 0xFFFFFFFF;
745 /****************************************************************************
747 ****************************************************************************/
748 BOOL
nt_time_is_zero(NTTIME
*nt
)