5e0f5646fca9eb3385f4bc6e9adc93ed67821b1e
[tprouty/samba.git] / source / lib / time.c
1 /* 
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22
23 /*
24   This stuff was largely rewritten by Paul Eggert <eggert@twinsun.com>
25   in May 1996 
26   */
27
28
29 int extra_time_offset = 0;
30
31 #ifndef CHAR_BIT
32 #define CHAR_BIT 8
33 #endif
34
35 #ifndef TIME_T_MIN
36 #define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
37                     : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
38 #endif
39 #ifndef TIME_T_MAX
40 #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
41 #endif
42
43 void get_nttime_max(NTTIME *t)
44 {
45         /* FIXME: This is incorrect */
46         unix_to_nt_time(t, get_time_t_max());
47 }
48
49 /*******************************************************************
50  External access to time_t_min and time_t_max.
51 ********************************************************************/
52
53 time_t get_time_t_max(void)
54 {
55         return TIME_T_MAX;
56 }
57
58 /*******************************************************************
59  A gettimeofday wrapper.
60 ********************************************************************/
61
62 void GetTimeOfDay(struct timeval *tval)
63 {
64 #ifdef HAVE_GETTIMEOFDAY_TZ
65         gettimeofday(tval,NULL);
66 #else
67         gettimeofday(tval);
68 #endif
69 }
70
71 #define TM_YEAR_BASE 1900
72
73 /*******************************************************************
74  Yield the difference between *A and *B, in seconds, ignoring leap seconds.
75 ********************************************************************/
76
77 static int tm_diff(struct tm *a, struct tm *b)
78 {
79         int ay = a->tm_year + (TM_YEAR_BASE - 1);
80         int by = b->tm_year + (TM_YEAR_BASE - 1);
81         int intervening_leap_days = (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
82         int years = ay - by;
83         int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
84         int hours = 24*days + (a->tm_hour - b->tm_hour);
85         int minutes = 60*hours + (a->tm_min - b->tm_min);
86         int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
87
88         return seconds;
89 }
90
91 /*******************************************************************
92  Return the UTC offset in seconds west of UTC, or 0 if it cannot be determined.
93 ******************************************************************/
94
95 static int TimeZone(time_t t)
96 {
97         struct tm *tm = gmtime(&t);
98         struct tm tm_utc;
99         if (!tm)
100                 return 0;
101         tm_utc = *tm;
102         tm = localtime(&t);
103         if (!tm)
104                 return 0;
105         return tm_diff(&tm_utc,tm);
106 }
107
108 static BOOL done_serverzone_init;
109
110 /*******************************************************************
111  Return the smb serverzone value.
112 ******************************************************************/
113
114 static int get_serverzone(void)
115 {
116         static int serverzone;
117
118         if (!done_serverzone_init) {
119                 serverzone = TimeZone(time(NULL));
120
121                 if ((serverzone % 60) != 0) {
122                         DEBUG(1,("WARNING: Your timezone is not a multiple of 1 minute.\n"));
123                 }
124
125                 DEBUG(4,("Serverzone is %d\n",serverzone));
126
127                 done_serverzone_init = True;
128         }
129
130         return serverzone;
131 }
132
133 /*******************************************************************
134  Re-read the smb serverzone value.
135 ******************************************************************/
136
137 static struct timeval start_time_hires;
138
139 void TimeInit(void)
140 {
141         done_serverzone_init = False;
142         get_serverzone();
143         /* Save the start time of this process. */
144         if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0)
145                 GetTimeOfDay(&start_time_hires);
146 }
147
148 /**********************************************************************
149  Return a timeval struct of the uptime of this process. As TimeInit is
150  done before a daemon fork then this is the start time from the parent
151  daemon start. JRA.
152 ***********************************************************************/
153
154 void get_process_uptime(struct timeval *ret_time)
155 {
156         struct timeval time_now_hires;
157
158         GetTimeOfDay(&time_now_hires);
159         ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
160         ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
161         if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
162                 ret_time->tv_sec -= 1;
163                 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
164         } else
165                 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
166 }
167
168 /*******************************************************************
169  Return the same value as TimeZone, but it should be more efficient.
170
171  We keep a table of DST offsets to prevent calling localtime() on each 
172  call of this function. This saves a LOT of time on many unixes.
173
174  Updated by Paul Eggert <eggert@twinsun.com>
175 ********************************************************************/
176
177 static int TimeZoneFaster(time_t t)
178 {
179         static struct dst_table {time_t start,end; int zone;} *tdt, *dst_table = NULL;
180         static int table_size = 0;
181         int i;
182         int zone = 0;
183
184         if (t == 0)
185                 t = time(NULL);
186
187         /* Tunis has a 8 day DST region, we need to be careful ... */
188 #define MAX_DST_WIDTH (365*24*60*60)
189 #define MAX_DST_SKIP (7*24*60*60)
190
191         for (i=0;i<table_size;i++)
192                 if (t >= dst_table[i].start && t <= dst_table[i].end)
193                         break;
194
195         if (i<table_size) {
196                 zone = dst_table[i].zone;
197         } else {
198                 time_t low,high;
199
200                 zone = TimeZone(t);
201                 tdt = SMB_REALLOC_ARRAY(dst_table, struct dst_table, i+1);
202                 if (!tdt) {
203                         DEBUG(0,("TimeZoneFaster: out of memory!\n"));
204                         SAFE_FREE(dst_table);
205                         table_size = 0;
206                 } else {
207                         dst_table = tdt;
208                         table_size++;
209
210                         dst_table[i].zone = zone; 
211                         dst_table[i].start = dst_table[i].end = t;
212     
213                         /* no entry will cover more than 6 months */
214                         low = t - MAX_DST_WIDTH/2;
215                         if (t < low)
216                                 low = TIME_T_MIN;
217       
218                         high = t + MAX_DST_WIDTH/2;
219                         if (high < t)
220                                 high = TIME_T_MAX;
221       
222                         /* widen the new entry using two bisection searches */
223                         while (low+60*60 < dst_table[i].start) {
224                                 if (dst_table[i].start - low > MAX_DST_SKIP*2)
225                                         t = dst_table[i].start - MAX_DST_SKIP;
226                                 else
227                                         t = low + (dst_table[i].start-low)/2;
228                                 if (TimeZone(t) == zone)
229                                         dst_table[i].start = t;
230                                 else
231                                         low = t;
232                         }
233
234                         while (high-60*60 > dst_table[i].end) {
235                                 if (high - dst_table[i].end > MAX_DST_SKIP*2)
236                                         t = dst_table[i].end + MAX_DST_SKIP;
237                                 else
238                                         t = high - (high-dst_table[i].end)/2;
239                                 if (TimeZone(t) == zone)
240                                         dst_table[i].end = t;
241                                 else
242                                         high = t;
243                         }
244 #if 0
245       DEBUG(1,("Added DST entry from %s ",
246                asctime(localtime(&dst_table[i].start))));
247       DEBUG(1,("to %s (%d)\n",asctime(localtime(&dst_table[i].end)),
248                dst_table[i].zone));
249 #endif
250                 }
251         }
252         return zone;
253 }
254
255 /****************************************************************************
256  Return the UTC offset in seconds west of UTC, adjusted for extra time offset.
257 **************************************************************************/
258
259 int TimeDiff(time_t t)
260 {
261         return TimeZoneFaster(t) + 60*extra_time_offset;
262 }
263
264 /****************************************************************************
265  Return the UTC offset in seconds west of UTC, adjusted for extra time
266  offset, for a local time value.  If ut = lt + LocTimeDiff(lt), then
267  lt = ut - TimeDiff(ut), but the converse does not necessarily hold near
268  daylight savings transitions because some local times are ambiguous.
269  LocTimeDiff(t) equals TimeDiff(t) except near daylight savings transitions.
270 **************************************************************************/
271
272 static int LocTimeDiff(time_t lte)
273 {
274         time_t lt = lte - 60*extra_time_offset;
275         int d = TimeZoneFaster(lt);
276         time_t t = lt + d;
277
278         /* if overflow occurred, ignore all the adjustments so far */
279         if (((lte < lt) ^ (extra_time_offset < 0))  |  ((t < lt) ^ (d < 0)))
280                 t = lte;
281
282         /* now t should be close enough to the true UTC to yield the right answer */
283         return TimeDiff(t);
284 }
285
286 /****************************************************************************
287  Try to optimise the localtime call, it can be quite expensive on some machines.
288 ****************************************************************************/
289
290 struct tm *LocalTime(time_t *t)
291 {
292         time_t t2 = *t;
293
294         t2 -= TimeDiff(t2);
295
296         return(gmtime(&t2));
297 }
298
299 #define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
300
301 /****************************************************************************
302  Interpret an 8 byte "filetime" structure to a time_t
303  It's originally in "100ns units since jan 1st 1601"
304
305  An 8 byte value of 0xffffffffffffffff will be returned as (time_t)0.
306
307  It appears to be kludge-GMT (at least for file listings). This means
308  its the GMT you get by taking a localtime and adding the
309  serverzone. This is NOT the same as GMT in some cases. This routine
310  converts this to real GMT.
311 ****************************************************************************/
312
313 time_t nt_time_to_unix(NTTIME *nt)
314 {
315         double d;
316         time_t ret;
317         /* The next two lines are a fix needed for the 
318                 broken SCO compiler. JRA. */
319         time_t l_time_min = TIME_T_MIN;
320         time_t l_time_max = TIME_T_MAX;
321
322         if (nt->high == 0 || (nt->high == 0xffffffff && nt->low == 0xffffffff))
323                 return(0);
324
325         d = ((double)nt->high)*4.0*(double)(1<<30);
326         d += (nt->low&0xFFF00000);
327         d *= 1.0e-7;
328  
329         /* now adjust by 369 years to make the secs since 1970 */
330         d -= TIME_FIXUP_CONSTANT;
331
332         if (d <= l_time_min)
333                 return (l_time_min);
334
335         if (d >= l_time_max)
336                 return (l_time_max);
337
338         ret = (time_t)(d+0.5);
339
340         /* this takes us from kludge-GMT to real GMT */
341         ret -= get_serverzone();
342         ret += LocTimeDiff(ret);
343
344         return(ret);
345 }
346
347 /****************************************************************************
348  Convert a NTTIME structure to a time_t.
349  It's originally in "100ns units".
350
351  This is an absolute version of the one above.
352  By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
353  if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
354 ****************************************************************************/
355
356 time_t nt_time_to_unix_abs(NTTIME *nt)
357 {
358         double d;
359         time_t ret;
360         /* The next two lines are a fix needed for the 
361            broken SCO compiler. JRA. */
362         time_t l_time_min = TIME_T_MIN;
363         time_t l_time_max = TIME_T_MAX;
364
365         if (nt->high == 0)
366                 return(0);
367
368         if (nt->high==0x80000000 && nt->low==0)
369                 return (time_t)-1;
370
371         /* reverse the time */
372         /* it's a negative value, turn it to positive */
373         nt->high=~nt->high;
374         nt->low=~nt->low;
375
376         d = ((double)nt->high)*4.0*(double)(1<<30);
377         d += (nt->low&0xFFF00000);
378         d *= 1.0e-7;
379   
380         if (!(l_time_min <= d && d <= l_time_max))
381                 return(0);
382
383         ret = (time_t)(d+0.5);
384
385         return(ret);
386 }
387
388 /****************************************************************************
389  Interprets an nt time into a unix time_t.
390  Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
391  will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
392 ****************************************************************************/
393
394 time_t interpret_long_date(char *p)
395 {
396         NTTIME nt;
397         nt.low = IVAL(p,0);
398         nt.high = IVAL(p,4);
399         if (nt.low == 0xFFFFFFFF && nt.high == 0xFFFFFFFF) {
400                 return (time_t)-1;
401         }
402         return nt_time_to_unix(&nt);
403 }
404
405 /****************************************************************************
406  Put a 8 byte filetime from a time_t
407  This takes real GMT as input and converts to kludge-GMT
408 ****************************************************************************/
409
410 void unix_to_nt_time(NTTIME *nt, time_t t)
411 {
412         double d;
413
414         if (t==0) {
415                 nt->low = 0;
416                 nt->high = 0;
417                 return;
418         }
419         if (t == TIME_T_MAX) {
420                 nt->low = 0xffffffff;
421                 nt->high = 0x7fffffff;
422                 return;
423         }               
424         if (t == (time_t)-1) {
425                 nt->low = 0xffffffff;
426                 nt->high = 0xffffffff;
427                 return;
428         }               
429
430         /* this converts GMT to kludge-GMT */
431         t -= TimeDiff(t) - get_serverzone(); 
432
433         d = (double)(t);
434         d += TIME_FIXUP_CONSTANT;
435         d *= 1.0e7;
436
437         nt->high = (uint32)(d * (1.0/(4.0*(double)(1<<30))));
438         nt->low  = (uint32)(d - ((double)nt->high)*4.0*(double)(1<<30));
439 }
440
441 /****************************************************************************
442  Convert a time_t to a NTTIME structure
443
444  This is an absolute version of the one above.
445  By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
446  If the nttime_t was 5 seconds, the NTTIME is 5 seconds. JFM
447 ****************************************************************************/
448
449 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
450 {
451         double d;
452
453         if (t==0) {
454                 nt->low = 0;
455                 nt->high = 0;
456                 return;
457         }
458
459         if (t == TIME_T_MAX) {
460                 nt->low = 0xffffffff;
461                 nt->high = 0x7fffffff;
462                 return;
463         }
464                 
465         if (t == (time_t)-1) {
466                 /* that's what NT uses for infinite */
467                 nt->low = 0x0;
468                 nt->high = 0x80000000;
469                 return;
470         }               
471
472         d = (double)(t);
473         d *= 1.0e7;
474
475         nt->high = (uint32)(d * (1.0/(4.0*(double)(1<<30))));
476         nt->low  = (uint32)(d - ((double)nt->high)*4.0*(double)(1<<30));
477
478         /* convert to a negative value */
479         nt->high=~nt->high;
480         nt->low=~nt->low;
481 }
482
483 /****************************************************************************
484  Take a Unix time and convert to an NTTIME structure and place in buffer 
485  pointed to by p.
486 ****************************************************************************/
487
488 void put_long_date(char *p,time_t t)
489 {
490         NTTIME nt;
491         unix_to_nt_time(&nt, t);
492         SIVAL(p, 0, nt.low);
493         SIVAL(p, 4, nt.high);
494 }
495
496 /****************************************************************************
497  Check if it's a null mtime.
498 ****************************************************************************/
499
500 BOOL null_mtime(time_t mtime)
501 {
502         if (mtime == 0 || mtime == (time_t)0xFFFFFFFF || mtime == (time_t)-1)
503                 return(True);
504         return(False);
505 }
506
507 /*******************************************************************
508  Create a 16 bit dos packed date.
509 ********************************************************************/
510
511 static uint16 make_dos_date1(struct tm *t)
512 {
513         uint16 ret=0;
514         ret = (((unsigned)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
515         ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
516         return(ret);
517 }
518
519 /*******************************************************************
520  Create a 16 bit dos packed time.
521 ********************************************************************/
522
523 static uint16 make_dos_time1(struct tm *t)
524 {
525         uint16 ret=0;
526         ret = ((((unsigned)t->tm_min >> 3)&0x7) | (((unsigned)t->tm_hour) << 3));
527         ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
528         return(ret);
529 }
530
531 /*******************************************************************
532  Create a 32 bit dos packed date/time from some parameters.
533  This takes a GMT time and returns a packed localtime structure.
534 ********************************************************************/
535
536 static uint32 make_dos_date(time_t unixdate)
537 {
538         struct tm *t;
539         uint32 ret=0;
540
541         t = LocalTime(&unixdate);
542         if (!t)
543                 return 0xFFFFFFFF;
544
545         ret = make_dos_date1(t);
546         ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
547
548         return(ret);
549 }
550
551 /*******************************************************************
552  Put a dos date into a buffer (time/date format).
553  This takes GMT time and puts local time in the buffer.
554 ********************************************************************/
555
556 void put_dos_date(char *buf,int offset,time_t unixdate)
557 {
558         uint32 x = make_dos_date(unixdate);
559         SIVAL(buf,offset,x);
560 }
561
562 /*******************************************************************
563  Put a dos date into a buffer (date/time format).
564  This takes GMT time and puts local time in the buffer.
565 ********************************************************************/
566
567 void put_dos_date2(char *buf,int offset,time_t unixdate)
568 {
569         uint32 x = make_dos_date(unixdate);
570         x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
571         SIVAL(buf,offset,x);
572 }
573
574 /*******************************************************************
575  Put a dos 32 bit "unix like" date into a buffer. This routine takes
576  GMT and converts it to LOCAL time before putting it (most SMBs assume
577  localtime for this sort of date)
578 ********************************************************************/
579
580 void put_dos_date3(char *buf,int offset,time_t unixdate)
581 {
582         if (!null_mtime(unixdate))
583                 unixdate -= TimeDiff(unixdate);
584         SIVAL(buf,offset,unixdate);
585 }
586
587 /*******************************************************************
588  Interpret a 32 bit dos packed date/time to some parameters.
589 ********************************************************************/
590
591 static void interpret_dos_date(uint32 date,int *year,int *month,int *day,int *hour,int *minute,int *second)
592 {
593         uint32 p0,p1,p2,p3;
594
595         p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF; 
596         p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
597
598         *second = 2*(p0 & 0x1F);
599         *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
600         *hour = (p1>>3)&0xFF;
601         *day = (p2&0x1F);
602         *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
603         *year = ((p3>>1)&0xFF) + 80;
604 }
605
606 /*******************************************************************
607  Create a unix date (int GMT) from a dos date (which is actually in
608  localtime).
609 ********************************************************************/
610
611 time_t make_unix_date(void *date_ptr)
612 {
613         uint32 dos_date=0;
614         struct tm t;
615         time_t ret;
616
617         dos_date = IVAL(date_ptr,0);
618
619         if (dos_date == 0)
620                 return(0);
621   
622         interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
623                         &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
624         t.tm_isdst = -1;
625   
626         /* mktime() also does the local to GMT time conversion for us */
627         ret = mktime(&t);
628
629         return(ret);
630 }
631
632 /*******************************************************************
633  Like make_unix_date() but the words are reversed.
634 ********************************************************************/
635
636 time_t make_unix_date2(void *date_ptr)
637 {
638         uint32 x,x2;
639
640         x = IVAL(date_ptr,0);
641         x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
642         SIVAL(&x,0,x2);
643
644         return(make_unix_date((void *)&x));
645 }
646
647 /*******************************************************************
648  Create a unix GMT date from a dos date in 32 bit "unix like" format
649  these generally arrive as localtimes, with corresponding DST.
650 ******************************************************************/
651
652 time_t make_unix_date3(void *date_ptr)
653 {
654         time_t t = (time_t)IVAL(date_ptr,0);
655         if (!null_mtime(t))
656                 t += LocTimeDiff(t);
657         return(t);
658 }
659
660 /***************************************************************************
661  Return a HTTP/1.0 time string.
662 ***************************************************************************/
663
664 char *http_timestring(time_t t)
665 {
666         static fstring buf;
667         struct tm *tm = LocalTime(&t);
668
669         if (!tm)
670                 slprintf(buf,sizeof(buf)-1,"%ld seconds since the Epoch",(long)t);
671         else
672 #ifndef HAVE_STRFTIME
673                 fstrcpy(buf, asctime(tm));
674         if(buf[strlen(buf)-1] == '\n')
675                 buf[strlen(buf)-1] = 0;
676 #else /* !HAVE_STRFTIME */
677                 strftime(buf, sizeof(buf)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
678 #endif /* !HAVE_STRFTIME */
679         return buf;
680 }
681
682 /****************************************************************************
683  Return the date and time as a string
684 ****************************************************************************/
685
686 char *timestring(BOOL hires)
687 {
688         static fstring TimeBuf;
689         struct timeval tp;
690         time_t t;
691         struct tm *tm;
692
693         if (hires) {
694                 GetTimeOfDay(&tp);
695                 t = (time_t)tp.tv_sec;
696         } else {
697                 t = time(NULL);
698         }
699         tm = LocalTime(&t);
700         if (!tm) {
701                 if (hires) {
702                         slprintf(TimeBuf,
703                                  sizeof(TimeBuf)-1,
704                                  "%ld.%06ld seconds since the Epoch",
705                                  (long)tp.tv_sec, 
706                                  (long)tp.tv_usec);
707                 } else {
708                         slprintf(TimeBuf,
709                                  sizeof(TimeBuf)-1,
710                                  "%ld seconds since the Epoch",
711                                  (long)t);
712                 }
713         } else {
714 #ifdef HAVE_STRFTIME
715                 if (hires) {
716                         strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
717                         slprintf(TimeBuf+strlen(TimeBuf),
718                                  sizeof(TimeBuf)-1 - strlen(TimeBuf), 
719                                  ".%06ld", 
720                                  (long)tp.tv_usec);
721                 } else {
722                         strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
723                 }
724 #else
725                 if (hires) {
726                         slprintf(TimeBuf, 
727                                  sizeof(TimeBuf)-1, 
728                                  "%s.%06ld", 
729                                  asctime(tm), 
730                                  (long)tp.tv_usec);
731                 } else {
732                         fstrcpy(TimeBuf, asctime(tm));
733                 }
734 #endif
735         }
736         return(TimeBuf);
737 }
738
739 /****************************************************************************
740  Return the best approximation to a 'create time' under UNIX from a stat
741  structure.
742 ****************************************************************************/
743
744 time_t get_create_time(SMB_STRUCT_STAT *st,BOOL fake_dirs)
745 {
746         time_t ret, ret1;
747
748         if(S_ISDIR(st->st_mode) && fake_dirs)
749                 return (time_t)315493200L;          /* 1/1/1980 */
750     
751         ret = MIN(st->st_ctime, st->st_mtime);
752         ret1 = MIN(ret, st->st_atime);
753
754         if(ret1 != (time_t)0)
755                 return ret1;
756
757         /*
758          * One of ctime, mtime or atime was zero (probably atime).
759          * Just return MIN(ctime, mtime).
760          */
761         return ret;
762 }
763
764 /****************************************************************************
765  Initialise an NTTIME to -1, which means "unknown" or "don't expire".
766 ****************************************************************************/
767
768 void init_nt_time(NTTIME *nt)
769 {
770         nt->high = 0x7FFFFFFF;
771         nt->low = 0xFFFFFFFF;
772 }
773
774 /****************************************************************************
775  Check if NTTIME is 0.
776 ****************************************************************************/
777
778 BOOL nt_time_is_zero(NTTIME *nt)
779 {
780         if(nt->high==0) 
781                 return True;
782         return False;
783 }
784
785 /****************************************************************************
786  Return a timeval difference in usec.
787 ****************************************************************************/
788
789 SMB_BIG_INT usec_time_diff(const struct timeval *larget, const struct timeval *smallt)
790 {
791         SMB_BIG_INT sec_diff = larget->tv_sec - smallt->tv_sec;
792         return (sec_diff * 1000000) + (SMB_BIG_INT)(larget->tv_usec - smallt->tv_usec);
793 }
794
795
796 /****************************************************************************
797  convert ASN.1 GeneralizedTime string to unix-time
798  returns 0 on failure; Currently ignores timezone. 
799 ****************************************************************************/
800 time_t generalized_to_unix_time(const char *str)
801
802         struct tm tm;
803
804         ZERO_STRUCT(tm);
805
806         if (sscanf(str, "%4d%2d%2d%2d%2d%2d", 
807                    &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 
808                    &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
809                 return 0;
810         }
811         tm.tm_year -= 1900;
812         tm.tm_mon -= 1;
813
814         return timegm(&tm);
815 }