[480] | 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 |
|
---|
[745] | 53 | uint32_t convert_time_t_to_uint32_t(time_t t)
|
---|
[480] | 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 |
|
---|
[745] | 66 | time_t convert_uint32_t_to_time_t(uint32_t u)
|
---|
[480] | 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 | {
|
---|
[745] | 138 | push_dos_date((uint8_t *)buf, offset, unixdate, server_zone_offset);
|
---|
[480] | 139 | }
|
---|
| 140 |
|
---|
| 141 | void srv_put_dos_date2(char *buf,int offset, time_t unixdate)
|
---|
| 142 | {
|
---|
[745] | 143 | push_dos_date2((uint8_t *)buf, offset, unixdate, server_zone_offset);
|
---|
[480] | 144 | }
|
---|
| 145 |
|
---|
| 146 | void srv_put_dos_date3(char *buf,int offset,time_t unixdate)
|
---|
| 147 | {
|
---|
[745] | 148 | push_dos_date3((uint8_t *)buf, offset, unixdate, server_zone_offset);
|
---|
[480] | 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 | unix_timespec_to_nt_time(&nt, ts);
|
---|
| 176 | SIVAL(p, 0, nt & 0xFFFFFFFF);
|
---|
| 177 | SIVAL(p, 4, nt >> 32);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | void put_long_date(char *p, time_t t)
|
---|
| 181 | {
|
---|
| 182 | struct timespec ts;
|
---|
| 183 | ts.tv_sec = t;
|
---|
| 184 | ts.tv_nsec = 0;
|
---|
| 185 | put_long_date_timespec(TIMESTAMP_SET_SECONDS, p, ts);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | void dos_filetime_timespec(struct timespec *tsp)
|
---|
| 189 | {
|
---|
| 190 | tsp->tv_sec &= ~1;
|
---|
| 191 | tsp->tv_nsec = 0;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /*******************************************************************
|
---|
| 195 | Create a unix date (int GMT) from a dos date (which is actually in
|
---|
| 196 | localtime).
|
---|
| 197 | ********************************************************************/
|
---|
| 198 |
|
---|
[745] | 199 | time_t make_unix_date(const void *date_ptr, int zone_offset)
|
---|
[480] | 200 | {
|
---|
| 201 | uint32_t dos_date=0;
|
---|
| 202 | struct tm t;
|
---|
| 203 | time_t ret;
|
---|
| 204 |
|
---|
| 205 | dos_date = IVAL(date_ptr,0);
|
---|
| 206 |
|
---|
| 207 | if (dos_date == 0) {
|
---|
| 208 | return 0;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
|
---|
| 212 | &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
|
---|
| 213 | t.tm_isdst = -1;
|
---|
| 214 |
|
---|
| 215 | ret = timegm(&t);
|
---|
| 216 |
|
---|
| 217 | ret += zone_offset;
|
---|
| 218 |
|
---|
| 219 | return(ret);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | /*******************************************************************
|
---|
| 223 | Like make_unix_date() but the words are reversed.
|
---|
| 224 | ********************************************************************/
|
---|
| 225 |
|
---|
| 226 | time_t make_unix_date2(const void *date_ptr, int zone_offset)
|
---|
| 227 | {
|
---|
| 228 | uint32_t x,x2;
|
---|
| 229 |
|
---|
| 230 | x = IVAL(date_ptr,0);
|
---|
| 231 | x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
|
---|
| 232 | SIVAL(&x,0,x2);
|
---|
| 233 |
|
---|
| 234 | return(make_unix_date((const void *)&x, zone_offset));
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /*******************************************************************
|
---|
| 238 | Create a unix GMT date from a dos date in 32 bit "unix like" format
|
---|
| 239 | these generally arrive as localtimes, with corresponding DST.
|
---|
| 240 | ******************************************************************/
|
---|
| 241 |
|
---|
| 242 | time_t make_unix_date3(const void *date_ptr, int zone_offset)
|
---|
| 243 | {
|
---|
| 244 | time_t t = (time_t)IVAL(date_ptr,0);
|
---|
[745] | 245 | if (!null_time(t)) {
|
---|
[480] | 246 | t += zone_offset;
|
---|
| 247 | }
|
---|
| 248 | return(t);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | time_t srv_make_unix_date(const void *date_ptr)
|
---|
| 252 | {
|
---|
| 253 | return make_unix_date(date_ptr, server_zone_offset);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | time_t srv_make_unix_date2(const void *date_ptr)
|
---|
| 257 | {
|
---|
| 258 | return make_unix_date2(date_ptr, server_zone_offset);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | time_t srv_make_unix_date3(const void *date_ptr)
|
---|
| 262 | {
|
---|
| 263 | return make_unix_date3(date_ptr, server_zone_offset);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /****************************************************************************
|
---|
| 267 | Convert a normalized timeval to a timespec.
|
---|
| 268 | ****************************************************************************/
|
---|
| 269 |
|
---|
| 270 | struct timespec convert_timeval_to_timespec(const struct timeval tv)
|
---|
| 271 | {
|
---|
| 272 | struct timespec ts;
|
---|
| 273 | ts.tv_sec = tv.tv_sec;
|
---|
| 274 | ts.tv_nsec = tv.tv_usec * 1000;
|
---|
| 275 | return ts;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | /****************************************************************************
|
---|
| 279 | Convert a normalized timespec to a timeval.
|
---|
| 280 | ****************************************************************************/
|
---|
| 281 |
|
---|
| 282 | struct timeval convert_timespec_to_timeval(const struct timespec ts)
|
---|
| 283 | {
|
---|
| 284 | struct timeval tv;
|
---|
| 285 | tv.tv_sec = ts.tv_sec;
|
---|
| 286 | tv.tv_usec = ts.tv_nsec / 1000;
|
---|
| 287 | return tv;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /****************************************************************************
|
---|
| 291 | Return a timespec for the current time
|
---|
| 292 | ****************************************************************************/
|
---|
| 293 |
|
---|
| 294 | struct timespec timespec_current(void)
|
---|
| 295 | {
|
---|
| 296 | struct timespec ts;
|
---|
[745] | 297 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
[480] | 298 | return ts;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /****************************************************************************
|
---|
| 302 | Return the lesser of two timespecs.
|
---|
| 303 | ****************************************************************************/
|
---|
| 304 |
|
---|
| 305 | struct timespec timespec_min(const struct timespec *ts1,
|
---|
| 306 | const struct timespec *ts2)
|
---|
| 307 | {
|
---|
| 308 | if (ts1->tv_sec < ts2->tv_sec) return *ts1;
|
---|
| 309 | if (ts1->tv_sec > ts2->tv_sec) return *ts2;
|
---|
| 310 | if (ts1->tv_nsec < ts2->tv_nsec) return *ts1;
|
---|
| 311 | return *ts2;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | /****************************************************************************
|
---|
| 315 | compare two timespec structures.
|
---|
| 316 | Return -1 if ts1 < ts2
|
---|
| 317 | Return 0 if ts1 == ts2
|
---|
| 318 | Return 1 if ts1 > ts2
|
---|
| 319 | ****************************************************************************/
|
---|
| 320 |
|
---|
| 321 | int timespec_compare(const struct timespec *ts1, const struct timespec *ts2)
|
---|
| 322 | {
|
---|
| 323 | if (ts1->tv_sec > ts2->tv_sec) return 1;
|
---|
| 324 | if (ts1->tv_sec < ts2->tv_sec) return -1;
|
---|
| 325 | if (ts1->tv_nsec > ts2->tv_nsec) return 1;
|
---|
| 326 | if (ts1->tv_nsec < ts2->tv_nsec) return -1;
|
---|
| 327 | return 0;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | /****************************************************************************
|
---|
| 331 | Round up a timespec if nsec > 500000000, round down if lower,
|
---|
| 332 | then zero nsec.
|
---|
| 333 | ****************************************************************************/
|
---|
| 334 |
|
---|
| 335 | void round_timespec_to_sec(struct timespec *ts)
|
---|
| 336 | {
|
---|
| 337 | ts->tv_sec = convert_timespec_to_time_t(*ts);
|
---|
| 338 | ts->tv_nsec = 0;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | /****************************************************************************
|
---|
| 342 | Round a timespec to usec value.
|
---|
| 343 | ****************************************************************************/
|
---|
| 344 |
|
---|
| 345 | void round_timespec_to_usec(struct timespec *ts)
|
---|
| 346 | {
|
---|
| 347 | struct timeval tv = convert_timespec_to_timeval(*ts);
|
---|
| 348 | *ts = convert_timeval_to_timespec(tv);
|
---|
[745] | 349 | while (ts->tv_nsec > 1000000000) {
|
---|
| 350 | ts->tv_sec += 1;
|
---|
| 351 | ts->tv_nsec -= 1000000000;
|
---|
| 352 | }
|
---|
[480] | 353 | }
|
---|
| 354 |
|
---|
| 355 | /****************************************************************************
|
---|
| 356 | Interprets an nt time into a unix struct timespec.
|
---|
| 357 | Differs from nt_time_to_unix in that an 8 byte value of 0xffffffffffffffff
|
---|
| 358 | will be returned as (time_t)-1, whereas nt_time_to_unix returns 0 in this case.
|
---|
| 359 | ****************************************************************************/
|
---|
| 360 |
|
---|
| 361 | struct timespec interpret_long_date(const char *p)
|
---|
| 362 | {
|
---|
| 363 | NTTIME nt;
|
---|
| 364 | nt = IVAL(p,0) + ((uint64_t)IVAL(p,4) << 32);
|
---|
| 365 | if (nt == (uint64_t)-1) {
|
---|
| 366 | struct timespec ret;
|
---|
| 367 | ret.tv_sec = (time_t)-1;
|
---|
| 368 | ret.tv_nsec = 0;
|
---|
| 369 | return ret;
|
---|
| 370 | }
|
---|
| 371 | return nt_time_to_unix_timespec(&nt);
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | /*******************************************************************
|
---|
| 375 | Re-read the smb serverzone value.
|
---|
| 376 | ******************************************************************/
|
---|
| 377 |
|
---|
| 378 | static struct timeval start_time_hires;
|
---|
| 379 |
|
---|
| 380 | void TimeInit(void)
|
---|
| 381 | {
|
---|
| 382 | set_server_zone_offset(time(NULL));
|
---|
| 383 |
|
---|
| 384 | DEBUG(4,("TimeInit: Serverzone is %d\n", server_zone_offset));
|
---|
| 385 |
|
---|
| 386 | /* Save the start time of this process. */
|
---|
| 387 | if (start_time_hires.tv_sec == 0 && start_time_hires.tv_usec == 0) {
|
---|
| 388 | GetTimeOfDay(&start_time_hires);
|
---|
| 389 | }
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | /**********************************************************************
|
---|
| 393 | Return a timeval struct of the uptime of this process. As TimeInit is
|
---|
| 394 | done before a daemon fork then this is the start time from the parent
|
---|
| 395 | daemon start. JRA.
|
---|
| 396 | ***********************************************************************/
|
---|
| 397 |
|
---|
| 398 | void get_process_uptime(struct timeval *ret_time)
|
---|
| 399 | {
|
---|
| 400 | struct timeval time_now_hires;
|
---|
| 401 |
|
---|
| 402 | GetTimeOfDay(&time_now_hires);
|
---|
| 403 | ret_time->tv_sec = time_now_hires.tv_sec - start_time_hires.tv_sec;
|
---|
| 404 | if (time_now_hires.tv_usec < start_time_hires.tv_usec) {
|
---|
| 405 | ret_time->tv_sec -= 1;
|
---|
| 406 | ret_time->tv_usec = 1000000 + (time_now_hires.tv_usec - start_time_hires.tv_usec);
|
---|
| 407 | } else {
|
---|
| 408 | ret_time->tv_usec = time_now_hires.tv_usec - start_time_hires.tv_usec;
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[745] | 412 | /**
|
---|
| 413 | * @brief Get the startup time of the server.
|
---|
| 414 | *
|
---|
| 415 | * @param[out] ret_time A pointer to a timveal structure to set the startup
|
---|
| 416 | * time.
|
---|
| 417 | */
|
---|
| 418 | void get_startup_time(struct timeval *ret_time)
|
---|
| 419 | {
|
---|
| 420 | ret_time->tv_sec = start_time_hires.tv_sec;
|
---|
| 421 | ret_time->tv_usec = start_time_hires.tv_usec;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 |
|
---|
[480] | 425 | /****************************************************************************
|
---|
| 426 | Convert a NTTIME structure to a time_t.
|
---|
| 427 | It's originally in "100ns units".
|
---|
| 428 |
|
---|
| 429 | This is an absolute version of the one above.
|
---|
| 430 | By absolute I mean, it doesn't adjust from 1/1/1601 to 1/1/1970
|
---|
| 431 | if the NTTIME was 5 seconds, the time_t is 5 seconds. JFM
|
---|
| 432 | ****************************************************************************/
|
---|
| 433 |
|
---|
| 434 | time_t nt_time_to_unix_abs(const NTTIME *nt)
|
---|
| 435 | {
|
---|
| 436 | uint64_t d;
|
---|
| 437 |
|
---|
| 438 | if (*nt == 0) {
|
---|
| 439 | return (time_t)0;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | if (*nt == (uint64_t)-1) {
|
---|
| 443 | return (time_t)-1;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | if (*nt == NTTIME_INFINITY) {
|
---|
| 447 | return (time_t)-1;
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | /* reverse the time */
|
---|
| 451 | /* it's a negative value, turn it to positive */
|
---|
| 452 | d=~*nt;
|
---|
| 453 |
|
---|
| 454 | d += 1000*1000*10/2;
|
---|
| 455 | d /= 1000*1000*10;
|
---|
| 456 |
|
---|
| 457 | if (!(TIME_T_MIN <= ((time_t)d) && ((time_t)d) <= TIME_T_MAX)) {
|
---|
| 458 | return (time_t)0;
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | return (time_t)d;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | time_t uint64s_nt_time_to_unix_abs(const uint64_t *src)
|
---|
| 465 | {
|
---|
| 466 | NTTIME nttime;
|
---|
| 467 | nttime = *src;
|
---|
| 468 | return nt_time_to_unix_abs(&nttime);
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | /****************************************************************************
|
---|
| 472 | Put a 8 byte filetime from a struct timespec. Uses GMT.
|
---|
| 473 | ****************************************************************************/
|
---|
| 474 |
|
---|
| 475 | void unix_timespec_to_nt_time(NTTIME *nt, struct timespec ts)
|
---|
| 476 | {
|
---|
| 477 | uint64_t d;
|
---|
| 478 |
|
---|
| 479 | if (ts.tv_sec ==0 && ts.tv_nsec == 0) {
|
---|
| 480 | *nt = 0;
|
---|
| 481 | return;
|
---|
| 482 | }
|
---|
| 483 | if (ts.tv_sec == TIME_T_MAX) {
|
---|
| 484 | *nt = 0x7fffffffffffffffLL;
|
---|
| 485 | return;
|
---|
| 486 | }
|
---|
| 487 | if (ts.tv_sec == (time_t)-1) {
|
---|
| 488 | *nt = (uint64_t)-1;
|
---|
| 489 | return;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | d = ts.tv_sec;
|
---|
| 493 | d += TIME_FIXUP_CONSTANT_INT;
|
---|
| 494 | d *= 1000*1000*10;
|
---|
| 495 | /* d is now in 100ns units. */
|
---|
| 496 | d += (ts.tv_nsec / 100);
|
---|
| 497 |
|
---|
| 498 | *nt = d;
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | #if 0
|
---|
| 502 | void nt_time_to_unix_timespec(struct timespec *ts, NTTIME t)
|
---|
| 503 | {
|
---|
| 504 | if (ts == NULL) {
|
---|
| 505 | return;
|
---|
| 506 | }
|
---|
| 507 |
|
---|
| 508 | /* t starts in 100 nsec units since 1601-01-01. */
|
---|
| 509 |
|
---|
| 510 | t *= 100;
|
---|
| 511 | /* t is now in nsec units since 1601-01-01. */
|
---|
| 512 |
|
---|
| 513 | t -= TIME_FIXUP_CONSTANT*1000*1000*100;
|
---|
| 514 | /* t is now in nsec units since the UNIX epoch 1970-01-01. */
|
---|
| 515 |
|
---|
| 516 | ts->tv_sec = t / 1000000000LL;
|
---|
| 517 |
|
---|
| 518 | if (TIME_T_MIN > ts->tv_sec || ts->tv_sec > TIME_T_MAX) {
|
---|
| 519 | ts->tv_sec = 0;
|
---|
| 520 | ts->tv_nsec = 0;
|
---|
| 521 | return;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | ts->tv_nsec = t - ts->tv_sec*1000000000LL;
|
---|
| 525 | }
|
---|
| 526 | #endif
|
---|
| 527 |
|
---|
| 528 | /****************************************************************************
|
---|
| 529 | Convert a time_t to a NTTIME structure
|
---|
| 530 |
|
---|
| 531 | This is an absolute version of the one above.
|
---|
| 532 | By absolute I mean, it doesn't adjust from 1/1/1970 to 1/1/1601
|
---|
| 533 | If the time_t was 5 seconds, the NTTIME is 5 seconds. JFM
|
---|
| 534 | ****************************************************************************/
|
---|
| 535 |
|
---|
| 536 | void unix_to_nt_time_abs(NTTIME *nt, time_t t)
|
---|
| 537 | {
|
---|
| 538 | double d;
|
---|
| 539 |
|
---|
| 540 | if (t==0) {
|
---|
| 541 | *nt = 0;
|
---|
| 542 | return;
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | if (t == TIME_T_MAX) {
|
---|
| 546 | *nt = 0x7fffffffffffffffLL;
|
---|
| 547 | return;
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | if (t == (time_t)-1) {
|
---|
| 551 | /* that's what NT uses for infinite */
|
---|
| 552 | *nt = NTTIME_INFINITY;
|
---|
| 553 | return;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | d = (double)(t);
|
---|
| 557 | d *= 1.0e7;
|
---|
| 558 |
|
---|
| 559 | *nt = (NTTIME)d;
|
---|
| 560 |
|
---|
| 561 | /* convert to a negative value */
|
---|
| 562 | *nt=~*nt;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 |
|
---|
| 566 | /****************************************************************************
|
---|
| 567 | Utility function that always returns a const string even if localtime
|
---|
| 568 | and asctime fail.
|
---|
| 569 | ****************************************************************************/
|
---|
| 570 |
|
---|
| 571 | const char *time_to_asc(const time_t t)
|
---|
| 572 | {
|
---|
| 573 | const char *asct;
|
---|
| 574 | struct tm *lt = localtime(&t);
|
---|
| 575 |
|
---|
| 576 | if (!lt) {
|
---|
| 577 | return "unknown time";
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | asct = asctime(lt);
|
---|
| 581 | if (!asct) {
|
---|
| 582 | return "unknown time";
|
---|
| 583 | }
|
---|
| 584 | return asct;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | const char *display_time(NTTIME nttime)
|
---|
| 588 | {
|
---|
| 589 | float high;
|
---|
| 590 | float low;
|
---|
| 591 | int sec;
|
---|
| 592 | int days, hours, mins, secs;
|
---|
| 593 |
|
---|
| 594 | if (nttime==0)
|
---|
| 595 | return "Now";
|
---|
| 596 |
|
---|
| 597 | if (nttime==NTTIME_INFINITY)
|
---|
| 598 | return "Never";
|
---|
| 599 |
|
---|
| 600 | high = 65536;
|
---|
| 601 | high = high/10000;
|
---|
| 602 | high = high*65536;
|
---|
| 603 | high = high/1000;
|
---|
| 604 | high = high * (~(nttime >> 32));
|
---|
| 605 |
|
---|
| 606 | low = ~(nttime & 0xFFFFFFFF);
|
---|
| 607 | low = low/(1000*1000*10);
|
---|
| 608 |
|
---|
| 609 | sec=(int)(high+low);
|
---|
| 610 |
|
---|
| 611 | days=sec/(60*60*24);
|
---|
| 612 | hours=(sec - (days*60*60*24)) / (60*60);
|
---|
| 613 | mins=(sec - (days*60*60*24) - (hours*60*60) ) / 60;
|
---|
| 614 | secs=sec - (days*60*60*24) - (hours*60*60) - (mins*60);
|
---|
| 615 |
|
---|
| 616 | return talloc_asprintf(talloc_tos(), "%u days, %u hours, %u minutes, "
|
---|
| 617 | "%u seconds", days, hours, mins, secs);
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | bool nt_time_is_set(const NTTIME *nt)
|
---|
| 621 | {
|
---|
| 622 | if (*nt == 0x7FFFFFFFFFFFFFFFLL) {
|
---|
| 623 | return false;
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | if (*nt == NTTIME_INFINITY) {
|
---|
| 627 | return false;
|
---|
| 628 | }
|
---|
| 629 |
|
---|
| 630 | return true;
|
---|
| 631 | }
|
---|