Debian Codegraph @

generic/tclClock.c

raw
1 /*
2 * tclClock.c --
3 *
4 * Contains the time and date related commands. This code is derived from
5 * the time and date facilities of TclX, by Mark Diekhans and Karl
6 * Lehenbauer.
7 *
8 * Copyright (c) 1991-1995 Karl Lehenbauer & Mark Diekhans.
9 * Copyright (c) 1995 Sun Microsystems, Inc.
10 * Copyright (c) 2004 by Kevin B. Kenny. All rights reserved.
11 *
12 * See the file "license.terms" for information on usage and redistribution of
13 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 */
15
16 #include "tclInt.h"
17
18 /*
19 * Windows has mktime. The configurators do not check.
20 */
21
22 #ifdef _WIN32
23 #define HAVE_MKTIME 1
24 #endif
25
26 /*
27 * Constants
28 */
29
30 #define JULIAN_DAY_POSIX_EPOCH 2440588
31 #define SECONDS_PER_DAY 86400
32 #define JULIAN_SEC_POSIX_EPOCH (((Tcl_WideInt)JULIAN_DAY_POSIX_EPOCH) \
33 * SECONDS_PER_DAY)
34 #define FOUR_CENTURIES 146097 /* days */
35 #define JDAY_1_JAN_1_CE_JULIAN 1721424
36 #define JDAY_1_JAN_1_CE_GREGORIAN 1721426
37 #define ONE_CENTURY_GREGORIAN 36524 /* days */
38 #define FOUR_YEARS 1461 /* days */
39 #define ONE_YEAR 365 /* days */
40
41 /*
42 * Table of the days in each month, leap and common years
43 */
44
45 static const int hath[2][12] = {
46 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
47 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
48 };
49 static const int daysInPriorMonths[2][13] = {
50 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
51 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
52 };
53
54 /*
55 * Enumeration of the string literals used in [clock]
56 */
57
58 typedef enum ClockLiteral {
59 LIT__NIL,
60 LIT__DEFAULT_FORMAT,
61 LIT_BCE, LIT_C,
62 LIT_CANNOT_USE_GMT_AND_TIMEZONE,
63 LIT_CE,
64 LIT_DAYOFMONTH, LIT_DAYOFWEEK, LIT_DAYOFYEAR,
65 LIT_ERA, LIT_GMT, LIT_GREGORIAN,
66 LIT_INTEGER_VALUE_TOO_LARGE,
67 LIT_ISO8601WEEK, LIT_ISO8601YEAR,
68 LIT_JULIANDAY, LIT_LOCALSECONDS,
69 LIT_MONTH,
70 LIT_SECONDS, LIT_TZNAME, LIT_TZOFFSET,
71 LIT_YEAR,
72 LIT__END
73 } ClockLiteral;
74 static const char *const literals[] = {
75 "",
76 "%a %b %d %H:%M:%S %Z %Y",
77 "BCE", "C",
78 "cannot use -gmt and -timezone in same call",
79 "CE",
80 "dayOfMonth", "dayOfWeek", "dayOfYear",
81 "era", ":GMT", "gregorian",
82 "integer value too large to represent",
83 "iso8601Week", "iso8601Year",
84 "julianDay", "localSeconds",
85 "month",
86 "seconds", "tzName", "tzOffset",
87 "year"
88 };
89
90 /*
91 * Structure containing the client data for [clock]
92 */
93
94 typedef struct {
95 size_t refCount; /* Number of live references. */
96 Tcl_Obj **literals; /* Pool of object literals. */
97 } ClockClientData;
98
99 /*
100 * Structure containing the fields used in [clock format] and [clock scan]
101 */
102
103 typedef struct TclDateFields {
104 Tcl_WideInt seconds; /* Time expressed in seconds from the Posix
105 * epoch */
106 Tcl_WideInt localSeconds; /* Local time expressed in nominal seconds
107 * from the Posix epoch */
108 int tzOffset; /* Time zone offset in seconds east of
109 * Greenwich */
110 Tcl_Obj *tzName; /* Time zone name */
111 int julianDay; /* Julian Day Number in local time zone */
112 enum {BCE=1, CE=0} era; /* Era */
113 int gregorian; /* Flag == 1 if the date is Gregorian */
114 int year; /* Year of the era */
115 int dayOfYear; /* Day of the year (1 January == 1) */
116 int month; /* Month number */
117 int dayOfMonth; /* Day of the month */
118 int iso8601Year; /* ISO8601 week-based year */
119 int iso8601Week; /* ISO8601 week number */
120 int dayOfWeek; /* Day of the week */
121 } TclDateFields;
122 static const char *const eras[] = { "CE", "BCE", NULL };
123
124 /*
125 * Thread specific data block holding a 'struct tm' for the 'gmtime' and
126 * 'localtime' library calls.
127 */
128
129 static Tcl_ThreadDataKey tmKey;
130
131 /*
132 * Mutex protecting 'gmtime', 'localtime' and 'mktime' calls and the statics
133 * in the date parsing code.
134 */
135
136 TCL_DECLARE_MUTEX(clockMutex)
137
138 /*
139 * Function prototypes for local procedures in this file:
140 */
141
142 static int ConvertUTCToLocal(Tcl_Interp *,
143 TclDateFields *, Tcl_Obj *, int);
144 static int ConvertUTCToLocalUsingTable(Tcl_Interp *,
145 TclDateFields *, int, Tcl_Obj *const[]);
146 static int ConvertUTCToLocalUsingC(Tcl_Interp *,
147 TclDateFields *, int);
148 static int ConvertLocalToUTC(Tcl_Interp *,
149 TclDateFields *, Tcl_Obj *, int);
150 static int ConvertLocalToUTCUsingTable(Tcl_Interp *,
151 TclDateFields *, int, Tcl_Obj *const[]);
152 static int ConvertLocalToUTCUsingC(Tcl_Interp *,
153 TclDateFields *, int);
154 static Tcl_Obj * LookupLastTransition(Tcl_Interp *, Tcl_WideInt,
155 int, Tcl_Obj *const *);
156 static void GetYearWeekDay(TclDateFields *, int);
157 static void GetGregorianEraYearDay(TclDateFields *, int);
158 static void GetMonthDay(TclDateFields *);
159 static void GetJulianDayFromEraYearWeekDay(TclDateFields *, int);
160 static void GetJulianDayFromEraYearMonthDay(TclDateFields *, int);
161 static int IsGregorianLeapYear(TclDateFields *);
162 static int WeekdayOnOrBefore(int, int);
163 static int ClockClicksObjCmd(
164 ClientData clientData, Tcl_Interp *interp,
165 int objc, Tcl_Obj *const objv[]);
166 static int ClockConvertlocaltoutcObjCmd(
167 ClientData clientData, Tcl_Interp *interp,
168 int objc, Tcl_Obj *const objv[]);
169 static int ClockGetdatefieldsObjCmd(
170 ClientData clientData, Tcl_Interp *interp,
171 int objc, Tcl_Obj *const objv[]);
172 static int ClockGetjuliandayfromerayearmonthdayObjCmd(
173 ClientData clientData, Tcl_Interp *interp,
174 int objc, Tcl_Obj *const objv[]);
175 static int ClockGetjuliandayfromerayearweekdayObjCmd(
176 ClientData clientData, Tcl_Interp *interp,
177 int objc, Tcl_Obj *const objv[]);
178 static int ClockGetenvObjCmd(
179 ClientData clientData, Tcl_Interp *interp,
180 int objc, Tcl_Obj *const objv[]);
181 static int ClockMicrosecondsObjCmd(
182 ClientData clientData, Tcl_Interp *interp,
183 int objc, Tcl_Obj *const objv[]);
184 static int ClockMillisecondsObjCmd(
185 ClientData clientData, Tcl_Interp *interp,
186 int objc, Tcl_Obj *const objv[]);
187 static int ClockParseformatargsObjCmd(
188 ClientData clientData, Tcl_Interp *interp,
189 int objc, Tcl_Obj *const objv[]);
190 static int ClockSecondsObjCmd(
191 ClientData clientData, Tcl_Interp *interp,
192 int objc, Tcl_Obj *const objv[]);
193 static struct tm * ThreadSafeLocalTime(const time_t *);
194 static void TzsetIfNecessary(void);
195 static void ClockDeleteCmdProc(ClientData);
196
197 /*
198 * Structure containing description of "native" clock commands to create.
199 */
200
201 struct ClockCommand {
202 const char *name; /* The tail of the command name. The full name
203 * is "::tcl::clock::<name>". When NULL marks
204 * the end of the table. */
205 Tcl_ObjCmdProc *objCmdProc; /* Function that implements the command. This
206 * will always have the ClockClientData sent
207 * to it, but may well ignore this data. */
208 };
209
210 static const struct ClockCommand clockCommands[] = {
211 { "getenv", ClockGetenvObjCmd },
212 { "Oldscan", TclClockOldscanObjCmd },
213 { "ConvertLocalToUTC", ClockConvertlocaltoutcObjCmd },
214 { "GetDateFields", ClockGetdatefieldsObjCmd },
215 { "GetJulianDayFromEraYearMonthDay",
216 ClockGetjuliandayfromerayearmonthdayObjCmd },
217 { "GetJulianDayFromEraYearWeekDay",
218 ClockGetjuliandayfromerayearweekdayObjCmd },
219 { "ParseFormatArgs", ClockParseformatargsObjCmd },
220 { NULL, NULL }
221 };
222
223 /*
224 *----------------------------------------------------------------------
225 *
226 * TclClockInit --
227 *
228 * Registers the 'clock' subcommands with the Tcl interpreter and
229 * initializes its client data (which consists mostly of constant
230 * Tcl_Obj's that it is too much trouble to keep recreating).
231 *
232 * Results:
233 * None.
234 *
235 * Side effects:
236 * Installs the commands and creates the client data
237 *
238 *----------------------------------------------------------------------
239 */
240
241 void
242 TclClockInit(
243 Tcl_Interp *interp) /* Tcl interpreter */
244 {
245 const struct ClockCommand *clockCmdPtr;
246 char cmdName[50]; /* Buffer large enough to hold the string
247 *::tcl::clock::GetJulianDayFromEraYearMonthDay
248 * plus a terminating NUL. */
249 ClockClientData *data;
250 int i;
251
252 /* Structure of the 'clock' ensemble */
253
254 static const EnsembleImplMap clockImplMap[] = {
255 {"add", NULL, TclCompileBasicMin1ArgCmd, NULL, NULL, 0},
256 {"clicks", ClockClicksObjCmd, TclCompileClockClicksCmd, NULL, NULL, 0},
257 {"format", NULL, TclCompileBasicMin1ArgCmd, NULL, NULL, 0},
258 {"microseconds", ClockMicrosecondsObjCmd, TclCompileClockReadingCmd, NULL, INT2PTR(1), 0},
259 {"milliseconds", ClockMillisecondsObjCmd, TclCompileClockReadingCmd, NULL, INT2PTR(2), 0},
260 {"scan", NULL, TclCompileBasicMin1ArgCmd, NULL, NULL , 0},
261 {"seconds", ClockSecondsObjCmd, TclCompileClockReadingCmd, NULL, INT2PTR(3), 0},
262 {NULL, NULL, NULL, NULL, NULL, 0}
263 };
264
265 /*
266 * Safe interps get [::clock] as alias to a parent, so do not need their
267 * own copies of the support routines.
268 */
269
270 if (Tcl_IsSafe(interp)) {
271 return;
272 }
273
274 /*
275 * Create the client data, which is a refcounted literal pool.
276 */
277
278 data = (ClockClientData *)ckalloc(sizeof(ClockClientData));
279 data->refCount = 0;
280 data->literals = (Tcl_Obj **)ckalloc(LIT__END * sizeof(Tcl_Obj*));
281 for (i = 0; i < LIT__END; ++i) {
282 data->literals[i] = Tcl_NewStringObj(literals[i], -1);
283 Tcl_IncrRefCount(data->literals[i]);
284 }
285
286 /*
287 * Install the commands.
288 * TODO - Let Tcl_MakeEnsemble do this?
289 */
290
291 #define TCL_CLOCK_PREFIX_LEN 14 /* == strlen("::tcl::clock::") */
292 memcpy(cmdName, "::tcl::clock::", TCL_CLOCK_PREFIX_LEN);
293 for (clockCmdPtr=clockCommands ; clockCmdPtr->name!=NULL ; clockCmdPtr++) {
294 strcpy(cmdName + TCL_CLOCK_PREFIX_LEN, clockCmdPtr->name);
295 data->refCount++;
296 Tcl_CreateObjCommand(interp, cmdName, clockCmdPtr->objCmdProc, data,
297 ClockDeleteCmdProc);
298 }
299
300 /* Make the clock ensemble */
301
302 TclMakeEnsemble(interp, "clock", clockImplMap);
303 }
304
305 /*
306 *----------------------------------------------------------------------
307 *
308 * ClockConvertlocaltoutcObjCmd --
309 *
310 * Tcl command that converts a UTC time to a local time by whatever means
311 * is available.
312 *
313 * Usage:
314 * ::tcl::clock::ConvertUTCToLocal dictionary tzdata changeover
315 *
316 * Parameters:
317 * dict - Dictionary containing a 'localSeconds' entry.
318 * tzdata - Time zone data
319 * changeover - Julian Day of the adoption of the Gregorian calendar.
320 *
321 * Results:
322 * Returns a standard Tcl result.
323 *
324 * Side effects:
325 * On success, sets the interpreter result to the given dictionary
326 * augmented with a 'seconds' field giving the UTC time. On failure,
327 * leaves an error message in the interpreter result.
328 *
329 *----------------------------------------------------------------------
330 */
331
332 static int
333 ClockConvertlocaltoutcObjCmd(
334 ClientData clientData, /* Client data */
335 Tcl_Interp *interp, /* Tcl interpreter */
336 int objc, /* Parameter count */
337 Tcl_Obj *const *objv) /* Parameter vector */
338 {
339 ClockClientData *data = (ClockClientData *)clientData;
340 Tcl_Obj *const *lit = data->literals;
341 Tcl_Obj *secondsObj;
342 Tcl_Obj *dict;
343 int changeover;
344 TclDateFields fields;
345 int created = 0;
346 int status;
347
348 /*
349 * Check params and convert time.
350 */
351
352 if (objc != 4) {
353 Tcl_WrongNumArgs(interp, 1, objv, "dict tzdata changeover");
354 return TCL_ERROR;
355 }
356 dict = objv[1];
357 if (Tcl_DictObjGet(interp, dict, lit[LIT_LOCALSECONDS],
358 &secondsObj)!= TCL_OK) {
359 return TCL_ERROR;
360 }
361 if (secondsObj == NULL) {
362 Tcl_SetObjResult(interp, Tcl_NewStringObj("key \"localseconds\" not "
363 "found in dictionary", -1));
364 return TCL_ERROR;
365 }
366 if ((TclGetWideIntFromObj(interp, secondsObj,
367 &fields.localSeconds) != TCL_OK)
368 || (TclGetIntFromObj(interp, objv[3], &changeover) != TCL_OK)
369 || ConvertLocalToUTC(interp, &fields, objv[2], changeover)) {
370 return TCL_ERROR;
371 }
372
373 /*
374 * Copy-on-write; set the 'seconds' field in the dictionary and place the
375 * modified dictionary in the interpreter result.
376 */
377
378 if (Tcl_IsShared(dict)) {
379 dict = Tcl_DuplicateObj(dict);
380 created = 1;
381 Tcl_IncrRefCount(dict);
382 }
383 status = Tcl_DictObjPut(interp, dict, lit[LIT_SECONDS],
384 Tcl_NewWideIntObj(fields.seconds));
385 if (status == TCL_OK) {
386 Tcl_SetObjResult(interp, dict);
387 }
388 if (created) {
389 Tcl_DecrRefCount(dict);
390 }
391 return status;
392 }
393
394 /*
395 *----------------------------------------------------------------------
396 *
397 * ClockGetdatefieldsObjCmd --
398 *
399 * Tcl command that determines the values that [clock format] will use in
400 * formatting a date, and populates a dictionary with them.
401 *
402 * Usage:
403 * ::tcl::clock::GetDateFields seconds tzdata changeover
404 *
405 * Parameters:
406 * seconds - Time expressed in seconds from the Posix epoch.
407 * tzdata - Time zone data of the time zone in which time is to be
408 * expressed.
409 * changeover - Julian Day Number at which the current locale adopted
410 * the Gregorian calendar
411 *
412 * Results:
413 * Returns a dictonary populated with the fields:
414 * seconds - Seconds from the Posix epoch
415 * localSeconds - Nominal seconds from the Posix epoch in the
416 * local time zone.
417 * tzOffset - Time zone offset in seconds east of Greenwich
418 * tzName - Time zone name
419 * julianDay - Julian Day Number in the local time zone
420 *
421 *----------------------------------------------------------------------
422 */
423
424 int
425 ClockGetdatefieldsObjCmd(
426 ClientData clientData, /* Opaque pointer to literal pool, etc. */
427 Tcl_Interp *interp, /* Tcl interpreter */
428 int objc, /* Parameter count */
429 Tcl_Obj *const *objv) /* Parameter vector */
430 {
431 TclDateFields fields;
432 Tcl_Obj *dict;
433 ClockClientData *data = (ClockClientData *)clientData;
434 Tcl_Obj *const *lit = data->literals;
435 int changeover;
436
437 /*
438 * Check params.
439 */
440
441 if (objc != 4) {
442 Tcl_WrongNumArgs(interp, 1, objv, "seconds tzdata changeover");
443 return TCL_ERROR;
444 }
445 if (TclGetWideIntFromObj(interp, objv[1], &fields.seconds) != TCL_OK
446 || TclGetIntFromObj(interp, objv[3], &changeover) != TCL_OK) {
447 return TCL_ERROR;
448 }
449
450 /*
451 * fields.seconds could be an unsigned number that overflowed. Make sure
452 * that it isn't.
453 */
454
455 if (objv[1]->typePtr == &tclBignumType) {
456 Tcl_SetObjResult(interp, lit[LIT_INTEGER_VALUE_TOO_LARGE]);
457 return TCL_ERROR;
458 }
459
460 /*
461 * Convert UTC time to local.
462 */
463
464 if (ConvertUTCToLocal(interp, &fields, objv[2], changeover) != TCL_OK) {
465 return TCL_ERROR;
466 }
467
468 /*
469 * Extract Julian day. Always round the quotient down by subtracting 1
470 * when the remainder is negative (i.e. if the quotient was rounded up).
471 */
472
473 fields.julianDay = (int) ((fields.localSeconds / SECONDS_PER_DAY) -
474 ((fields.localSeconds % SECONDS_PER_DAY) < 0) +
475 JULIAN_DAY_POSIX_EPOCH);
476
477 /*
478 * Convert to Julian or Gregorian calendar.
479 */
480
481 GetGregorianEraYearDay(&fields, changeover);
482 GetMonthDay(&fields);
483 GetYearWeekDay(&fields, changeover);
484
485 dict = Tcl_NewDictObj();
486 Tcl_DictObjPut(NULL, dict, lit[LIT_LOCALSECONDS],
487 Tcl_NewWideIntObj(fields.localSeconds));
488 Tcl_DictObjPut(NULL, dict, lit[LIT_SECONDS],
489 Tcl_NewWideIntObj(fields.seconds));
490 Tcl_DictObjPut(NULL, dict, lit[LIT_TZNAME], fields.tzName);
491 Tcl_DecrRefCount(fields.tzName);
492 Tcl_DictObjPut(NULL, dict, lit[LIT_TZOFFSET],
493 Tcl_NewIntObj(fields.tzOffset));
494 Tcl_DictObjPut(NULL, dict, lit[LIT_JULIANDAY],
495 Tcl_NewIntObj(fields.julianDay));
496 Tcl_DictObjPut(NULL, dict, lit[LIT_GREGORIAN],
497 Tcl_NewIntObj(fields.gregorian));
498 Tcl_DictObjPut(NULL, dict, lit[LIT_ERA],
499 lit[fields.era ? LIT_BCE : LIT_CE]);
500 Tcl_DictObjPut(NULL, dict, lit[LIT_YEAR],
501 Tcl_NewIntObj(fields.year));
502 Tcl_DictObjPut(NULL, dict, lit[LIT_DAYOFYEAR],
503 Tcl_NewIntObj(fields.dayOfYear));
504 Tcl_DictObjPut(NULL, dict, lit[LIT_MONTH],
505 Tcl_NewIntObj(fields.month));
506 Tcl_DictObjPut(NULL, dict, lit[LIT_DAYOFMONTH],
507 Tcl_NewIntObj(fields.dayOfMonth));
508 Tcl_DictObjPut(NULL, dict, lit[LIT_ISO8601YEAR],
509 Tcl_NewIntObj(fields.iso8601Year));
510 Tcl_DictObjPut(NULL, dict, lit[LIT_ISO8601WEEK],
511 Tcl_NewIntObj(fields.iso8601Week));
512 Tcl_DictObjPut(NULL, dict, lit[LIT_DAYOFWEEK],
513 Tcl_NewIntObj(fields.dayOfWeek));
514 Tcl_SetObjResult(interp, dict);
515
516 return TCL_OK;
517 }
518
519 /*
520 *----------------------------------------------------------------------
521 *
522 * ClockGetjuliandayfromerayearmonthdayObjCmd --
523 *
524 * Tcl command that converts a time from era-year-month-day to a Julian
525 * Day Number.
526 *
527 * Parameters:
528 * dict - Dictionary that contains 'era', 'year', 'month' and
529 * 'dayOfMonth' keys.
530 * changeover - Julian Day of changeover to the Gregorian calendar
531 *
532 * Results:
533 * Result is either TCL_OK, with the interpreter result being the
534 * dictionary augmented with a 'julianDay' key, or TCL_ERROR,
535 * with the result being an error message.
536 *
537 *----------------------------------------------------------------------
538 */
539
540 static int
541 FetchEraField(
542 Tcl_Interp *interp,
543 Tcl_Obj *dict,
544 Tcl_Obj *key,
545 int *storePtr)
546 {
547 Tcl_Obj *value = NULL;
548
549 if (Tcl_DictObjGet(interp, dict, key, &value) != TCL_OK) {
550 return TCL_ERROR;
551 }
552 if (value == NULL) {
553 Tcl_SetObjResult(interp, Tcl_NewStringObj(
554 "expected key(s) not found in dictionary", -1));
555 return TCL_ERROR;
556 }
557 return Tcl_GetIndexFromObj(interp, value, eras, "era", TCL_EXACT, storePtr);
558 }
559
560 static int
561 FetchIntField(
562 Tcl_Interp *interp,
563 Tcl_Obj *dict,
564 Tcl_Obj *key,
565 int *storePtr)
566 {
567 Tcl_Obj *value = NULL;
568
569 if (Tcl_DictObjGet(interp, dict, key, &value) != TCL_OK) {
570 return TCL_ERROR;
571 }
572 if (value == NULL) {
573 Tcl_SetObjResult(interp, Tcl_NewStringObj(
574 "expected key(s) not found in dictionary", -1));
575 return TCL_ERROR;
576 }
577 return TclGetIntFromObj(interp, value, storePtr);
578 }
579
580 static int
581 ClockGetjuliandayfromerayearmonthdayObjCmd(
582 ClientData clientData, /* Opaque pointer to literal pool, etc. */
583 Tcl_Interp *interp, /* Tcl interpreter */
584 int objc, /* Parameter count */
585 Tcl_Obj *const *objv) /* Parameter vector */
586 {
587 TclDateFields fields;
588 Tcl_Obj *dict;
589 ClockClientData *data = (ClockClientData *)clientData;
590 Tcl_Obj *const *lit = data->literals;
591 int changeover;
592 int copied = 0;
593 int status;
594 int era = 0;
595
596 /*
597 * Check params.
598 */
599
600 if (objc != 3) {
601 Tcl_WrongNumArgs(interp, 1, objv, "dict changeover");
602 return TCL_ERROR;
603 }
604 dict = objv[1];
605 if (FetchEraField(interp, dict, lit[LIT_ERA], &era) != TCL_OK
606 || FetchIntField(interp, dict, lit[LIT_YEAR], &fields.year)
607 != TCL_OK
608 || FetchIntField(interp, dict, lit[LIT_MONTH], &fields.month)
609 != TCL_OK
610 || FetchIntField(interp, dict, lit[LIT_DAYOFMONTH],
611 &fields.dayOfMonth) != TCL_OK
612 || TclGetIntFromObj(interp, objv[2], &changeover) != TCL_OK) {
613 return TCL_ERROR;
614 }
615 fields.era = era;
616
617 /*
618 * Get Julian day.
619 */
620
621 GetJulianDayFromEraYearMonthDay(&fields, changeover);
622
623 /*
624 * Store Julian day in the dictionary - copy on write.
625 */
626
627 if (Tcl_IsShared(dict)) {
628 dict = Tcl_DuplicateObj(dict);
629 Tcl_IncrRefCount(dict);
630 copied = 1;
631 }
632 status = Tcl_DictObjPut(interp, dict, lit[LIT_JULIANDAY],
633 Tcl_NewIntObj(fields.julianDay));
634 if (status == TCL_OK) {
635 Tcl_SetObjResult(interp, dict);
636 }
637 if (copied) {
638 Tcl_DecrRefCount(dict);
639 }
640 return status;
641 }
642
643 /*
644 *----------------------------------------------------------------------
645 *
646 * ClockGetjuliandayfromerayearweekdayObjCmd --
647 *
648 * Tcl command that converts a time from the ISO calendar to a Julian Day
649 * Number.
650 *
651 * Parameters:
652 * dict - Dictionary that contains 'era', 'iso8601Year', 'iso8601Week'
653 * and 'dayOfWeek' keys.
654 * changeover - Julian Day of changeover to the Gregorian calendar
655 *
656 * Results:
657 * Result is either TCL_OK, with the interpreter result being the
658 * dictionary augmented with a 'julianDay' key, or TCL_ERROR, with the
659 * result being an error message.
660 *
661 *----------------------------------------------------------------------
662 */
663
664 static int
665 ClockGetjuliandayfromerayearweekdayObjCmd(
666 ClientData clientData, /* Opaque pointer to literal pool, etc. */
667 Tcl_Interp *interp, /* Tcl interpreter */
668 int objc, /* Parameter count */
669 Tcl_Obj *const *objv) /* Parameter vector */
670 {
671 TclDateFields fields;
672 Tcl_Obj *dict;
673 ClockClientData *data = (ClockClientData *)clientData;
674 Tcl_Obj *const *lit = data->literals;
675 int changeover;
676 int copied = 0;
677 int status;
678 int era = 0;
679
680 /*
681 * Check params.
682 */
683
684 if (objc != 3) {
685 Tcl_WrongNumArgs(interp, 1, objv, "dict changeover");
686 return TCL_ERROR;
687 }
688 dict = objv[1];
689 if (FetchEraField(interp, dict, lit[LIT_ERA], &era) != TCL_OK
690 || FetchIntField(interp, dict, lit[LIT_ISO8601YEAR],
691 &fields.iso8601Year) != TCL_OK
692 || FetchIntField(interp, dict, lit[LIT_ISO8601WEEK],
693 &fields.iso8601Week) != TCL_OK
694 || FetchIntField(interp, dict, lit[LIT_DAYOFWEEK],
695 &fields.dayOfWeek) != TCL_OK
696 || TclGetIntFromObj(interp, objv[2], &changeover) != TCL_OK) {
697 return TCL_ERROR;
698 }
699 fields.era = era;
700
701 /*
702 * Get Julian day.
703 */
704
705 GetJulianDayFromEraYearWeekDay(&fields, changeover);
706
707 /*
708 * Store Julian day in the dictionary - copy on write.
709 */
710
711 if (Tcl_IsShared(dict)) {
712 dict = Tcl_DuplicateObj(dict);
713 Tcl_IncrRefCount(dict);
714 copied = 1;
715 }
716 status = Tcl_DictObjPut(interp, dict, lit[LIT_JULIANDAY],
717 Tcl_NewIntObj(fields.julianDay));
718 if (status == TCL_OK) {
719 Tcl_SetObjResult(interp, dict);
720 }
721 if (copied) {
722 Tcl_DecrRefCount(dict);
723 }
724 return status;
725 }
726
727 /*
728 *----------------------------------------------------------------------
729 *
730 * ConvertLocalToUTC --
731 *
732 * Converts a time (in a TclDateFields structure) from the local wall
733 * clock to UTC.
734 *
735 * Results:
736 * Returns a standard Tcl result.
737 *
738 * Side effects:
739 * Populates the 'seconds' field if successful; stores an error message
740 * in the interpreter result on failure.
741 *
742 *----------------------------------------------------------------------
743 */
744
745 static int
746 ConvertLocalToUTC(
747 Tcl_Interp *interp, /* Tcl interpreter */
748 TclDateFields *fields, /* Fields of the time */
749 Tcl_Obj *tzdata, /* Time zone data */
750 int changeover) /* Julian Day of the Gregorian transition */
751 {
752 int rowc; /* Number of rows in tzdata */
753 Tcl_Obj **rowv; /* Pointers to the rows */
754
755 /*
756 * Unpack the tz data.
757 */
758
759 if (TclListObjGetElements(interp, tzdata, &rowc, &rowv) != TCL_OK) {
760 return TCL_ERROR;
761 }
762
763 /*
764 * Special case: If the time zone is :localtime, the tzdata will be empty.
765 * Use 'mktime' to convert the time to local
766 */
767
768 if (rowc == 0) {
769 return ConvertLocalToUTCUsingC(interp, fields, changeover);
770 } else {
771 return ConvertLocalToUTCUsingTable(interp, fields, rowc, rowv);
772 }
773 }
774
775 /*
776 *----------------------------------------------------------------------
777 *
778 * ConvertLocalToUTCUsingTable --
779 *
780 * Converts a time (in a TclDateFields structure) from local time in a
781 * given time zone to UTC.
782 *
783 * Results:
784 * Returns a standard Tcl result.
785 *
786 * Side effects:
787 * Stores an error message in the interpreter if an error occurs; if
788 * successful, stores the 'seconds' field in 'fields.
789 *
790 *----------------------------------------------------------------------
791 */
792
793 static int
794 ConvertLocalToUTCUsingTable(
795 Tcl_Interp *interp, /* Tcl interpreter */
796 TclDateFields *fields, /* Time to convert, with 'seconds' filled in */
797 int rowc, /* Number of points at which time changes */
798 Tcl_Obj *const rowv[]) /* Points at which time changes */
799 {
800 Tcl_Obj *row;
801 int cellc;
802 Tcl_Obj **cellv;
803 int have[8];
804 int nHave = 0;
805 int i;
806 int found;
807
808 /*
809 * Perform an initial lookup assuming that local == UTC, and locate the
810 * last time conversion prior to that time. Get the offset from that row,
811 * and look up again. Continue until we find an offset that we found
812 * before. This definition, rather than "the same offset" ensures that we
813 * don't enter an endless loop, as would otherwise happen when trying to
814 * convert a non-existent time such as 02:30 during the US Spring Daylight
815 * Saving Time transition.
816 */
817
818 found = 0;
819 fields->tzOffset = 0;
820 fields->seconds = fields->localSeconds;
821 while (!found) {
822 row = LookupLastTransition(interp, fields->seconds, rowc, rowv);
823 if ((row == NULL)
824 || TclListObjGetElements(interp, row, &cellc,
825 &cellv) != TCL_OK
826 || TclGetIntFromObj(interp, cellv[1],
827 &fields->tzOffset) != TCL_OK) {
828 return TCL_ERROR;
829 }
830 found = 0;
831 for (i = 0; !found && i < nHave; ++i) {
832 if (have[i] == fields->tzOffset) {
833 found = 1;
834 break;
835 }
836 }
837 if (!found) {
838 if (nHave == 8) {
839 Tcl_Panic("loop in ConvertLocalToUTCUsingTable");
840 }
841 have[nHave++] = fields->tzOffset;
842 }
843 fields->seconds = fields->localSeconds - fields->tzOffset;
844 }
845 fields->tzOffset = have[i];
846 fields->seconds = fields->localSeconds - fields->tzOffset;
847 return TCL_OK;
848 }
849
850 /*
851 *----------------------------------------------------------------------
852 *
853 * ConvertLocalToUTCUsingC --
854 *
855 * Converts a time from local wall clock to UTC when the local time zone
856 * cannot be determined. Uses 'mktime' to do the job.
857 *
858 * Results:
859 * Returns a standard Tcl result.
860 *
861 * Side effects:
862 * Stores an error message in the interpreter if an error occurs; if
863 * successful, stores the 'seconds' field in 'fields.
864 *
865 *----------------------------------------------------------------------
866 */
867
868 static int
869 ConvertLocalToUTCUsingC(
870 Tcl_Interp *interp, /* Tcl interpreter */
871 TclDateFields *fields, /* Time to convert, with 'seconds' filled in */
872 int changeover) /* Julian Day of the Gregorian transition */
873 {
874 struct tm timeVal;
875 int localErrno;
876 int secondOfDay;
877 Tcl_WideInt jsec;
878
879 /*
880 * Convert the given time to a date.
881 */
882
883 jsec = fields->localSeconds + JULIAN_SEC_POSIX_EPOCH;
884 fields->julianDay = (int) (jsec / SECONDS_PER_DAY);
885 secondOfDay = (int)(jsec % SECONDS_PER_DAY);
886 if (secondOfDay < 0) {
887 secondOfDay += SECONDS_PER_DAY;
888 fields->julianDay--;
889 }
890 GetGregorianEraYearDay(fields, changeover);
891 GetMonthDay(fields);
892
893 /*
894 * Convert the date/time to a 'struct tm'.
895 */
896
897 timeVal.tm_year = fields->year - 1900;
898 timeVal.tm_mon = fields->month - 1;
899 timeVal.tm_mday = fields->dayOfMonth;
900 timeVal.tm_hour = (secondOfDay / 3600) % 24;
901 timeVal.tm_min = (secondOfDay / 60) % 60;
902 timeVal.tm_sec = secondOfDay % 60;
903 timeVal.tm_isdst = -1;
904 timeVal.tm_wday = -1;
905 timeVal.tm_yday = -1;
906
907 /*
908 * Get local time. It is rumored that mktime is not thread safe on some
909 * platforms, so seize a mutex before attempting this.
910 */
911
912 TzsetIfNecessary();
913 Tcl_MutexLock(&clockMutex);
914 errno = 0;
915 fields->seconds = (Tcl_WideInt)mktime(&timeVal);
916 localErrno = (fields->seconds == -1) ? errno : 0;
917 Tcl_MutexUnlock(&clockMutex);
918
919 /*
920 * If conversion fails, report an error.
921 */
922
923 if (localErrno != 0
924 || (fields->seconds == -1 && timeVal.tm_yday == -1)) {
925 Tcl_SetObjResult(interp, Tcl_NewStringObj(
926 "time value too large/small to represent", -1));
927 return TCL_ERROR;
928 }
929 return TCL_OK;
930 }
931
932 /*
933 *----------------------------------------------------------------------
934 *
935 * ConvertUTCToLocal --
936 *
937 * Converts a time (in a TclDateFields structure) from UTC to local time.
938 *
939 * Results:
940 * Returns a standard Tcl result.
941 *
942 * Side effects:
943 * Populates the 'tzName' and 'tzOffset' fields.
944 *
945 *----------------------------------------------------------------------
946 */
947
948 static int
949 ConvertUTCToLocal(
950 Tcl_Interp *interp, /* Tcl interpreter */
951 TclDateFields *fields, /* Fields of the time */
952 Tcl_Obj *tzdata, /* Time zone data */
953 int changeover) /* Julian Day of the Gregorian transition */
954 {
955 int rowc; /* Number of rows in tzdata */
956 Tcl_Obj **rowv; /* Pointers to the rows */
957
958 /*
959 * Unpack the tz data.
960 */
961
962 if (TclListObjGetElements(interp, tzdata, &rowc, &rowv) != TCL_OK) {
963 return TCL_ERROR;
964 }
965
966 /*
967 * Special case: If the time zone is :localtime, the tzdata will be empty.
968 * Use 'localtime' to convert the time to local
969 */
970
971 if (rowc == 0) {
972 return ConvertUTCToLocalUsingC(interp, fields, changeover);
973 } else {
974 return ConvertUTCToLocalUsingTable(interp, fields, rowc, rowv);
975 }
976 }
977
978 /*
979 *----------------------------------------------------------------------
980 *
981 * ConvertUTCToLocalUsingTable --
982 *
983 * Converts UTC to local time, given a table of transition points
984 *
985 * Results:
986 * Returns a standard Tcl result
987 *
988 * Side effects:
989 * On success, fills fields->tzName, fields->tzOffset and
990 * fields->localSeconds. On failure, places an error message in the
991 * interpreter result.
992 *
993 *----------------------------------------------------------------------
994 */
995
996 static int
997 ConvertUTCToLocalUsingTable(
998 Tcl_Interp *interp, /* Tcl interpreter */
999 TclDateFields *fields, /* Fields of the date */
1000 int rowc, /* Number of rows in the conversion table
1001 * (>= 1) */
1002 Tcl_Obj *const rowv[]) /* Rows of the conversion table */
1003 {
1004 Tcl_Obj *row; /* Row containing the current information */
1005 int cellc; /* Count of cells in the row (must be 4) */
1006 Tcl_Obj **cellv; /* Pointers to the cells */
1007
1008 /*
1009 * Look up the nearest transition time.
1010 */
1011
1012 row = LookupLastTransition(interp, fields->seconds, rowc, rowv);
1013 if (row == NULL ||
1014 TclListObjGetElements(interp, row, &cellc, &cellv) != TCL_OK ||
1015 TclGetIntFromObj(interp, cellv[1], &fields->tzOffset) != TCL_OK) {
1016 return TCL_ERROR;
1017 }
1018
1019 /*
1020 * Convert the time.
1021 */
1022
1023 fields->tzName = cellv[3];
1024 Tcl_IncrRefCount(fields->tzName);
1025 fields->localSeconds = fields->seconds + fields->tzOffset;
1026 return TCL_OK;
1027 }
1028
1029 /*
1030 *----------------------------------------------------------------------
1031 *
1032 * ConvertUTCToLocalUsingC --
1033 *
1034 * Converts UTC to localtime in cases where the local time zone is not
1035 * determinable, using the C 'localtime' function to do it.
1036 *
1037 * Results:
1038 * Returns a standard Tcl result.
1039 *
1040 * Side effects:
1041 * On success, fills fields->tzName, fields->tzOffset and
1042 * fields->localSeconds. On failure, places an error message in the
1043 * interpreter result.
1044 *
1045 *----------------------------------------------------------------------
1046 */
1047
1048 static int
1049 ConvertUTCToLocalUsingC(
1050 Tcl_Interp *interp, /* Tcl interpreter */
1051 TclDateFields *fields, /* Time to convert, with 'seconds' filled in */
1052 int changeover) /* Julian Day of the Gregorian transition */
1053 {
1054 time_t tock;
1055 struct tm *timeVal; /* Time after conversion */
1056 int diff; /* Time zone diff local-Greenwich */
1057 char buffer[16]; /* Buffer for time zone name */
1058
1059 /*
1060 * Use 'localtime' to determine local year, month, day, time of day.
1061 */
1062
1063 tock = (time_t) fields->seconds;
1064 if ((Tcl_WideInt)tock != fields->seconds) {
1065 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1066 "number too large to represent as a Posix time", -1));
1067 Tcl_SetErrorCode(interp, "CLOCK", "argTooLarge", (char *)NULL);
1068 return TCL_ERROR;
1069 }
1070 TzsetIfNecessary();
1071 timeVal = ThreadSafeLocalTime(&tock);
1072 if (timeVal == NULL) {
1073 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1074 "localtime failed (clock value may be too "
1075 "large/small to represent)", -1));
1076 Tcl_SetErrorCode(interp, "CLOCK", "localtimeFailed", (char *)NULL);
1077 return TCL_ERROR;
1078 }
1079
1080 /*
1081 * Fill in the date in 'fields' and use it to derive Julian Day.
1082 */
1083
1084 fields->era = CE;
1085 fields->year = timeVal->tm_year + 1900;
1086 fields->month = timeVal->tm_mon + 1;
1087 fields->dayOfMonth = timeVal->tm_mday;
1088 GetJulianDayFromEraYearMonthDay(fields, changeover);
1089
1090 /*
1091 * Convert that value to seconds.
1092 */
1093
1094 fields->localSeconds = (((fields->julianDay * (Tcl_WideInt)24
1095 + timeVal->tm_hour) * 60 + timeVal->tm_min) * 60
1096 + timeVal->tm_sec) - JULIAN_SEC_POSIX_EPOCH;
1097
1098 /*
1099 * Determine a time zone offset and name; just use +hhmm for the name.
1100 */
1101
1102 diff = (int) (fields->localSeconds - fields->seconds);
1103 fields->tzOffset = diff;
1104 if (diff < 0) {
1105 *buffer = '-';
1106 diff = -diff;
1107 } else {
1108 *buffer = '+';
1109 }
1110 snprintf(buffer+1, sizeof(buffer) - 1, "%02d", diff / 3600);
1111 diff %= 3600;
1112 snprintf(buffer+3, sizeof(buffer) - 3, "%02d", diff / 60);
1113 diff %= 60;
1114 if (diff > 0) {
1115 snprintf(buffer+5, sizeof(buffer) - 5, "%02d", diff);
1116 }
1117 fields->tzName = Tcl_NewStringObj(buffer, -1);
1118 Tcl_IncrRefCount(fields->tzName);
1119 return TCL_OK;
1120 }
1121
1122 /*
1123 *----------------------------------------------------------------------
1124 *
1125 * LookupLastTransition --
1126 *
1127 * Given a UTC time and a tzdata array, looks up the last transition on
1128 * or before the given time.
1129 *
1130 * Results:
1131 * Returns a pointer to the row, or NULL if an error occurs.
1132 *
1133 *----------------------------------------------------------------------
1134 */
1135
1136 static Tcl_Obj *
1137 LookupLastTransition(
1138 Tcl_Interp *interp, /* Interpreter for error messages */
1139 Tcl_WideInt tick, /* Time from the epoch */
1140 int rowc, /* Number of rows of tzdata */
1141 Tcl_Obj *const *rowv) /* Rows in tzdata */
1142 {
1143 int l;
1144 int u;
1145 Tcl_Obj *compObj;
1146 Tcl_WideInt compVal;
1147
1148 /*
1149 * Examine the first row to make sure we're in bounds.
1150 */
1151
1152 if (Tcl_ListObjIndex(interp, rowv[0], 0, &compObj) != TCL_OK
1153 || TclGetWideIntFromObj(interp, compObj, &compVal) != TCL_OK) {
1154 return NULL;
1155 }
1156
1157 /*
1158 * Bizarre case - first row doesn't begin at MIN_WIDE_INT. Return it
1159 * anyway.
1160 */
1161
1162 if (tick < compVal) {
1163 return rowv[0];
1164 }
1165
1166 /*
1167 * Binary-search to find the transition.
1168 */
1169
1170 l = 0;
1171 u = rowc-1;
1172 while (l < u) {
1173 int m = (l + u + 1) / 2;
1174
1175 if (Tcl_ListObjIndex(interp, rowv[m], 0, &compObj) != TCL_OK ||
1176 TclGetWideIntFromObj(interp, compObj, &compVal) != TCL_OK) {
1177 return NULL;
1178 }
1179 if (tick >= compVal) {
1180 l = m;
1181 } else {
1182 u = m-1;
1183 }
1184 }
1185 return rowv[l];
1186 }
1187
1188 /*
1189 *----------------------------------------------------------------------
1190 *
1191 * GetYearWeekDay --
1192 *
1193 * Given a date with Julian Calendar Day, compute the year, week, and day
1194 * in the ISO8601 calendar.
1195 *
1196 * Results:
1197 * None.
1198 *
1199 * Side effects:
1200 * Stores 'iso8601Year', 'iso8601Week' and 'dayOfWeek' in the date
1201 * fields.
1202 *
1203 *----------------------------------------------------------------------
1204 */
1205
1206 static void
1207 GetYearWeekDay(
1208 TclDateFields *fields, /* Date to convert, must have 'julianDay' */
1209 int changeover) /* Julian Day Number of the Gregorian
1210 * transition */
1211 {
1212 TclDateFields temp;
1213 int dayOfFiscalYear;
1214
1215 /*
1216 * Find the given date, minus three days, plus one year. That date's
1217 * iso8601 year is an upper bound on the ISO8601 year of the given date.
1218 */
1219
1220 temp.julianDay = fields->julianDay - 3;
1221 GetGregorianEraYearDay(&temp, changeover);
1222 if (temp.era == BCE) {
1223 temp.iso8601Year = temp.year - 1;
1224 } else {
1225 temp.iso8601Year = temp.year + 1;
1226 }
1227 temp.iso8601Week = 1;
1228 temp.dayOfWeek = 1;
1229 GetJulianDayFromEraYearWeekDay(&temp, changeover);
1230
1231 /*
1232 * temp.julianDay is now the start of an ISO8601 year, either the one
1233 * corresponding to the given date, or the one after. If we guessed high,
1234 * move one year earlier
1235 */
1236
1237 if (fields->julianDay < temp.julianDay) {
1238 if (temp.era == BCE) {
1239 temp.iso8601Year += 1;
1240 } else {
1241 temp.iso8601Year -= 1;
1242 }
1243 GetJulianDayFromEraYearWeekDay(&temp, changeover);
1244 }
1245
1246 fields->iso8601Year = temp.iso8601Year;
1247 dayOfFiscalYear = fields->julianDay - temp.julianDay;
1248 fields->iso8601Week = (dayOfFiscalYear / 7) + 1;
1249 fields->dayOfWeek = (dayOfFiscalYear + 1) % 7;
1250 if (fields->dayOfWeek < 1) {
1251 fields->dayOfWeek += 7;
1252 }
1253 }
1254
1255 /*
1256 *----------------------------------------------------------------------
1257 *
1258 * GetGregorianEraYearDay --
1259 *
1260 * Given a Julian Day Number, extracts the year and day of the year and
1261 * puts them into TclDateFields, along with the era (BCE or CE) and a
1262 * flag indicating whether the date is Gregorian or Julian.
1263 *
1264 * Results:
1265 * None.
1266 *
1267 * Side effects:
1268 * Stores 'era', 'gregorian', 'year', and 'dayOfYear'.
1269 *
1270 *----------------------------------------------------------------------
1271 */
1272
1273 static void
1274 GetGregorianEraYearDay(
1275 TclDateFields *fields, /* Date fields containing 'julianDay' */
1276 int changeover) /* Gregorian transition date */
1277 {
1278 int jday = fields->julianDay;
1279 int day;
1280 int year;
1281 int n;
1282
1283 if (jday >= changeover) {
1284 /*
1285 * Gregorian calendar.
1286 */
1287
1288 fields->gregorian = 1;
1289 year = 1;
1290
1291 /*
1292 * n = Number of 400-year cycles since 1 January, 1 CE in the
1293 * proleptic Gregorian calendar. day = remaining days.
1294 */
1295
1296 day = jday - JDAY_1_JAN_1_CE_GREGORIAN;
1297 n = day / FOUR_CENTURIES;
1298 day %= FOUR_CENTURIES;
1299 if (day < 0) {
1300 day += FOUR_CENTURIES;
1301 n--;
1302 }
1303 year += 400 * n;
1304
1305 /*
1306 * n = number of centuries since the start of (year);
1307 * day = remaining days
1308 */
1309
1310 n = day / ONE_CENTURY_GREGORIAN;
1311 day %= ONE_CENTURY_GREGORIAN;
1312 if (n > 3) {
1313 /*
1314 * 31 December in the last year of a 400-year cycle.
1315 */
1316
1317 n = 3;
1318 day += ONE_CENTURY_GREGORIAN;
1319 }
1320 year += 100 * n;
1321 } else {
1322 /*
1323 * Julian calendar.
1324 */
1325
1326 fields->gregorian = 0;
1327 year = 1;
1328 day = jday - JDAY_1_JAN_1_CE_JULIAN;
1329 }
1330
1331 /*
1332 * n = number of 4-year cycles; days = remaining days.
1333 */
1334
1335 n = day / FOUR_YEARS;
1336 day %= FOUR_YEARS;
1337 if (day < 0) {
1338 day += FOUR_YEARS;
1339 n--;
1340 }
1341 year += 4 * n;
1342
1343 /*
1344 * n = number of years; days = remaining days.
1345 */
1346
1347 n = day / ONE_YEAR;
1348 day %= ONE_YEAR;
1349 if (n > 3) {
1350 /*
1351 * 31 December of a leap year.
1352 */
1353
1354 n = 3;
1355 day += 365;
1356 }
1357 year += n;
1358
1359 /*
1360 * store era/year/day back into fields.
1361 */
1362
1363 if (year <= 0) {
1364 fields->era = BCE;
1365 fields->year = 1 - year;
1366 } else {
1367 fields->era = CE;
1368 fields->year = year;
1369 }
1370 fields->dayOfYear = day + 1;
1371 }
1372
1373 /*
1374 *----------------------------------------------------------------------
1375 *
1376 * GetMonthDay --
1377 *
1378 * Given a date as year and day-of-year, find month and day.
1379 *
1380 * Results:
1381 * None.
1382 *
1383 * Side effects:
1384 * Stores 'month' and 'dayOfMonth' in the 'fields' structure.
1385 *
1386 *----------------------------------------------------------------------
1387 */
1388
1389 static void
1390 GetMonthDay(
1391 TclDateFields *fields) /* Date to convert */
1392 {
1393 int day = fields->dayOfYear;
1394 int month;
1395 const int *h = hath[IsGregorianLeapYear(fields)];
1396
1397 for (month = 0; month < 12 && day > h[month]; ++month) {
1398 day -= h[month];
1399 }
1400 fields->month = month+1;
1401 fields->dayOfMonth = day;
1402 }
1403
1404 /*
1405 *----------------------------------------------------------------------
1406 *
1407 * GetJulianDayFromEraYearWeekDay --
1408 *
1409 * Given a TclDateFields structure containing era, ISO8601 year, ISO8601
1410 * week, and day of week, computes the Julian Day Number.
1411 *
1412 * Results:
1413 * None.
1414 *
1415 * Side effects:
1416 * Stores 'julianDay' in the fields.
1417 *
1418 *----------------------------------------------------------------------
1419 */
1420
1421 static void
1422 GetJulianDayFromEraYearWeekDay(
1423 TclDateFields *fields, /* Date to convert */
1424 int changeover) /* Julian Day Number of the Gregorian
1425 * transition */
1426 {
1427 int firstMonday; /* Julian day number of week 1, day 1 in the
1428 * given year */
1429 TclDateFields firstWeek;
1430
1431 /*
1432 * Find January 4 in the ISO8601 year, which will always be in week 1.
1433 */
1434
1435 firstWeek.era = fields->era;
1436 firstWeek.year = fields->iso8601Year;
1437 firstWeek.month = 1;
1438 firstWeek.dayOfMonth = 4;
1439 GetJulianDayFromEraYearMonthDay(&firstWeek, changeover);
1440
1441 /*
1442 * Find Monday of week 1.
1443 */
1444
1445 firstMonday = WeekdayOnOrBefore(1, firstWeek.julianDay);
1446
1447 /*
1448 * Advance to the given week and day.
1449 */
1450
1451 fields->julianDay = firstMonday + 7 * (fields->iso8601Week - 1)
1452 + fields->dayOfWeek - 1;
1453 }
1454
1455 /*
1456 *----------------------------------------------------------------------
1457 *
1458 * GetJulianDayFromEraYearMonthDay --
1459 *
1460 * Given era, year, month, and dayOfMonth (in TclDateFields), and the
1461 * Gregorian transition date, computes the Julian Day Number.
1462 *
1463 * Results:
1464 * None.
1465 *
1466 * Side effects:
1467 * Stores day number in 'julianDay'
1468 *
1469 *----------------------------------------------------------------------
1470 */
1471
1472 static void
1473 GetJulianDayFromEraYearMonthDay(
1474 TclDateFields *fields, /* Date to convert */
1475 int changeover) /* Gregorian transition date as a Julian Day */
1476 {
1477 int year, ym1, month, mm1, q, r, ym1o4, ym1o100, ym1o400;
1478
1479 if (fields->era == BCE) {
1480 year = 1 - fields->year;
1481 } else {
1482 year = fields->year;
1483 }
1484
1485 /*
1486 * Reduce month modulo 12.
1487 */
1488
1489 month = fields->month;
1490 mm1 = month - 1;
1491 q = mm1 / 12;
1492 r = (mm1 % 12);
1493 if (r < 0) {
1494 r += 12;
1495 q -= 1;
1496 }
1497 year += q;
1498 month = r + 1;
1499 ym1 = year - 1;
1500
1501 /*
1502 * Adjust the year after reducing the month.
1503 */
1504
1505 fields->gregorian = 1;
1506 if (year < 1) {
1507 fields->era = BCE;
1508 fields->year = 1-year;
1509 } else {
1510 fields->era = CE;
1511 fields->year = year;
1512 }
1513
1514 /*
1515 * Try an initial conversion in the Gregorian calendar.
1516 */
1517
1518 #if 0 /* BUG https://core.tcl-lang.org/tcl/tktview?name=da340d4f32 */
1519 ym1o4 = ym1 / 4;
1520 #else
1521 /*
1522 * Have to make sure quotient is truncated towards 0 when negative.
1523 * See above bug for details. The casts are necessary.
1524 */
1525 if (ym1 >= 0) {
1526 ym1o4 = ym1 / 4;
1527 } else {
1528 ym1o4 = - (int) (((unsigned int) -ym1) / 4);
1529 }
1530 #endif
1531 if (ym1 % 4 < 0) {
1532 ym1o4--;
1533 }
1534 ym1o100 = ym1 / 100;
1535 if (ym1 % 100 < 0) {
1536 ym1o100--;
1537 }
1538 ym1o400 = ym1 / 400;
1539 if (ym1 % 400 < 0) {
1540 ym1o400--;
1541 }
1542 fields->julianDay = JDAY_1_JAN_1_CE_GREGORIAN - 1
1543 + fields->dayOfMonth
1544 + daysInPriorMonths[IsGregorianLeapYear(fields)][month - 1]
1545 + (ONE_YEAR * ym1)
1546 + ym1o4
1547 - ym1o100
1548 + ym1o400;
1549
1550 /*
1551 * If the resulting date is before the Gregorian changeover, convert in
1552 * the Julian calendar instead.
1553 */
1554
1555 if (fields->julianDay < changeover) {
1556 fields->gregorian = 0;
1557 fields->julianDay = JDAY_1_JAN_1_CE_JULIAN - 1
1558 + fields->dayOfMonth
1559 + daysInPriorMonths[year%4 == 0][month - 1]
1560 + (365 * ym1)
1561 + ym1o4;
1562 }
1563 }
1564
1565 /*
1566 *----------------------------------------------------------------------
1567 *
1568 * IsGregorianLeapYear --
1569 *
1570 * Tests whether a given year is a leap year, in either Julian or
1571 * Gregorian calendar.
1572 *
1573 * Results:
1574 * Returns 1 for a leap year, 0 otherwise.
1575 *
1576 *----------------------------------------------------------------------
1577 */
1578
1579 static int
1580 IsGregorianLeapYear(
1581 TclDateFields *fields) /* Date to test */
1582 {
1583 int year = fields->year;
1584
1585 if (fields->era == BCE) {
1586 year = 1 - year;
1587 }
1588 if (year%4 != 0) {
1589 return 0;
1590 } else if (!(fields->gregorian)) {
1591 return 1;
1592 } else if (year%400 == 0) {
1593 return 1;
1594 } else if (year%100 == 0) {
1595 return 0;
1596 } else {
1597 return 1;
1598 }
1599 }
1600
1601 /*
1602 *----------------------------------------------------------------------
1603 *
1604 * WeekdayOnOrBefore --
1605 *
1606 * Finds the Julian Day Number of a given day of the week that falls on
1607 * or before a given date, expressed as Julian Day Number.
1608 *
1609 * Results:
1610 * Returns the Julian Day Number
1611 *
1612 *----------------------------------------------------------------------
1613 */
1614
1615 static int
1616 WeekdayOnOrBefore(
1617 int dayOfWeek, /* Day of week; Sunday == 0 or 7 */
1618 int julianDay) /* Reference date */
1619 {
1620 int k = (dayOfWeek + 6) % 7;
1621 if (k < 0) {
1622 k += 7;
1623 }
1624 return julianDay - ((julianDay - k) % 7);
1625 }
1626
1627 /*
1628 *----------------------------------------------------------------------
1629 *
1630 * ClockGetenvObjCmd --
1631 *
1632 * Tcl command that reads an environment variable from the system
1633 *
1634 * Usage:
1635 * ::tcl::clock::getEnv NAME
1636 *
1637 * Parameters:
1638 * NAME - Name of the environment variable desired
1639 *
1640 * Results:
1641 * Returns a standard Tcl result. Returns an error if the variable does
1642 * not exist, with a message left in the interpreter. Returns TCL_OK and
1643 * the value of the variable if the variable does exist,
1644 *
1645 *----------------------------------------------------------------------
1646 */
1647
1648 int
1649 ClockGetenvObjCmd(
1650 ClientData clientData,
1651 Tcl_Interp *interp,
1652 int objc,
1653 Tcl_Obj *const objv[])
1654 {
1655 #ifdef _WIN32
1656 const WCHAR *varName;
1657 const WCHAR *varValue;
1658 Tcl_DString ds;
1659 #else
1660 const char *varName;
1661 const char *varValue;
1662 #endif
1663 (void)clientData;
1664
1665 if (objc != 2) {
1666 Tcl_WrongNumArgs(interp, 1, objv, "name");
1667 return TCL_ERROR;
1668 }
1669 #ifdef _WIN32
1670 varName = (const WCHAR *)Tcl_WinUtfToTChar(TclGetString(objv[1]), -1, &ds);
1671 varValue = _wgetenv(varName);
1672 Tcl_DStringFree(&ds);
1673 if (varValue == NULL) {
1674 varValue = L"";
1675 }
1676 Tcl_WinTCharToUtf((TCHAR *)varValue, -1, &ds);
1677 Tcl_DStringResult(interp, &ds);
1678 #else
1679 varName = TclGetString(objv[1]);
1680 varValue = getenv(varName);
1681 if (varValue == NULL) {
1682 varValue = "";
1683 }
1684 Tcl_SetObjResult(interp, Tcl_NewStringObj(varValue, -1));
1685 #endif
1686 return TCL_OK;
1687 }
1688
1689 /*
1690 *----------------------------------------------------------------------
1691 *
1692 * ThreadSafeLocalTime --
1693 *
1694 * Wrapper around the 'localtime' library function to make it thread
1695 * safe.
1696 *
1697 * Results:
1698 * Returns a pointer to a 'struct tm' in thread-specific data.
1699 *
1700 * Side effects:
1701 * Invokes localtime or localtime_r as appropriate.
1702 *
1703 *----------------------------------------------------------------------
1704 */
1705
1706 static struct tm *
1707 ThreadSafeLocalTime(
1708 const time_t *timePtr) /* Pointer to the number of seconds since the
1709 * local system's epoch */
1710 {
1711 /*
1712 * Get a thread-local buffer to hold the returned time.
1713 */
1714
1715 struct tm *tmPtr = (struct tm *)Tcl_GetThreadData(&tmKey, sizeof(struct tm));
1716 #ifdef HAVE_LOCALTIME_R
1717 tmPtr = localtime_r(timePtr, tmPtr);
1718 #else
1719 struct tm *sysTmPtr;
1720
1721 Tcl_MutexLock(&clockMutex);
1722 sysTmPtr = localtime(timePtr);
1723 if (sysTmPtr == NULL) {
1724 Tcl_MutexUnlock(&clockMutex);
1725 return NULL;
1726 }
1727 memcpy(tmPtr, sysTmPtr, sizeof(struct tm));
1728 Tcl_MutexUnlock(&clockMutex);
1729 #endif
1730 return tmPtr;
1731 }
1732
1733 /*----------------------------------------------------------------------
1734 *
1735 * ClockClicksObjCmd --
1736 *
1737 * Returns a high-resolution counter.
1738 *
1739 * Results:
1740 * Returns a standard Tcl result.
1741 *
1742 * Side effects:
1743 * None.
1744 *
1745 * This function implements the 'clock clicks' Tcl command. Refer to the user
1746 * documentation for details on what it does.
1747 *
1748 *----------------------------------------------------------------------
1749 */
1750
1751 int
1752 ClockClicksObjCmd(
1753 ClientData clientData, /* Client data is unused */
1754 Tcl_Interp *interp, /* Tcl interpreter */
1755 int objc, /* Parameter count */
1756 Tcl_Obj *const *objv) /* Parameter values */
1757 {
1758 static const char *const clicksSwitches[] = {
1759 "-milliseconds", "-microseconds", NULL
1760 };
1761 enum ClicksSwitch {
1762 CLICKS_MILLIS, CLICKS_MICROS, CLICKS_NATIVE
1763 };
1764 int index = CLICKS_NATIVE;
1765 Tcl_Time now;
1766 Tcl_WideInt clicks = 0;
1767 (void)clientData;
1768
1769 switch (objc) {
1770 case 1:
1771 break;
1772 case 2:
1773 if (Tcl_GetIndexFromObj(interp, objv[1], clicksSwitches, "option", 0,
1774 &index) != TCL_OK) {
1775 return TCL_ERROR;
1776 }
1777 break;
1778 default:
1779 Tcl_WrongNumArgs(interp, 1, objv, "?-switch?");
1780 return TCL_ERROR;
1781 }
1782
1783 switch (index) {
1784 case CLICKS_MILLIS:
1785 Tcl_GetTime(&now);
1786 clicks = (Tcl_WideInt)now.sec * 1000 + now.usec / 1000;
1787 break;
1788 case CLICKS_NATIVE:
1789 #ifdef TCL_WIDE_CLICKS
1790 clicks = TclpGetWideClicks();
1791 #else
1792 clicks = TclpGetClicks();
1793 #endif
1794 break;
1795 case CLICKS_MICROS:
1796 clicks = TclpGetMicroseconds();
1797 break;
1798 }
1799
1800 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(clicks));
1801 return TCL_OK;
1802 }
1803
1804 /*----------------------------------------------------------------------
1805 *
1806 * ClockMillisecondsObjCmd -
1807 *
1808 * Returns a count of milliseconds since the epoch.
1809 *
1810 * Results:
1811 * Returns a standard Tcl result.
1812 *
1813 * Side effects:
1814 * None.
1815 *
1816 * This function implements the 'clock milliseconds' Tcl command. Refer to the
1817 * user documentation for details on what it does.
1818 *
1819 *----------------------------------------------------------------------
1820 */
1821
1822 int
1823 ClockMillisecondsObjCmd(
1824 ClientData clientData, /* Client data is unused */
1825 Tcl_Interp *interp, /* Tcl interpreter */
1826 int objc, /* Parameter count */
1827 Tcl_Obj *const *objv) /* Parameter values */
1828 {
1829 Tcl_Time now;
1830 (void)clientData;
1831
1832 if (objc != 1) {
1833 Tcl_WrongNumArgs(interp, 1, objv, NULL);
1834 return TCL_ERROR;
1835 }
1836 Tcl_GetTime(&now);
1837 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(
1838 (Tcl_WideInt)now.sec * 1000 + now.usec / 1000));
1839 return TCL_OK;
1840 }
1841
1842 /*----------------------------------------------------------------------
1843 *
1844 * ClockMicrosecondsObjCmd -
1845 *
1846 * Returns a count of microseconds since the epoch.
1847 *
1848 * Results:
1849 * Returns a standard Tcl result.
1850 *
1851 * Side effects:
1852 * None.
1853 *
1854 * This function implements the 'clock microseconds' Tcl command. Refer to the
1855 * user documentation for details on what it does.
1856 *
1857 *----------------------------------------------------------------------
1858 */
1859
1860 int
1861 ClockMicrosecondsObjCmd(
1862 ClientData clientData, /* Client data is unused */
1863 Tcl_Interp *interp, /* Tcl interpreter */
1864 int objc, /* Parameter count */
1865 Tcl_Obj *const *objv) /* Parameter values */
1866 {
1867 (void)clientData;
1868 if (objc != 1) {
1869 Tcl_WrongNumArgs(interp, 1, objv, NULL);
1870 return TCL_ERROR;
1871 }
1872 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(TclpGetMicroseconds()));
1873 return TCL_OK;
1874 }
1875
1876 /*
1877 *-----------------------------------------------------------------------------
1878 *
1879 * ClockParseformatargsObjCmd --
1880 *
1881 * Parses the arguments for [clock format].
1882 *
1883 * Results:
1884 * Returns a standard Tcl result, whose value is a four-element list
1885 * comprising the time format, the locale, and the timezone.
1886 *
1887 * This function exists because the loop that parses the [clock format]
1888 * options is a known performance "hot spot", and is implemented in an effort
1889 * to speed that particular code up.
1890 *
1891 *-----------------------------------------------------------------------------
1892 */
1893
1894 static int
1895 ClockParseformatargsObjCmd(
1896 ClientData clientData, /* Client data containing literal pool */
1897 Tcl_Interp *interp, /* Tcl interpreter */
1898 int objc, /* Parameter count */
1899 Tcl_Obj *const objv[]) /* Parameter vector */
1900 {
1901 ClockClientData *dataPtr = (ClockClientData *)clientData;
1902 Tcl_Obj **litPtr = dataPtr->literals;
1903 Tcl_Obj *results[3]; /* Format, locale and timezone */
1904 #define formatObj results[0]
1905 #define localeObj results[1]
1906 #define timezoneObj results[2]
1907 int gmtFlag = 0;
1908 static const char *const options[] = { /* Command line options expected */
1909 "-format", "-gmt", "-locale",
1910 "-timezone", NULL };
1911 enum optionInd {
1912 CLOCK_FORMAT_FORMAT, CLOCK_FORMAT_GMT, CLOCK_FORMAT_LOCALE,
1913 CLOCK_FORMAT_TIMEZONE
1914 };
1915 int optionIndex; /* Index of an option. */
1916 int saw = 0; /* Flag == 1 if option was seen already. */
1917 Tcl_WideInt clockVal; /* Clock value - just used to parse. */
1918 int i;
1919
1920 /*
1921 * Args consist of a time followed by keyword-value pairs.
1922 */
1923
1924 if (objc < 2 || (objc % 2) != 0) {
1925 Tcl_WrongNumArgs(interp, 0, objv,
1926 "clock format clockval ?-format string? "
1927 "?-gmt boolean? ?-locale LOCALE? ?-timezone ZONE?");
1928 Tcl_SetErrorCode(interp, "CLOCK", "wrongNumArgs", (char *)NULL);
1929 return TCL_ERROR;
1930 }
1931
1932 /*
1933 * Extract values for the keywords.
1934 */
1935
1936 formatObj = litPtr[LIT__DEFAULT_FORMAT];
1937 localeObj = litPtr[LIT_C];
1938 timezoneObj = litPtr[LIT__NIL];
1939 for (i = 2; i < objc; i+=2) {
1940 if (Tcl_GetIndexFromObj(interp, objv[i], options, "option", 0,
1941 &optionIndex) != TCL_OK) {
1942 Tcl_SetErrorCode(interp, "CLOCK", "badOption",
1943 TclGetString(objv[i]), (char *)NULL);
1944 return TCL_ERROR;
1945 }
1946 switch (optionIndex) {
1947 case CLOCK_FORMAT_FORMAT:
1948 formatObj = objv[i+1];
1949 break;
1950 case CLOCK_FORMAT_GMT:
1951 if (Tcl_GetBooleanFromObj(interp, objv[i+1], &gmtFlag) != TCL_OK){
1952 return TCL_ERROR;
1953 }
1954 break;
1955 case CLOCK_FORMAT_LOCALE:
1956 localeObj = objv[i+1];
1957 break;
1958 case CLOCK_FORMAT_TIMEZONE:
1959 timezoneObj = objv[i+1];
1960 break;
1961 }
1962 saw |= 1 << optionIndex;
1963 }
1964
1965 /*
1966 * Check options.
1967 */
1968
1969 if (TclGetWideIntFromObj(interp, objv[1], &clockVal) != TCL_OK) {
1970 return TCL_ERROR;
1971 }
1972 if ((saw & (1 << CLOCK_FORMAT_GMT))
1973 && (saw & (1 << CLOCK_FORMAT_TIMEZONE))) {
1974 Tcl_SetObjResult(interp, litPtr[LIT_CANNOT_USE_GMT_AND_TIMEZONE]);
1975 Tcl_SetErrorCode(interp, "CLOCK", "gmtWithTimezone", (char *)NULL);
1976 return TCL_ERROR;
1977 }
1978 if (gmtFlag) {
1979 timezoneObj = litPtr[LIT_GMT];
1980 }
1981
1982 /*
1983 * Return options as a list.
1984 */
1985
1986 Tcl_SetObjResult(interp, Tcl_NewListObj(3, results));
1987 return TCL_OK;
1988
1989 #undef timezoneObj
1990 #undef localeObj
1991 #undef formatObj
1992 }
1993
1994 /*----------------------------------------------------------------------
1995 *
1996 * ClockSecondsObjCmd -
1997 *
1998 * Returns a count of microseconds since the epoch.
1999 *
2000 * Results:
2001 * Returns a standard Tcl result.
2002 *
2003 * Side effects:
2004 * None.
2005 *
2006 * This function implements the 'clock seconds' Tcl command. Refer to the user
2007 * documentation for details on what it does.
2008 *
2009 *----------------------------------------------------------------------
2010 */
2011
2012 int
2013 ClockSecondsObjCmd(
2014 ClientData clientData, /* Client data is unused */
2015 Tcl_Interp *interp, /* Tcl interpreter */
2016 int objc, /* Parameter count */
2017 Tcl_Obj *const *objv) /* Parameter values */
2018 {
2019 Tcl_Time now;
2020 (void)clientData;
2021
2022 if (objc != 1) {
2023 Tcl_WrongNumArgs(interp, 1, objv, NULL);
2024 return TCL_ERROR;
2025 }
2026 Tcl_GetTime(&now);
2027 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(now.sec));
2028 return TCL_OK;
2029 }
2030
2031 /*
2032 *----------------------------------------------------------------------
2033 *
2034 * TzsetIfNecessary --
2035 *
2036 * Calls the tzset() library function if the contents of the TZ
2037 * environment variable has changed.
2038 *
2039 * Results:
2040 * None.
2041 *
2042 * Side effects:
2043 * Calls tzset.
2044 *
2045 *----------------------------------------------------------------------
2046 */
2047
2048 #ifdef _WIN32
2049 #define getenv(x) _wgetenv(L##x)
2050 #else
2051 #define WCHAR char
2052 #define wcslen strlen
2053 #define wcscmp strcmp
2054 #define wcscpy strcpy
2055 #endif
2056
2057 static void
2058 TzsetIfNecessary(void)
2059 {
2060 static WCHAR* tzWas = (WCHAR *)INT2PTR(-1); /* Previous value of TZ, protected by
2061 * clockMutex. */
2062 static long tzLastRefresh = 0; /* Used for latency before next refresh */
2063 static size_t tzEnvEpoch = 0; /* Last env epoch, for faster signaling,
2064 that TZ changed via TCL */
2065 const WCHAR *tzIsNow; /* Current value of TZ */
2066
2067 /*
2068 * Prevent performance regression on some platforms by resolving of system time zone:
2069 * small latency for check whether environment was changed (once per second)
2070 * no latency if environment was changed with tcl-env (compare both epoch values)
2071 */
2072 Tcl_Time now;
2073 Tcl_GetTime(&now);
2074 if (now.sec == tzLastRefresh && tzEnvEpoch == TclEnvEpoch) {
2075 return;
2076 }
2077
2078 tzEnvEpoch = TclEnvEpoch;
2079 tzLastRefresh = now.sec;
2080
2081 Tcl_MutexLock(&clockMutex);
2082 tzIsNow = getenv("TZ");
2083 if (tzIsNow != NULL && (tzWas == NULL || tzWas == (WCHAR *)INT2PTR(-1)
2084 || wcscmp(tzIsNow, tzWas) != 0)) {
2085 tzset();
2086 if (tzWas != NULL && tzWas != (WCHAR *)INT2PTR(-1)) {
2087 ckfree(tzWas);
2088 }
2089 tzWas = (WCHAR *)ckalloc(sizeof(WCHAR) * (wcslen(tzIsNow) + 1));
2090 wcscpy(tzWas, tzIsNow);
2091 } else if (tzIsNow == NULL && tzWas != NULL) {
2092 tzset();
2093 if (tzWas != (WCHAR *)INT2PTR(-1)) ckfree(tzWas);
2094 tzWas = NULL;
2095 }
2096 Tcl_MutexUnlock(&clockMutex);
2097 }
2098
2099 /*
2100 *----------------------------------------------------------------------
2101 *
2102 * ClockDeleteCmdProc --
2103 *
2104 * Remove a reference to the clock client data, and clean up memory
2105 * when it's all gone.
2106 *
2107 * Results:
2108 * None.
2109 *
2110 *----------------------------------------------------------------------
2111 */
2112
2113 static void
2114 ClockDeleteCmdProc(
2115 ClientData clientData) /* Opaque pointer to the client data */
2116 {
2117 ClockClientData *data = (ClockClientData *)clientData;
2118 int i;
2119
2120 if (data->refCount-- <= 1) {
2121 for (i = 0; i < LIT__END; ++i) {
2122 Tcl_DecrRefCount(data->literals[i]);
2123 }
2124 ckfree(data->literals);
2125 ckfree(data);
2126 }
2127 }
2128
2129 /*
2130 * Local Variables:
2131 * mode: c
2132 * c-basic-offset: 4
2133 * fill-column: 78
2134 * End:
2135 */