libsmb: Pass NTTIME to interpret_long_date()
[Samba.git] / source3 / lib / time.c
blobec93f6c534714285a195cc87ed80d2cb70ace018
1 /*
2 Unix SMB/CIFS implementation.
3 time handling functions
5 Copyright (C) Andrew Tridgell 1992-2004
6 Copyright (C) Stefan (metze) Metzmacher 2002
7 Copyright (C) Jeremy Allison 2007
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.
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.
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/>.
23 #include "includes.h"
25 /**
26 * @file
27 * @brief time handling functions
31 #define NTTIME_INFINITY (NTTIME)0x8000000000000000LL
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
39 /**************************************************************
40 Handle conversions between time_t and uint32, taking care to
41 preserve the "special" values.
42 **************************************************************/
44 uint32_t convert_time_t_to_uint32_t(time_t t)
46 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
47 /* time_t is 64-bit. */
48 if (t == 0x8000000000000000LL) {
49 return 0x80000000;
50 } else if (t == 0x7FFFFFFFFFFFFFFFLL) {
51 return 0x7FFFFFFF;
53 #endif
54 return (uint32_t)t;
57 time_t convert_uint32_t_to_time_t(uint32_t u)
59 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
60 /* time_t is 64-bit. */
61 if (u == 0x80000000) {
62 return (time_t)0x8000000000000000LL;
63 } else if (u == 0x7FFFFFFF) {
64 return (time_t)0x7FFFFFFFFFFFFFFFLL;
66 #endif
67 return (time_t)u;
70 /****************************************************************************
71 Check if NTTIME is 0.
72 ****************************************************************************/
74 bool nt_time_is_zero(const NTTIME *nt)
76 return (*nt == 0);
79 /****************************************************************************
80 Convert ASN.1 GeneralizedTime string to unix-time.
81 Returns 0 on failure; Currently ignores timezone.
82 ****************************************************************************/
84 time_t generalized_to_unix_time(const char *str)
86 struct tm tm;
88 ZERO_STRUCT(tm);
90 if (sscanf(str, "%4d%2d%2d%2d%2d%2d",
91 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
92 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
93 return 0;
95 tm.tm_year -= 1900;
96 tm.tm_mon -= 1;
98 return timegm(&tm);
101 /*******************************************************************
102 Accessor function for the server time zone offset.
103 set_server_zone_offset() must have been called first.
104 ******************************************************************/
106 static int server_zone_offset;
108 int get_server_zone_offset(void)
110 return server_zone_offset;
113 /*******************************************************************
114 Initialize the server time zone offset. Called when a client connects.
115 ******************************************************************/
117 int set_server_zone_offset(time_t t)
119 server_zone_offset = get_time_zone(t);
120 return server_zone_offset;
123 /***************************************************************************
124 Server versions of the above functions.
125 ***************************************************************************/
127 void srv_put_dos_date(char *buf,int offset,time_t unixdate)
129 push_dos_date((uint8_t *)buf, offset, unixdate, server_zone_offset);
132 void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
134 push_dos_date2((uint8_t *)buf, offset, unixdate, server_zone_offset);
137 void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
139 push_dos_date3((uint8_t *)buf, offset, unixdate, server_zone_offset);
142 void round_timespec(enum timestamp_set_resolution res, struct timespec *ts)
144 if (is_omit_timespec(ts)) {
145 return;
148 switch (res) {
149 case TIMESTAMP_SET_SECONDS:
150 round_timespec_to_sec(ts);
151 break;
152 case TIMESTAMP_SET_MSEC:
153 round_timespec_to_usec(ts);
154 break;
155 case TIMESTAMP_SET_NT_OR_BETTER:
156 /* No rounding needed. */
157 break;
161 /****************************************************************************
162 Take a Unix time and convert to an NTTIME structure and place in buffer
163 pointed to by p, rounded to the correct resolution.
164 ****************************************************************************/
166 void put_long_date_timespec(enum timestamp_set_resolution res, char *p, struct timespec ts)
168 NTTIME nt;
169 round_timespec(res, &ts);
170 nt = unix_timespec_to_nt_time(ts);
171 SBVAL(p, 0, nt);
174 void put_long_date_full_timespec(enum timestamp_set_resolution res,
175 char *p,
176 const struct timespec *_ts)
178 struct timespec ts = *_ts;
179 NTTIME nt;
181 round_timespec(res, &ts);
182 nt = full_timespec_to_nt_time(&ts);
183 SBVAL(p, 0, nt);
186 struct timespec pull_long_date_full_timespec(const char *p)
188 NTTIME nt = BVAL(p, 0);
190 return nt_time_to_full_timespec(nt);
193 void put_long_date(char *p, time_t t)
195 struct timespec ts;
196 ts.tv_sec = t;
197 ts.tv_nsec = 0;
198 put_long_date_timespec(TIMESTAMP_SET_SECONDS, p, ts);
201 void dos_filetime_timespec(struct timespec *tsp)
203 tsp->tv_sec &= ~1;
204 tsp->tv_nsec = 0;
207 /*******************************************************************
208 Create a unix date (int GMT) from a dos date (which is actually in
209 localtime).
210 ********************************************************************/
212 time_t make_unix_date(const void *date_ptr, int zone_offset)
214 return pull_dos_date(date_ptr, zone_offset);
217 /*******************************************************************
218 Like make_unix_date() but the words are reversed.
219 ********************************************************************/
221 time_t make_unix_date2(const void *date_ptr, int zone_offset)
223 return pull_dos_date2(date_ptr, zone_offset);
226 /*******************************************************************
227 Create a unix GMT date from a dos date in 32 bit "unix like" format
228 these generally arrive as localtimes, with corresponding DST.
229 ******************************************************************/
231 time_t make_unix_date3(const void *date_ptr, int zone_offset)
233 return pull_dos_date3(date_ptr, zone_offset);
236 time_t srv_make_unix_date(const void *date_ptr)
238 return make_unix_date(date_ptr, server_zone_offset);
241 time_t srv_make_unix_date2(const void *date_ptr)
243 return make_unix_date2(date_ptr, server_zone_offset);
246 time_t srv_make_unix_date3(const void *date_ptr)
248 return make_unix_date3(date_ptr, server_zone_offset);
251 /****************************************************************************
252 Interprets an nt time into a unix struct timespec.
253 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
254 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
255 ****************************************************************************/
257 struct timespec interpret_long_date(NTTIME nt)
259 if (nt == (uint64_t)-1) {
260 struct timespec ret;
261 ret.tv_sec = (time_t)-1;
262 ret.tv_nsec = 0;
263 return ret;
265 return nt_time_to_full_timespec(nt);
268 /*******************************************************************
269 Re-read the smb serverzone value.
270 ******************************************************************/
272 static struct timeval start_time_hires;
274 void TimeInit(void)
276 set_server_zone_offset(time(NULL));
278 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
280 /* Save the start time of this process. */
281 if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
282 GetTimeOfDay(&start_time_hires);
286 /**********************************************************************
287 Return a timeval struct of the uptime of this process. As TimeInit is
288 done before a daemon fork then this is the start time from the parent
289 daemon start. JRA.
290 ***********************************************************************/
292 void get_process_uptime(struct timeval *ret_time)
294 struct timeval time_now_hires;
296 GetTimeOfDay(&time_now_hires);
297 ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
298 if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
299 ret_time->tv_sec -= 1;
300 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
301 } else {
302 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
307 * @brief Get the startup time of the server.
309 * @param[out] ret_time A pointer to a timveal structure to set the startup
310 * time.
312 void get_startup_time(struct timeval *ret_time)
314 ret_time->tv_sec = start_time_hires.tv_sec;
315 ret_time->tv_usec = start_time_hires.tv_usec;
319 /****************************************************************************
320 Convert a NTTIME structure to a time_t.
321 It's originally in "100ns units".
323 This is an absolute version of the one above.
324 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
325 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
326 ****************************************************************************/
328 time_t nt_time_to_unix_abs(const NTTIME *nt)
330 uint64_t d;
332 if (*nt == 0) {
333 return (time_t)0;
336 if (*nt == (uint64_t)-1) {
337 return (time_t)-1;
340 if (*nt == NTTIME_INFINITY) {
341 return (time_t)-1;
344 /* reverse the time */
345 /* it's a negative value, turn it to positive */
346 d=~*nt;
348 d += 1000*1000*10/2;
349 d /= 1000*1000*10;
351 if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
352 return (time_t)0;
355 return (time_t)d;
358 /****************************************************************************
359 Convert a time_t to a NTTIME structure
361 This is an absolute version of the one above.
362 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
363 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
364 ****************************************************************************/
366 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
368 double d;
370 if (t==0) {
371 *nt = 0;
372 return;
375 if (t == TIME_T_MAX) {
376 *nt = 0x7fffffffffffffffLL;
377 return;
380 if (t == (time_t)-1) {
381 /* that's what NT uses for infinite */
382 *nt = NTTIME_INFINITY;
383 return;
386 d = (double)(t);
387 d *= 1.0e7;
389 *nt = (NTTIME)d;
391 /* convert to a negative value */
392 *nt=~*nt;
396 /****************************************************************************
397 Utility function that always returns a const string even if localtime
398 and asctime fail.
399 ****************************************************************************/
401 const char *time_to_asc(const time_t t)
403 const char *asct;
404 struct tm *lt = localtime(&t);
406 if (!lt) {
407 return "unknown time\n";
410 asct = asctime(lt);
411 if (!asct) {
412 return "unknown time\n";
414 return asct;
417 const char *display_time(NTTIME nttime)
419 float high;
420 float low;
421 int sec;
422 int days, hours, mins, secs;
424 if (nttime==0)
425 return "Now";
427 if (nttime==NTTIME_INFINITY)
428 return "Never";
430 high = 65536;
431 high = high/10000;
432 high = high*65536;
433 high = high/1000;
434 high = high * (~(nttime >> 32));
436 low = ~(nttime & 0xFFFFFFFF);
437 low = low/(1000*1000*10);
439 sec=(int)(high+low);
441 days=sec/(60*60*24);
442 hours=(sec - (days*60*60*24)) / (60*60);
443 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
444 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
446 return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
447 "%u seconds", days, hours, mins, secs);
450 bool nt_time_is_set(const NTTIME *nt)
452 if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
453 return false;
456 if (*nt == NTTIME_INFINITY) {
457 return false;
460 return true;