time.c 16 KB

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