s3:utils: let smbstatus report anonymous signing/encryption explicitly
[Samba.git] / source3 / lib / time.c
blob0d44a08d3f399e6c197ea35f9210ffe07b0b543a
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 #define TIME_FIXUP_CONSTANT_INT INT64_C(11644473600)
35 /**************************************************************
36 Handle conversions between time_t and uint32, taking care to
37 preserve the "special" values.
38 **************************************************************/
40 uint32_t convert_time_t_to_uint32_t(time_t t)
42 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
43 /* time_t is 64-bit. */
44 if (t == 0x8000000000000000LL) {
45 return 0x80000000;
46 } else if (t == 0x7FFFFFFFFFFFFFFFLL) {
47 return 0x7FFFFFFF;
49 #endif
50 return (uint32_t)t;
53 time_t convert_uint32_t_to_time_t(uint32_t u)
55 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
56 /* time_t is 64-bit. */
57 if (u == 0x80000000) {
58 return (time_t)0x8000000000000000LL;
59 } else if (u == 0x7FFFFFFF) {
60 return (time_t)0x7FFFFFFFFFFFFFFFLL;
62 #endif
63 return (time_t)u;
66 /****************************************************************************
67 Check if NTTIME is 0.
68 ****************************************************************************/
70 bool nt_time_is_zero(const NTTIME *nt)
72 return (*nt == 0);
75 /****************************************************************************
76 Convert ASN.1 GeneralizedTime string to unix-time.
77 Returns 0 on failure; Currently ignores timezone.
78 ****************************************************************************/
80 time_t generalized_to_unix_time(const char *str)
82 struct tm tm;
84 ZERO_STRUCT(tm);
86 if (sscanf(str, "%4d%2d%2d%2d%2d%2d",
87 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
88 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
89 return 0;
91 tm.tm_year -= 1900;
92 tm.tm_mon -= 1;
94 return timegm(&tm);
97 /*******************************************************************
98 Accessor function for the server time zone offset.
99 set_server_zone_offset() must have been called first.
100 ******************************************************************/
102 static int server_zone_offset;
104 int get_server_zone_offset(void)
106 return server_zone_offset;
109 /*******************************************************************
110 Initialize the server time zone offset. Called when a client connects.
111 ******************************************************************/
113 int set_server_zone_offset(time_t t)
115 server_zone_offset = get_time_zone(t);
116 return server_zone_offset;
119 /***************************************************************************
120 Server versions of the above functions.
121 ***************************************************************************/
123 void srv_put_dos_date(char *buf,int offset,time_t unixdate)
125 push_dos_date((uint8_t *)buf, offset, unixdate, server_zone_offset);
128 void srv_put_dos_date2_ts(char *buf, int offset, struct timespec unix_ts)
130 time_t unixdate = convert_timespec_to_time_t(unix_ts);
131 push_dos_date2((uint8_t *)buf, offset, unixdate, server_zone_offset);
134 void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
136 push_dos_date3((uint8_t *)buf, offset, unixdate, server_zone_offset);
139 void round_timespec(enum timestamp_set_resolution res, struct timespec *ts)
141 if (is_omit_timespec(ts)) {
142 return;
145 switch (res) {
146 case TIMESTAMP_SET_SECONDS:
147 round_timespec_to_sec(ts);
148 break;
149 case TIMESTAMP_SET_MSEC:
150 round_timespec_to_usec(ts);
151 break;
152 case TIMESTAMP_SET_NT_OR_BETTER:
153 /* No rounding needed. */
154 break;
158 /****************************************************************************
159 Take a Unix time and convert to an NTTIME structure and place in buffer
160 pointed to by p, rounded to the correct resolution.
161 ****************************************************************************/
163 void put_long_date_timespec(enum timestamp_set_resolution res, char *p, struct timespec ts)
165 NTTIME nt;
166 round_timespec(res, &ts);
167 nt = unix_timespec_to_nt_time(ts);
168 SBVAL(p, 0, nt);
171 void put_long_date_full_timespec(enum timestamp_set_resolution res,
172 char *p,
173 const struct timespec *_ts)
175 struct timespec ts = *_ts;
176 NTTIME nt;
178 round_timespec(res, &ts);
179 nt = full_timespec_to_nt_time(&ts);
180 SBVAL(p, 0, nt);
183 struct timespec pull_long_date_full_timespec(const char *p)
185 NTTIME nt = BVAL(p, 0);
187 return nt_time_to_full_timespec(nt);
190 void put_long_date(char *p, time_t t)
192 struct timespec ts;
193 ts.tv_sec = t;
194 ts.tv_nsec = 0;
195 put_long_date_timespec(TIMESTAMP_SET_SECONDS, p, ts);
198 void dos_filetime_timespec(struct timespec *tsp)
200 tsp->tv_sec &= ~1;
201 tsp->tv_nsec = 0;
204 /*******************************************************************
205 Create a unix date (int GMT) from a dos date (which is actually in
206 localtime).
207 ********************************************************************/
209 time_t make_unix_date(const void *date_ptr, int zone_offset)
211 return pull_dos_date(date_ptr, zone_offset);
214 /*******************************************************************
215 Like make_unix_date() but the words are reversed.
216 ********************************************************************/
218 time_t make_unix_date2(const void *date_ptr, int zone_offset)
220 return pull_dos_date2(date_ptr, zone_offset);
223 /*******************************************************************
224 Create a unix GMT date from a dos date in 32 bit "unix like" format
225 these generally arrive as localtimes, with corresponding DST.
226 ******************************************************************/
228 time_t make_unix_date3(const void *date_ptr, int zone_offset)
230 return pull_dos_date3(date_ptr, zone_offset);
233 time_t srv_make_unix_date(const void *date_ptr)
235 return make_unix_date(date_ptr, server_zone_offset);
238 time_t srv_make_unix_date2(const void *date_ptr)
240 return make_unix_date2(date_ptr, server_zone_offset);
243 time_t srv_make_unix_date3(const void *date_ptr)
245 return make_unix_date3(date_ptr, server_zone_offset);
248 /****************************************************************************
249 Interprets an nt time into a unix struct timespec.
250 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
251 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
252 ****************************************************************************/
254 struct timespec interpret_long_date(NTTIME nt)
256 if (nt == (uint64_t)-1) {
257 struct timespec ret;
258 ret.tv_sec = (time_t)-1;
259 ret.tv_nsec = 0;
260 return ret;
262 return nt_time_to_full_timespec(nt);
265 /*******************************************************************
266 Re-read the smb serverzone value.
267 ******************************************************************/
269 static struct timeval start_time_hires;
271 void TimeInit(void)
273 set_server_zone_offset(time(NULL));
275 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
277 /* Save the start time of this process. */
278 if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
279 GetTimeOfDay(&start_time_hires);
283 /**********************************************************************
284 Return a timeval struct of the uptime of this process. As TimeInit is
285 done before a daemon fork then this is the start time from the parent
286 daemon start. JRA.
287 ***********************************************************************/
289 void get_process_uptime(struct timeval *ret_time)
291 struct timeval time_now_hires;
293 GetTimeOfDay(&time_now_hires);
294 ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
295 if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
296 ret_time->tv_sec -= 1;
297 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
298 } else {
299 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
304 * @brief Get the startup time of the server.
306 * @param[out] ret_time A pointer to a timveal structure to set the startup
307 * time.
309 void get_startup_time(struct timeval *ret_time)
311 ret_time->tv_sec = start_time_hires.tv_sec;
312 ret_time->tv_usec = start_time_hires.tv_usec;
316 /****************************************************************************
317 Convert a NTTIME structure to a time_t.
318 It's originally in "100ns units".
320 This is an absolute version of the one above.
321 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
322 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
323 ****************************************************************************/
325 time_t nt_time_to_unix_abs(const NTTIME *nt)
327 uint64_t d;
329 if (*nt == 0) {
330 return (time_t)0;
333 if (*nt == (uint64_t)-1) {
334 return (time_t)-1;
337 if (*nt == NTTIME_INFINITY) {
338 return (time_t)-1;
341 /* reverse the time */
342 /* it's a negative value, turn it to positive */
343 d=~*nt;
345 d += 1000*1000*10/2;
346 d /= 1000*1000*10;
348 if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
349 return (time_t)0;
352 return (time_t)d;
355 /****************************************************************************
356 Convert a time_t to a NTTIME structure
358 This is an absolute version of the one above.
359 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
360 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
361 ****************************************************************************/
363 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
365 double d;
367 if (t==0) {
368 *nt = 0;
369 return;
372 if (t == TIME_T_MAX) {
373 *nt = 0x7fffffffffffffffLL;
374 return;
377 if (t == (time_t)-1) {
378 /* that's what NT uses for infinite */
379 *nt = NTTIME_INFINITY;
380 return;
383 d = (double)(t);
384 d *= 1.0e7;
386 *nt = (NTTIME)d;
388 /* convert to a negative value */
389 *nt=~*nt;
393 /****************************************************************************
394 Utility function that always returns a const string even if localtime
395 and asctime fail.
396 ****************************************************************************/
398 const char *time_to_asc(const time_t t)
400 const char *asct;
401 struct tm *lt = localtime(&t);
403 if (!lt) {
404 return "unknown time\n";
407 asct = asctime(lt);
408 if (!asct) {
409 return "unknown time\n";
411 return asct;
414 const char *display_time(NTTIME nttime)
416 float high;
417 float low;
418 int sec;
419 int days, hours, mins, secs;
421 if (nttime==0)
422 return "Now";
424 if (nttime==NTTIME_INFINITY)
425 return "Never";
427 high = 65536;
428 high = high/10000;
429 high = high*65536;
430 high = high/1000;
431 high = high * (~(nttime >> 32));
433 low = ~(nttime & 0xFFFFFFFF);
434 low = low/(1000*1000*10);
436 sec=(int)(high+low);
438 days=sec/(60*60*24);
439 hours=(sec - (days*60*60*24)) / (60*60);
440 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
441 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
443 return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
444 "%u seconds", days, hours, mins, secs);
447 bool nt_time_is_set(const NTTIME *nt)
449 if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
450 return false;
453 if (*nt == NTTIME_INFINITY) {
454 return false;
457 return true;