Makefile: Added AIX 3.2.5.
[samba.git] / source / lib / time.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    time handling functions
5    Copyright (C) Andrew Tridgell 1992-1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 /*
25   This stuff was largely rewritten by Paul Eggert <eggert@twinsun.com>
26   in May 1996 
27   */
28
29
30 int serverzone=0;
31 int extra_time_offset = 0;
32
33 extern int DEBUGLEVEL;
34
35 #ifndef CHAR_BIT
36 #define CHAR_BIT 8
37 #endif
38
39 #ifndef TIME_T_MIN
40 #define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
41                     : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
42 #endif
43 #ifndef TIME_T_MAX
44 #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
45 #endif
46
47
48
49 /*******************************************************************
50 a gettimeofday wrapper
51 ********************************************************************/
52 void GetTimeOfDay(struct timeval *tval)
53 {
54 #ifdef GETTIMEOFDAY1
55   gettimeofday(tval);
56 #else
57   gettimeofday(tval,NULL);
58 #endif
59 }
60
61 #define TM_YEAR_BASE 1900
62
63 /*******************************************************************
64 yield the difference between *A and *B, in seconds, ignoring leap seconds
65 ********************************************************************/
66 static int tm_diff(struct tm *a, struct tm *b)
67 {
68   int ay = a->tm_year + (TM_YEAR_BASE - 1);
69   int by = b->tm_year + (TM_YEAR_BASE - 1);
70   int intervening_leap_days =
71     (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
72   int years = ay - by;
73   int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
74   int hours = 24*days + (a->tm_hour - b->tm_hour);
75   int minutes = 60*hours + (a->tm_min - b->tm_min);
76   int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
77
78   return seconds;
79 }
80
81 /*******************************************************************
82   return the UTC offset in seconds west of UTC
83   ******************************************************************/
84 static int TimeZone(time_t t)
85 {
86   struct tm tm_utc = *(gmtime(&t));
87   return tm_diff(&tm_utc,localtime(&t));
88 }
89
90
91 /*******************************************************************
92 init the time differences
93 ********************************************************************/
94 void TimeInit(void)
95 {
96   serverzone = TimeZone(time(NULL));
97
98   if ((serverzone % 60) != 0) {
99           DEBUG(1,("WARNING: Your timezone is not a multiple of 1 minute.\n"));
100   }
101
102   DEBUG(4,("Serverzone is %d\n",serverzone));
103 }
104
105
106 /*******************************************************************
107 return the same value as TimeZone, but it should be more efficient.
108
109 We keep a table of DST offsets to prevent calling localtime() on each 
110 call of this function. This saves a LOT of time on many unixes.
111
112 Updated by Paul Eggert <eggert@twinsun.com>
113 ********************************************************************/
114 static int TimeZoneFaster(time_t t)
115 {
116   static struct dst_table {time_t start,end; int zone;} *dst_table = NULL;
117   static int table_size = 0;
118   int i;
119   int zone = 0;
120
121   if (t == 0) t = time(NULL);
122
123   /* Tunis has a 8 day DST region, we need to be careful ... */
124 #define MAX_DST_WIDTH (365*24*60*60)
125 #define MAX_DST_SKIP (7*24*60*60)
126
127   for (i=0;i<table_size;i++)
128     if (t >= dst_table[i].start && t <= dst_table[i].end) break;
129
130   if (i<table_size) {
131     zone = dst_table[i].zone;
132   } else {
133     time_t low,high;
134
135     zone = TimeZone(t);
136     dst_table = (struct dst_table *)Realloc(dst_table,
137                                               sizeof(dst_table[0])*(i+1));
138     if (!dst_table) {
139       table_size = 0;
140     } else {
141       table_size++;
142
143       dst_table[i].zone = zone; 
144       dst_table[i].start = dst_table[i].end = t;
145     
146       /* no entry will cover more than 6 months */
147       low = t - MAX_DST_WIDTH/2;
148       if (t < low)
149         low = TIME_T_MIN;
150       
151       high = t + MAX_DST_WIDTH/2;
152       if (high < t)
153         high = TIME_T_MAX;
154       
155       /* widen the new entry using two bisection searches */
156       while (low+60*60 < dst_table[i].start) {
157         if (dst_table[i].start - low > MAX_DST_SKIP*2)
158           t = dst_table[i].start - MAX_DST_SKIP;
159         else
160           t = low + (dst_table[i].start-low)/2;
161         if (TimeZone(t) == zone)
162           dst_table[i].start = t;
163         else
164           low = t;
165       }
166
167       while (high-60*60 > dst_table[i].end) {
168         if (high - dst_table[i].end > MAX_DST_SKIP*2)
169           t = dst_table[i].end + MAX_DST_SKIP;
170         else
171           t = high - (high-dst_table[i].end)/2;
172         if (TimeZone(t) == zone)
173           dst_table[i].end = t;
174         else
175           high = t;
176       }
177 #if 0
178       DEBUG(1,("Added DST entry from %s ",
179                asctime(localtime(&dst_table[i].start))));
180       DEBUG(1,("to %s (%d)\n",asctime(localtime(&dst_table[i].end)),
181                dst_table[i].zone));
182 #endif
183     }
184   }
185   return zone;
186 }
187
188 /****************************************************************************
189   return the UTC offset in seconds west of UTC, adjusted for extra time offset
190   **************************************************************************/
191 int TimeDiff(time_t t)
192 {
193   return TimeZoneFaster(t) + 60*extra_time_offset;
194 }
195
196
197 /****************************************************************************
198   return the UTC offset in seconds west of UTC, adjusted for extra time
199   offset, for a local time value.  If ut = lt + LocTimeDiff(lt), then
200   lt = ut - TimeDiff(ut), but the converse does not necessarily hold near
201   daylight savings transitions because some local times are ambiguous.
202   LocTimeDiff(t) equals TimeDiff(t) except near daylight savings transitions.
203   +**************************************************************************/
204 static int LocTimeDiff(time_t lte)
205 {
206   time_t lt = lte - 60*extra_time_offset;
207   int d = TimeZoneFaster(lt);
208   time_t t = lt + d;
209
210   /* if overflow occurred, ignore all the adjustments so far */
211   if (((lte < lt) ^ (extra_time_offset < 0))  |  ((t < lt) ^ (d < 0)))
212     t = lte;
213
214   /* now t should be close enough to the true UTC to yield the right answer */
215   return TimeDiff(t);
216 }
217
218
219 /****************************************************************************
220 try to optimise the localtime call, it can be quite expenive on some machines
221 ****************************************************************************/
222 struct tm *LocalTime(time_t *t)
223 {
224   time_t t2 = *t;
225
226   t2 -= TimeDiff(t2);
227
228   return(gmtime(&t2));
229 }
230
231
232 #define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
233
234 /****************************************************************************
235 interpret an 8 byte "filetime" structure to a time_t
236 It's originally in "100ns units since jan 1st 1601"
237
238 It appears to be kludge-GMT (at least for file listings). This means
239 its the GMT you get by taking a localtime and adding the
240 serverzone. This is NOT the same as GMT in some cases. This routine
241 converts this to real GMT.
242 ****************************************************************************/
243 time_t interpret_long_date(char *p)
244 {
245   double d;
246   time_t ret;
247   uint32 tlow,thigh;
248   tlow = IVAL(p,0);
249   thigh = IVAL(p,4);
250
251   if (thigh == 0) return(0);
252
253   d = ((double)thigh)*4.0*(double)(1<<30);
254   d += (tlow&0xFFF00000);
255   d *= 1.0e-7;
256  
257   /* now adjust by 369 years to make the secs since 1970 */
258   d -= TIME_FIXUP_CONSTANT;
259
260   if (!(TIME_T_MIN <= d && d <= TIME_T_MAX))
261     return(0);
262
263   ret = (time_t)(d+0.5);
264
265   /* this takes us from kludge-GMT to real GMT */
266   ret -= serverzone;
267   ret += LocTimeDiff(ret);
268
269   return(ret);
270 }
271
272
273 /****************************************************************************
274 put a 8 byte filetime from a time_t
275 This takes real GMT as input and converts to kludge-GMT
276 ****************************************************************************/
277 void put_long_date(char *p,time_t t)
278 {
279   uint32 tlow,thigh;
280   double d;
281
282   if (t==0) {
283     SIVAL(p,0,0); SIVAL(p,4,0);
284     return;
285   }
286
287   /* this converts GMT to kludge-GMT */
288   t -= LocTimeDiff(t) - serverzone; 
289
290   d = (double) (t);
291
292   d += TIME_FIXUP_CONSTANT;
293
294   d *= 1.0e7;
295
296   thigh = (uint32)(d * (1.0/(4.0*(double)(1<<30))));
297   tlow = (uint32)(d - ((double)thigh)*4.0*(double)(1<<30));
298
299   SIVAL(p,0,tlow);
300   SIVAL(p,4,thigh);
301 }
302
303
304 /****************************************************************************
305 check if it's a null mtime
306 ****************************************************************************/
307 BOOL null_mtime(time_t mtime)
308 {
309   if (mtime == 0 || mtime == 0xFFFFFFFF || mtime == (time_t)-1)
310     return(True);
311   return(False);
312 }
313
314 /*******************************************************************
315   create a 16 bit dos packed date
316 ********************************************************************/
317 static uint16 make_dos_date1(time_t unixdate,struct tm *t)
318 {
319   uint16 ret=0;
320   ret = (((unsigned)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
321   ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
322   return(ret);
323 }
324
325 /*******************************************************************
326   create a 16 bit dos packed time
327 ********************************************************************/
328 static uint16 make_dos_time1(time_t unixdate,struct tm *t)
329 {
330   uint16 ret=0;
331   ret = ((((unsigned)t->tm_min >> 3)&0x7) | (((unsigned)t->tm_hour) << 3));
332   ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
333   return(ret);
334 }
335
336 /*******************************************************************
337   create a 32 bit dos packed date/time from some parameters
338   This takes a GMT time and returns a packed localtime structure
339 ********************************************************************/
340 static uint32 make_dos_date(time_t unixdate)
341 {
342   struct tm *t;
343   uint32 ret=0;
344
345   t = LocalTime(&unixdate);
346
347   ret = make_dos_date1(unixdate,t);
348   ret = ((ret&0xFFFF)<<16) | make_dos_time1(unixdate,t);
349
350   return(ret);
351 }
352
353 /*******************************************************************
354 put a dos date into a buffer (time/date format)
355 This takes GMT time and puts local time in the buffer
356 ********************************************************************/
357 void put_dos_date(char *buf,int offset,time_t unixdate)
358 {
359   uint32 x = make_dos_date(unixdate);
360   SIVAL(buf,offset,x);
361 }
362
363 /*******************************************************************
364 put a dos date into a buffer (date/time format)
365 This takes GMT time and puts local time in the buffer
366 ********************************************************************/
367 void put_dos_date2(char *buf,int offset,time_t unixdate)
368 {
369   uint32 x = make_dos_date(unixdate);
370   x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
371   SIVAL(buf,offset,x);
372 }
373
374 /*******************************************************************
375 put a dos 32 bit "unix like" date into a buffer. This routine takes
376 GMT and converts it to LOCAL time before putting it (most SMBs assume
377 localtime for this sort of date)
378 ********************************************************************/
379 void put_dos_date3(char *buf,int offset,time_t unixdate)
380 {
381   if (!null_mtime(unixdate))
382     unixdate -= TimeDiff(unixdate);
383   SIVAL(buf,offset,unixdate);
384 }
385
386 /*******************************************************************
387   interpret a 32 bit dos packed date/time to some parameters
388 ********************************************************************/
389 static void interpret_dos_date(uint32 date,int *year,int *month,int *day,int *hour,int *minute,int *second)
390 {
391   uint32 p0,p1,p2,p3;
392
393   p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF; 
394   p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
395
396   *second = 2*(p0 & 0x1F);
397   *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
398   *hour = (p1>>3)&0xFF;
399   *day = (p2&0x1F);
400   *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
401   *year = ((p3>>1)&0xFF) + 80;
402 }
403
404 /*******************************************************************
405   create a unix date (int GMT) from a dos date (which is actually in
406   localtime)
407 ********************************************************************/
408 time_t make_unix_date(void *date_ptr)
409 {
410   uint32 dos_date=0;
411   struct tm t;
412   time_t ret;
413
414   dos_date = IVAL(date_ptr,0);
415
416   if (dos_date == 0) return(0);
417   
418   interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
419                      &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
420   t.tm_isdst = -1;
421   
422   /* mktime() also does the local to GMT time conversion for us */
423   ret = mktime(&t);
424
425   return(ret);
426 }
427
428 /*******************************************************************
429 like make_unix_date() but the words are reversed
430 ********************************************************************/
431 time_t make_unix_date2(void *date_ptr)
432 {
433   uint32 x,x2;
434
435   x = IVAL(date_ptr,0);
436   x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
437   SIVAL(&x,0,x2);
438
439   return(make_unix_date((void *)&x));
440 }
441
442 /*******************************************************************
443   create a unix GMT date from a dos date in 32 bit "unix like" format
444   these generally arrive as localtimes, with corresponding DST
445   ******************************************************************/
446 time_t make_unix_date3(void *date_ptr)
447 {
448   time_t t = IVAL(date_ptr,0);
449   if (!null_mtime(t))
450     t += LocTimeDiff(t);
451   return(t);
452 }
453
454 /****************************************************************************
455   return the date and time as a string
456 ****************************************************************************/
457 char *timestring(void )
458 {
459   static fstring TimeBuf;
460   time_t t = time(NULL);
461   struct tm *tm = LocalTime(&t);
462
463 #ifdef NO_STRFTIME
464   fstrcpy(TimeBuf, asctime(tm));
465 #elif defined(CLIX) || defined(CONVEX)
466   strftime(TimeBuf,100,"%m/%d/%Y %I:%M:%S %p",tm);
467 #elif defined(AMPM)
468   strftime(TimeBuf,100,"%m/%d/%Y %r",tm);
469 #elif defined(TZ_TIME)
470   {
471     int zone = TimeDiff(t);
472     int absZoneMinutes = (zone<0 ? -zone : zone) / 60;
473     size_t len = strftime(TimeBuf,sizeof(TimeBuf)-6,"%m/%d/%Y %T",tm);
474     sprintf(TimeBuf+len," %c%02d%02d",
475             zone<0?'+':'-',absZoneMinutes/60,absZoneMinutes%60);
476   }
477 #else
478   strftime(TimeBuf,100,"%m/%d/%Y %T",tm);
479 #endif
480   return(TimeBuf);
481 }
482
483 /****************************************************************************
484   return the best approximation to a 'create time' under UNIX from a stat
485   structure.
486 ****************************************************************************/
487
488 time_t get_create_time(struct stat *st)
489 {
490   time_t ret = MIN(st->st_ctime, st->st_mtime);
491   time_t ret1 = MIN(ret, st->st_atime);
492
493   if(ret1 != (time_t)0)
494     return ret1;
495
496   /*
497    * One of ctime, mtime or atime was zero (probably atime).
498    * Just return MIN(ctime, mtime).
499    */
500   return ret;
501 }
502
503 /****************************************************************************
504   return the 'access time' under UNIX from a stat structure. 
505   This function exists to allow modifications to be done depending
506   on what we want to return. Just return the normal atime (for now).
507 ****************************************************************************/
508     
509 time_t get_access_time(struct stat *st)
510 {
511   if (lp_win95_bug_compatibility())
512     return st->st_mtime;
513   else
514     return st->st_atime;
515 }