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 | Copyright (C) Jeremy Allison 2007
|
---|
8 |
|
---|
9 | This program is free software; you can redistribute it and/or modify
|
---|
10 | it under the terms of the GNU General Public License as published by
|
---|
11 | the Free Software Foundation; either version 3 of the License, or
|
---|
12 | (at your option) any later version.
|
---|
13 |
|
---|
14 | This program is distributed in the hope that it will be useful,
|
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | GNU General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU General Public License
|
---|
20 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "includes.h"
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @file
|
---|
27 | * @brief time handling functions
|
---|
28 | */
|
---|
29 |
|
---|
30 |
|
---|
31 | #define NTTIME_INFINITY (NTTIME)0x8000000000000000LL
|
---|
32 |
|
---|
33 | #if (SIZEOF_LONG == 8)
|
---|
34 | #define TIME_FIXUP_CONSTANT_INT 11644473600L
|
---|
35 | #elif (SIZEOF_LONG_LONG == 8)
|
---|
36 | #define TIME_FIXUP_CONSTANT_INT 11644473600LL
|
---|
37 | #endif
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | parse a nttime as a large integer in a string and return a NTTIME
|
---|
42 | */
|
---|
43 | NTTIME nttime_from_string(const char *s)
|
---|
44 | {
|
---|
45 | return strtoull(s, NULL, 0);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**************************************************************
|
---|
49 | Handle conversions between time_t and uint32, taking care to
|
---|
50 | preserve the "special" values.
|
---|
51 | **************************************************************/
|
---|
52 |
|
---|
53 | uint32_t convert_time_t_to_uint32_t(time_t t)
|
---|
54 | {
|
---|
55 | #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
|
---|
56 | /* time_t is 64-bit. */
|
---|
57 | if (t == 0x8000000000000000LL) {
|
---|
58 | return 0x80000000;
|
---|
59 | } else if (t == 0x7FFFFFFFFFFFFFFFLL) {
|
---|
60 | return 0x7FFFFFFF;
|
---|
61 | }
|
---|
62 | #endif
|
---|
63 | return (uint32_t)t;
|
---|
64 | }
|
---|
65 |
|
---|
66 | time_t convert_uint32_t_to_time_t(uint32_t u)
|
---|
67 | {
|
---|
68 | #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
|
---|
69 | /* time_t is 64-bit. */
|
---|
70 | if (u == 0x80000000) {
|
---|
71 | return (time_t)0x8000000000000000LL;
|
---|
72 | } else if (u == 0x7FFFFFFF) {
|
---|
73 | return (time_t)0x7FFFFFFFFFFFFFFFLL;
|
---|
74 | }
|
---|
75 | #endif
|
---|
76 | return (time_t)u;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /****************************************************************************
|
---|
80 | Check if NTTIME is 0.
|
---|
81 | ****************************************************************************/
|
---|
82 |
|
---|
83 | bool nt_time_is_zero(const NTTIME *nt)
|
---|
84 | {
|
---|
85 | return (*nt == 0);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /****************************************************************************
|
---|
89 | Convert ASN.1 GeneralizedTime string to unix-time.
|
---|
90 | Returns 0 on failure; Currently ignores timezone.
|
---|
91 | ****************************************************************************/
|
---|
92 |
|
---|
93 | time_t generalized_to_unix_time(const char *str)
|
---|
94 | {
|
---|
95 | struct tm tm;
|
---|
96 |
|
---|
97 | ZERO_STRUCT(tm);
|
---|
98 |
|
---|
99 | if (sscanf(str, "%4d%2d%2d%2d%2d%2d",
|
---|
100 | &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
|
---|
101 | &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
|
---|
102 | return 0;
|
---|
103 | }
|
---|
104 | tm.tm_year -= 1900;
|
---|
105 | tm.tm_mon -= 1;
|
---|
106 |
|
---|
107 | return timegm(&tm);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /*******************************************************************
|
---|
111 | Accessor function for the server time zone offset.
|
---|
112 | set_server_zone_offset() must have been called first.
|
---|
113 | ******************************************************************/
|
---|
114 |
|
---|
115 | static int server_zone_offset;
|
---|
116 |
|
---|
117 | int get_server_zone_offset(void)
|
---|
118 | {
|
---|
119 | return server_zone_offset;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /*******************************************************************
|
---|
123 | Initialize the server time zone offset. Called when a client connects.
|
---|
124 | ******************************************************************/
|
---|
125 |
|
---|
126 | int set_server_zone_offset(time_t t)
|
---|
127 | {
|
---|
128 | server_zone_offset = get_time_zone(t);
|
---|
129 | return server_zone_offset;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /***************************************************************************
|
---|
133 | Server versions of the above functions.
|
---|
134 | ***************************************************************************/
|
---|
135 |
|
---|
136 | void srv_put_dos_date(char *buf,int offset,time_t unixdate)
|
---|
137 | {
|
---|
138 | push_dos_date((uint8_t *)buf, offset, unixdate, server_zone_offset);
|
---|
139 | }
|
---|
140 |
|
---|
141 | void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
|
---|
142 | {
|
---|
143 | push_dos_date2((uint8_t *)buf, offset, unixdate, server_zone_offset);
|
---|
144 | }
|
---|
145 |
|
---|
146 | void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
|
---|
147 | {
|
---|
148 | push_dos_date3((uint8_t *)buf, offset, unixdate, server_zone_offset);
|
---|
149 | }
|
---|
150 |
|
---|
151 | void round_timespec(enum timestamp_set_resolution res, struct timespec *ts)
|
---|
152 | {
|
---|
153 | switch (res) {
|
---|
154 | case TIMESTAMP_SET_SECONDS:
|
---|
155 | round_timespec_to_sec(ts);
|
---|
156 | break;
|
---|
157 | case TIMESTAMP_SET_MSEC:
|
---|
158 | round_timespec_to_usec(ts);
|
---|
159 | break;
|
---|
160 | case TIMESTAMP_SET_NT_OR_BETTER:
|
---|
161 | /* No rounding needed. */
|
---|
162 | break;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | /****************************************************************************
|
---|
167 | Take a Unix time and convert to an NTTIME structure and place in buffer
|
---|
168 | pointed to by p, rounded to the correct resolution.
|
---|
169 | ****************************************************************************/
|
---|
170 |
|
---|
171 | void put_long_date_timespec(enum timestamp_set_resolution res, char *p, struct timespec ts)
|
---|
172 | {
|
---|
173 | NTTIME nt;
|
---|
174 | round_timespec(res, &ts);
|
---|
175 | nt = unix_timespec_to_nt_time(ts);
|
---|
176 | SBVAL(p, 0, nt);
|
---|
177 | }
|
---|
178 |
|
---|
179 | void put_long_date(char *p, time_t t)
|
---|
180 | {
|
---|
181 | struct timespec ts;
|
---|
182 | ts.tv_sec = t;
|
---|
183 | ts.tv_nsec = 0;
|
---|
184 | put_long_date_timespec(TIMESTAMP_SET_SECONDS, p, ts);
|
---|
185 | }
|
---|
186 |
|
---|
187 | void dos_filetime_timespec(struct timespec *tsp)
|
---|
188 | {
|
---|
189 | tsp->tv_sec &= ~1;
|
---|
190 | tsp->tv_nsec = 0;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /*******************************************************************
|
---|
194 | Create a unix date (int GMT) from a dos date (which is actually in
|
---|
195 | localtime).
|
---|
196 | ********************************************************************/
|
---|
197 |
|
---|
198 | time_t make_unix_date(const void *date_ptr, int zone_offset)
|
---|
199 | {
|
---|
200 | return pull_dos_date(date_ptr, zone_offset);
|
---|
201 | }
|
---|
202 |
|
---|
203 | /*******************************************************************
|
---|
204 | Like make_unix_date() but the words are reversed.
|
---|
205 | ********************************************************************/
|
---|
206 |
|
---|
207 | time_t make_unix_date2(const void *date_ptr, int zone_offset)
|
---|
208 | {
|
---|
209 | return pull_dos_date2(date_ptr, zone_offset);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /*******************************************************************
|
---|
213 | Create a unix GMT date from a dos date in 32 bit "unix like" format
|
---|
214 | these generally arrive as localtimes, with corresponding DST.
|
---|
215 | ******************************************************************/
|
---|
216 |
|
---|
217 | time_t make_unix_date3(const void *date_ptr, int zone_offset)
|
---|
218 | {
|
---|
219 | return pull_dos_date3(date_ptr, zone_offset);
|
---|
220 | }
|
---|
221 |
|
---|
222 | time_t srv_make_unix_date(const void *date_ptr)
|
---|
223 | {
|
---|
224 | return make_unix_date(date_ptr, server_zone_offset);
|
---|
225 | }
|
---|
226 |
|
---|
227 | time_t srv_make_unix_date2(const void *date_ptr)
|
---|
228 | {
|
---|
229 | return make_unix_date2(date_ptr, server_zone_offset);
|
---|
230 | }
|
---|
231 |
|
---|
232 | time_t srv_make_unix_date3(const void *date_ptr)
|
---|
233 | {
|
---|
234 | return make_unix_date3(date_ptr, server_zone_offset);
|
---|
235 | }
|
---|
236 |
|
---|
237 | /****************************************************************************
|
---|
238 | Interprets an nt time into a unix struct timespec.
|
---|
239 | Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
|
---|
240 | will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
|
---|
241 | ****************************************************************************/
|
---|
242 |
|
---|
243 | struct timespec interpret_long_date(const char *p)
|
---|
244 | {
|
---|
245 | NTTIME nt;
|
---|
246 | nt = BVAL(p, 0);
|
---|
247 | if (nt == (uint64_t)-1) {
|
---|
248 | struct timespec ret;
|
---|
249 | ret.tv_sec = (time_t)-1;
|
---|
250 | ret.tv_nsec = 0;
|
---|
251 | return ret;
|
---|
252 | }
|
---|
253 | return nt_time_to_unix_timespec(nt);
|
---|
254 | }
|
---|
255 |
|
---|
256 | /*******************************************************************
|
---|
257 | Re-read the smb serverzone value.
|
---|
258 | ******************************************************************/
|
---|
259 |
|
---|
260 | static struct timeval start_time_hires;
|
---|
261 |
|
---|
262 | void TimeInit(void)
|
---|
263 | {
|
---|
264 | set_server_zone_offset(time(NULL));
|
---|
265 |
|
---|
266 | DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
|
---|
267 |
|
---|
268 | /* Save the start time of this process. */
|
---|
269 | if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
|
---|
270 | GetTimeOfDay(&start_time_hires);
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**********************************************************************
|
---|
275 | Return a timeval struct of the uptime of this process. As TimeInit is
|
---|
276 | done before a daemon fork then this is the start time from the parent
|
---|
277 | daemon start. JRA.
|
---|
278 | ***********************************************************************/
|
---|
279 |
|
---|
280 | void get_process_uptime(struct timeval *ret_time)
|
---|
281 | {
|
---|
282 | struct timeval time_now_hires;
|
---|
283 |
|
---|
284 | GetTimeOfDay(&time_now_hires);
|
---|
285 | ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
|
---|
286 | if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
|
---|
287 | ret_time->tv_sec -= 1;
|
---|
288 | ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
|
---|
289 | } else {
|
---|
290 | ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * @brief Get the startup time of the server.
|
---|
296 | *
|
---|
297 | * @param[out] ret_time A pointer to a timveal structure to set the startup
|
---|
298 | * time.
|
---|
299 | */
|
---|
300 | void get_startup_time(struct timeval *ret_time)
|
---|
301 | {
|
---|
302 | ret_time->tv_sec = start_time_hires.tv_sec;
|
---|
303 | ret_time->tv_usec = start_time_hires.tv_usec;
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 | /****************************************************************************
|
---|
308 | Convert a NTTIME structure to a time_t.
|
---|
309 | It's originally in "100ns units".
|
---|
310 |
|
---|
311 | This is an absolute version of the one above.
|
---|
312 | By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
|
---|
313 | if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
|
---|
314 | ****************************************************************************/
|
---|
315 |
|
---|
316 | time_t nt_time_to_unix_abs(const NTTIME *nt)
|
---|
317 | {
|
---|
318 | uint64_t d;
|
---|
319 |
|
---|
320 | if (*nt == 0) {
|
---|
321 | return (time_t)0;
|
---|
322 | }
|
---|
323 |
|
---|
324 | if (*nt == (uint64_t)-1) {
|
---|
325 | return (time_t)-1;
|
---|
326 | }
|
---|
327 |
|
---|
328 | if (*nt == NTTIME_INFINITY) {
|
---|
329 | return (time_t)-1;
|
---|
330 | }
|
---|
331 |
|
---|
332 | /* reverse the time */
|
---|
333 | /* it's a negative value, turn it to positive */
|
---|
334 | d=~*nt;
|
---|
335 |
|
---|
336 | d += 1000*1000*10/2;
|
---|
337 | d /= 1000*1000*10;
|
---|
338 |
|
---|
339 | if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
|
---|
340 | return (time_t)0;
|
---|
341 | }
|
---|
342 |
|
---|
343 | return (time_t)d;
|
---|
344 | }
|
---|
345 |
|
---|
346 | time_t uint64s_nt_time_to_unix_abs(const uint64_t *src)
|
---|
347 | {
|
---|
348 | NTTIME nttime;
|
---|
349 | nttime = *src;
|
---|
350 | return nt_time_to_unix_abs(&nttime);
|
---|
351 | }
|
---|
352 |
|
---|
353 | /****************************************************************************
|
---|
354 | Convert a time_t to a NTTIME structure
|
---|
355 |
|
---|
356 | This is an absolute version of the one above.
|
---|
357 | By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
|
---|
358 | If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
|
---|
359 | ****************************************************************************/
|
---|
360 |
|
---|
361 | void unix_to_nt_time_abs(NTTIME *nt, time_t t)
|
---|
362 | {
|
---|
363 | double d;
|
---|
364 |
|
---|
365 | if (t==0) {
|
---|
366 | *nt = 0;
|
---|
367 | return;
|
---|
368 | }
|
---|
369 |
|
---|
370 | if (t == TIME_T_MAX) {
|
---|
371 | *nt = 0x7fffffffffffffffLL;
|
---|
372 | return;
|
---|
373 | }
|
---|
374 |
|
---|
375 | if (t == (time_t)-1) {
|
---|
376 | /* that's what NT uses for infinite */
|
---|
377 | *nt = NTTIME_INFINITY;
|
---|
378 | return;
|
---|
379 | }
|
---|
380 |
|
---|
381 | d = (double)(t);
|
---|
382 | d *= 1.0e7;
|
---|
383 |
|
---|
384 | *nt = (NTTIME)d;
|
---|
385 |
|
---|
386 | /* convert to a negative value */
|
---|
387 | *nt=~*nt;
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | /****************************************************************************
|
---|
392 | Utility function that always returns a const string even if localtime
|
---|
393 | and asctime fail.
|
---|
394 | ****************************************************************************/
|
---|
395 |
|
---|
396 | const char *time_to_asc(const time_t t)
|
---|
397 | {
|
---|
398 | const char *asct;
|
---|
399 | struct tm *lt = localtime(&t);
|
---|
400 |
|
---|
401 | if (!lt) {
|
---|
402 | return "unknown time";
|
---|
403 | }
|
---|
404 |
|
---|
405 | asct = asctime(lt);
|
---|
406 | if (!asct) {
|
---|
407 | return "unknown time";
|
---|
408 | }
|
---|
409 | return asct;
|
---|
410 | }
|
---|
411 |
|
---|
412 | const char *display_time(NTTIME nttime)
|
---|
413 | {
|
---|
414 | float high;
|
---|
415 | float low;
|
---|
416 | int sec;
|
---|
417 | int days, hours, mins, secs;
|
---|
418 |
|
---|
419 | if (nttime==0)
|
---|
420 | return "Now";
|
---|
421 |
|
---|
422 | if (nttime==NTTIME_INFINITY)
|
---|
423 | return "Never";
|
---|
424 |
|
---|
425 | high = 65536;
|
---|
426 | high = high/10000;
|
---|
427 | high = high*65536;
|
---|
428 | high = high/1000;
|
---|
429 | high = high * (~(nttime >> 32));
|
---|
430 |
|
---|
431 | low = ~(nttime & 0xFFFFFFFF);
|
---|
432 | low = low/(1000*1000*10);
|
---|
433 |
|
---|
434 | sec=(int)(high+low);
|
---|
435 |
|
---|
436 | days=sec/(60*60*24);
|
---|
437 | hours=(sec - (days*60*60*24)) / (60*60);
|
---|
438 | mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
|
---|
439 | secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
|
---|
440 |
|
---|
441 | return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
|
---|
442 | "%u seconds", days, hours, mins, secs);
|
---|
443 | }
|
---|
444 |
|
---|
445 | bool nt_time_is_set(const NTTIME *nt)
|
---|
446 | {
|
---|
447 | if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
|
---|
448 | return false;
|
---|
449 | }
|
---|
450 |
|
---|
451 | if (*nt == NTTIME_INFINITY) {
|
---|
452 | return false;
|
---|
453 | }
|
---|
454 |
|
---|
455 | return true;
|
---|
456 | }
|
---|