rpcclient registry commands.
[ira/wip.git] / source3 / 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 HAVE_GETTIMEOFDAY_TZ
55         gettimeofday(tval,NULL);
56 #else
57         gettimeofday(tval);
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, or 0 if it cannot be determined
83   ******************************************************************/
84 static int TimeZone(time_t t)
85 {
86   struct tm *tm = gmtime(&t);
87   struct tm tm_utc;
88   if (!tm)
89     return 0;
90   tm_utc = *tm;
91   tm = localtime(&t);
92   if (!tm)
93     return 0;
94   return tm_diff(&tm_utc,tm);
95
96 }
97
98
99 /*******************************************************************
100 init the time differences
101 ********************************************************************/
102 void TimeInit(void)
103 {
104   serverzone = TimeZone(time(NULL));
105
106   if ((serverzone % 60) != 0) {
107           DEBUG(1,("WARNING: Your timezone is not a multiple of 1 minute.\n"));
108   }
109
110   DEBUG(4,("Serverzone is %d\n",serverzone));
111 }
112
113
114 /*******************************************************************
115 return the same value as TimeZone, but it should be more efficient.
116
117 We keep a table of DST offsets to prevent calling localtime() on each 
118 call of this function. This saves a LOT of time on many unixes.
119
120 Updated by Paul Eggert <eggert@twinsun.com>
121 ********************************************************************/
122 static int TimeZoneFaster(time_t t)
123 {
124   static struct dst_table {time_t start,end; int zone;} *dst_table = NULL;
125   static int table_size = 0;
126   int i;
127   int zone = 0;
128
129   if (t == 0) t = time(NULL);
130
131   /* Tunis has a 8 day DST region, we need to be careful ... */
132 #define MAX_DST_WIDTH (365*24*60*60)
133 #define MAX_DST_SKIP (7*24*60*60)
134
135   for (i=0;i<table_size;i++)
136     if (t >= dst_table[i].start && t <= dst_table[i].end) break;
137
138   if (i<table_size) {
139     zone = dst_table[i].zone;
140   } else {
141     time_t low,high;
142
143     zone = TimeZone(t);
144     dst_table = (struct dst_table *)Realloc(dst_table,
145                                               sizeof(dst_table[0])*(i+1));
146     if (!dst_table) {
147       table_size = 0;
148     } else {
149       table_size++;
150
151       dst_table[i].zone = zone; 
152       dst_table[i].start = dst_table[i].end = t;
153     
154       /* no entry will cover more than 6 months */
155       low = t - MAX_DST_WIDTH/2;
156       if (t < low)
157         low = TIME_T_MIN;
158       
159       high = t + MAX_DST_WIDTH/2;
160       if (high < t)
161         high = TIME_T_MAX;
162       
163       /* widen the new entry using two bisection searches */
164       while (low+60*60 < dst_table[i].start) {
165         if (dst_table[i].start - low > MAX_DST_SKIP*2)
166           t = dst_table[i].start - MAX_DST_SKIP;
167         else
168           t = low + (dst_table[i].start-low)/2;
169         if (TimeZone(t) == zone)
170           dst_table[i].start = t;
171         else
172           low = t;
173       }
174
175       while (high-60*60 > dst_table[i].end) {
176         if (high - dst_table[i].end > MAX_DST_SKIP*2)
177           t = dst_table[i].end + MAX_DST_SKIP;
178         else
179           t = high - (high-dst_table[i].end)/2;
180         if (TimeZone(t) == zone)
181           dst_table[i].end = t;
182         else
183           high = t;
184       }
185 #if 0
186       DEBUG(1,("Added DST entry from %s ",
187                asctime(localtime(&dst_table[i].start))));
188       DEBUG(1,("to %s (%d)\n",asctime(localtime(&dst_table[i].end)),
189                dst_table[i].zone));
190 #endif
191     }
192   }
193   return zone;
194 }
195
196 /****************************************************************************
197   return the UTC offset in seconds west of UTC, adjusted for extra time offset
198   **************************************************************************/
199 int TimeDiff(time_t t)
200 {
201   return TimeZoneFaster(t) + 60*extra_time_offset;
202 }
203
204
205 /****************************************************************************
206   return the UTC offset in seconds west of UTC, adjusted for extra time
207   offset, for a local time value.  If ut = lt + LocTimeDiff(lt), then
208   lt = ut - TimeDiff(ut), but the converse does not necessarily hold near
209   daylight savings transitions because some local times are ambiguous.
210   LocTimeDiff(t) equals TimeDiff(t) except near daylight savings transitions.
211   +**************************************************************************/
212 static int LocTimeDiff(time_t lte)
213 {
214   time_t lt = lte - 60*extra_time_offset;
215   int d = TimeZoneFaster(lt);
216   time_t t = lt + d;
217
218   /* if overflow occurred, ignore all the adjustments so far */
219   if (((lte < lt) ^ (extra_time_offset < 0))  |  ((t < lt) ^ (d < 0)))
220     t = lte;
221
222   /* now t should be close enough to the true UTC to yield the right answer */
223   return TimeDiff(t);
224 }
225
226
227 /****************************************************************************
228 try to optimise the localtime call, it can be quite expensive on some machines
229 ****************************************************************************/
230 struct tm *LocalTime(time_t *t)
231 {
232   time_t t2 = *t;
233
234   t2 -= TimeDiff(t2);
235
236   return(gmtime(&t2));
237 }
238
239 #define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
240
241 /****************************************************************************
242 interpret an 8 byte "filetime" structure to a time_t
243 It's originally in "100ns units since jan 1st 1601"
244
245 It appears to be kludge-GMT (at least for file listings). This means
246 its the GMT you get by taking a localtime and adding the
247 serverzone. This is NOT the same as GMT in some cases. This routine
248 converts this to real GMT.
249 ****************************************************************************/
250 time_t nt_time_to_unix(NTTIME *nt)
251 {
252   double d;
253   time_t ret;
254   /* The next two lines are a fix needed for the 
255      broken SCO compiler. JRA. */
256   time_t l_time_min = TIME_T_MIN;
257   time_t l_time_max = TIME_T_MAX;
258
259   if (nt->high == 0) return(0);
260
261   d = ((double)nt->high)*4.0*(double)(1<<30);
262   d += (nt->low&0xFFF00000);
263   d *= 1.0e-7;
264  
265   /* now adjust by 369 years to make the secs since 1970 */
266   d -= TIME_FIXUP_CONSTANT;
267
268   if (!(l_time_min <= d && d <= l_time_max))
269     return(0);
270
271   ret = (time_t)(d+0.5);
272
273   /* this takes us from kludge-GMT to real GMT */
274   ret -= serverzone;
275   ret += LocTimeDiff(ret);
276
277   return(ret);
278 }
279
280
281
282 /****************************************************************************
283 interprets an nt time into a unix time_t
284 ****************************************************************************/
285 time_t interpret_long_date(char *p)
286 {
287         NTTIME nt;
288         nt.low = IVAL(p,0);
289         nt.high = IVAL(p,4);
290         return nt_time_to_unix(&nt);
291 }
292
293 /****************************************************************************
294 put a 8 byte filetime from a time_t
295 This takes real GMT as input and converts to kludge-GMT
296 ****************************************************************************/
297 void unix_to_nt_time(NTTIME *nt, time_t t)
298 {
299         double d;
300
301         if (t==0)
302         {
303                 nt->low = 0;
304                 nt->high = 0;
305                 return;
306         }
307
308         /* this converts GMT to kludge-GMT */
309         t -= LocTimeDiff(t) - serverzone; 
310
311         d = (double)(t);
312         d += TIME_FIXUP_CONSTANT;
313         d *= 1.0e7;
314
315         nt->high = (uint32)(d * (1.0/(4.0*(double)(1<<30))));
316         nt->low  = (uint32)(d - ((double)nt->high)*4.0*(double)(1<<30));
317 }
318
319
320 /****************************************************************************
321 take an NTTIME structure, containing high / low time.  convert to unix time.
322 lkclXXXX this may need 2 SIVALs not a memcpy.  we'll see...
323 ****************************************************************************/
324 void put_long_date(char *p,time_t t)
325 {
326         NTTIME nt;
327         unix_to_nt_time(&nt, t);
328         SIVAL(p, 0, nt.low);
329         SIVAL(p, 4, nt.high);
330 }
331
332 /****************************************************************************
333 check if it's a null mtime
334 ****************************************************************************/
335 BOOL null_mtime(time_t mtime)
336 {
337   if (mtime == 0 || mtime == 0xFFFFFFFF || mtime == (time_t)-1)
338     return(True);
339   return(False);
340 }
341
342 /*******************************************************************
343   create a 16 bit dos packed date
344 ********************************************************************/
345 static uint16 make_dos_date1(struct tm *t)
346 {
347   uint16 ret=0;
348   ret = (((unsigned)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
349   ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
350   return(ret);
351 }
352
353 /*******************************************************************
354   create a 16 bit dos packed time
355 ********************************************************************/
356 static uint16 make_dos_time1(struct tm *t)
357 {
358   uint16 ret=0;
359   ret = ((((unsigned)t->tm_min >> 3)&0x7) | (((unsigned)t->tm_hour) << 3));
360   ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
361   return(ret);
362 }
363
364 /*******************************************************************
365   create a 32 bit dos packed date/time from some parameters
366   This takes a GMT time and returns a packed localtime structure
367 ********************************************************************/
368 static uint32 make_dos_date(time_t unixdate)
369 {
370   struct tm *t;
371   uint32 ret=0;
372
373   t = LocalTime(&unixdate);
374   if (!t)
375     return 0xFFFFFFFF;
376
377   ret = make_dos_date1(t);
378   ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
379
380   return(ret);
381 }
382
383 /*******************************************************************
384 put a dos date into a buffer (time/date format)
385 This takes GMT time and puts local time in the buffer
386 ********************************************************************/
387 void put_dos_date(char *buf,int offset,time_t unixdate)
388 {
389   uint32 x = make_dos_date(unixdate);
390   SIVAL(buf,offset,x);
391 }
392
393 /*******************************************************************
394 put a dos date into a buffer (date/time format)
395 This takes GMT time and puts local time in the buffer
396 ********************************************************************/
397 void put_dos_date2(char *buf,int offset,time_t unixdate)
398 {
399   uint32 x = make_dos_date(unixdate);
400   x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
401   SIVAL(buf,offset,x);
402 }
403
404 /*******************************************************************
405 put a dos 32 bit "unix like" date into a buffer. This routine takes
406 GMT and converts it to LOCAL time before putting it (most SMBs assume
407 localtime for this sort of date)
408 ********************************************************************/
409 void put_dos_date3(char *buf,int offset,time_t unixdate)
410 {
411   if (!null_mtime(unixdate))
412     unixdate -= TimeDiff(unixdate);
413   SIVAL(buf,offset,unixdate);
414 }
415
416 /*******************************************************************
417   interpret a 32 bit dos packed date/time to some parameters
418 ********************************************************************/
419 static void interpret_dos_date(uint32 date,int *year,int *month,int *day,int *hour,int *minute,int *second)
420 {
421   uint32 p0,p1,p2,p3;
422
423   p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF; 
424   p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
425
426   *second = 2*(p0 & 0x1F);
427   *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
428   *hour = (p1>>3)&0xFF;
429   *day = (p2&0x1F);
430   *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
431   *year = ((p3>>1)&0xFF) + 80;
432 }
433
434 /*******************************************************************
435   create a unix date (int GMT) from a dos date (which is actually in
436   localtime)
437 ********************************************************************/
438 time_t make_unix_date(void *date_ptr)
439 {
440   uint32 dos_date=0;
441   struct tm t;
442   time_t ret;
443
444   dos_date = IVAL(date_ptr,0);
445
446   if (dos_date == 0) return(0);
447   
448   interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
449                      &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
450   t.tm_isdst = -1;
451   
452   /* mktime() also does the local to GMT time conversion for us */
453   ret = mktime(&t);
454
455   return(ret);
456 }
457
458 /*******************************************************************
459 like make_unix_date() but the words are reversed
460 ********************************************************************/
461 time_t make_unix_date2(void *date_ptr)
462 {
463   uint32 x,x2;
464
465   x = IVAL(date_ptr,0);
466   x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
467   SIVAL(&x,0,x2);
468
469   return(make_unix_date((void *)&x));
470 }
471
472 /*******************************************************************
473   create a unix GMT date from a dos date in 32 bit "unix like" format
474   these generally arrive as localtimes, with corresponding DST
475   ******************************************************************/
476 time_t make_unix_date3(void *date_ptr)
477 {
478   time_t t = (time_t)IVAL(date_ptr,0);
479   if (!null_mtime(t))
480     t += LocTimeDiff(t);
481   return(t);
482 }
483
484
485 /***************************************************************************
486 return a HTTP/1.0 time string
487   ***************************************************************************/
488 char *http_timestring(time_t t)
489 {
490   static fstring buf;
491   struct tm *tm = LocalTime(&t);
492
493   if (!tm)
494     slprintf(buf,sizeof(buf)-1,"%ld seconds since the Epoch",(long)t);
495   else
496 #ifndef HAVE_STRFTIME
497   fstrcpy(buf, asctime(tm));
498 #else /* !HAVE_STRFTIME */
499   strftime(buf, sizeof(buf)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
500 #endif /* !HAVE_STRFTIME */
501   return buf;
502 }
503
504
505
506 /****************************************************************************
507   return the date and time as a string
508 ****************************************************************************/
509 char *timestring(void )
510 {
511         static fstring TimeBuf;
512         time_t t = time(NULL);
513         struct tm *tm = LocalTime(&t);
514
515         if (!tm) {
516                 slprintf(TimeBuf,sizeof(TimeBuf)-1,"%ld seconds since the Epoch",(long)t);
517         } else {
518 #ifdef HAVE_STRFTIME
519                 strftime(TimeBuf,100,"%Y/%m/%d %T",tm);
520 #else
521                 fstrcpy(TimeBuf, asctime(tm));
522 #endif
523         }
524         return(TimeBuf);
525 }
526
527 /****************************************************************************
528   return the best approximation to a 'create time' under UNIX from a stat
529   structure.
530 ****************************************************************************/
531
532 time_t get_create_time(SMB_STRUCT_STAT *st,BOOL fake_dirs)
533 {
534   time_t ret, ret1;
535
536   if(S_ISDIR(st->st_mode) && fake_dirs)
537     return (time_t)315493200L;          /* 1/1/1980 */
538     
539   ret = MIN(st->st_ctime, st->st_mtime);
540   ret1 = MIN(ret, st->st_atime);
541
542   if(ret1 != (time_t)0)
543     return ret1;
544
545   /*
546    * One of ctime, mtime or atime was zero (probably atime).
547    * Just return MIN(ctime, mtime).
548    */
549   return ret;
550 }
551