1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | time handling functions
|
---|
4 |
|
---|
5 | Copyright (C) Andrew Tridgell 1992-2004
|
---|
6 | Copyright (C) Stefan (metze) Metzmacher 2002
|
---|
7 |
|
---|
8 | This program is free software; you can redistribute it and/or modify
|
---|
9 | it under the terms of the GNU General Public License as published by
|
---|
10 | the Free Software Foundation; either version 2 of the License, or
|
---|
11 | (at your option) any later version.
|
---|
12 |
|
---|
13 | This program is distributed in the hope that it will be useful,
|
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | GNU General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License
|
---|
19 | along with this program; if not, write to the Free Software
|
---|
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "includes.h"
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @file
|
---|
27 | * @brief time handling functions
|
---|
28 | */
|
---|
29 |
|
---|
30 |
|
---|
31 | #ifndef TIME_T_MIN
|
---|
32 | #define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
|
---|
33 | : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
|
---|
34 | #endif
|
---|
35 | #ifndef TIME_T_MAX
|
---|
36 | #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | /**
|
---|
40 | External access to time_t_min and time_t_max.
|
---|
41 | **/
|
---|
42 | time_t get_time_t_max(void)
|
---|
43 | {
|
---|
44 | return TIME_T_MAX;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | a gettimeofday wrapper
|
---|
49 | **/
|
---|
50 | void GetTimeOfDay(struct timeval *tval)
|
---|
51 | {
|
---|
52 | #ifdef HAVE_GETTIMEOFDAY_TZ
|
---|
53 | gettimeofday(tval,NULL);
|
---|
54 | #else
|
---|
55 | gettimeofday(tval);
|
---|
56 | #endif
|
---|
57 | }
|
---|
58 |
|
---|
59 | struct timespec convert_time_t_to_timespec(time_t t)
|
---|
60 | {
|
---|
61 | struct timespec ts;
|
---|
62 | ts.tv_sec = t;
|
---|
63 | ts.tv_nsec = 0;
|
---|
64 | return ts;
|
---|
65 | }
|
---|
66 |
|
---|
67 | #if (SIZEOF_LONG == 8)
|
---|
68 | #define TIME_FIXUP_CONSTANT_INT 11644473600L
|
---|
69 | #elif (SIZEOF_LONG_LONG == 8)
|
---|
70 | #define TIME_FIXUP_CONSTANT_INT 11644473600LL
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | /****************************************************************************
|
---|
74 | Interpret an 8 byte "filetime" structure to a time_t
|
---|
75 | It's originally in "100ns units since jan 1st 1601"
|
---|
76 |
|
---|
77 | An 8 byte value of 0xffffffffffffffff will be returned as a timespec of
|
---|
78 |
|
---|
79 | tv_sec = 0
|
---|
80 | tv_nsec = 0;
|
---|
81 |
|
---|
82 | Returns GMT.
|
---|
83 | ****************************************************************************/
|
---|
84 |
|
---|
85 | time_t nt_time_to_unix(NTTIME nt)
|
---|
86 | {
|
---|
87 | return convert_timespec_to_time_t(nt_time_to_unix_timespec(&nt));
|
---|
88 | }
|
---|
89 |
|
---|
90 | /****************************************************************************
|
---|
91 | Put a 8 byte filetime from a time_t. Uses GMT.
|
---|
92 | ****************************************************************************/
|
---|
93 |
|
---|
94 | void unix_to_nt_time(NTTIME *nt, time_t t)
|
---|
95 | {
|
---|
96 | uint64_t t2;
|
---|
97 |
|
---|
98 | if (t == (time_t)-1) {
|
---|
99 | *nt = (NTTIME)-1LL;
|
---|
100 | return;
|
---|
101 | }
|
---|
102 | if (t == 0) {
|
---|
103 | *nt = 0;
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | t2 = t;
|
---|
108 | t2 += TIME_FIXUP_CONSTANT_INT;
|
---|
109 | t2 *= 1000*1000*10;
|
---|
110 |
|
---|
111 | *nt = t2;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | check if it's a null unix time
|
---|
117 | **/
|
---|
118 | BOOL null_time(time_t t)
|
---|
119 | {
|
---|
120 | return t == 0 ||
|
---|
121 | t == (time_t)0xFFFFFFFF ||
|
---|
122 | t == (time_t)-1;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /**
|
---|
127 | check if it's a null NTTIME
|
---|
128 | **/
|
---|
129 | BOOL null_nttime(NTTIME t)
|
---|
130 | {
|
---|
131 | return t == 0 || t == (NTTIME)-1;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*******************************************************************
|
---|
135 | create a 16 bit dos packed date
|
---|
136 | ********************************************************************/
|
---|
137 | static uint16_t make_dos_date1(struct tm *t)
|
---|
138 | {
|
---|
139 | uint16_t ret=0;
|
---|
140 | ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
|
---|
141 | ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
|
---|
142 | return ret;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /*******************************************************************
|
---|
146 | create a 16 bit dos packed time
|
---|
147 | ********************************************************************/
|
---|
148 | static uint16_t make_dos_time1(struct tm *t)
|
---|
149 | {
|
---|
150 | uint16_t ret=0;
|
---|
151 | ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));
|
---|
152 | ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
|
---|
153 | return ret;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /*******************************************************************
|
---|
157 | create a 32 bit dos packed date/time from some parameters
|
---|
158 | This takes a GMT time and returns a packed localtime structure
|
---|
159 | ********************************************************************/
|
---|
160 | static uint32_t make_dos_date(time_t unixdate, int zone_offset)
|
---|
161 | {
|
---|
162 | struct tm *t;
|
---|
163 | uint32_t ret=0;
|
---|
164 |
|
---|
165 | if (unixdate == 0) {
|
---|
166 | return 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 | unixdate -= zone_offset;
|
---|
170 |
|
---|
171 | t = gmtime(&unixdate);
|
---|
172 | if (!t) {
|
---|
173 | return 0xFFFFFFFF;
|
---|
174 | }
|
---|
175 |
|
---|
176 | ret = make_dos_date1(t);
|
---|
177 | ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
|
---|
178 |
|
---|
179 | return ret;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | put a dos date into a buffer (time/date format)
|
---|
184 | This takes GMT time and puts local time in the buffer
|
---|
185 | **/
|
---|
186 | void push_dos_date(uint8_t *buf, int offset, time_t unixdate, int zone_offset)
|
---|
187 | {
|
---|
188 | uint32_t x = make_dos_date(unixdate, zone_offset);
|
---|
189 | SIVAL(buf,offset,x);
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | put a dos date into a buffer (date/time format)
|
---|
194 | This takes GMT time and puts local time in the buffer
|
---|
195 | **/
|
---|
196 | void push_dos_date2(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
|
---|
197 | {
|
---|
198 | uint32_t x;
|
---|
199 | x = make_dos_date(unixdate, zone_offset);
|
---|
200 | x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
|
---|
201 | SIVAL(buf,offset,x);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | put a dos 32 bit "unix like" date into a buffer. This routine takes
|
---|
206 | GMT and converts it to LOCAL time before putting it (most SMBs assume
|
---|
207 | localtime for this sort of date)
|
---|
208 | **/
|
---|
209 | void push_dos_date3(uint8_t *buf,int offset,time_t unixdate, int zone_offset)
|
---|
210 | {
|
---|
211 | if (!null_time(unixdate)) {
|
---|
212 | unixdate -= zone_offset;
|
---|
213 | }
|
---|
214 | SIVAL(buf,offset,unixdate);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /*******************************************************************
|
---|
218 | interpret a 32 bit dos packed date/time to some parameters
|
---|
219 | ********************************************************************/
|
---|
220 | static void interpret_dos_date(uint32_t date,int *year,int *month,int *day,int *hour,int *minute,int *second)
|
---|
221 | {
|
---|
222 | uint32_t p0,p1,p2,p3;
|
---|
223 |
|
---|
224 | p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF;
|
---|
225 | p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
|
---|
226 |
|
---|
227 | *second = 2*(p0 & 0x1F);
|
---|
228 | *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
|
---|
229 | *hour = (p1>>3)&0xFF;
|
---|
230 | *day = (p2&0x1F);
|
---|
231 | *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
|
---|
232 | *year = ((p3>>1)&0xFF) + 80;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | create a unix date (int GMT) from a dos date (which is actually in
|
---|
237 | localtime)
|
---|
238 | **/
|
---|
239 | time_t pull_dos_date(const uint8_t *date_ptr, int zone_offset)
|
---|
240 | {
|
---|
241 | uint32_t dos_date=0;
|
---|
242 | struct tm t;
|
---|
243 | time_t ret;
|
---|
244 |
|
---|
245 | dos_date = IVAL(date_ptr,0);
|
---|
246 |
|
---|
247 | if (dos_date == 0) return (time_t)0;
|
---|
248 |
|
---|
249 | interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
|
---|
250 | &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
|
---|
251 | t.tm_isdst = -1;
|
---|
252 |
|
---|
253 | ret = timegm(&t);
|
---|
254 |
|
---|
255 | ret += zone_offset;
|
---|
256 |
|
---|
257 | return ret;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | like make_unix_date() but the words are reversed
|
---|
262 | **/
|
---|
263 | time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset)
|
---|
264 | {
|
---|
265 | uint32_t x,x2;
|
---|
266 |
|
---|
267 | x = IVAL(date_ptr,0);
|
---|
268 | x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
|
---|
269 | SIVAL(&x,0,x2);
|
---|
270 |
|
---|
271 | return pull_dos_date((const uint8_t *)&x, zone_offset);
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | create a unix GMT date from a dos date in 32 bit "unix like" format
|
---|
276 | these generally arrive as localtimes, with corresponding DST
|
---|
277 | **/
|
---|
278 | time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
|
---|
279 | {
|
---|
280 | time_t t = (time_t)IVAL(date_ptr,0);
|
---|
281 | if (!null_time(t)) {
|
---|
282 | t += zone_offset;
|
---|
283 | }
|
---|
284 | return t;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /***************************************************************************
|
---|
288 | Return a HTTP/1.0 time string.
|
---|
289 | ***************************************************************************/
|
---|
290 |
|
---|
291 | char *http_timestring(time_t t)
|
---|
292 | {
|
---|
293 | static fstring buf;
|
---|
294 | struct tm *tm = localtime(&t);
|
---|
295 |
|
---|
296 | if (!tm) {
|
---|
297 | slprintf(buf,sizeof(buf)-1,"%ld seconds since the Epoch",(long)t);
|
---|
298 | } else {
|
---|
299 | #ifndef HAVE_STRFTIME
|
---|
300 | const char *asct = asctime(tm);
|
---|
301 | fstrcpy(buf, asct ? asct : "unknown");
|
---|
302 | }
|
---|
303 | if(buf[strlen(buf)-1] == '\n') {
|
---|
304 | buf[strlen(buf)-1] = 0;
|
---|
305 | #else /* !HAVE_STRFTIME */
|
---|
306 | strftime(buf, sizeof(buf)-1, "%a, %d %b %Y %H:%M:%S %Z", tm);
|
---|
307 | #endif /* !HAVE_STRFTIME */
|
---|
308 | }
|
---|
309 | return buf;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | /**
|
---|
314 | Return the date and time as a string
|
---|
315 | **/
|
---|
316 | char *timestring(TALLOC_CTX *mem_ctx, time_t t)
|
---|
317 | {
|
---|
318 | char *TimeBuf;
|
---|
319 | char tempTime[80];
|
---|
320 | struct tm *tm;
|
---|
321 |
|
---|
322 | tm = localtime(&t);
|
---|
323 | if (!tm) {
|
---|
324 | return talloc_asprintf(mem_ctx,
|
---|
325 | "%ld seconds since the Epoch",
|
---|
326 | (long)t);
|
---|
327 | }
|
---|
328 |
|
---|
329 | #ifdef HAVE_STRFTIME
|
---|
330 | /* some versions of gcc complain about using %c. This is a bug
|
---|
331 | in the gcc warning, not a bug in this code. See a recent
|
---|
332 | strftime() manual page for details.
|
---|
333 | */
|
---|
334 | strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm);
|
---|
335 | TimeBuf = talloc_strdup(mem_ctx, tempTime);
|
---|
336 | #else
|
---|
337 | TimeBuf = talloc_strdup(mem_ctx, asctime(tm));
|
---|
338 | #endif
|
---|
339 |
|
---|
340 | return TimeBuf;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /**
|
---|
344 | return a talloced string representing a NTTIME for human consumption
|
---|
345 | */
|
---|
346 | const char *nt_time_string(TALLOC_CTX *mem_ctx, NTTIME nt)
|
---|
347 | {
|
---|
348 | time_t t;
|
---|
349 | if (nt == 0) {
|
---|
350 | return "NTTIME(0)";
|
---|
351 | }
|
---|
352 | t = nt_time_to_unix(nt);
|
---|
353 | return timestring(mem_ctx, t);
|
---|
354 | }
|
---|
355 |
|
---|
356 |
|
---|
357 | /**
|
---|
358 | parse a nttime as a large integer in a string and return a NTTIME
|
---|
359 | */
|
---|
360 | NTTIME nttime_from_string(const char *s)
|
---|
361 | {
|
---|
362 | return strtoull(s, NULL, 0);
|
---|
363 | }
|
---|
364 |
|
---|
365 | /**
|
---|
366 | return (tv1 - tv2) in microseconds
|
---|
367 | */
|
---|
368 | int64_t usec_time_diff(struct timeval *tv1, struct timeval *tv2)
|
---|
369 | {
|
---|
370 | int64_t sec_diff = tv1->tv_sec - tv2->tv_sec;
|
---|
371 | return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec);
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 | /**
|
---|
376 | return a zero timeval
|
---|
377 | */
|
---|
378 | struct timeval timeval_zero(void)
|
---|
379 | {
|
---|
380 | struct timeval tv;
|
---|
381 | tv.tv_sec = 0;
|
---|
382 | tv.tv_usec = 0;
|
---|
383 | return tv;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /**
|
---|
387 | return True if a timeval is zero
|
---|
388 | */
|
---|
389 | BOOL timeval_is_zero(const struct timeval *tv)
|
---|
390 | {
|
---|
391 | return tv->tv_sec == 0 && tv->tv_usec == 0;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | return a timeval for the current time
|
---|
396 | */
|
---|
397 | struct timeval timeval_current(void)
|
---|
398 | {
|
---|
399 | struct timeval tv;
|
---|
400 | GetTimeOfDay(&tv);
|
---|
401 | return tv;
|
---|
402 | }
|
---|
403 |
|
---|
404 | /**
|
---|
405 | return a timeval struct with the given elements
|
---|
406 | */
|
---|
407 | struct timeval timeval_set(uint32_t secs, uint32_t usecs)
|
---|
408 | {
|
---|
409 | struct timeval tv;
|
---|
410 | tv.tv_sec = secs;
|
---|
411 | tv.tv_usec = usecs;
|
---|
412 | return tv;
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | /**
|
---|
417 | return a timeval ofs microseconds after tv
|
---|
418 | */
|
---|
419 | struct timeval timeval_add(const struct timeval *tv,
|
---|
420 | uint32_t secs, uint32_t usecs)
|
---|
421 | {
|
---|
422 | struct timeval tv2 = *tv;
|
---|
423 | const unsigned int million = 1000000;
|
---|
424 | tv2.tv_sec += secs;
|
---|
425 | tv2.tv_usec += usecs;
|
---|
426 | tv2.tv_sec += tv2.tv_usec / million;
|
---|
427 | tv2.tv_usec = tv2.tv_usec % million;
|
---|
428 | return tv2;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /**
|
---|
432 | return the sum of two timeval structures
|
---|
433 | */
|
---|
434 | struct timeval timeval_sum(const struct timeval *tv1,
|
---|
435 | const struct timeval *tv2)
|
---|
436 | {
|
---|
437 | return timeval_add(tv1, tv2->tv_sec, tv2->tv_usec);
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | return a timeval secs/usecs into the future
|
---|
442 | */
|
---|
443 | struct timeval timeval_current_ofs(uint32_t secs, uint32_t usecs)
|
---|
444 | {
|
---|
445 | struct timeval tv = timeval_current();
|
---|
446 | return timeval_add(&tv, secs, usecs);
|
---|
447 | }
|
---|
448 |
|
---|
449 | /**
|
---|
450 | compare two timeval structures.
|
---|
451 | Return -1 if tv1 < tv2
|
---|
452 | Return 0 if tv1 == tv2
|
---|
453 | Return 1 if tv1 > tv2
|
---|
454 | */
|
---|
455 | int timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
|
---|
456 | {
|
---|
457 | if (tv1->tv_sec > tv2->tv_sec) return 1;
|
---|
458 | if (tv1->tv_sec < tv2->tv_sec) return -1;
|
---|
459 | if (tv1->tv_usec > tv2->tv_usec) return 1;
|
---|
460 | if (tv1->tv_usec < tv2->tv_usec) return -1;
|
---|
461 | return 0;
|
---|
462 | }
|
---|
463 |
|
---|
464 | /**
|
---|
465 | return True if a timer is in the past
|
---|
466 | */
|
---|
467 | BOOL timeval_expired(const struct timeval *tv)
|
---|
468 | {
|
---|
469 | struct timeval tv2 = timeval_current();
|
---|
470 | if (tv2.tv_sec > tv->tv_sec) return True;
|
---|
471 | if (tv2.tv_sec < tv->tv_sec) return False;
|
---|
472 | return (tv2.tv_usec >= tv->tv_usec);
|
---|
473 | }
|
---|
474 |
|
---|
475 | /**
|
---|
476 | return the number of seconds elapsed between two times
|
---|
477 | */
|
---|
478 | double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
|
---|
479 | {
|
---|
480 | return (tv2->tv_sec - tv1->tv_sec) +
|
---|
481 | (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
|
---|
482 | }
|
---|
483 |
|
---|
484 | /**
|
---|
485 | return the number of seconds elapsed since a given time
|
---|
486 | */
|
---|
487 | double timeval_elapsed(const struct timeval *tv)
|
---|
488 | {
|
---|
489 | struct timeval tv2 = timeval_current();
|
---|
490 | return timeval_elapsed2(tv, &tv2);
|
---|
491 | }
|
---|
492 |
|
---|
493 | /**
|
---|
494 | return the lesser of two timevals
|
---|
495 | */
|
---|
496 | struct timeval timeval_min(const struct timeval *tv1,
|
---|
497 | const struct timeval *tv2)
|
---|
498 | {
|
---|
499 | if (tv1->tv_sec < tv2->tv_sec) return *tv1;
|
---|
500 | if (tv1->tv_sec > tv2->tv_sec) return *tv2;
|
---|
501 | if (tv1->tv_usec < tv2->tv_usec) return *tv1;
|
---|
502 | return *tv2;
|
---|
503 | }
|
---|
504 |
|
---|
505 | /**
|
---|
506 | return the greater of two timevals
|
---|
507 | */
|
---|
508 | struct timeval timeval_max(const struct timeval *tv1,
|
---|
509 | const struct timeval *tv2)
|
---|
510 | {
|
---|
511 | if (tv1->tv_sec > tv2->tv_sec) return *tv1;
|
---|
512 | if (tv1->tv_sec < tv2->tv_sec) return *tv2;
|
---|
513 | if (tv1->tv_usec > tv2->tv_usec) return *tv1;
|
---|
514 | return *tv2;
|
---|
515 | }
|
---|
516 |
|
---|
517 | /**
|
---|
518 | return the difference between two timevals as a timeval
|
---|
519 | if tv1 comes after tv2, then return a zero timeval
|
---|
520 | (this is *tv2 - *tv1)
|
---|
521 | */
|
---|
522 | struct timeval timeval_until(const struct timeval *tv1,
|
---|
523 | const struct timeval *tv2)
|
---|
524 | {
|
---|
525 | struct timeval t;
|
---|
526 | if (timeval_compare(tv1, tv2) >= 0) {
|
---|
527 | return timeval_zero();
|
---|
528 | }
|
---|
529 | t.tv_sec = tv2->tv_sec - tv1->tv_sec;
|
---|
530 | if (tv1->tv_usec > tv2->tv_usec) {
|
---|
531 | t.tv_sec--;
|
---|
532 | t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
|
---|
533 | } else {
|
---|
534 | t.tv_usec = tv2->tv_usec - tv1->tv_usec;
|
---|
535 | }
|
---|
536 | return t;
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 | /**
|
---|
541 | convert a timeval to a NTTIME
|
---|
542 | */
|
---|
543 | NTTIME timeval_to_nttime(const struct timeval *tv)
|
---|
544 | {
|
---|
545 | return 10*(tv->tv_usec +
|
---|
546 | ((TIME_FIXUP_CONSTANT_INT + (uint64_t)tv->tv_sec) * 1000000));
|
---|
547 | }
|
---|
548 |
|
---|
549 | /*******************************************************************
|
---|
550 | yield the difference between *A and *B, in seconds, ignoring leap seconds
|
---|
551 | ********************************************************************/
|
---|
552 | static int tm_diff(struct tm *a, struct tm *b)
|
---|
553 | {
|
---|
554 | int ay = a->tm_year + (1900 - 1);
|
---|
555 | int by = b->tm_year + (1900 - 1);
|
---|
556 | int intervening_leap_days =
|
---|
557 | (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
|
---|
558 | int years = ay - by;
|
---|
559 | int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
|
---|
560 | int hours = 24*days + (a->tm_hour - b->tm_hour);
|
---|
561 | int minutes = 60*hours + (a->tm_min - b->tm_min);
|
---|
562 | int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
|
---|
563 |
|
---|
564 | return seconds;
|
---|
565 | }
|
---|
566 |
|
---|
567 | int extra_time_offset=0;
|
---|
568 |
|
---|
569 | /**
|
---|
570 | return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
|
---|
571 | */
|
---|
572 | int get_time_zone(time_t t)
|
---|
573 | {
|
---|
574 | struct tm *tm = gmtime(&t);
|
---|
575 | struct tm tm_utc;
|
---|
576 | if (!tm)
|
---|
577 | return 0;
|
---|
578 | tm_utc = *tm;
|
---|
579 | tm = localtime(&t);
|
---|
580 | if (!tm)
|
---|
581 | return 0;
|
---|
582 | return tm_diff(&tm_utc,tm)+60*extra_time_offset;
|
---|
583 | }
|
---|
584 |
|
---|
585 | /****************************************************************************
|
---|
586 | Check if NTTIME is 0.
|
---|
587 | ****************************************************************************/
|
---|
588 |
|
---|
589 | BOOL nt_time_is_zero(const NTTIME *nt)
|
---|
590 | {
|
---|
591 | return (*nt == 0);
|
---|
592 | }
|
---|
593 |
|
---|
594 | /****************************************************************************
|
---|
595 | Convert ASN.1 GeneralizedTime string to unix-time.
|
---|
596 | Returns 0 on failure; Currently ignores timezone.
|
---|
597 | ****************************************************************************/
|
---|
598 |
|
---|
599 | time_t generalized_to_unix_time(const char *str)
|
---|
600 | {
|
---|
601 | struct tm tm;
|
---|
602 |
|
---|
603 | ZERO_STRUCT(tm);
|
---|
604 |
|
---|
605 | if (sscanf(str, "%4d%2d%2d%2d%2d%2d",
|
---|
606 | &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
|
---|
607 | &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
|
---|
608 | return 0;
|
---|
609 | }
|
---|
610 | tm.tm_year -= 1900;
|
---|
611 | tm.tm_mon -= 1;
|
---|
612 |
|
---|
613 | return timegm(&tm);
|
---|
614 | }
|
---|
615 |
|
---|
616 | /*******************************************************************
|
---|
617 | Accessor function for the server time zone offset.
|
---|
618 | set_server_zone_offset() must have been called first.
|
---|
619 | ******************************************************************/
|
---|
620 |
|
---|
621 | static int server_zone_offset;
|
---|
622 |
|
---|
623 | int get_server_zone_offset(void)
|
---|
624 | {
|
---|
625 | return server_zone_offset;
|
---|
626 | }
|
---|
627 |
|
---|
628 | /*******************************************************************
|
---|
629 | Initialize the server time zone offset. Called when a client connects.
|
---|
630 | ******************************************************************/
|
---|
631 |
|
---|
632 | int set_server_zone_offset(time_t t)
|
---|
633 | {
|
---|
634 | server_zone_offset = get_time_zone(t);
|
---|
635 | return server_zone_offset;
|
---|
636 | }
|
---|
637 |
|
---|
638 | /****************************************************************************
|
---|
639 | Return the date and time as a string
|
---|
640 | ****************************************************************************/
|
---|
641 |
|
---|
642 | char *current_timestring(BOOL hires)
|
---|
643 | {
|
---|
644 | static fstring TimeBuf;
|
---|
645 | struct timeval tp;
|
---|
646 | time_t t;
|
---|
647 | struct tm *tm;
|
---|
648 |
|
---|
649 | if (hires) {
|
---|
650 | GetTimeOfDay(&tp);
|
---|
651 | t = (time_t)tp.tv_sec;
|
---|
652 | } else {
|
---|
653 | t = time(NULL);
|
---|
654 | }
|
---|
655 | tm = localtime(&t);
|
---|
656 | if (!tm) {
|
---|
657 | if (hires) {
|
---|
658 | slprintf(TimeBuf,
|
---|
659 | sizeof(TimeBuf)-1,
|
---|
660 | "%ld.%06ld seconds since the Epoch",
|
---|
661 | (long)tp.tv_sec,
|
---|
662 | (long)tp.tv_usec);
|
---|
663 | } else {
|
---|
664 | slprintf(TimeBuf,
|
---|
665 | sizeof(TimeBuf)-1,
|
---|
666 | "%ld seconds since the Epoch",
|
---|
667 | (long)t);
|
---|
668 | }
|
---|
669 | } else {
|
---|
670 | #ifdef HAVE_STRFTIME
|
---|
671 | if (hires) {
|
---|
672 | strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
|
---|
673 | slprintf(TimeBuf+strlen(TimeBuf),
|
---|
674 | sizeof(TimeBuf)-1 - strlen(TimeBuf),
|
---|
675 | ".%06ld",
|
---|
676 | (long)tp.tv_usec);
|
---|
677 | } else {
|
---|
678 | strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
|
---|
679 | }
|
---|
680 | #else
|
---|
681 | if (hires) {
|
---|
682 | const char *asct = asctime(tm);
|
---|
683 | slprintf(TimeBuf,
|
---|
684 | sizeof(TimeBuf)-1,
|
---|
685 | "%s.%06ld",
|
---|
686 | asct ? asct : "unknown",
|
---|
687 | (long)tp.tv_usec);
|
---|
688 | } else {
|
---|
689 | const char *asct = asctime(tm);
|
---|
690 | fstrcpy(TimeBuf, asct ? asct : "unknown");
|
---|
691 | }
|
---|
692 | #endif
|
---|
693 | }
|
---|
694 | return(TimeBuf);
|
---|
695 | }
|
---|
696 |
|
---|
697 |
|
---|
698 | /*******************************************************************
|
---|
699 | Put a dos date into a buffer (time/date format).
|
---|
700 | This takes GMT time and puts local time in the buffer.
|
---|
701 | ********************************************************************/
|
---|
702 |
|
---|
703 | static void put_dos_date(char *buf,int offset,time_t unixdate, int zone_offset)
|
---|
704 | {
|
---|
705 | uint32 x = make_dos_date(unixdate, zone_offset);
|
---|
706 | SIVAL(buf,offset,x);
|
---|
707 | }
|
---|
708 |
|
---|
709 | /*******************************************************************
|
---|
710 | Put a dos date into a buffer (date/time format).
|
---|
711 | This takes GMT time and puts local time in the buffer.
|
---|
712 | ********************************************************************/
|
---|
713 |
|
---|
714 | static void put_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)
|
---|
715 | {
|
---|
716 | uint32 x = make_dos_date(unixdate, zone_offset);
|
---|
717 | x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
|
---|
718 | SIVAL(buf,offset,x);
|
---|
719 | }
|
---|
720 |
|
---|
721 | /*******************************************************************
|
---|
722 | Put a dos 32 bit "unix like" date into a buffer. This routine takes
|
---|
723 | GMT and converts it to LOCAL time before putting it (most SMBs assume
|
---|
724 | localtime for this sort of date)
|
---|
725 | ********************************************************************/
|
---|
726 |
|
---|
727 | static void put_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
|
---|
728 | {
|
---|
729 | if (!null_mtime(unixdate)) {
|
---|
730 | unixdate -= zone_offset;
|
---|
731 | }
|
---|
732 | SIVAL(buf,offset,unixdate);
|
---|
733 | }
|
---|
734 |
|
---|
735 |
|
---|
736 | /***************************************************************************
|
---|
737 | Server versions of the above functions.
|
---|
738 | ***************************************************************************/
|
---|
739 |
|
---|
740 | void srv_put_dos_date(char *buf,int offset,time_t unixdate)
|
---|
741 | {
|
---|
742 | put_dos_date(buf, offset, unixdate, server_zone_offset);
|
---|
743 | }
|
---|
744 |
|
---|
745 | void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
|
---|
746 | {
|
---|
747 | put_dos_date2(buf, offset, unixdate, server_zone_offset);
|
---|
748 | }
|
---|
749 |
|
---|
750 | void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
|
---|
751 | {
|
---|
752 | put_dos_date3(buf, offset, unixdate, server_zone_offset);
|
---|
753 | }
|
---|
754 |
|
---|
755 | /****************************************************************************
|
---|
756 | Take a Unix time and convert to an NTTIME structure and place in buffer
|
---|
757 | pointed to by p.
|
---|
758 | ****************************************************************************/
|
---|
759 |
|
---|
760 | void put_long_date_timespec(char *p, struct timespec ts)
|
---|
761 | {
|
---|
762 | NTTIME nt;
|
---|
763 | unix_timespec_to_nt_time(&nt, ts);
|
---|
764 | SIVAL(p, 0, nt & 0xFFFFFFFF);
|
---|
765 | SIVAL(p, 4, nt >> 32);
|
---|
766 | }
|
---|
767 |
|
---|
768 | void put_long_date(char *p, time_t t)
|
---|
769 | {
|
---|
770 | struct timespec ts;
|
---|
771 | ts.tv_sec = t;
|
---|
772 | ts.tv_nsec = 0;
|
---|
773 | put_long_date_timespec(p, ts);
|
---|
774 | }
|
---|
775 |
|
---|
776 | /****************************************************************************
|
---|
777 | Return the best approximation to a 'create time' under UNIX from a stat
|
---|
778 | structure.
|
---|
779 | ****************************************************************************/
|
---|
780 |
|
---|
781 | time_t get_create_time(SMB_STRUCT_STAT *st,BOOL fake_dirs)
|
---|
782 | {
|
---|
783 | time_t ret, ret1;
|
---|
784 |
|
---|
785 | if(S_ISDIR(st->st_mode) && fake_dirs) {
|
---|
786 | return (time_t)315493200L; /* 1/1/1980 */
|
---|
787 | }
|
---|
788 |
|
---|
789 | ret = MIN(st->st_ctime, st->st_mtime);
|
---|
790 | ret1 = MIN(ret, st->st_atime);
|
---|
791 |
|
---|
792 | if(ret1 != (time_t)0) {
|
---|
793 | return ret1;
|
---|
794 | }
|
---|
795 |
|
---|
796 | /*
|
---|
797 | * One of ctime, mtime or atime was zero (probably atime).
|
---|
798 | * Just return MIN(ctime, mtime).
|
---|
799 | */
|
---|
800 | return ret;
|
---|
801 | }
|
---|
802 |
|
---|
803 | struct timespec get_create_timespec(SMB_STRUCT_STAT *st,BOOL fake_dirs)
|
---|
804 | {
|
---|
805 | struct timespec ts;
|
---|
806 | ts.tv_sec = get_create_time(st, fake_dirs);
|
---|
807 | ts.tv_nsec = 0;
|
---|
808 | return ts;
|
---|
809 | }
|
---|
810 |
|
---|
811 | /****************************************************************************
|
---|
812 | Get/Set all the possible time fields from a stat struct as a timespec.
|
---|
813 | ****************************************************************************/
|
---|
814 |
|
---|
815 | struct timespec get_atimespec(SMB_STRUCT_STAT *pst)
|
---|
816 | {
|
---|
817 | #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
|
---|
818 | struct timespec ret;
|
---|
819 |
|
---|
820 | /* Old system - no ns timestamp. */
|
---|
821 | ret.tv_sec = pst->st_atime;
|
---|
822 | ret.tv_nsec = 0;
|
---|
823 | return ret;
|
---|
824 | #else
|
---|
825 | #if defined(HAVE_STAT_ST_ATIM)
|
---|
826 | return pst->st_atim;
|
---|
827 | #elif defined(HAVE_STAT_ST_ATIMENSEC)
|
---|
828 | struct timespec ret;
|
---|
829 | ret.tv_sec = pst->st_atime;
|
---|
830 | ret.tv_nsec = pst->st_atimensec;
|
---|
831 | return ret;
|
---|
832 | #else
|
---|
833 | #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
|
---|
834 | #endif
|
---|
835 | #endif
|
---|
836 | }
|
---|
837 |
|
---|
838 | void set_atimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
|
---|
839 | {
|
---|
840 | #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
|
---|
841 | /* Old system - no ns timestamp. */
|
---|
842 | pst->st_atime = ts.tv_sec;
|
---|
843 | #else
|
---|
844 | #if defined(HAVE_STAT_ST_ATIM)
|
---|
845 | pst->st_atim = ts;
|
---|
846 | #elif defined(HAVE_STAT_ST_ATIMENSEC)
|
---|
847 | pst->st_atime = ts.tv_sec;
|
---|
848 | pst->st_atimensec = ts.tv_nsec
|
---|
849 | #else
|
---|
850 | #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
|
---|
851 | #endif
|
---|
852 | #endif
|
---|
853 | }
|
---|
854 |
|
---|
855 | struct timespec get_mtimespec(SMB_STRUCT_STAT *pst)
|
---|
856 | {
|
---|
857 | #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
|
---|
858 | struct timespec ret;
|
---|
859 |
|
---|
860 | /* Old system - no ns timestamp. */
|
---|
861 | ret.tv_sec = pst->st_mtime;
|
---|
862 | ret.tv_nsec = 0;
|
---|
863 | return ret;
|
---|
864 | #else
|
---|
865 | #if defined(HAVE_STAT_ST_MTIM)
|
---|
866 | return pst->st_mtim;
|
---|
867 | #elif defined(HAVE_STAT_ST_MTIMENSEC)
|
---|
868 | struct timespec ret;
|
---|
869 | ret.tv_sec = pst->st_mtime;
|
---|
870 | ret.tv_nsec = pst->st_mtimensec;
|
---|
871 | return ret;
|
---|
872 | #else
|
---|
873 | #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
|
---|
874 | #endif
|
---|
875 | #endif
|
---|
876 | }
|
---|
877 |
|
---|
878 | void set_mtimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
|
---|
879 | {
|
---|
880 | #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
|
---|
881 | /* Old system - no ns timestamp. */
|
---|
882 | pst->st_mtime = ts.tv_sec;
|
---|
883 | #else
|
---|
884 | #if defined(HAVE_STAT_ST_MTIM)
|
---|
885 | pst->st_mtim = ts;
|
---|
886 | #elif defined(HAVE_STAT_ST_MTIMENSEC)
|
---|
887 | pst->st_mtime = ts.tv_sec;
|
---|
888 | pst->st_mtimensec = ts.tv_nsec
|
---|
889 | #else
|
---|
890 | #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
|
---|
891 | #endif
|
---|
892 | #endif
|
---|
893 | }
|
---|
894 |
|
---|
895 | struct timespec get_ctimespec(SMB_STRUCT_STAT *pst)
|
---|
896 | {
|
---|
897 | #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
|
---|
898 | struct timespec ret;
|
---|
899 |
|
---|
900 | /* Old system - no ns timestamp. */
|
---|
901 | ret.tv_sec = pst->st_ctime;
|
---|
902 | ret.tv_nsec = 0;
|
---|
903 | return ret;
|
---|
904 | #else
|
---|
905 | #if defined(HAVE_STAT_ST_CTIM)
|
---|
906 | return pst->st_ctim;
|
---|
907 | #elif defined(HAVE_STAT_ST_CTIMENSEC)
|
---|
908 | struct timespec ret;
|
---|
909 | ret.tv_sec = pst->st_ctime;
|
---|
910 | ret.tv_nsec = pst->st_ctimensec;
|
---|
911 | return ret;
|
---|
912 | #else
|
---|
913 | #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
|
---|
914 | #endif
|
---|
915 | #endif
|
---|
916 | }
|
---|
917 |
|
---|
918 | void set_ctimespec(SMB_STRUCT_STAT *pst, struct timespec ts)
|
---|
919 | {
|
---|
920 | #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
|
---|
921 | /* Old system - no ns timestamp. */
|
---|
922 | pst->st_ctime = ts.tv_sec;
|
---|
923 | #else
|
---|
924 | #if defined(HAVE_STAT_ST_CTIM)
|
---|
925 | pst->st_ctim = ts;
|
---|
926 | #elif defined(HAVE_STAT_ST_CTIMENSEC)
|
---|
927 | pst->st_ctime = ts.tv_sec;
|
---|
928 | pst->st_ctimensec = ts.tv_nsec
|
---|
929 | #else
|
---|
930 | #error CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
|
---|
931 | #endif
|
---|
932 | #endif
|
---|
933 | }
|
---|
934 |
|
---|
935 | void dos_filetime_timespec(struct timespec *tsp)
|
---|
936 | {
|
---|
937 | tsp->tv_sec &= ~1;
|
---|
938 | tsp->tv_nsec = 0;
|
---|
939 | }
|
---|
940 |
|
---|
941 | /*******************************************************************
|
---|
942 | Create a unix date (int GMT) from a dos date (which is actually in
|
---|
943 | localtime).
|
---|
944 | ********************************************************************/
|
---|
945 |
|
---|
946 | static time_t make_unix_date(const void *date_ptr, int zone_offset)
|
---|
947 | {
|
---|
948 | uint32 dos_date=0;
|
---|
949 | struct tm t;
|
---|
950 | time_t ret;
|
---|
951 |
|
---|
952 | dos_date = IVAL(date_ptr,0);
|
---|
953 |
|
---|
954 | if (dos_date == 0) {
|
---|
955 | return 0;
|
---|
956 | }
|
---|
957 |
|
---|
958 | interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
|
---|
959 | &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
|
---|
960 | t.tm_isdst = -1;
|
---|
961 |
|
---|
962 | ret = timegm(&t);
|
---|
963 |
|
---|
964 | ret += zone_offset;
|
---|
965 |
|
---|
966 | return(ret);
|
---|
967 | }
|
---|
968 |
|
---|
969 | /*******************************************************************
|
---|
970 | Like make_unix_date() but the words are reversed.
|
---|
971 | ********************************************************************/
|
---|
972 |
|
---|
973 | static time_t make_unix_date2(const void *date_ptr, int zone_offset)
|
---|
974 | {
|
---|
975 | uint32 x,x2;
|
---|
976 |
|
---|
977 | x = IVAL(date_ptr,0);
|
---|
978 | x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
|
---|
979 | SIVAL(&x,0,x2);
|
---|
980 |
|
---|
981 | return(make_unix_date((const void *)&x, zone_offset));
|
---|
982 | }
|
---|
983 |
|
---|
984 | /*******************************************************************
|
---|
985 | Create a unix GMT date from a dos date in 32 bit "unix like" format
|
---|
986 | these generally arrive as localtimes, with corresponding DST.
|
---|
987 | ******************************************************************/
|
---|
988 |
|
---|
989 | static time_t make_unix_date3(const void *date_ptr, int zone_offset)
|
---|
990 | {
|
---|
991 | time_t t = (time_t)IVAL(date_ptr,0);
|
---|
992 | if (!null_mtime(t)) {
|
---|
993 | t += zone_offset;
|
---|
994 | }
|
---|
995 | return(t);
|
---|
996 | }
|
---|
997 |
|
---|
998 | time_t srv_make_unix_date(const void *date_ptr)
|
---|
999 | {
|
---|
1000 | return make_unix_date(date_ptr, server_zone_offset);
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | time_t srv_make_unix_date2(const void *date_ptr)
|
---|
1004 | {
|
---|
1005 | return make_unix_date2(date_ptr, server_zone_offset);
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | time_t srv_make_unix_date3(const void *date_ptr)
|
---|
1009 | {
|
---|
1010 | return make_unix_date3(date_ptr, server_zone_offset);
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | time_t convert_timespec_to_time_t(struct timespec ts)
|
---|
1014 | {
|
---|
1015 | /* 1 ns == 1,000,000,000 - one thousand millionths of a second.
|
---|
1016 | increment if it's greater than 500 millionth of a second. */
|
---|
1017 | if (ts.tv_nsec > 500000000) {
|
---|
1018 | return ts.tv_sec + 1;
|
---|
1019 | }
|
---|
1020 | return ts.tv_sec;
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | /****************************************************************************
|
---|
1024 | Interprets an nt time into a unix struct timespec.
|
---|
1025 | Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
|
---|
1026 | will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
|
---|
1027 | ****************************************************************************/
|
---|
1028 |
|
---|
1029 | struct timespec interpret_long_date(const char *p)
|
---|
1030 | {
|
---|
1031 | NTTIME nt;
|
---|
1032 | nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);
|
---|
1033 | if (nt == (uint64_t)-1) {
|
---|
1034 | struct timespec ret;
|
---|
1035 | ret.tv_sec = (time_t)-1;
|
---|
1036 | ret.tv_nsec = 0;
|
---|
1037 | return ret;
|
---|
1038 | }
|
---|
1039 | return nt_time_to_unix_timespec(&nt);
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | /***************************************************************************
|
---|
1043 | Client versions of the above functions.
|
---|
1044 | ***************************************************************************/
|
---|
1045 |
|
---|
1046 | void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate)
|
---|
1047 | {
|
---|
1048 | put_dos_date(buf, offset, unixdate, cli->serverzone);
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate)
|
---|
1052 | {
|
---|
1053 | put_dos_date2(buf, offset, unixdate, cli->serverzone);
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate)
|
---|
1057 | {
|
---|
1058 | put_dos_date3(buf, offset, unixdate, cli->serverzone);
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | time_t cli_make_unix_date(struct cli_state *cli, void *date_ptr)
|
---|
1062 | {
|
---|
1063 | return make_unix_date(date_ptr, cli->serverzone);
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | time_t cli_make_unix_date2(struct cli_state *cli, void *date_ptr)
|
---|
1067 | {
|
---|
1068 | return make_unix_date2(date_ptr, cli->serverzone);
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | time_t cli_make_unix_date3(struct cli_state *cli, void *date_ptr)
|
---|
1072 | {
|
---|
1073 | return make_unix_date3(date_ptr, cli->serverzone);
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | /* Large integer version. */
|
---|
1077 | struct timespec nt_time_to_unix_timespec(NTTIME *nt)
|
---|
1078 | {
|
---|
1079 | int64 d;
|
---|
1080 | struct timespec ret;
|
---|
1081 |
|
---|
1082 | if (*nt == 0 || *nt == (int64)-1) {
|
---|
1083 | ret.tv_sec = 0;
|
---|
1084 | ret.tv_nsec = 0;
|
---|
1085 | return ret;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | d = (int64)*nt;
|
---|
1089 | /* d is now in 100ns units, since jan 1st 1601".
|
---|
1090 | Save off the ns fraction. */
|
---|
1091 |
|
---|
1092 | ret.tv_nsec = (long) ((d % 100) * 100);
|
---|
1093 |
|
---|
1094 | /* Convert to seconds */
|
---|
1095 | d /= 1000*1000*10;
|
---|
1096 |
|
---|
1097 | /* Now adjust by 369 years to make the secs since 1970 */
|
---|
1098 | d -= TIME_FIXUP_CONSTANT_INT;
|
---|
1099 |
|
---|
1100 | if (d <= (int64)TIME_T_MIN) {
|
---|
1101 | ret.tv_sec = TIME_T_MIN;
|
---|
1102 | ret.tv_nsec = 0;
|
---|
1103 | return ret;
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | if (d >= (int64)TIME_T_MAX) {
|
---|
1107 | ret.tv_sec = TIME_T_MAX;
|
---|
1108 | ret.tv_nsec = 0;
|
---|
1109 | return ret;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | ret.tv_sec = (time_t)d;
|
---|
1113 | return ret;
|
---|
1114 | }
|
---|
1115 | /****************************************************************************
|
---|
1116 | Check if two NTTIMEs are the same.
|
---|
1117 | ****************************************************************************/
|
---|
1118 |
|
---|
1119 | BOOL nt_time_equals(const NTTIME *nt1, const NTTIME *nt2)
|
---|
1120 | {
|
---|
1121 | return (*nt1 == *nt2);
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | /*******************************************************************
|
---|
1125 | Re-read the smb serverzone value.
|
---|
1126 | ******************************************************************/
|
---|
1127 |
|
---|
1128 | static struct timeval start_time_hires;
|
---|
1129 |
|
---|
1130 | void TimeInit(void)
|
---|
1131 | {
|
---|
1132 | set_server_zone_offset(time(NULL));
|
---|
1133 |
|
---|
1134 | DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
|
---|
1135 |
|
---|
1136 | /* Save the start time of this process. */
|
---|
1137 | if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
|
---|
1138 | GetTimeOfDay(&start_time_hires);
|
---|
1139 | }
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | /**********************************************************************
|
---|
1143 | Return a timeval struct of the uptime of this process. As TimeInit is
|
---|
1144 | done before a daemon fork then this is the start time from the parent
|
---|
1145 | daemon start. JRA.
|
---|
1146 | ***********************************************************************/
|
---|
1147 |
|
---|
1148 | void get_process_uptime(struct timeval *ret_time)
|
---|
1149 | {
|
---|
1150 | struct timeval time_now_hires;
|
---|
1151 |
|
---|
1152 | GetTimeOfDay(&time_now_hires);
|
---|
1153 | ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
|
---|
1154 | if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
|
---|
1155 | ret_time->tv_sec -= 1;
|
---|
1156 | ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
|
---|
1157 | } else {
|
---|
1158 | ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
|
---|
1159 | }
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | /****************************************************************************
|
---|
1163 | Convert a NTTIME structure to a time_t.
|
---|
1164 | It's originally in "100ns units".
|
---|
1165 |
|
---|
1166 | This is an absolute version of the one above.
|
---|
1167 | By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
|
---|
1168 | if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
|
---|
1169 | ****************************************************************************/
|
---|
1170 |
|
---|
1171 | time_t nt_time_to_unix_abs(const NTTIME *nt)
|
---|
1172 | {
|
---|
1173 | uint64 d;
|
---|
1174 |
|
---|
1175 | if (*nt == 0) {
|
---|
1176 | return (time_t)0;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | if (*nt == (uint64)-1) {
|
---|
1180 | return (time_t)-1;
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | /* reverse the time */
|
---|
1184 | /* it's a negative value, turn it to positive */
|
---|
1185 | d=~*nt;
|
---|
1186 |
|
---|
1187 | d += 1000*1000*10/2;
|
---|
1188 | d /= 1000*1000*10;
|
---|
1189 |
|
---|
1190 | if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
|
---|
1191 | return (time_t)0;
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | return (time_t)d;
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | /****************************************************************************
|
---|
1198 | Put a 8 byte filetime from a struct timespec. Uses GMT.
|
---|
1199 | ****************************************************************************/
|
---|
1200 |
|
---|
1201 | void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
|
---|
1202 | {
|
---|
1203 | uint64 d;
|
---|
1204 |
|
---|
1205 | if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
|
---|
1206 | *nt = 0;
|
---|
1207 | return;
|
---|
1208 | }
|
---|
1209 | if (ts.tv_sec == TIME_T_MAX) {
|
---|
1210 | *nt = 0x7fffffffffffffffLL;
|
---|
1211 | return;
|
---|
1212 | }
|
---|
1213 | if (ts.tv_sec == (time_t)-1) {
|
---|
1214 | *nt = (uint64)-1;
|
---|
1215 | return;
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | d = ts.tv_sec;
|
---|
1219 | d += TIME_FIXUP_CONSTANT_INT;
|
---|
1220 | d *= 1000*1000*10;
|
---|
1221 | /* d is now in 100ns units. */
|
---|
1222 | d += (ts.tv_nsec / 100);
|
---|
1223 |
|
---|
1224 | *nt = d;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | /****************************************************************************
|
---|
1228 | Convert a time_t to a NTTIME structure
|
---|
1229 |
|
---|
1230 | This is an absolute version of the one above.
|
---|
1231 | By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
|
---|
1232 | If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
|
---|
1233 | ****************************************************************************/
|
---|
1234 |
|
---|
1235 | void unix_to_nt_time_abs(NTTIME *nt, time_t t)
|
---|
1236 | {
|
---|
1237 | double d;
|
---|
1238 |
|
---|
1239 | if (t==0) {
|
---|
1240 | *nt = 0;
|
---|
1241 | return;
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | if (t == TIME_T_MAX) {
|
---|
1245 | *nt = 0x7fffffffffffffffLL;
|
---|
1246 | return;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | if (t == (time_t)-1) {
|
---|
1250 | /* that's what NT uses for infinite */
|
---|
1251 | *nt = 0x8000000000000000LL;
|
---|
1252 | return;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | d = (double)(t);
|
---|
1256 | d *= 1.0e7;
|
---|
1257 |
|
---|
1258 | *nt = d;
|
---|
1259 |
|
---|
1260 | /* convert to a negative value */
|
---|
1261 | *nt=~*nt;
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 |
|
---|
1265 | /****************************************************************************
|
---|
1266 | Check if it's a null mtime.
|
---|
1267 | ****************************************************************************/
|
---|
1268 |
|
---|
1269 | BOOL null_mtime(time_t mtime)
|
---|
1270 | {
|
---|
1271 | if (mtime == 0 || mtime == (time_t)0xFFFFFFFF || mtime == (time_t)-1)
|
---|
1272 | return(True);
|
---|
1273 | return(False);
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | /****************************************************************************
|
---|
1277 | Utility function that always returns a const string even if localtime
|
---|
1278 | and asctime fail.
|
---|
1279 | ****************************************************************************/
|
---|
1280 |
|
---|
1281 | const char *time_to_asc(const time_t *t)
|
---|
1282 | {
|
---|
1283 | const char *asct;
|
---|
1284 | struct tm *lt = localtime(t);
|
---|
1285 |
|
---|
1286 | if (!lt) {
|
---|
1287 | return "unknown time";
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | asct = asctime(lt);
|
---|
1291 | if (!asct) {
|
---|
1292 | return "unknown time";
|
---|
1293 | }
|
---|
1294 | return asct;
|
---|
1295 | }
|
---|
1296 |
|
---|
1297 | const char *display_time(NTTIME nttime)
|
---|
1298 | {
|
---|
1299 | static fstring string;
|
---|
1300 |
|
---|
1301 | float high;
|
---|
1302 | float low;
|
---|
1303 | int sec;
|
---|
1304 | int days, hours, mins, secs;
|
---|
1305 |
|
---|
1306 | if (nttime==0)
|
---|
1307 | return "Now";
|
---|
1308 |
|
---|
1309 | if (nttime==0x8000000000000000LL)
|
---|
1310 | return "Never";
|
---|
1311 |
|
---|
1312 | high = 65536;
|
---|
1313 | high = high/10000;
|
---|
1314 | high = high*65536;
|
---|
1315 | high = high/1000;
|
---|
1316 | high = high * (~(nttime >> 32));
|
---|
1317 |
|
---|
1318 | low = ~(nttime & 0xFFFFFFFF);
|
---|
1319 | low = low/(1000*1000*10);
|
---|
1320 |
|
---|
1321 | sec=high+low;
|
---|
1322 |
|
---|
1323 | days=sec/(60*60*24);
|
---|
1324 | hours=(sec - (days*60*60*24)) / (60*60);
|
---|
1325 | mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
|
---|
1326 | secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
|
---|
1327 |
|
---|
1328 | fstr_sprintf(string, "%u days, %u hours, %u minutes, %u seconds", days, hours, mins, secs);
|
---|
1329 | return (string);
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 | BOOL nt_time_is_set(const NTTIME *nt)
|
---|
1333 | {
|
---|
1334 | if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
|
---|
1335 | return False;
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | if (*nt == 0x8000000000000000LL) {
|
---|
1339 | return False;
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | return True;
|
---|
1343 | }
|
---|
1344 |
|
---|