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/>.
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
) {
46 } else if (t
== 0x7FFFFFFFFFFFFFFFLL
) {
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
;
66 /****************************************************************************
68 ****************************************************************************/
70 bool nt_time_is_zero(const NTTIME
*nt
)
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
)
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) {
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
)) {
146 case TIMESTAMP_SET_SECONDS
:
147 round_timespec_to_sec(ts
);
149 case TIMESTAMP_SET_MSEC
:
150 round_timespec_to_usec(ts
);
152 case TIMESTAMP_SET_NT_OR_BETTER
:
153 /* No rounding needed. */
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
)
166 round_timespec(res
, &ts
);
167 nt
= unix_timespec_to_nt_time(ts
);
171 void put_long_date_full_timespec(enum timestamp_set_resolution res
,
173 const struct timespec
*_ts
)
175 struct timespec ts
= *_ts
;
178 round_timespec(res
, &ts
);
179 nt
= full_timespec_to_nt_time(&ts
);
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
)
195 put_long_date_timespec(TIMESTAMP_SET_SECONDS
, p
, ts
);
198 void dos_filetime_timespec(struct timespec
*tsp
)
204 /*******************************************************************
205 Create a unix date (int GMT) from a dos date (which is actually in
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) {
258 ret
.tv_sec
= (time_t)-1;
262 return nt_time_to_full_timespec(nt
);
265 /*******************************************************************
266 Re-read the smb serverzone value.
267 ******************************************************************/
269 static struct timeval start_time_hires
;
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
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
);
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
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
)
333 if (*nt
== (uint64_t)-1) {
337 if (*nt
== NTTIME_INFINITY
) {
341 /* reverse the time */
342 /* it's a negative value, turn it to positive */
348 if (!(TIME_T_MIN
<= ((time_t)d
) && ((time_t)d
) <= TIME_T_MAX
)) {
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
)
372 if (t
== TIME_T_MAX
) {
373 *nt
= 0x7fffffffffffffffLL
;
377 if (t
== (time_t)-1) {
378 /* that's what NT uses for infinite */
379 *nt
= NTTIME_INFINITY
;
388 /* convert to a negative value */
393 /****************************************************************************
394 Utility function that always returns a const string even if localtime
396 ****************************************************************************/
398 const char *time_to_asc(const time_t t
)
401 struct tm
*lt
= localtime(&t
);
404 return "unknown time\n";
409 return "unknown time\n";
414 const char *display_time(NTTIME nttime
)
419 int days
, hours
, mins
, secs
;
424 if (nttime
==NTTIME_INFINITY
)
431 high
= high
* (~(nttime
>> 32));
433 low
= ~(nttime
& 0xFFFFFFFF);
434 low
= low
/(1000*1000*10);
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
) {
453 if (*nt
== NTTIME_INFINITY
) {