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
32 #define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
33 : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
36 #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
39 #define NTTIME_INFINITY (NTTIME)0x8000000000000000LL
41 #if (SIZEOF_LONG == 8)
42 #define TIME_FIXUP_CONSTANT_INT 11644473600L
43 #elif (SIZEOF_LONG_LONG == 8)
44 #define TIME_FIXUP_CONSTANT_INT 11644473600LL
47 /*******************************************************************
48 create a 16 bit dos packed date
49 ********************************************************************/
50 static uint16_t make_dos_date1(struct tm *t)
53 ret = (((unsigned int)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
54 ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
58 /*******************************************************************
59 create a 16 bit dos packed time
60 ********************************************************************/
61 static uint16_t make_dos_time1(struct tm *t)
64 ret = ((((unsigned int)t->tm_min >> 3)&0x7) | (((unsigned int)t->tm_hour) << 3));
65 ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
69 /*******************************************************************
70 create a 32 bit dos packed date/time from some parameters
71 This takes a GMT time and returns a packed localtime structure
72 ********************************************************************/
73 static uint32_t make_dos_date(time_t unixdate, int zone_offset)
82 unixdate -= zone_offset;
84 t = gmtime(&unixdate);
89 ret = make_dos_date1(t);
90 ret = ((ret&0xFFFF)<<16) | make_dos_time1(t);
96 parse a nttime as a large integer in a string and return a NTTIME
98 NTTIME nttime_from_string(const char *s)
100 return strtoull(s, NULL, 0);
103 /**************************************************************
104 Handle conversions between time_t and uint32, taking care to
105 preserve the "special" values.
106 **************************************************************/
108 uint32_t convert_time_t_to_uint32(time_t t)
110 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
111 /* time_t is 64-bit. */
112 if (t == 0x8000000000000000LL) {
114 } else if (t == 0x7FFFFFFFFFFFFFFFLL) {
121 time_t convert_uint32_to_time_t(uint32_t u)
123 #if (defined(SIZEOF_TIME_T) && (SIZEOF_TIME_T == 8))
124 /* time_t is 64-bit. */
125 if (u == 0x80000000) {
126 return (time_t)0x8000000000000000LL;
127 } else if (u == 0x7FFFFFFF) {
128 return (time_t)0x7FFFFFFFFFFFFFFFLL;
134 /****************************************************************************
135 Check if NTTIME is 0.
136 ****************************************************************************/
138 bool nt_time_is_zero(const NTTIME *nt)
143 /****************************************************************************
144 Convert ASN.1 GeneralizedTime string to unix-time.
145 Returns 0 on failure; Currently ignores timezone.
146 ****************************************************************************/
148 time_t generalized_to_unix_time(const char *str)
154 if (sscanf(str, "%4d%2d%2d%2d%2d%2d",
155 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
156 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
165 /*******************************************************************
166 Accessor function for the server time zone offset.
167 set_server_zone_offset() must have been called first.
168 ******************************************************************/
170 static int server_zone_offset;
172 int get_server_zone_offset(void)
174 return server_zone_offset;
177 /*******************************************************************
178 Initialize the server time zone offset. Called when a client connects.
179 ******************************************************************/
181 int set_server_zone_offset(time_t t)
183 server_zone_offset = get_time_zone(t);
184 return server_zone_offset;
187 /****************************************************************************
188 Return the date and time as a string
189 ****************************************************************************/
191 char *current_timestring(TALLOC_CTX *ctx, bool hires)
200 t = (time_t)tp.tv_sec;
209 "%ld.%06ld seconds since the Epoch",
215 "%ld seconds since the Epoch",
221 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
222 slprintf(TimeBuf+strlen(TimeBuf),
223 sizeof(TimeBuf)-1 - strlen(TimeBuf),
227 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
231 const char *asct = asctime(tm);
235 asct ? asct : "unknown",
238 const char *asct = asctime(tm);
239 fstrcpy(TimeBuf, asct ? asct : "unknown");
243 return talloc_strdup(ctx, TimeBuf);
247 /*******************************************************************
248 Put a dos date into a buffer (time/date format).
249 This takes GMT time and puts local time in the buffer.
250 ********************************************************************/
252 static void put_dos_date(char *buf,int offset,time_t unixdate, int zone_offset)
254 uint32_t x = make_dos_date(unixdate, zone_offset);
258 /*******************************************************************
259 Put a dos date into a buffer (date/time format).
260 This takes GMT time and puts local time in the buffer.
261 ********************************************************************/
263 static void put_dos_date2(char *buf,int offset,time_t unixdate, int zone_offset)
265 uint32_t x = make_dos_date(unixdate, zone_offset);
266 x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
270 /*******************************************************************
271 Put a dos 32 bit "unix like" date into a buffer. This routine takes
272 GMT and converts it to LOCAL time before putting it (most SMBs assume
273 localtime for this sort of date)
274 ********************************************************************/
276 static void put_dos_date3(char *buf,int offset,time_t unixdate, int zone_offset)
278 if (!null_mtime(unixdate)) {
279 unixdate -= zone_offset;
281 SIVAL(buf,offset,unixdate);
285 /***************************************************************************
286 Server versions of the above functions.
287 ***************************************************************************/
289 void srv_put_dos_date(char *buf,int offset,time_t unixdate)
291 put_dos_date(buf, offset, unixdate, server_zone_offset);
294 void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
296 put_dos_date2(buf, offset, unixdate, server_zone_offset);
299 void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
301 put_dos_date3(buf, offset, unixdate, server_zone_offset);
304 /****************************************************************************
305 Take a Unix time and convert to an NTTIME structure and place in buffer
307 ****************************************************************************/
309 void put_long_date_timespec(char *p, struct timespec ts)
312 unix_timespec_to_nt_time(&nt, ts);
313 SIVAL(p, 0, nt & 0xFFFFFFFF);
314 SIVAL(p, 4, nt >> 32);
317 void put_long_date(char *p, time_t t)
322 put_long_date_timespec(p, ts);
325 void dos_filetime_timespec(struct timespec *tsp)
331 /*******************************************************************
332 Create a unix date (int GMT) from a dos date (which is actually in
334 ********************************************************************/
336 static time_t make_unix_date(const void *date_ptr, int zone_offset)
342 dos_date = IVAL(date_ptr,0);
348 interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
349 &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
359 /*******************************************************************
360 Like make_unix_date() but the words are reversed.
361 ********************************************************************/
363 time_t make_unix_date2(const void *date_ptr, int zone_offset)
367 x = IVAL(date_ptr,0);
368 x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
371 return(make_unix_date((const void *)&x, zone_offset));
374 /*******************************************************************
375 Create a unix GMT date from a dos date in 32 bit "unix like" format
376 these generally arrive as localtimes, with corresponding DST.
377 ******************************************************************/
379 time_t make_unix_date3(const void *date_ptr, int zone_offset)
381 time_t t = (time_t)IVAL(date_ptr,0);
382 if (!null_mtime(t)) {
388 time_t srv_make_unix_date(const void *date_ptr)
390 return make_unix_date(date_ptr, server_zone_offset);
393 time_t srv_make_unix_date2(const void *date_ptr)
395 return make_unix_date2(date_ptr, server_zone_offset);
398 time_t srv_make_unix_date3(const void *date_ptr)
400 return make_unix_date3(date_ptr, server_zone_offset);
403 /****************************************************************************
404 Convert a normalized timeval to a timespec.
405 ****************************************************************************/
407 struct timespec convert_timeval_to_timespec(const struct timeval tv)
410 ts.tv_sec = tv.tv_sec;
411 ts.tv_nsec = tv.tv_usec * 1000;
415 /****************************************************************************
416 Convert a normalized timespec to a timeval.
417 ****************************************************************************/
419 struct timeval convert_timespec_to_timeval(const struct timespec ts)
422 tv.tv_sec = ts.tv_sec;
423 tv.tv_usec = ts.tv_nsec / 1000;
427 /****************************************************************************
428 Return a timespec for the current time
429 ****************************************************************************/
431 struct timespec timespec_current(void)
436 ts.tv_sec = tv.tv_sec;
437 ts.tv_nsec = tv.tv_usec * 1000;
441 /****************************************************************************
442 Return the lesser of two timespecs.
443 ****************************************************************************/
445 struct timespec timespec_min(const struct timespec *ts1,
446 const struct timespec *ts2)
448 if (ts1->tv_sec < ts2->tv_sec) return *ts1;
449 if (ts1->tv_sec > ts2->tv_sec) return *ts2;
450 if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
454 /****************************************************************************
455 compare two timespec structures.
456 Return -1 if ts1 < ts2
457 Return 0 if ts1 == ts2
458 Return 1 if ts1 > ts2
459 ****************************************************************************/
461 int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
463 if (ts1->tv_sec > ts2->tv_sec) return 1;
464 if (ts1->tv_sec < ts2->tv_sec) return -1;
465 if (ts1->tv_nsec > ts2->tv_nsec) return 1;
466 if (ts1->tv_nsec < ts2->tv_nsec) return -1;
470 /****************************************************************************
471 Interprets an nt time into a unix struct timespec.
472 Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
473 will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
474 ****************************************************************************/
476 struct timespec interpret_long_date(const char *p)
479 nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);
480 if (nt == (uint64_t)-1) {
482 ret.tv_sec = (time_t)-1;
486 return nt_time_to_unix_timespec(&nt);
489 /***************************************************************************
490 Client versions of the above functions.
491 ***************************************************************************/
493 void cli_put_dos_date(struct cli_state *cli, char *buf, int offset, time_t unixdate)
495 put_dos_date(buf, offset, unixdate, cli->serverzone);
498 void cli_put_dos_date2(struct cli_state *cli, char *buf, int offset, time_t unixdate)
500 put_dos_date2(buf, offset, unixdate, cli->serverzone);
503 void cli_put_dos_date3(struct cli_state *cli, char *buf, int offset, time_t unixdate)
505 put_dos_date3(buf, offset, unixdate, cli->serverzone);
508 time_t cli_make_unix_date(struct cli_state *cli, const void *date_ptr)
510 return make_unix_date(date_ptr, cli->serverzone);
513 time_t cli_make_unix_date2(struct cli_state *cli, const void *date_ptr)
515 return make_unix_date2(date_ptr, cli->serverzone);
518 time_t cli_make_unix_date3(struct cli_state *cli, const void *date_ptr)
520 return make_unix_date3(date_ptr, cli->serverzone);
523 /****************************************************************************
524 Check if two NTTIMEs are the same.
525 ****************************************************************************/
527 bool nt_time_equals(const NTTIME *nt1, const NTTIME *nt2)
529 return (*nt1 == *nt2);
532 /*******************************************************************
533 Re-read the smb serverzone value.
534 ******************************************************************/
536 static struct timeval start_time_hires;
540 set_server_zone_offset(time(NULL));
542 DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
544 /* Save the start time of this process. */
545 if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
546 GetTimeOfDay(&start_time_hires);
550 /**********************************************************************
551 Return a timeval struct of the uptime of this process. As TimeInit is
552 done before a daemon fork then this is the start time from the parent
554 ***********************************************************************/
556 void get_process_uptime(struct timeval *ret_time)
558 struct timeval time_now_hires;
560 GetTimeOfDay(&time_now_hires);
561 ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
562 if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
563 ret_time->tv_sec -= 1;
564 ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
566 ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
570 /****************************************************************************
571 Convert a NTTIME structure to a time_t.
572 It's originally in "100ns units".
574 This is an absolute version of the one above.
575 By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
576 if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
577 ****************************************************************************/
579 time_t nt_time_to_unix_abs(const NTTIME *nt)
587 if (*nt == (uint64_t)-1) {
591 if (*nt == NTTIME_INFINITY) {
595 /* reverse the time */
596 /* it's a negative value, turn it to positive */
602 if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
609 time_t uint64s_nt_time_to_unix_abs(const uint64_t *src)
613 return nt_time_to_unix_abs(&nttime);
616 /****************************************************************************
617 Put a 8 byte filetime from a struct timespec. Uses GMT.
618 ****************************************************************************/
620 void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
624 if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
628 if (ts.tv_sec == TIME_T_MAX) {
629 *nt = 0x7fffffffffffffffLL;
632 if (ts.tv_sec == (time_t)-1) {
638 d += TIME_FIXUP_CONSTANT_INT;
640 /* d is now in 100ns units. */
641 d += (ts.tv_nsec / 100);
646 /****************************************************************************
647 Convert a time_t to a NTTIME structure
649 This is an absolute version of the one above.
650 By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
651 If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
652 ****************************************************************************/
654 void unix_to_nt_time_abs(NTTIME *nt, time_t t)
663 if (t == TIME_T_MAX) {
664 *nt = 0x7fffffffffffffffLL;
668 if (t == (time_t)-1) {
669 /* that's what NT uses for infinite */
670 *nt = NTTIME_INFINITY;
679 /* convert to a negative value */
684 /****************************************************************************
685 Check if it's a null mtime.
686 ****************************************************************************/
688 bool null_mtime(time_t mtime)
690 if (mtime == 0 || mtime == (time_t)0xFFFFFFFF || mtime == (time_t)-1)
695 /****************************************************************************
696 Utility function that always returns a const string even if localtime
698 ****************************************************************************/
700 const char *time_to_asc(const time_t t)
703 struct tm *lt = localtime(&t);
706 return "unknown time";
711 return "unknown time";
716 const char *display_time(NTTIME nttime)
721 int days, hours, mins, secs;
726 if (nttime==NTTIME_INFINITY)
733 high = high * (~(nttime >> 32));
735 low = ~(nttime & 0xFFFFFFFF);
736 low = low/(1000*1000*10);
741 hours=(sec - (days*60*60*24)) / (60*60);
742 mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
743 secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
745 return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
746 "%u seconds", days, hours, mins, secs);
749 bool nt_time_is_set(const NTTIME *nt)
751 if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
755 if (*nt == NTTIME_INFINITY) {