| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include "tkPort.h" |
| 15 |
#include "Lang.h" |
| 16 |
#ifdef TCL_EVENT_IMPLEMENT |
| 17 |
|
| 18 |
#if TIME_WITH_SYS_TIME |
| 19 |
# include <sys/time.h> |
| 20 |
# include <time.h> |
| 21 |
#else |
| 22 |
# if HAVE_SYS_TIME_H |
| 23 |
# include <sys/time.h> |
| 24 |
# else |
| 25 |
# include <time.h> |
| 26 |
# endif |
| 27 |
#endif |
| 28 |
|
| 29 |
#if 0 |
| 30 |
#include "tclInt.h" |
| 31 |
#include "tclPort.h" |
| 32 |
#include <locale.h> |
| 33 |
#endif |
| 34 |
#ifdef TIMEOFDAY_NO_TZ |
| 35 |
#define Tk_timeofday(x) gettimeofday(x) |
| 36 |
extern int gettimeofday _ANSI_ARGS_((struct timeval *tp)); |
| 37 |
#else |
| 38 |
#ifdef TIMEOFDAY_TZ |
| 39 |
#define Tk_timeofday(x) gettimeofday(x, (struct timezone *) 0) |
| 40 |
extern int gettimeofday _ANSI_ARGS_((struct timeval *tp ,struct timezone *tzp)); |
| 41 |
#else |
| 42 |
#define Tk_timeofday(x) gettimeofday(x, (void *) 0) |
| 43 |
#ifdef TIMEOFDAY_DOTS |
| 44 |
extern int gettimeofday _ANSI_ARGS_((struct timeval *tp, ...)); |
| 45 |
#else |
| 46 |
extern int gettimeofday _ANSI_ARGS_((struct timeval *tp, void *)); |
| 47 |
#endif |
| 48 |
#endif |
| 49 |
#endif |
| 50 |
|
| 51 |
#define TM_YEAR_BASE 1900 |
| 52 |
#define IsLeapYear(x) ((x % 4 == 0) && (x % 100 != 0 || x % 400 == 0)) |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static Tcl_ThreadDataKey tmKey; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#if !defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R) |
| 68 |
TCL_DECLARE_MUTEX(tmMutex) |
| 69 |
#endif |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
static struct tm *ThreadSafeGMTime _ANSI_ARGS_(( CONST time_t* )); |
| 76 |
static struct tm *ThreadSafeLocalTime _ANSI_ARGS_(( CONST time_t* )); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
unsigned long |
| 96 |
TclpGetSeconds() |
| 97 |
{ |
| 98 |
return time((time_t *) NULL); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
unsigned long |
| 121 |
TclpGetClicks() |
| 122 |
{ |
| 123 |
unsigned long now; |
| 124 |
#ifdef NO_GETTOD |
| 125 |
struct tms dummy; |
| 126 |
now = (unsigned long) times(&dummy); |
| 127 |
#else |
| 128 |
struct timeval date; |
| 129 |
Tk_timeofday(&date); |
| 130 |
now = date.tv_sec*1000000 + date.tv_usec; |
| 131 |
#endif |
| 132 |
|
| 133 |
return now; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
#if 0 |
| 155 |
int |
| 156 |
TclpGetTimeZone (currentTime) |
| 157 |
unsigned long currentTime; |
| 158 |
{ |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
#if defined(HAVE_TM_TZADJ) |
| 169 |
# define TCL_GOT_TIMEZONE |
| 170 |
time_t curTime = (time_t) currentTime; |
| 171 |
struct tm *timeDataPtr = ThreadSafeLocalTime(&curTime); |
| 172 |
int timeZone; |
| 173 |
|
| 174 |
timeZone = timeDataPtr->tm_tzadj / 60; |
| 175 |
if (timeDataPtr->tm_isdst) { |
| 176 |
timeZone += 60; |
| 177 |
} |
| 178 |
|
| 179 |
return timeZone; |
| 180 |
#endif |
| 181 |
|
| 182 |
#if defined(HAVE_TM_GMTOFF) && !defined (TCL_GOT_TIMEZONE) |
| 183 |
# define TCL_GOT_TIMEZONE |
| 184 |
time_t curTime = (time_t) currentTime; |
| 185 |
struct tm *timeDataPtr = ThreadSafeLocalTime(&curTime); |
| 186 |
int timeZone; |
| 187 |
|
| 188 |
timeZone = -(timeDataPtr->tm_gmtoff / 60); |
| 189 |
if (timeDataPtr->tm_isdst) { |
| 190 |
timeZone += 60; |
| 191 |
} |
| 192 |
|
| 193 |
return timeZone; |
| 194 |
#endif |
| 195 |
|
| 196 |
#if defined(USE_DELTA_FOR_TZ) |
| 197 |
#define TCL_GOT_TIMEZONE 1 |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
int timeZone; |
| 205 |
time_t tt; |
| 206 |
struct tm *stm; |
| 207 |
tt = 849268800L; |
| 208 |
stm = ThreadSafeLocalTime(&tt); |
| 209 |
|
| 210 |
timeZone = (12 - stm->tm_hour)*60 + (0 - stm->tm_min); |
| 211 |
return timeZone; |
| 212 |
#endif |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#if defined(HAVE_TIMEZONE_VAR) && !defined (TCL_GOT_TIMEZONE) |
| 221 |
# define TCL_GOT_TIMEZONE |
| 222 |
static int setTZ = 0; |
| 223 |
#ifdef TCL_THREADS |
| 224 |
static Tcl_Mutex tzMutex; |
| 225 |
#endif |
| 226 |
int timeZone; |
| 227 |
|
| 228 |
Tcl_MutexLock(&tzMutex); |
| 229 |
if (!setTZ) { |
| 230 |
tzset(); |
| 231 |
setTZ = 1; |
| 232 |
} |
| 233 |
Tcl_MutexUnlock(&tzMutex); |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
timeZone = timezone / 60; |
| 241 |
|
| 242 |
return timeZone; |
| 243 |
#endif |
| 244 |
|
| 245 |
#if defined(HAVE_GETTIMEOFDAY) && !defined (TCL_GOT_TIMEZONE) && !defined (TIMEOFDAY_NO_TZ) |
| 246 |
# define TCL_GOT_TIMEZONE |
| 247 |
struct timeval tv; |
| 248 |
struct timezone tz; |
| 249 |
int timeZone; |
| 250 |
|
| 251 |
gettimeofday(&tv, &tz); |
| 252 |
timeZone = tz.tz_minuteswest; |
| 253 |
if (tz.tz_dsttime) { |
| 254 |
timeZone += 60; |
| 255 |
} |
| 256 |
|
| 257 |
return timeZone; |
| 258 |
#endif |
| 259 |
|
| 260 |
#ifndef TCL_GOT_TIMEZONE |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
error: autoconf did not figure out how to determine the timezone. |
| 265 |
#endif |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
#endif |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
void |
| 288 |
Tcl_GetTime(timePtr) |
| 289 |
Tcl_Time *timePtr; |
| 290 |
{ |
| 291 |
#ifdef NO_GETTOD |
| 292 |
|
| 293 |
timePtr->sec = time((time_t *) NULL) |
| 294 |
timePtr->usec = 0; |
| 295 |
#else |
| 296 |
struct timeval tv; |
| 297 |
Tk_timeofday(&tv); |
| 298 |
timePtr->sec = tv.tv_sec; |
| 299 |
timePtr->usec = tv.tv_usec; |
| 300 |
#endif |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
struct tm * |
| 322 |
TclpGetDate(time, useGMT) |
| 323 |
TclpTime_t time; |
| 324 |
int useGMT; |
| 325 |
{ |
| 326 |
CONST time_t *tp = (CONST time_t *)time; |
| 327 |
|
| 328 |
if (useGMT) { |
| 329 |
return ThreadSafeGMTime(tp); |
| 330 |
} else { |
| 331 |
return ThreadSafeLocalTime(tp); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
#ifndef _LANG |
| 353 |
size_t |
| 354 |
TclpStrftime(s, maxsize, format, t, useGMT) |
| 355 |
char *s; |
| 356 |
size_t maxsize; |
| 357 |
CONST char *format; |
| 358 |
CONST struct tm *t; |
| 359 |
int useGMT; |
| 360 |
{ |
| 361 |
if (format[0] == '%' && format[1] == 'Q') { |
| 362 |
|
| 363 |
sprintf(s, "Stardate %2d%03d.%01d", |
| 364 |
(((t->tm_year + TM_YEAR_BASE) + 377) - 2323), |
| 365 |
(((t->tm_yday + 1) * 1000) / |
| 366 |
(365 + IsLeapYear((t->tm_year + TM_YEAR_BASE)))), |
| 367 |
(((t->tm_hour * 60) + t->tm_min)/144)); |
| 368 |
return(strlen(s)); |
| 369 |
} |
| 370 |
setlocale(LC_TIME, ""); |
| 371 |
return strftime(s, maxsize, format, t); |
| 372 |
} |
| 373 |
#endif |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
static struct tm * |
| 393 |
ThreadSafeGMTime(timePtr) |
| 394 |
CONST time_t *timePtr; |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
{ |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
struct tm *tmPtr = (struct tm *) |
| 404 |
Tcl_GetThreadData(&tmKey, sizeof(struct tm)); |
| 405 |
#ifdef HAVE_GMTIME_R |
| 406 |
gmtime_r(timePtr, tmPtr); |
| 407 |
#else |
| 408 |
Tcl_MutexLock(&tmMutex); |
| 409 |
memcpy((VOID *) tmPtr, (VOID *) gmtime(timePtr), sizeof(struct tm)); |
| 410 |
Tcl_MutexUnlock(&tmMutex); |
| 411 |
#endif |
| 412 |
return tmPtr; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
static struct tm * |
| 433 |
ThreadSafeLocalTime(timePtr) |
| 434 |
CONST time_t *timePtr; |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
{ |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
struct tm *tmPtr = (struct tm *) |
| 444 |
Tcl_GetThreadData(&tmKey, sizeof(struct tm)); |
| 445 |
#ifdef HAVE_LOCALTIME_R |
| 446 |
localtime_r(timePtr, tmPtr); |
| 447 |
#else |
| 448 |
Tcl_MutexLock(&tmMutex); |
| 449 |
memcpy((VOID *) tmPtr, (VOID *) localtime(timePtr), sizeof(struct tm)); |
| 450 |
Tcl_MutexUnlock(&tmMutex); |
| 451 |
#endif |
| 452 |
return tmPtr; |
| 453 |
} |
| 454 |
|
| 455 |
#endif |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|