2 Unix SMB/Netbios implementation.
4 time handling functions
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 This stuff was largely rewritten by Paul Eggert <eggert@twinsun.com>
30 int extra_time_offset
= 0;
36 /*******************************************************************
37 External access to time_t_min and time_t_max.
38 ********************************************************************/
40 time_t get_time_t_min(void)
45 time_t get_time_t_max(void)
50 /*******************************************************************
51 a gettimeofday wrapper
52 ********************************************************************/
53 void GetTimeOfDay(struct timeval
*tval
)
55 #ifdef HAVE_GETTIMEOFDAY_TZ
56 gettimeofday(tval
,NULL
);
62 #define TM_YEAR_BASE 1900
64 /*******************************************************************
65 yield the difference between *A and *B, in seconds, ignoring leap seconds
66 ********************************************************************/
67 static int tm_diff(struct tm
*a
, struct tm
*b
)
69 int ay
= a
->tm_year
+ (TM_YEAR_BASE
- 1);
70 int by
= b
->tm_year
+ (TM_YEAR_BASE
- 1);
71 int intervening_leap_days
=
72 (ay
/4 - by
/4) - (ay
/100 - by
/100) + (ay
/400 - by
/400);
74 int days
= 365*years
+ intervening_leap_days
+ (a
->tm_yday
- b
->tm_yday
);
75 int hours
= 24*days
+ (a
->tm_hour
- b
->tm_hour
);
76 int minutes
= 60*hours
+ (a
->tm_min
- b
->tm_min
);
77 int seconds
= 60*minutes
+ (a
->tm_sec
- b
->tm_sec
);
82 /*******************************************************************
83 return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
84 ******************************************************************/
85 static int TimeZone(time_t t
)
87 struct tm
*tm
= gmtime(&t
);
95 return tm_diff(&tm_utc
,tm
);
99 static BOOL done_serverzone_init
;
101 /* Return the smb serverzone value */
103 static int get_serverzone(void)
105 static int serverzone
;
107 if (!done_serverzone_init
) {
108 serverzone
= TimeZone(time(NULL
));
110 if ((serverzone
% 60) != 0) {
111 DEBUG(1,("WARNING: Your timezone is not a multiple of 1 minute.\n"));
114 DEBUG(4,("Serverzone is %d\n",serverzone
));
116 done_serverzone_init
= True
;
122 /* Re-read the smb serverzone value */
124 static struct timeval start_time_hires
;
128 done_serverzone_init
= False
;
130 /* Save the start time of this process. */
131 if (start_time_hires
.tv_sec
== 0 && start_time_hires
.tv_usec
== 0)
132 GetTimeOfDay(&start_time_hires
);
135 /**********************************************************************
136 Return a timeval struct of the uptime of this process. As TimeInit is
137 done before a daemon fork then this is the start time from the parent
139 ***********************************************************************/
141 void get_process_uptime(struct timeval
*ret_time
)
143 struct timeval time_now_hires
;
145 GetTimeOfDay(&time_now_hires
);
146 ret_time
->tv_sec
= time_now_hires
.tv_sec
- start_time_hires
.tv_sec
;
147 ret_time
->tv_usec
= time_now_hires
.tv_usec
- start_time_hires
.tv_usec
;
148 if (time_now_hires
.tv_usec
< start_time_hires
.tv_usec
) {
149 ret_time
->tv_sec
-= 1;
150 ret_time
->tv_usec
= 1000000 + (time_now_hires
.tv_usec
- start_time_hires
.tv_usec
);
152 ret_time
->tv_usec
= time_now_hires
.tv_usec
- start_time_hires
.tv_usec
;
155 /*******************************************************************
156 return the same value as TimeZone, but it should be more efficient.
158 We keep a table of DST offsets to prevent calling localtime() on each
159 call of this function. This saves a LOT of time on many unixes.
161 Updated by Paul Eggert <eggert@twinsun.com>
162 ********************************************************************/
163 static int TimeZoneFaster(time_t t
)
165 static struct dst_table
{time_t start
,end
; int zone
;} *tdt
, *dst_table
= NULL
;
166 static int table_size
= 0;
170 if (t
== 0) t
= time(NULL
);
172 /* Tunis has a 8 day DST region, we need to be careful ... */
173 #define MAX_DST_WIDTH (365*24*60*60)
174 #define MAX_DST_SKIP (7*24*60*60)
176 for (i
=0;i
<table_size
;i
++)
177 if (t
>= dst_table
[i
].start
&& t
<= dst_table
[i
].end
) break;
180 zone
= dst_table
[i
].zone
;
185 tdt
= (struct dst_table
*)Realloc(dst_table
,
186 sizeof(dst_table
[0])*(i
+1));
188 DEBUG(0,("TimeZoneFaster: out of memory!\n"));
189 SAFE_FREE(dst_table
);
195 dst_table
[i
].zone
= zone
;
196 dst_table
[i
].start
= dst_table
[i
].end
= t
;
198 /* no entry will cover more than 6 months */
199 low
= t
- MAX_DST_WIDTH
/2;
203 high
= t
+ MAX_DST_WIDTH
/2;
207 /* widen the new entry using two bisection searches */
208 while (low
+60*60 < dst_table
[i
].start
) {
209 if (dst_table
[i
].start
- low
> MAX_DST_SKIP
*2)
210 t
= dst_table
[i
].start
- MAX_DST_SKIP
;
212 t
= low
+ (dst_table
[i
].start
-low
)/2;
213 if (TimeZone(t
) == zone
)
214 dst_table
[i
].start
= t
;
219 while (high
-60*60 > dst_table
[i
].end
) {
220 if (high
- dst_table
[i
].end
> MAX_DST_SKIP
*2)
221 t
= dst_table
[i
].end
+ MAX_DST_SKIP
;
223 t
= high
- (high
-dst_table
[i
].end
)/2;
224 if (TimeZone(t
) == zone
)
225 dst_table
[i
].end
= t
;
230 DEBUG(1,("Added DST entry from %s ",
231 asctime(localtime(&dst_table
[i
].start
))));
232 DEBUG(1,("to %s (%d)\n",asctime(localtime(&dst_table
[i
].end
)),
240 /****************************************************************************
241 return the UTC offset in seconds west of UTC, adjusted for extra time offset
242 **************************************************************************/
243 int TimeDiff(time_t t
)
245 return TimeZoneFaster(t
) + 60*extra_time_offset
;
249 /****************************************************************************
250 return the UTC offset in seconds west of UTC, adjusted for extra time
251 offset, for a local time value. If ut = lt + LocTimeDiff(lt), then
252 lt = ut - TimeDiff(ut), but the converse does not necessarily hold near
253 daylight savings transitions because some local times are ambiguous.
254 LocTimeDiff(t) equals TimeDiff(t) except near daylight savings transitions.
255 +**************************************************************************/
256 static int LocTimeDiff(time_t lte
)
258 time_t lt
= lte
- 60*extra_time_offset
;
259 int d
= TimeZoneFaster(lt
);
262 /* if overflow occurred, ignore all the adjustments so far */
263 if (((lte
< lt
) ^ (extra_time_offset
< 0)) | ((t
< lt
) ^ (d
< 0)))
266 /* now t should be close enough to the true UTC to yield the right answer */
271 /****************************************************************************
272 try to optimise the localtime call, it can be quite expensive on some machines
273 ****************************************************************************/
274 struct tm
*LocalTime(time_t *t
)
283 #define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
285 /****************************************************************************
286 interpret an 8 byte "filetime" structure to a time_t
287 It's originally in "100ns units since jan 1st 1601"
289 It appears to be kludge-GMT (at least for file listings). This means
290 its the GMT you get by taking a localtime and adding the
291 serverzone. This is NOT the same as GMT in some cases. This routine
292 converts this to real GMT.
293 ****************************************************************************/
294 time_t nt_time_to_unix(NTTIME
*nt
)
298 /* The next two lines are a fix needed for the
299 broken SCO compiler. JRA. */
300 time_t l_time_min
= TIME_T_MIN
;
301 time_t l_time_max
= TIME_T_MAX
;
303 if (nt
->high
== 0) return(0);
305 d
= ((double)nt
->high
)*4.0*(double)(1<<30);
306 d
+= (nt
->low
&0xFFF00000);
309 /* now adjust by 369 years to make the secs since 1970 */
310 d
-= TIME_FIXUP_CONSTANT
;
312 if (!(l_time_min
<= d
&& d
<= l_time_max
))
315 ret
= (time_t)(d
+0.5);
317 /* this takes us from kludge-GMT to real GMT */
318 ret
-= get_serverzone();
319 ret
+= LocTimeDiff(ret
);
324 /****************************************************************************
325 convert a NTTIME structure to a time_t
326 It's originally in "100ns units"
328 this is an absolute version of the one above.
329 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
330 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
331 ****************************************************************************/
332 time_t nt_time_to_unix_abs(NTTIME
*nt
)
336 /* The next two lines are a fix needed for the
337 broken SCO compiler. JRA. */
338 time_t l_time_min
= TIME_T_MIN
;
339 time_t l_time_max
= TIME_T_MAX
;
344 if (nt
->high
==0x80000000 && nt
->low
==0)
347 /* reverse the time */
348 /* it's a negative value, turn it to positive */
352 d
= ((double)nt
->high
)*4.0*(double)(1<<30);
353 d
+= (nt
->low
&0xFFF00000);
356 if (!(l_time_min
<= d
&& d
<= l_time_max
))
359 ret
= (time_t)(d
+0.5);
361 /* this takes us from kludge-GMT to real GMT */
362 ret
-= get_serverzone();
363 ret
+= LocTimeDiff(ret
);
370 /****************************************************************************
371 interprets an nt time into a unix time_t
372 ****************************************************************************/
373 time_t interpret_long_date(char *p
)
378 return nt_time_to_unix(&nt
);
381 /****************************************************************************
382 put a 8 byte filetime from a time_t
383 This takes real GMT as input and converts to kludge-GMT
384 ****************************************************************************/
385 void unix_to_nt_time(NTTIME
*nt
, time_t t
)
397 nt
->low
= 0xffffffff;
398 nt
->high
= 0x7fffffff;
403 nt
->low
= 0xffffffff;
404 nt
->high
= 0xffffffff;
408 /* this converts GMT to kludge-GMT */
409 t
-= TimeDiff(t
) - get_serverzone();
412 d
+= TIME_FIXUP_CONSTANT
;
415 nt
->high
= (uint32
)(d
* (1.0/(4.0*(double)(1<<30))));
416 nt
->low
= (uint32
)(d
- ((double)nt
->high
)*4.0*(double)(1<<30));
419 /****************************************************************************
420 convert a time_t to a NTTIME structure
422 this is an absolute version of the one above.
423 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
424 if the nttime_t was 5 seconds, the NTTIME is 5 seconds. JFM
425 ****************************************************************************/
426 void unix_to_nt_time_abs(NTTIME
*nt
, time_t t
)
436 if (t
== TIME_T_MAX
) {
437 nt
->low
= 0xffffffff;
438 nt
->high
= 0x7fffffff;
443 /* that's what NT uses for infinite */
445 nt
->high
= 0x80000000;
449 /* this converts GMT to kludge-GMT */
450 t
-= LocTimeDiff(t
) - get_serverzone();
455 nt
->high
= (uint32
)(d
* (1.0/(4.0*(double)(1<<30))));
456 nt
->low
= (uint32
)(d
- ((double)nt
->high
)*4.0*(double)(1<<30));
458 /* convert to a negative value */
464 /****************************************************************************
465 take an NTTIME structure, containing high / low time. convert to unix time.
466 lkclXXXX this may need 2 SIVALs not a memcpy. we'll see...
467 ****************************************************************************/
468 void put_long_date(char *p
,time_t t
)
471 unix_to_nt_time(&nt
, t
);
473 SIVAL(p
, 4, nt
.high
);
476 /****************************************************************************
477 check if it's a null mtime
478 ****************************************************************************/
479 BOOL
null_mtime(time_t mtime
)
481 if (mtime
== 0 || mtime
== 0xFFFFFFFF || mtime
== (time_t)-1)
486 /*******************************************************************
487 create a 16 bit dos packed date
488 ********************************************************************/
489 static uint16
make_dos_date1(struct tm
*t
)
492 ret
= (((unsigned)(t
->tm_mon
+1)) >> 3) | ((t
->tm_year
-80) << 1);
493 ret
= ((ret
&0xFF)<<8) | (t
->tm_mday
| (((t
->tm_mon
+1) & 0x7) << 5));
497 /*******************************************************************
498 create a 16 bit dos packed time
499 ********************************************************************/
500 static uint16
make_dos_time1(struct tm
*t
)
503 ret
= ((((unsigned)t
->tm_min
>> 3)&0x7) | (((unsigned)t
->tm_hour
) << 3));
504 ret
= ((ret
&0xFF)<<8) | ((t
->tm_sec
/2) | ((t
->tm_min
& 0x7) << 5));
508 /*******************************************************************
509 create a 32 bit dos packed date/time from some parameters
510 This takes a GMT time and returns a packed localtime structure
511 ********************************************************************/
512 static uint32
make_dos_date(time_t unixdate
)
517 t
= LocalTime(&unixdate
);
521 ret
= make_dos_date1(t
);
522 ret
= ((ret
&0xFFFF)<<16) | make_dos_time1(t
);
527 /*******************************************************************
528 put a dos date into a buffer (time/date format)
529 This takes GMT time and puts local time in the buffer
530 ********************************************************************/
531 void put_dos_date(char *buf
,int offset
,time_t unixdate
)
533 uint32 x
= make_dos_date(unixdate
);
537 /*******************************************************************
538 put a dos date into a buffer (date/time format)
539 This takes GMT time and puts local time in the buffer
540 ********************************************************************/
541 void put_dos_date2(char *buf
,int offset
,time_t unixdate
)
543 uint32 x
= make_dos_date(unixdate
);
544 x
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
548 /*******************************************************************
549 put a dos 32 bit "unix like" date into a buffer. This routine takes
550 GMT and converts it to LOCAL time before putting it (most SMBs assume
551 localtime for this sort of date)
552 ********************************************************************/
553 void put_dos_date3(char *buf
,int offset
,time_t unixdate
)
555 if (!null_mtime(unixdate
))
556 unixdate
-= TimeDiff(unixdate
);
557 SIVAL(buf
,offset
,unixdate
);
560 /*******************************************************************
561 interpret a 32 bit dos packed date/time to some parameters
562 ********************************************************************/
563 static void interpret_dos_date(uint32 date
,int *year
,int *month
,int *day
,int *hour
,int *minute
,int *second
)
567 p0
=date
&0xFF; p1
=((date
&0xFF00)>>8)&0xFF;
568 p2
=((date
&0xFF0000)>>16)&0xFF; p3
=((date
&0xFF000000)>>24)&0xFF;
570 *second
= 2*(p0
& 0x1F);
571 *minute
= ((p0
>>5)&0xFF) + ((p1
&0x7)<<3);
572 *hour
= (p1
>>3)&0xFF;
574 *month
= ((p2
>>5)&0xFF) + ((p3
&0x1)<<3) - 1;
575 *year
= ((p3
>>1)&0xFF) + 80;
578 /*******************************************************************
579 create a unix date (int GMT) from a dos date (which is actually in
581 ********************************************************************/
582 time_t make_unix_date(void *date_ptr
)
588 dos_date
= IVAL(date_ptr
,0);
590 if (dos_date
== 0) return(0);
592 interpret_dos_date(dos_date
,&t
.tm_year
,&t
.tm_mon
,
593 &t
.tm_mday
,&t
.tm_hour
,&t
.tm_min
,&t
.tm_sec
);
596 /* mktime() also does the local to GMT time conversion for us */
602 /*******************************************************************
603 like make_unix_date() but the words are reversed
604 ********************************************************************/
605 time_t make_unix_date2(void *date_ptr
)
609 x
= IVAL(date_ptr
,0);
610 x2
= ((x
&0xFFFF)<<16) | ((x
&0xFFFF0000)>>16);
613 return(make_unix_date((void *)&x
));
616 /*******************************************************************
617 create a unix GMT date from a dos date in 32 bit "unix like" format
618 these generally arrive as localtimes, with corresponding DST
619 ******************************************************************/
620 time_t make_unix_date3(void *date_ptr
)
622 time_t t
= (time_t)IVAL(date_ptr
,0);
629 /***************************************************************************
630 return a HTTP/1.0 time string
631 ***************************************************************************/
632 char *http_timestring(time_t t
)
635 struct tm
*tm
= LocalTime(&t
);
638 slprintf(buf
,sizeof(buf
)-1,"%ld seconds since the Epoch",(long)t
);
640 #ifndef HAVE_STRFTIME
641 fstrcpy(buf
, asctime(tm
));
642 if(buf
[strlen(buf
)-1] == '\n')
643 buf
[strlen(buf
)-1] = 0;
644 #else /* !HAVE_STRFTIME */
645 strftime(buf
, sizeof(buf
)-1, "%a, %d %b %Y %H:%M:%S %Z", tm
);
646 #endif /* !HAVE_STRFTIME */
652 /****************************************************************************
653 Return the date and time as a string
654 ****************************************************************************/
656 char *timestring(BOOL hires
)
658 static fstring TimeBuf
;
665 t
= (time_t)tp
.tv_sec
;
674 "%ld.%06ld seconds since the Epoch",
680 "%ld seconds since the Epoch",
686 strftime(TimeBuf
,sizeof(TimeBuf
)-1,"%Y/%m/%d %H:%M:%S",tm
);
687 slprintf(TimeBuf
+strlen(TimeBuf
),
688 sizeof(TimeBuf
)-1 - strlen(TimeBuf
),
692 strftime(TimeBuf
,100,"%Y/%m/%d %H:%M:%S",tm
);
702 fstrcpy(TimeBuf
, asctime(tm
));
709 /****************************************************************************
710 return the best approximation to a 'create time' under UNIX from a stat
712 ****************************************************************************/
714 time_t get_create_time(SMB_STRUCT_STAT
*st
,BOOL fake_dirs
)
718 if(S_ISDIR(st
->st_mode
) && fake_dirs
)
719 return (time_t)315493200L; /* 1/1/1980 */
721 ret
= MIN(st
->st_ctime
, st
->st_mtime
);
722 ret1
= MIN(ret
, st
->st_atime
);
724 if(ret1
!= (time_t)0)
728 * One of ctime, mtime or atime was zero (probably atime).
729 * Just return MIN(ctime, mtime).
734 /****************************************************************************
735 initialise an NTTIME to -1, which means "unknown" or "don't expire"
736 ****************************************************************************/
738 void init_nt_time(NTTIME
*nt
)
740 nt
->high
= 0x7FFFFFFF;
741 nt
->low
= 0xFFFFFFFF;