if we are adding a new sambaAccount, make sure that we add a
[Samba.git] / source / lib / time.c
blobaa433a769c673f7f1d1e26bca1c79baa520cfb88
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
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.
22 #include "includes.h"
25 This stuff was largely rewritten by Paul Eggert <eggert@twinsun.com>
26 in May 1996
30 int extra_time_offset = 0;
32 #ifndef CHAR_BIT
33 #define CHAR_BIT 8
34 #endif
36 /*******************************************************************
37 External access to time_t_min and time_t_max.
38 ********************************************************************/
40 time_t get_time_t_min(void)
42 return TIME_T_MIN;
45 time_t get_time_t_max(void)
47 return TIME_T_MAX;
50 /*******************************************************************
51 a gettimeofday wrapper
52 ********************************************************************/
53 void GetTimeOfDay(struct timeval *tval)
55 #ifdef HAVE_GETTIMEOFDAY_TZ
56 gettimeofday(tval,NULL);
57 #else
58 gettimeofday(tval);
59 #endif
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);
73 int years = ay - by;
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);
79 return seconds;
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);
88 struct tm tm_utc;
89 if (!tm)
90 return 0;
91 tm_utc = *tm;
92 tm = localtime(&t);
93 if (!tm)
94 return 0;
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;
119 return serverzone;
122 /* Re-read the smb serverzone value */
124 static struct timeval start_time_hires;
126 void TimeInit(void)
128 done_serverzone_init = False;
129 get_serverzone();
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
138 daemon start. JRA.
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);
151 } else
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;
167 int i;
168 int zone = 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;
179 if (i<table_size) {
180 zone = dst_table[i].zone;
181 } else {
182 time_t low,high;
184 zone = TimeZone(t);
185 tdt = (struct dst_table *)Realloc(dst_table,
186 sizeof(dst_table[0])*(i+1));
187 if (!tdt) {
188 DEBUG(0,("TimeZoneFaster: out of memory!\n"));
189 SAFE_FREE(dst_table);
190 table_size = 0;
191 } else {
192 dst_table = tdt;
193 table_size++;
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;
200 if (t < low)
201 low = TIME_T_MIN;
203 high = t + MAX_DST_WIDTH/2;
204 if (high < t)
205 high = TIME_T_MAX;
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;
211 else
212 t = low + (dst_table[i].start-low)/2;
213 if (TimeZone(t) == zone)
214 dst_table[i].start = t;
215 else
216 low = 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;
222 else
223 t = high - (high-dst_table[i].end)/2;
224 if (TimeZone(t) == zone)
225 dst_table[i].end = t;
226 else
227 high = t;
229 #if 0
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)),
233 dst_table[i].zone));
234 #endif
237 return zone;
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);
260 time_t t = lt + d;
262 /* if overflow occurred, ignore all the adjustments so far */
263 if (((lte < lt) ^ (extra_time_offset < 0)) | ((t < lt) ^ (d < 0)))
264 t = lte;
266 /* now t should be close enough to the true UTC to yield the right answer */
267 return TimeDiff(t);
271 /****************************************************************************
272 try to optimise the localtime call, it can be quite expensive on some machines
273 ****************************************************************************/
274 struct tm *LocalTime(time_t *t)
276 time_t t2 = *t;
278 t2 -= TimeDiff(t2);
280 return(gmtime(&t2));
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)
296 double d;
297 time_t ret;
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);
307 d *= 1.0e-7;
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))
313 return(0);
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);
321 return(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)
334 double d;
335 time_t ret;
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;
341 if (nt->high == 0)
342 return(0);
344 if (nt->high==0x80000000 && nt->low==0)
345 return -1;
347 /* reverse the time */
348 /* it's a negative value, turn it to positive */
349 nt->high=~nt->high;
350 nt->low=~nt->low;
352 d = ((double)nt->high)*4.0*(double)(1<<30);
353 d += (nt->low&0xFFF00000);
354 d *= 1.0e-7;
356 if (!(l_time_min <= d && d <= l_time_max))
357 return(0);
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);
365 return(ret);
370 /****************************************************************************
371 interprets an nt time into a unix time_t
372 ****************************************************************************/
373 time_t interpret_long_date(char *p)
375 NTTIME nt;
376 nt.low = IVAL(p,0);
377 nt.high = IVAL(p,4);
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)
387 double d;
389 if (t==0)
391 nt->low = 0;
392 nt->high = 0;
393 return;
395 if (t == TIME_T_MAX)
397 nt->low = 0xffffffff;
398 nt->high = 0x7fffffff;
399 return;
401 if (t == -1)
403 nt->low = 0xffffffff;
404 nt->high = 0xffffffff;
405 return;
408 /* this converts GMT to kludge-GMT */
409 t -= TimeDiff(t) - get_serverzone();
411 d = (double)(t);
412 d += TIME_FIXUP_CONSTANT;
413 d *= 1.0e7;
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)
428 double d;
430 if (t==0) {
431 nt->low = 0;
432 nt->high = 0;
433 return;
436 if (t == TIME_T_MAX) {
437 nt->low = 0xffffffff;
438 nt->high = 0x7fffffff;
439 return;
442 if (t == -1) {
443 /* that's what NT uses for infinite */
444 nt->low = 0x0;
445 nt->high = 0x80000000;
446 return;
449 /* this converts GMT to kludge-GMT */
450 t -= LocTimeDiff(t) - get_serverzone();
452 d = (double)(t);
453 d *= 1.0e7;
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 */
459 nt->high=~nt->high;
460 nt->low=~nt->low;
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)
470 NTTIME nt;
471 unix_to_nt_time(&nt, t);
472 SIVAL(p, 0, nt.low);
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)
482 return(True);
483 return(False);
486 /*******************************************************************
487 create a 16 bit dos packed date
488 ********************************************************************/
489 static uint16 make_dos_date1(struct tm *t)
491 uint16 ret=0;
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));
494 return(ret);
497 /*******************************************************************
498 create a 16 bit dos packed time
499 ********************************************************************/
500 static uint16 make_dos_time1(struct tm *t)
502 uint16 ret=0;
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));
505 return(ret);
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)
514 struct tm *t;
515 uint32 ret=0;
517 t = LocalTime(&unixdate);
518 if (!t)
519 return 0xFFFFFFFF;
521 ret = make_dos_date1(t);
522 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
524 return(ret);
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);
534 SIVAL(buf,offset,x);
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);
545 SIVAL(buf,offset,x);
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)
565 uint32 p0,p1,p2,p3;
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;
573 *day = (p2&0x1F);
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
580 localtime)
581 ********************************************************************/
582 time_t make_unix_date(void *date_ptr)
584 uint32 dos_date=0;
585 struct tm t;
586 time_t ret;
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);
594 t.tm_isdst = -1;
596 /* mktime() also does the local to GMT time conversion for us */
597 ret = mktime(&t);
599 return(ret);
602 /*******************************************************************
603 like make_unix_date() but the words are reversed
604 ********************************************************************/
605 time_t make_unix_date2(void *date_ptr)
607 uint32 x,x2;
609 x = IVAL(date_ptr,0);
610 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
611 SIVAL(&x,0,x2);
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);
623 if (!null_mtime(t))
624 t += LocTimeDiff(t);
625 return(t);
629 /***************************************************************************
630 return a HTTP/1.0 time string
631 ***************************************************************************/
632 char *http_timestring(time_t t)
634 static fstring buf;
635 struct tm *tm = LocalTime(&t);
637 if (!tm)
638 slprintf(buf,sizeof(buf)-1,"%ld seconds since the Epoch",(long)t);
639 else
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 */
647 return buf;
652 /****************************************************************************
653 Return the date and time as a string
654 ****************************************************************************/
656 char *timestring(BOOL hires)
658 static fstring TimeBuf;
659 struct timeval tp;
660 time_t t;
661 struct tm *tm;
663 if (hires) {
664 GetTimeOfDay(&tp);
665 t = (time_t)tp.tv_sec;
666 } else {
667 t = time(NULL);
669 tm = LocalTime(&t);
670 if (!tm) {
671 if (hires) {
672 slprintf(TimeBuf,
673 sizeof(TimeBuf)-1,
674 "%ld.%06ld seconds since the Epoch",
675 (long)tp.tv_sec,
676 (long)tp.tv_usec);
677 } else {
678 slprintf(TimeBuf,
679 sizeof(TimeBuf)-1,
680 "%ld seconds since the Epoch",
681 (long)t);
683 } else {
684 #ifdef HAVE_STRFTIME
685 if (hires) {
686 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
687 slprintf(TimeBuf+strlen(TimeBuf),
688 sizeof(TimeBuf)-1 - strlen(TimeBuf),
689 ".%06ld",
690 (long)tp.tv_usec);
691 } else {
692 strftime(TimeBuf,100,"%Y/%m/%d %H:%M:%S",tm);
694 #else
695 if (hires) {
696 slprintf(TimeBuf,
697 sizeof(TimeBuf)-1,
698 "%s.%06ld",
699 asctime(tm),
700 (long)tp.tv_usec);
701 } else {
702 fstrcpy(TimeBuf, asctime(tm));
704 #endif
706 return(TimeBuf);
709 /****************************************************************************
710 return the best approximation to a 'create time' under UNIX from a stat
711 structure.
712 ****************************************************************************/
714 time_t get_create_time(SMB_STRUCT_STAT *st,BOOL fake_dirs)
716 time_t ret, ret1;
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)
725 return ret1;
728 * One of ctime, mtime or atime was zero (probably atime).
729 * Just return MIN(ctime, mtime).
731 return ret;
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;