Changeset 740 for vendor/current/lib/util/time.c
- Timestamp:
- Nov 14, 2012, 12:59:34 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/lib/util/time.c
r478 r740 5 5 Copyright (C) Andrew Tridgell 1992-2004 6 6 Copyright (C) Stefan (metze) Metzmacher 2002 7 Copyright (C) Jeremy Allison 2007 8 Copyright (C) Andrew Bartlett 2011 7 9 8 10 This program is free software; you can redistribute it and/or modify … … 56 58 } 57 59 60 /** 61 a wrapper to preferably get the monotonic time 62 **/ 63 _PUBLIC_ void clock_gettime_mono(struct timespec *tp) 64 { 65 if (clock_gettime(CUSTOM_CLOCK_MONOTONIC,tp) != 0) { 66 clock_gettime(CLOCK_REALTIME,tp); 67 } 68 } 69 70 /** 71 a wrapper to preferably get the monotonic time in seconds 72 as this is only second resolution we can use the cached 73 (and much faster) COARSE clock variant 74 **/ 75 _PUBLIC_ time_t time_mono(time_t *t) 76 { 77 struct timespec tp; 78 int rc = -1; 79 #ifdef CLOCK_MONOTONIC_COARSE 80 rc = clock_gettime(CLOCK_MONOTONIC_COARSE,&tp); 81 #endif 82 if (rc != 0) { 83 clock_gettime_mono(&tp); 84 } 85 if (t != NULL) { 86 *t = tp.tv_sec; 87 } 88 return tp.tv_sec; 89 } 90 58 91 59 92 #define TIME_FIXUP_CONSTANT 11644473600LL … … 61 94 time_t convert_timespec_to_time_t(struct timespec ts) 62 95 { 96 /* Ensure tv_nsec is less than 1sec. */ 97 while (ts.tv_nsec > 1000000000) { 98 ts.tv_sec += 1; 99 ts.tv_nsec -= 1000000000; 100 } 101 63 102 /* 1 ns == 1,000,000,000 - one thousand millionths of a second. 64 103 increment if it's greater than 500 millionth of a second. */ 104 65 105 if (ts.tv_nsec > 500000000) { 66 106 return ts.tv_sec + 1; … … 300 340 301 341 342 /**************************************************************************** 343 Return the date and time as a string 344 ****************************************************************************/ 345 346 char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires) 347 { 348 time_t t; 349 struct tm *tm; 350 351 t = (time_t)tp->tv_sec; 352 tm = localtime(&t); 353 if (!tm) { 354 if (hires) { 355 return talloc_asprintf(ctx, 356 "%ld.%06ld seconds since the Epoch", 357 (long)tp->tv_sec, 358 (long)tp->tv_usec); 359 } else { 360 return talloc_asprintf(ctx, 361 "%ld seconds since the Epoch", 362 (long)t); 363 } 364 } else { 365 #ifdef HAVE_STRFTIME 366 char TimeBuf[60]; 367 if (hires) { 368 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm); 369 return talloc_asprintf(ctx, 370 "%s.%06ld", TimeBuf, 371 (long)tp->tv_usec); 372 } else { 373 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm); 374 return talloc_strdup(ctx, TimeBuf); 375 } 376 #else 377 if (hires) { 378 const char *asct = asctime(tm); 379 return talloc_asprintf(ctx, "%s.%06ld", 380 asct ? asct : "unknown", 381 (long)tp->tv_usec); 382 } else { 383 const char *asct = asctime(tm); 384 return talloc_asprintf(ctx, asct ? asct : "unknown"); 385 } 386 #endif 387 } 388 } 389 390 char *current_timestring(TALLOC_CTX *ctx, bool hires) 391 { 392 struct timeval tv; 393 394 GetTimeOfDay(&tv); 395 return timeval_string(ctx, &tv, hires); 396 } 397 398 302 399 /** 303 400 return a HTTP/1.0 time string … … 347 444 348 445 #ifdef HAVE_STRFTIME 349 /* some versions of gcc complain about using %c. This is a bug 350 in the gcc warning, not a bug in this code. See a recent 351 strftime() manual page for details. 352 */ 353 strftime(tempTime,sizeof(tempTime)-1,"%c %Z",tm); 446 /* Some versions of gcc complain about using some special format 447 * specifiers. This is a bug in gcc, not a bug in this code. See a 448 * recent strftime() manual page for details. */ 449 strftime(tempTime,sizeof(tempTime)-1,"%a %b %e %X %Y %Z",tm); 354 450 TimeBuf = talloc_strdup(mem_ctx, tempTime); 355 451 #else … … 398 494 int64_t sec_diff = tv1->tv_sec - tv2->tv_sec; 399 495 return (sec_diff * 1000000) + (int64_t)(tv1->tv_usec - tv2->tv_usec); 496 } 497 498 /** 499 return (tp1 - tp2) in microseconds 500 */ 501 _PUBLIC_ int64_t nsec_time_diff(const struct timespec *tp1, const struct timespec *tp2) 502 { 503 int64_t sec_diff = tp1->tv_sec - tp2->tv_sec; 504 return (sec_diff * 1000000000) + (int64_t)(tp1->tv_nsec - tp2->tv_nsec); 400 505 } 401 506
Note:
See TracChangeset
for help on using the changeset viewer.