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;
37 #define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
38 : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
41 #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
44 /*******************************************************************
45 External access to time_t_min and time_t_max.
46 ********************************************************************/
48 time_t get_time_t_min(void)
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 */
134 done_serverzone_init = False;
138 /*******************************************************************
139 return the same value as TimeZone, but it should be more efficient.
141 We keep a table of DST offsets to prevent calling localtime() on each
142 call of this function. This saves a LOT of time on many unixes.
144 Updated by Paul Eggert <eggert@twinsun.com>
145 ********************************************************************/
146 static int TimeZoneFaster(time_t t)
148 static struct dst_table {time_t start,end; int zone;} *tdt, *dst_table = NULL;
149 static int table_size = 0;
153 if (t == 0) t = time(NULL);
155 /* Tunis has a 8 day DST region, we need to be careful ... */
156 #define MAX_DST_WIDTH (365*24*60*60)
157 #define MAX_DST_SKIP (7*24*60*60)
159 for (i=0;i<table_size;i++)
160 if (t >= dst_table[i].start && t <= dst_table[i].end) break;
163 zone = dst_table[i].zone;
168 tdt = (struct dst_table *)Realloc(dst_table,
169 sizeof(dst_table[0])*(i+1));
171 DEBUG(0,("TimeZoneFaster: out of memory!\n"));
172 SAFE_FREE(dst_table);
178 dst_table[i].zone = zone;
179 dst_table[i].start = dst_table[i].end = t;
181 /* no entry will cover more than 6 months */
182 low = t - MAX_DST_WIDTH/2;
186 high = t + MAX_DST_WIDTH/2;
190 /* widen the new entry using two bisection searches */
191 while (low+60*60 < dst_table[i].start) {
192 if (dst_table[i].start - low > MAX_DST_SKIP*2)
193 t = dst_table[i].start - MAX_DST_SKIP;
195 t = low + (dst_table[i].start-low)/2;
196 if (TimeZone(t) == zone)
197 dst_table[i].start = t;
202 while (high-60*60 > dst_table[i].end) {
203 if (high - dst_table[i].end > MAX_DST_SKIP*2)
204 t = dst_table[i].end + MAX_DST_SKIP;
206 t = high - (high-dst_table[i].end)/2;
207 if (TimeZone(t) == zone)
208 dst_table[i].end = t;
213 DEBUG(1,("Added DST entry from %s ",
214 asctime(localtime(&dst_table[i].start))));
215 DEBUG(1,("to %s (%d)\n",asctime(localtime(&dst_table[i].end)),
223 /****************************************************************************
224 return the UTC offset in seconds west of UTC, adjusted for extra time offset
225 **************************************************************************/
226 int TimeDiff(time_t t)
228 return TimeZoneFaster(t) + 60*extra_time_offset;
232 /****************************************************************************
233 return the UTC offset in seconds west of UTC, adjusted for extra time
234 offset, for a local time value. If ut = lt + LocTimeDiff(lt), then
235 lt = ut - TimeDiff(ut), but the converse does not necessarily hold near
236 daylight savings transitions because some local times are ambiguous.
237 LocTimeDiff(t) equals TimeDiff(t) except near daylight savings transitions.
238 +**************************************************************************/
239 static int LocTimeDiff(time_t lte)
241 time_t lt = lte - 60*extra_time_offset;
242 int d = TimeZoneFaster(lt);
245 /* if overflow occurred, ignore all the adjustments so far */
246 if (((lte < lt) ^ (extra_time_offset < 0)) | ((t < lt) ^ (d < 0)))
249 /* now t should be close enough to the true UTC to yield the right answer */
254 /****************************************************************************
255 try to optimise the localtime call, it can be quite expensive on some machines
256 ****************************************************************************/
257 struct tm *LocalTime(time_t *t)
266 #define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
268 /****************************************************************************
269 interpret an 8 byte "filetime" structure to a time_t
270 It's originally in "100ns units since jan 1st 1601"
272 It appears to be kludge-GMT (at least for file listings). This means
273 its the GMT you get by taking a localtime and adding the
274 serverzone. This is NOT the same as GMT in some cases. This routine
275 converts this to real GMT.
276 ****************************************************************************/
277 time_t nt_time_to_unix(NTTIME *nt)
281 /* The next two lines are a fix needed for the
282 broken SCO compiler. JRA. */
283 time_t l_time_min = TIME_T_MIN;
284 time_t l_time_max = TIME_T_MAX;
286 if (nt->high == 0) return(0);
288 d = ((double)nt->high)*4.0*(double)(1<<30);
289 d += (nt->low&0xFFF00000);
292 /* now adjust by 369 years to make the secs since 1970 */
293 d -= TIME_FIXUP_CONSTANT;
295 if (!(l_time_min <= d && d <= l_time_max))
298 ret = (time_t)(d+0.5);
300 /* this takes us from kludge-GMT to real GMT */
301 ret -= get_serverzone();
302 ret += LocTimeDiff(ret);
309 /****************************************************************************
310 interprets an nt time into a unix time_t
311 ****************************************************************************/
312 time_t interpret_long_date(char *p)
317 return nt_time_to_unix(&nt);
320 /****************************************************************************
321 put a 8 byte filetime from a time_t
322 This takes real GMT as input and converts to kludge-GMT
323 ****************************************************************************/
324 void unix_to_nt_time(NTTIME *nt, time_t t)
336 nt->low = 0xffffffff;
337 nt->high = 0x7fffffff;
342 nt->low = 0xffffffff;
343 nt->high = 0xffffffff;
347 /* this converts GMT to kludge-GMT */
348 t -= LocTimeDiff(t) - get_serverzone();
351 d += TIME_FIXUP_CONSTANT;
354 nt->high = (uint32)(d * (1.0/(4.0*(double)(1<<30))));
355 nt->low = (uint32)(d - ((double)nt->high)*4.0*(double)(1<<30));
359 /****************************************************************************
360 take an NTTIME structure, containing high / low time. convert to unix time.
361 lkclXXXX this may need 2 SIVALs not a memcpy. we'll see...
362 ****************************************************************************/
363 void put_long_date(char *p,time_t t)
366 unix_to_nt_time(&nt, t);
368 SIVAL(p, 4, nt.high);
371 /****************************************************************************
372 check if it's a null mtime
373 ****************************************************************************/
374 BOOL null_mtime(time_t mtime)
376 if (mtime == 0 || mtime == 0xFFFFFFFF || mtime == (time_t)-1)
381 /*******************************************************************
382 create a 16 bit dos packed date
383 ********************************************************************/
384 static uint16 make_dos_date1(struct tm *t)
387 ret = (((unsigned)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
388 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
392 /*******************************************************************
393 create a 16 bit dos packed time
394 ********************************************************************/
395 static uint16 make_dos_time1(struct tm *t)
398 ret = ((((unsigned)t->tm_min >> 3)&0x7) | (((unsigned)t->tm_hour) << 3));
399 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
403 /*******************************************************************
404 create a 32 bit dos packed date/time from some parameters
405 This takes a GMT time and returns a packed localtime structure
406 ********************************************************************/
407 static uint32 make_dos_date(time_t unixdate)
412 t = LocalTime(&unixdate);
416 ret = make_dos_date1(t);
417 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
422 /*******************************************************************
423 put a dos date into a buffer (time/date format)
424 This takes GMT time and puts local time in the buffer
425 ********************************************************************/
426 void put_dos_date(char *buf,int offset,time_t unixdate)
428 uint32 x = make_dos_date(unixdate);
432 /*******************************************************************
433 put a dos date into a buffer (date/time format)
434 This takes GMT time and puts local time in the buffer
435 ********************************************************************/
436 void put_dos_date2(char *buf,int offset,time_t unixdate)
438 uint32 x = make_dos_date(unixdate);
439 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
443 /*******************************************************************
444 put a dos 32 bit "unix like" date into a buffer. This routine takes
445 GMT and converts it to LOCAL time before putting it (most SMBs assume
446 localtime for this sort of date)
447 ********************************************************************/
448 void put_dos_date3(char *buf,int offset,time_t unixdate)
450 if (!null_mtime(unixdate))
451 unixdate -= TimeDiff(unixdate);
452 SIVAL(buf,offset,unixdate);
455 /*******************************************************************
456 interpret a 32 bit dos packed date/time to some parameters
457 ********************************************************************/
458 static void interpret_dos_date(uint32 date,int *year,int *month,int *day,int *hour,int *minute,int *second)
462 p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
463 p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
465 *second = 2*(p0 & 0x1F);
466 *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
467 *hour = (p1>>3)&0xFF;
469 *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
470 *year = ((p3>>1)&0xFF) + 80;
473 /*******************************************************************
474 create a unix date (int GMT) from a dos date (which is actually in
476 ********************************************************************/
477 time_t make_unix_date(void *date_ptr)
483 dos_date = IVAL(date_ptr,0);
485 if (dos_date == 0) return(0);
487 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
488 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
491 /* mktime() also does the local to GMT time conversion for us */
497 /*******************************************************************
498 like make_unix_date() but the words are reversed
499 ********************************************************************/
500 time_t make_unix_date2(void *date_ptr)
504 x = IVAL(date_ptr,0);
505 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
508 return(make_unix_date((void *)&x));
511 /*******************************************************************
512 create a unix GMT date from a dos date in 32 bit "unix like" format
513 these generally arrive as localtimes, with corresponding DST
514 ******************************************************************/
515 time_t make_unix_date3(void *date_ptr)
517 time_t t = (time_t)IVAL(date_ptr,0);
524 /***************************************************************************
525 return a HTTP/1.0 time string
526 ***************************************************************************/
527 char *http_timestring(time_t t)
530 struct tm *tm = LocalTime(&t);
533 slprintf(buf,sizeof(buf)-1,"%ld seconds since the Epoch",(long)t);
535 #ifndef HAVE_STRFTIME
536 fstrcpy(buf, asctime(tm));
537 if(buf[strlen(buf)-1] == '\n')
538 buf[strlen(buf)-1] = 0;
539 #else /* !HAVE_STRFTIME */
540 strftime(buf, sizeof(buf)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
541 #endif /* !HAVE_STRFTIME */
547 /****************************************************************************
548 Return the date and time as a string
549 ****************************************************************************/
551 char *timestring(BOOL hires)
553 static fstring TimeBuf;
560 t = (time_t)tp.tv_sec;
569 "%ld.%06ld seconds since the Epoch",
575 "%ld seconds since the Epoch",
581 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
582 slprintf(TimeBuf+strlen(TimeBuf),
583 sizeof(TimeBuf)-1 - strlen(TimeBuf),
587 strftime(TimeBuf,100,"%Y/%m/%d %H:%M:%S",tm);
597 fstrcpy(TimeBuf, asctime(tm));
604 /****************************************************************************
605 return the best approximation to a 'create time' under UNIX from a stat
607 ****************************************************************************/
609 time_t get_create_time(SMB_STRUCT_STAT *st,BOOL fake_dirs)
613 if(S_ISDIR(st->st_mode) && fake_dirs)
614 return (time_t)315493200L; /* 1/1/1980 */
616 ret = MIN(st->st_ctime, st->st_mtime);
617 ret1 = MIN(ret, st->st_atime);
619 if(ret1 != (time_t)0)
623 * One of ctime, mtime or atime was zero (probably atime).
624 * Just return MIN(ctime, mtime).
629 /****************************************************************************
630 initialise an NTTIME to -1, which means "unknown" or "don't expire"
631 ****************************************************************************/
633 void init_nt_time(NTTIME *nt)
635 nt->high = 0x7FFFFFFF;
636 nt->low = 0xFFFFFFFF;