| 1 |
/* |
| 2 |
* tclUnixTime.c -- |
| 3 |
* |
| 4 |
* Contains Unix specific versions of Tcl functions that obtain time |
| 5 |
* values from the operating system. |
| 6 |
* |
| 7 |
* Copyright (c) 1995 Sun Microsystems, Inc. |
| 8 |
* |
| 9 |
* See the file "license.terms" for information on usage and redistribution of |
| 10 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "tclInt.h" |
| 14 |
#include <locale.h> |
| 15 |
#if defined(TCL_WIDE_CLICKS) && defined(MAC_OSX_TCL) |
| 16 |
#include <mach/mach_time.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
/* |
| 20 |
* TclpGetDate is coded to return a pointer to a 'struct tm'. For thread |
| 21 |
* safety, this structure must be in thread-specific data. The 'tmKey' |
| 22 |
* variable is the key to this buffer. |
| 23 |
*/ |
| 24 |
|
| 25 |
static Tcl_ThreadDataKey tmKey; |
| 26 |
typedef struct ThreadSpecificData { |
| 27 |
struct tm gmtime_buf; |
| 28 |
struct tm localtime_buf; |
| 29 |
} ThreadSpecificData; |
| 30 |
|
| 31 |
/* |
| 32 |
* If we fall back on the thread-unsafe versions of gmtime and localtime, use |
| 33 |
* this mutex to try to protect them. |
| 34 |
*/ |
| 35 |
|
| 36 |
TCL_DECLARE_MUTEX(tmMutex) |
| 37 |
|
| 38 |
static char *lastTZ = NULL; /* Holds the last setting of the TZ |
| 39 |
* environment variable, or an empty string if |
| 40 |
* the variable was not set. */ |
| 41 |
|
| 42 |
/* |
| 43 |
* Static functions declared in this file. |
| 44 |
*/ |
| 45 |
|
| 46 |
static void SetTZIfNecessary(void); |
| 47 |
static void CleanupMemory(ClientData clientData); |
| 48 |
static void NativeScaleTime(Tcl_Time *timebuf, |
| 49 |
ClientData clientData); |
| 50 |
static void NativeGetTime(Tcl_Time *timebuf, |
| 51 |
ClientData clientData); |
| 52 |
|
| 53 |
/* |
| 54 |
* TIP #233 (Virtualized Time): Data for the time hooks, if any. |
| 55 |
*/ |
| 56 |
|
| 57 |
Tcl_GetTimeProc *tclGetTimeProcPtr = NativeGetTime; |
| 58 |
Tcl_ScaleTimeProc *tclScaleTimeProcPtr = NativeScaleTime; |
| 59 |
ClientData tclTimeClientData = NULL; |
| 60 |
|
| 61 |
/* |
| 62 |
*---------------------------------------------------------------------- |
| 63 |
* |
| 64 |
* TclpGetSeconds -- |
| 65 |
* |
| 66 |
* This procedure returns the number of seconds from the epoch. On most |
| 67 |
* Unix systems the epoch is Midnight Jan 1, 1970 GMT. |
| 68 |
* |
| 69 |
* Results: |
| 70 |
* Number of seconds from the epoch. |
| 71 |
* |
| 72 |
* Side effects: |
| 73 |
* None. |
| 74 |
* |
| 75 |
*---------------------------------------------------------------------- |
| 76 |
*/ |
| 77 |
|
| 78 |
unsigned long |
| 79 |
TclpGetSeconds(void) |
| 80 |
{ |
| 81 |
return time(NULL); |
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
*---------------------------------------------------------------------- |
| 86 |
* |
| 87 |
* TclpGetMicroseconds -- |
| 88 |
* |
| 89 |
* This procedure returns the number of microseconds from the epoch. |
| 90 |
* On most Unix systems the epoch is Midnight Jan 1, 1970 GMT. |
| 91 |
* |
| 92 |
* Results: |
| 93 |
* Number of microseconds from the epoch. |
| 94 |
* |
| 95 |
* Side effects: |
| 96 |
* None. |
| 97 |
* |
| 98 |
*---------------------------------------------------------------------- |
| 99 |
*/ |
| 100 |
|
| 101 |
Tcl_WideInt |
| 102 |
TclpGetMicroseconds(void) |
| 103 |
{ |
| 104 |
Tcl_Time time; |
| 105 |
|
| 106 |
tclGetTimeProcPtr(&time, tclTimeClientData); |
| 107 |
return ((Tcl_WideInt)time.sec)*1000000 + time.usec; |
| 108 |
} |
| 109 |
|
| 110 |
/* |
| 111 |
*---------------------------------------------------------------------- |
| 112 |
* |
| 113 |
* TclpGetClicks -- |
| 114 |
* |
| 115 |
* This procedure returns a value that represents the highest resolution |
| 116 |
* clock available on the system. There are no guarantees on what the |
| 117 |
* resolution will be. In Tcl we will call this value a "click". The |
| 118 |
* start time is also system dependent. |
| 119 |
* |
| 120 |
* Results: |
| 121 |
* Number of clicks from some start time. |
| 122 |
* |
| 123 |
* Side effects: |
| 124 |
* None. |
| 125 |
* |
| 126 |
*---------------------------------------------------------------------- |
| 127 |
*/ |
| 128 |
|
| 129 |
unsigned long |
| 130 |
TclpGetClicks(void) |
| 131 |
{ |
| 132 |
unsigned long now; |
| 133 |
|
| 134 |
#ifdef NO_GETTOD |
| 135 |
if (tclGetTimeProcPtr != NativeGetTime) { |
| 136 |
Tcl_Time time; |
| 137 |
|
| 138 |
tclGetTimeProcPtr(&time, tclTimeClientData); |
| 139 |
now = ((unsigned long)(time.sec)*1000000UL) + (unsigned long)(time.usec); |
| 140 |
} else { |
| 141 |
/* |
| 142 |
* A semi-NativeGetTime, specialized to clicks. |
| 143 |
*/ |
| 144 |
struct tms dummy; |
| 145 |
|
| 146 |
now = (unsigned long) times(&dummy); |
| 147 |
} |
| 148 |
#else |
| 149 |
Tcl_Time time; |
| 150 |
|
| 151 |
tclGetTimeProcPtr(&time, tclTimeClientData); |
| 152 |
now = ((unsigned long)(time.sec)*1000000UL) + (unsigned long)(time.usec); |
| 153 |
#endif |
| 154 |
|
| 155 |
return now; |
| 156 |
} |
| 157 |
#ifdef TCL_WIDE_CLICKS |
| 158 |
|
| 159 |
/* |
| 160 |
*---------------------------------------------------------------------- |
| 161 |
* |
| 162 |
* TclpGetWideClicks -- |
| 163 |
* |
| 164 |
* This procedure returns a WideInt value that represents the highest |
| 165 |
* resolution clock available on the system. There are no guarantees on |
| 166 |
* what the resolution will be. In Tcl we will call this value a "click". |
| 167 |
* The start time is also system dependent. |
| 168 |
* |
| 169 |
* Results: |
| 170 |
* Number of WideInt clicks from some start time. |
| 171 |
* |
| 172 |
* Side effects: |
| 173 |
* None. |
| 174 |
* |
| 175 |
*---------------------------------------------------------------------- |
| 176 |
*/ |
| 177 |
|
| 178 |
Tcl_WideInt |
| 179 |
TclpGetWideClicks(void) |
| 180 |
{ |
| 181 |
Tcl_WideInt now; |
| 182 |
|
| 183 |
if (tclGetTimeProcPtr != NativeGetTime) { |
| 184 |
Tcl_Time time; |
| 185 |
|
| 186 |
tclGetTimeProcPtr(&time, tclTimeClientData); |
| 187 |
now = ((Tcl_WideInt)time.sec)*1000000 + time.usec; |
| 188 |
} else { |
| 189 |
#ifdef MAC_OSX_TCL |
| 190 |
now = (Tcl_WideInt)(mach_absolute_time() & INT64_MAX); |
| 191 |
#else |
| 192 |
#error Wide high-resolution clicks not implemented on this platform |
| 193 |
#endif |
| 194 |
} |
| 195 |
|
| 196 |
return now; |
| 197 |
} |
| 198 |
|
| 199 |
/* |
| 200 |
*---------------------------------------------------------------------- |
| 201 |
* |
| 202 |
* TclpWideClicksToNanoseconds -- |
| 203 |
* |
| 204 |
* This procedure converts click values from the TclpGetWideClicks native |
| 205 |
* resolution to nanosecond resolution. |
| 206 |
* |
| 207 |
* Results: |
| 208 |
* Number of nanoseconds from some start time. |
| 209 |
* |
| 210 |
* Side effects: |
| 211 |
* None. |
| 212 |
* |
| 213 |
*---------------------------------------------------------------------- |
| 214 |
*/ |
| 215 |
|
| 216 |
double |
| 217 |
TclpWideClicksToNanoseconds( |
| 218 |
Tcl_WideInt clicks) |
| 219 |
{ |
| 220 |
double nsec; |
| 221 |
|
| 222 |
if (tclGetTimeProcPtr != NativeGetTime) { |
| 223 |
nsec = clicks * 1000; |
| 224 |
} else { |
| 225 |
#ifdef MAC_OSX_TCL |
| 226 |
static mach_timebase_info_data_t tb; |
| 227 |
static uint64_t maxClicksForUInt64; |
| 228 |
|
| 229 |
if (!tb.denom) { |
| 230 |
mach_timebase_info(&tb); |
| 231 |
maxClicksForUInt64 = UINT64_MAX / tb.numer; |
| 232 |
} |
| 233 |
if ((uint64_t) clicks < maxClicksForUInt64) { |
| 234 |
nsec = ((uint64_t) clicks) * tb.numer / tb.denom; |
| 235 |
} else { |
| 236 |
nsec = ((long double) (uint64_t) clicks) * tb.numer / tb.denom; |
| 237 |
} |
| 238 |
#else |
| 239 |
#error Wide high-resolution clicks not implemented on this platform |
| 240 |
#endif |
| 241 |
} |
| 242 |
|
| 243 |
return nsec; |
| 244 |
} |
| 245 |
|
| 246 |
/* |
| 247 |
*---------------------------------------------------------------------- |
| 248 |
* |
| 249 |
* TclpWideClickInMicrosec -- |
| 250 |
* |
| 251 |
* This procedure return scale to convert click values from the |
| 252 |
* TclpGetWideClicks native resolution to microsecond resolution |
| 253 |
* and back. |
| 254 |
* |
| 255 |
* Results: |
| 256 |
* 1 click in microseconds as double. |
| 257 |
* |
| 258 |
* Side effects: |
| 259 |
* None. |
| 260 |
* |
| 261 |
*---------------------------------------------------------------------- |
| 262 |
*/ |
| 263 |
|
| 264 |
double |
| 265 |
TclpWideClickInMicrosec(void) |
| 266 |
{ |
| 267 |
if (tclGetTimeProcPtr != NativeGetTime) { |
| 268 |
return 1.0; |
| 269 |
} else { |
| 270 |
#ifdef MAC_OSX_TCL |
| 271 |
static int initialized = 0; |
| 272 |
static double scale = 0.0; |
| 273 |
|
| 274 |
if (initialized) { |
| 275 |
return scale; |
| 276 |
} else { |
| 277 |
mach_timebase_info_data_t tb; |
| 278 |
|
| 279 |
mach_timebase_info(&tb); |
| 280 |
/* value of tb.numer / tb.denom = 1 click in nanoseconds */ |
| 281 |
scale = ((double)tb.numer) / tb.denom / 1000; |
| 282 |
initialized = 1; |
| 283 |
return scale; |
| 284 |
} |
| 285 |
#else |
| 286 |
#error Wide high-resolution clicks not implemented on this platform |
| 287 |
#endif |
| 288 |
} |
| 289 |
} |
| 290 |
#endif /* TCL_WIDE_CLICKS */ |
| 291 |
|
| 292 |
/* |
| 293 |
*---------------------------------------------------------------------- |
| 294 |
* |
| 295 |
* Tcl_GetTime -- |
| 296 |
* |
| 297 |
* Gets the current system time in seconds and microseconds since the |
| 298 |
* beginning of the epoch: 00:00 UCT, January 1, 1970. |
| 299 |
* |
| 300 |
* This function is hooked, allowing users to specify their own virtual |
| 301 |
* system time. |
| 302 |
* |
| 303 |
* Results: |
| 304 |
* Returns the current time in timePtr. |
| 305 |
* |
| 306 |
* Side effects: |
| 307 |
* None. |
| 308 |
* |
| 309 |
*---------------------------------------------------------------------- |
| 310 |
*/ |
| 311 |
|
| 312 |
void |
| 313 |
Tcl_GetTime( |
| 314 |
Tcl_Time *timePtr) /* Location to store time information. */ |
| 315 |
{ |
| 316 |
tclGetTimeProcPtr(timePtr, tclTimeClientData); |
| 317 |
} |
| 318 |
|
| 319 |
/* |
| 320 |
*---------------------------------------------------------------------- |
| 321 |
* |
| 322 |
* TclpGetDate -- |
| 323 |
* |
| 324 |
* This function converts between seconds and struct tm. If useGMT is |
| 325 |
* true, then the returned date will be in Greenwich Mean Time (GMT). |
| 326 |
* Otherwise, it will be in the local time zone. |
| 327 |
* |
| 328 |
* Results: |
| 329 |
* Returns a static tm structure. |
| 330 |
* |
| 331 |
* Side effects: |
| 332 |
* None. |
| 333 |
* |
| 334 |
*---------------------------------------------------------------------- |
| 335 |
*/ |
| 336 |
|
| 337 |
struct tm * |
| 338 |
TclpGetDate( |
| 339 |
const time_t *time, |
| 340 |
int useGMT) |
| 341 |
{ |
| 342 |
if (useGMT) { |
| 343 |
return TclpGmtime(time); |
| 344 |
} else { |
| 345 |
return TclpLocaltime(time); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
/* |
| 350 |
*---------------------------------------------------------------------- |
| 351 |
* |
| 352 |
* TclpGmtime -- |
| 353 |
* |
| 354 |
* Wrapper around the 'gmtime' library function to make it thread safe. |
| 355 |
* |
| 356 |
* Results: |
| 357 |
* Returns a pointer to a 'struct tm' in thread-specific data. |
| 358 |
* |
| 359 |
* Side effects: |
| 360 |
* Invokes gmtime or gmtime_r as appropriate. |
| 361 |
* |
| 362 |
*---------------------------------------------------------------------- |
| 363 |
*/ |
| 364 |
|
| 365 |
struct tm * |
| 366 |
TclpGmtime( |
| 367 |
const time_t *timePtr) /* Pointer to the number of seconds since the |
| 368 |
* local system's epoch */ |
| 369 |
{ |
| 370 |
/* |
| 371 |
* Get a thread-local buffer to hold the returned time. |
| 372 |
*/ |
| 373 |
|
| 374 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&tmKey); |
| 375 |
|
| 376 |
#ifdef HAVE_GMTIME_R |
| 377 |
gmtime_r(timePtr, &tsdPtr->gmtime_buf); |
| 378 |
#else |
| 379 |
Tcl_MutexLock(&tmMutex); |
| 380 |
memcpy(&tsdPtr->gmtime_buf, gmtime(timePtr), sizeof(struct tm)); |
| 381 |
Tcl_MutexUnlock(&tmMutex); |
| 382 |
#endif |
| 383 |
|
| 384 |
return &tsdPtr->gmtime_buf; |
| 385 |
} |
| 386 |
|
| 387 |
/* |
| 388 |
*---------------------------------------------------------------------- |
| 389 |
* |
| 390 |
* TclpLocaltime -- |
| 391 |
* |
| 392 |
* Wrapper around the 'localtime' library function to make it thread |
| 393 |
* safe. |
| 394 |
* |
| 395 |
* Results: |
| 396 |
* Returns a pointer to a 'struct tm' in thread-specific data. |
| 397 |
* |
| 398 |
* Side effects: |
| 399 |
* Invokes localtime or localtime_r as appropriate. |
| 400 |
* |
| 401 |
*---------------------------------------------------------------------- |
| 402 |
*/ |
| 403 |
|
| 404 |
struct tm * |
| 405 |
TclpLocaltime( |
| 406 |
const time_t *timePtr) /* Pointer to the number of seconds since the |
| 407 |
* local system's epoch */ |
| 408 |
{ |
| 409 |
/* |
| 410 |
* Get a thread-local buffer to hold the returned time. |
| 411 |
*/ |
| 412 |
|
| 413 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&tmKey); |
| 414 |
|
| 415 |
SetTZIfNecessary(); |
| 416 |
#ifdef HAVE_LOCALTIME_R |
| 417 |
localtime_r(timePtr, &tsdPtr->localtime_buf); |
| 418 |
#else |
| 419 |
Tcl_MutexLock(&tmMutex); |
| 420 |
memcpy(&tsdPtr->localtime_buf, localtime(timePtr), sizeof(struct tm)); |
| 421 |
Tcl_MutexUnlock(&tmMutex); |
| 422 |
#endif |
| 423 |
|
| 424 |
return &tsdPtr->localtime_buf; |
| 425 |
} |
| 426 |
|
| 427 |
/* |
| 428 |
*---------------------------------------------------------------------- |
| 429 |
* |
| 430 |
* Tcl_SetTimeProc -- |
| 431 |
* |
| 432 |
* TIP #233 (Virtualized Time): Registers two handlers for the |
| 433 |
* virtualization of Tcl's access to time information. |
| 434 |
* |
| 435 |
* Results: |
| 436 |
* None. |
| 437 |
* |
| 438 |
* Side effects: |
| 439 |
* Remembers the handlers, alters core behaviour. |
| 440 |
* |
| 441 |
*---------------------------------------------------------------------- |
| 442 |
*/ |
| 443 |
|
| 444 |
void |
| 445 |
Tcl_SetTimeProc( |
| 446 |
Tcl_GetTimeProc *getProc, |
| 447 |
Tcl_ScaleTimeProc *scaleProc, |
| 448 |
ClientData clientData) |
| 449 |
{ |
| 450 |
tclGetTimeProcPtr = getProc; |
| 451 |
tclScaleTimeProcPtr = scaleProc; |
| 452 |
tclTimeClientData = clientData; |
| 453 |
} |
| 454 |
|
| 455 |
/* |
| 456 |
*---------------------------------------------------------------------- |
| 457 |
* |
| 458 |
* Tcl_QueryTimeProc -- |
| 459 |
* |
| 460 |
* TIP #233 (Virtualized Time): Query which time handlers are registered. |
| 461 |
* |
| 462 |
* Results: |
| 463 |
* None. |
| 464 |
* |
| 465 |
* Side effects: |
| 466 |
* None. |
| 467 |
* |
| 468 |
*---------------------------------------------------------------------- |
| 469 |
*/ |
| 470 |
|
| 471 |
void |
| 472 |
Tcl_QueryTimeProc( |
| 473 |
Tcl_GetTimeProc **getProc, |
| 474 |
Tcl_ScaleTimeProc **scaleProc, |
| 475 |
ClientData *clientData) |
| 476 |
{ |
| 477 |
if (getProc) { |
| 478 |
*getProc = tclGetTimeProcPtr; |
| 479 |
} |
| 480 |
if (scaleProc) { |
| 481 |
*scaleProc = tclScaleTimeProcPtr; |
| 482 |
} |
| 483 |
if (clientData) { |
| 484 |
*clientData = tclTimeClientData; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
/* |
| 489 |
*---------------------------------------------------------------------- |
| 490 |
* |
| 491 |
* NativeScaleTime -- |
| 492 |
* |
| 493 |
* TIP #233: Scale from virtual time to the real-time. For native scaling |
| 494 |
* the relationship is 1:1 and nothing has to be done. |
| 495 |
* |
| 496 |
* Results: |
| 497 |
* Scales the time in timePtr. |
| 498 |
* |
| 499 |
* Side effects: |
| 500 |
* See above. |
| 501 |
* |
| 502 |
*---------------------------------------------------------------------- |
| 503 |
*/ |
| 504 |
|
| 505 |
static void |
| 506 |
NativeScaleTime( |
| 507 |
Tcl_Time *timePtr, |
| 508 |
ClientData clientData) |
| 509 |
{ |
| 510 |
/* Native scale is 1:1. Nothing is done */ |
| 511 |
} |
| 512 |
|
| 513 |
/* |
| 514 |
*---------------------------------------------------------------------- |
| 515 |
* |
| 516 |
* NativeGetTime -- |
| 517 |
* |
| 518 |
* TIP #233: Gets the current system time in seconds and microseconds |
| 519 |
* since the beginning of the epoch: 00:00 UCT, January 1, 1970. |
| 520 |
* |
| 521 |
* Results: |
| 522 |
* Returns the current time in timePtr. |
| 523 |
* |
| 524 |
* Side effects: |
| 525 |
* None. |
| 526 |
* |
| 527 |
*---------------------------------------------------------------------- |
| 528 |
*/ |
| 529 |
|
| 530 |
static void |
| 531 |
NativeGetTime( |
| 532 |
Tcl_Time *timePtr, |
| 533 |
ClientData clientData) |
| 534 |
{ |
| 535 |
struct timeval tv; |
| 536 |
|
| 537 |
(void) gettimeofday(&tv, NULL); |
| 538 |
timePtr->sec = tv.tv_sec; |
| 539 |
timePtr->usec = tv.tv_usec; |
| 540 |
} |
| 541 |
/* |
| 542 |
*---------------------------------------------------------------------- |
| 543 |
* |
| 544 |
* SetTZIfNecessary -- |
| 545 |
* |
| 546 |
* Determines whether a call to 'tzset' is needed prior to the next call |
| 547 |
* to 'localtime' or examination of the 'timezone' variable. |
| 548 |
* |
| 549 |
* Results: |
| 550 |
* None. |
| 551 |
* |
| 552 |
* Side effects: |
| 553 |
* If 'tzset' has never been called in the current process, or if the |
| 554 |
* value of the environment variable TZ has changed since the last call |
| 555 |
* to 'tzset', then 'tzset' is called again. |
| 556 |
* |
| 557 |
*---------------------------------------------------------------------- |
| 558 |
*/ |
| 559 |
|
| 560 |
static void |
| 561 |
SetTZIfNecessary(void) |
| 562 |
{ |
| 563 |
const char *newTZ = getenv("TZ"); |
| 564 |
|
| 565 |
Tcl_MutexLock(&tmMutex); |
| 566 |
if (newTZ == NULL) { |
| 567 |
newTZ = ""; |
| 568 |
} |
| 569 |
if (lastTZ == NULL || strcmp(lastTZ, newTZ)) { |
| 570 |
tzset(); |
| 571 |
if (lastTZ == NULL) { |
| 572 |
Tcl_CreateExitHandler(CleanupMemory, NULL); |
| 573 |
} else { |
| 574 |
ckfree(lastTZ); |
| 575 |
} |
| 576 |
lastTZ = ckalloc(strlen(newTZ) + 1); |
| 577 |
strcpy(lastTZ, newTZ); |
| 578 |
} |
| 579 |
Tcl_MutexUnlock(&tmMutex); |
| 580 |
} |
| 581 |
|
| 582 |
/* |
| 583 |
*---------------------------------------------------------------------- |
| 584 |
* |
| 585 |
* CleanupMemory -- |
| 586 |
* |
| 587 |
* Releases the private copy of the TZ environment variable upon exit |
| 588 |
* from Tcl. |
| 589 |
* |
| 590 |
* Results: |
| 591 |
* None. |
| 592 |
* |
| 593 |
* Side effects: |
| 594 |
* Frees allocated memory. |
| 595 |
* |
| 596 |
*---------------------------------------------------------------------- |
| 597 |
*/ |
| 598 |
|
| 599 |
static void |
| 600 |
CleanupMemory( |
| 601 |
ClientData ignored) |
| 602 |
{ |
| 603 |
ckfree(lastTZ); |
| 604 |
} |
| 605 |
|
| 606 |
/* |
| 607 |
* Local Variables: |
| 608 |
* mode: c |
| 609 |
* c-basic-offset: 4 |
| 610 |
* fill-column: 78 |
| 611 |
* End: |
| 612 |
*/ |