📄 time.c
字号:
/* Unix SMB/Netbios implementation. Version 1.9. time handling functions Copyright (C) Andrew Tridgell 1992-1998 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "includes.h"/* This stuff was largely rewritten by Paul Eggert <eggert@twinsun.com> in May 1996 */int serverzone=0;int extra_time_offset = 0;extern int DEBUGLEVEL;#ifndef CHAR_BIT#define CHAR_BIT 8#endif#ifndef TIME_T_MIN#define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \ : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))#endif#ifndef TIME_T_MAX#define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)#endif/*******************************************************************a gettimeofday wrapper********************************************************************/void GetTimeOfDay(struct timeval *tval){#ifdef HAVE_GETTIMEOFDAY_TZ gettimeofday(tval,NULL);#else gettimeofday(tval);#endif}#define TM_YEAR_BASE 1900/*******************************************************************yield the difference between *A and *B, in seconds, ignoring leap seconds********************************************************************/static int tm_diff(struct tm *a, struct tm *b){ int ay = a->tm_year + (TM_YEAR_BASE - 1); int by = b->tm_year + (TM_YEAR_BASE - 1); int intervening_leap_days = (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400); int years = ay - by; int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday); int hours = 24*days + (a->tm_hour - b->tm_hour); int minutes = 60*hours + (a->tm_min - b->tm_min); int seconds = 60*minutes + (a->tm_sec - b->tm_sec); return seconds;}/******************************************************************* return the UTC offset in seconds west of UTC, or 0 if it cannot be determined ******************************************************************/static int TimeZone(time_t t){ struct tm *tm = gmtime(&t); struct tm tm_utc; if (!tm) return 0; tm_utc = *tm; tm = localtime(&t); if (!tm) return 0; return tm_diff(&tm_utc,tm);}/*******************************************************************init the time differences********************************************************************/void TimeInit(void){ serverzone = TimeZone(time(NULL)); if ((serverzone % 60) != 0) { DEBUG(1,("WARNING: Your timezone is not a multiple of 1 minute.\n")); } DEBUG(4,("Serverzone is %d\n",serverzone));}/*******************************************************************return the same value as TimeZone, but it should be more efficient.We keep a table of DST offsets to prevent calling localtime() on each call of this function. This saves a LOT of time on many unixes.Updated by Paul Eggert <eggert@twinsun.com>********************************************************************/static int TimeZoneFaster(time_t t){ static struct dst_table {time_t start,end; int zone;} *dst_table = NULL; static int table_size = 0; int i; int zone = 0; if (t == 0) t = time(NULL); /* Tunis has a 8 day DST region, we need to be careful ... */#define MAX_DST_WIDTH (365*24*60*60)#define MAX_DST_SKIP (7*24*60*60) for (i=0;i<table_size;i++) if (t >= dst_table[i].start && t <= dst_table[i].end) break; if (i<table_size) { zone = dst_table[i].zone; } else { time_t low,high; zone = TimeZone(t); dst_table = (struct dst_table *)Realloc(dst_table, sizeof(dst_table[0])*(i+1)); if (!dst_table) { table_size = 0; } else { table_size++; dst_table[i].zone = zone; dst_table[i].start = dst_table[i].end = t; /* no entry will cover more than 6 months */ low = t - MAX_DST_WIDTH/2; if (t < low) low = TIME_T_MIN; high = t + MAX_DST_WIDTH/2; if (high < t) high = TIME_T_MAX; /* widen the new entry using two bisection searches */ while (low+60*60 < dst_table[i].start) { if (dst_table[i].start - low > MAX_DST_SKIP*2) t = dst_table[i].start - MAX_DST_SKIP; else t = low + (dst_table[i].start-low)/2; if (TimeZone(t) == zone) dst_table[i].start = t; else low = t; } while (high-60*60 > dst_table[i].end) { if (high - dst_table[i].end > MAX_DST_SKIP*2) t = dst_table[i].end + MAX_DST_SKIP; else t = high - (high-dst_table[i].end)/2; if (TimeZone(t) == zone) dst_table[i].end = t; else high = t; }#if 0 DEBUG(1,("Added DST entry from %s ", asctime(localtime(&dst_table[i].start)))); DEBUG(1,("to %s (%d)\n",asctime(localtime(&dst_table[i].end)), dst_table[i].zone));#endif } } return zone;}/**************************************************************************** return the UTC offset in seconds west of UTC, adjusted for extra time offset **************************************************************************/int TimeDiff(time_t t){ return TimeZoneFaster(t) + 60*extra_time_offset;}/**************************************************************************** return the UTC offset in seconds west of UTC, adjusted for extra time offset, for a local time value. If ut = lt + LocTimeDiff(lt), then lt = ut - TimeDiff(ut), but the converse does not necessarily hold near daylight savings transitions because some local times are ambiguous. LocTimeDiff(t) equals TimeDiff(t) except near daylight savings transitions. +**************************************************************************/static int LocTimeDiff(time_t lte){ time_t lt = lte - 60*extra_time_offset; int d = TimeZoneFaster(lt); time_t t = lt + d; /* if overflow occurred, ignore all the adjustments so far */ if (((lte < lt) ^ (extra_time_offset < 0)) | ((t < lt) ^ (d < 0))) t = lte; /* now t should be close enough to the true UTC to yield the right answer */ return TimeDiff(t);}/****************************************************************************try to optimise the localtime call, it can be quite expensive on some machines****************************************************************************/struct tm *LocalTime(time_t *t){ time_t t2 = *t; t2 -= TimeDiff(t2); return(gmtime(&t2));}#define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))/****************************************************************************interpret an 8 byte "filetime" structure to a time_tIt's originally in "100ns units since jan 1st 1601"It appears to be kludge-GMT (at least for file listings). This meansits the GMT you get by taking a localtime and adding theserverzone. This is NOT the same as GMT in some cases. This routineconverts this to real GMT.****************************************************************************/time_t nt_time_to_unix(NTTIME *nt){ double d; time_t ret; /* The next two lines are a fix needed for the broken SCO compiler. JRA. */ time_t l_time_min = TIME_T_MIN; time_t l_time_max = TIME_T_MAX; if (nt->high == 0) return(0); d = ((double)nt->high)*4.0*(double)(1<<30); d += (nt->low&0xFFF00000); d *= 1.0e-7; /* now adjust by 369 years to make the secs since 1970 */ d -= TIME_FIXUP_CONSTANT; if (!(l_time_min <= d && d <= l_time_max)) return(0); ret = (time_t)(d+0.5); /* this takes us from kludge-GMT to real GMT */ ret -= serverzone; ret += LocTimeDiff(ret);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -