diff options
Diffstat (limited to 'generic/tclClock.c')
-rw-r--r-- | generic/tclClock.c | 622 |
1 files changed, 346 insertions, 276 deletions
diff --git a/generic/tclClock.c b/generic/tclClock.c index 6189a5a..6a334ad 100644 --- a/generic/tclClock.c +++ b/generic/tclClock.c @@ -12,7 +12,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclClock.c,v 1.50 2005/12/13 22:43:17 kennykb Exp $ + * RCS: @(#) $Id: tclClock.c,v 1.51 2006/02/01 23:34:38 dkf Exp $ */ #include "tclInt.h" @@ -32,7 +32,7 @@ #define JULIAN_DAY_POSIX_EPOCH 2440588 #define SECONDS_PER_DAY 86400 #define JULIAN_SEC_POSIX_EPOCH (((Tcl_WideInt) JULIAN_DAY_POSIX_EPOCH) \ - * SECONDS_PER_DAY) + * SECONDS_PER_DAY) #define FOUR_CENTURIES 146097 /* days */ #define JDAY_1_JAN_1_CE_JULIAN 1721424 #define JDAY_1_JAN_1_CE_GREGORIAN 1721426 @@ -58,24 +58,24 @@ static const int daysInPriorMonths[2][13] = { */ typedef enum ClockLiteral { - LIT_BCE, LIT_CE, - LIT_DAYOFMONTH, LIT_DAYOFWEEK, LIT_DAYOFYEAR, - LIT_ERA, LIT_GREGORIAN, + LIT_BCE, LIT_CE, + LIT_DAYOFMONTH, LIT_DAYOFWEEK, LIT_DAYOFYEAR, + LIT_ERA, LIT_GREGORIAN, LIT_ISO8601WEEK, LIT_ISO8601YEAR, LIT_JULIANDAY, LIT_LOCALSECONDS, LIT_MONTH, - LIT_SECONDS, LIT_TZNAME, LIT_TZOFFSET, + LIT_SECONDS, LIT_TZNAME, LIT_TZOFFSET, LIT_YEAR, LIT__END } ClockLiteral; static const char *const literals[] = { - "BCE", "CE", - "dayOfMonth", "dayOfWeek", "dayOfYear", - "era", "gregorian", + "BCE", "CE", + "dayOfMonth", "dayOfWeek", "dayOfYear", + "era", "gregorian", "iso8601Week", "iso8601Year", "julianDay", "localSeconds", "month", - "seconds", "tzName", "tzOffset", + "seconds", "tzName", "tzOffset", "year" }; @@ -93,8 +93,8 @@ typedef struct ClockClientData { */ typedef struct TclDateFields { - Tcl_WideInt seconds; /* Time expressed in seconds from the - * Posix epoch */ + Tcl_WideInt seconds; /* Time expressed in seconds from the Posix + * epoch */ Tcl_WideInt localSeconds; /* Local time expressed in nominal seconds * from the Posix epoch */ int tzOffset; /* Time zone offset in seconds east of @@ -110,7 +110,6 @@ typedef struct TclDateFields { int iso8601Year; /* ISO8601 week-based year */ int iso8601Week; /* ISO8601 week number */ int dayOfWeek; /* Day of the week */ - } TclDateFields; static CONST char* eras[] = { "CE", "BCE", NULL }; @@ -132,17 +131,17 @@ TCL_DECLARE_MUTEX(clockMutex) * Function prototypes for local procedures in this file: */ -static int ConvertUTCToLocal(Tcl_Interp*, +static int ConvertUTCToLocal(Tcl_Interp*, TclDateFields*, Tcl_Obj*, int); -static int ConvertUTCToLocalUsingTable(Tcl_Interp*, +static int ConvertUTCToLocalUsingTable(Tcl_Interp*, TclDateFields*, int, Tcl_Obj *CONST[]); -static int ConvertUTCToLocalUsingC(Tcl_Interp*, +static int ConvertUTCToLocalUsingC(Tcl_Interp*, TclDateFields*, int); -static int ConvertLocalToUTC(Tcl_Interp*, +static int ConvertLocalToUTC(Tcl_Interp*, TclDateFields*, Tcl_Obj*, int); -static int ConvertLocalToUTCUsingTable(Tcl_Interp*, +static int ConvertLocalToUTCUsingTable(Tcl_Interp*, TclDateFields*, int, Tcl_Obj *CONST[]); -static int ConvertLocalToUTCUsingC(Tcl_Interp*, +static int ConvertLocalToUTCUsingC(Tcl_Interp*, TclDateFields*, int); static Tcl_Obj* LookupLastTransition(Tcl_Interp*, Tcl_WideInt, int, Tcl_Obj *CONST *); @@ -190,10 +189,9 @@ static void ClockDeleteCmdProc(ClientData); * * TclClockInit -- * - * Registers the 'clock' subcommands with the Tcl interpreter - * and initializes its client data (which consists mostly of - * constant Tcl_Obj's that it is too much trouble to keep - * recreating). + * Registers the 'clock' subcommands with the Tcl interpreter and + * initializes its client data (which consists mostly of constant + * Tcl_Obj's that it is too much trouble to keep recreating). * * Results: * None. @@ -210,22 +208,27 @@ TclClockInit( ) { int i; - /* Create the client data */ + /* + * Create the client data. + */ ClockClientData *data = (ClockClientData*) ckalloc(sizeof(ClockClientData)); data->refCount = 0; - + /* - * Create the literal pool + * Create the literal pool. */ + data->literals = (Tcl_Obj**) ckalloc(LIT__END * sizeof(Tcl_Obj*)); for (i = 0; i < LIT__END; ++i) { data->literals[i] = Tcl_NewStringObj(literals[i], -1); Tcl_IncrRefCount(data->literals[i]); } - /* Install the commands */ + /* + * Install the commands. + */ Tcl_CreateObjCommand(interp, "::tcl::clock::clicks", ClockClicksObjCmd, (ClientData) NULL, NULL); @@ -247,17 +250,16 @@ TclClockInit( ClockGetdatefieldsObjCmd,(ClientData) data, ClockDeleteCmdProc); ++data->refCount; - Tcl_CreateObjCommand(interp, + Tcl_CreateObjCommand(interp, "::tcl::clock::GetJulianDayFromEraYearMonthDay", ClockGetjuliandayfromerayearmonthdayObjCmd,(ClientData) data, ClockDeleteCmdProc); ++data->refCount; - Tcl_CreateObjCommand(interp, + Tcl_CreateObjCommand(interp, "::tcl::clock::GetJulianDayFromEraYearWeekDay", ClockGetjuliandayfromerayearweekdayObjCmd,(ClientData) data, ClockDeleteCmdProc); ++data->refCount; - } /* @@ -265,8 +267,8 @@ TclClockInit( * * ClockConvertlocaltoutcObjCmd -- * - * Tcl command that converts a UTC time to a local time by - * whatever means is available. + * Tcl command that converts a UTC time to a local time by whatever means + * is available. * * Usage: * ::tcl::clock::ConvertUTCToLocal dictionary tzdata changeover @@ -281,7 +283,7 @@ TclClockInit( * * Side effects: * On success, sets the interpreter result to the given dictionary - * augmented with a 'seconds' field giving the UTC time. On failure, + * augmented with a 'seconds' field giving the UTC time. On failure, * leaves an error message in the interpreter result. * *---------------------------------------------------------------------- @@ -303,25 +305,27 @@ ClockConvertlocaltoutcObjCmd( int created = 0; int status; - /* Check params and convert time */ + /* + * Check params and convert time. + */ if (objc != 4) { Tcl_WrongNumArgs(interp, 1, objv, "dict tzdata changeover"); return TCL_ERROR; } dict = objv[1]; - if ((Tcl_DictObjGet(interp, dict, literals[LIT_LOCALSECONDS], &secondsObj) - != TCL_OK) - || (Tcl_GetWideIntFromObj(interp, secondsObj, &(fields.localSeconds)) - != TCL_OK) - || (Tcl_GetIntFromObj(interp, objv[3], &changeover) != TCL_OK) - || ConvertLocalToUTC(interp, &fields, objv[2], changeover)) { + if ((Tcl_DictObjGet(interp, dict, literals[LIT_LOCALSECONDS], + &secondsObj) != TCL_OK) + || (Tcl_GetWideIntFromObj(interp, secondsObj, + &(fields.localSeconds)) != TCL_OK) + || (Tcl_GetIntFromObj(interp, objv[3], &changeover) != TCL_OK) + || ConvertLocalToUTC(interp, &fields, objv[2], changeover)) { return TCL_ERROR; } - /* - * Copy-on-write; set the 'seconds' field in the dictionary and - * place the modified dictionary in the interpreter result. + /* + * Copy-on-write; set the 'seconds' field in the dictionary and place the + * modified dictionary in the interpreter result. */ if (Tcl_IsShared(dict)) { @@ -330,7 +334,7 @@ ClockConvertlocaltoutcObjCmd( Tcl_IncrRefCount(dict); } status = Tcl_DictObjPut(interp, dict, literals[LIT_SECONDS], - Tcl_NewWideIntObj(fields.seconds)); + Tcl_NewWideIntObj(fields.seconds)); if (status == TCL_OK) { Tcl_SetObjResult(interp, dict); } @@ -345,15 +349,15 @@ ClockConvertlocaltoutcObjCmd( * * ClockGetdatefieldsObjCmd -- * - * Tcl command that determines the values that [clock format] will - * use in formatting a date, and populates a dictionary with them. + * Tcl command that determines the values that [clock format] will use in + * formatting a date, and populates a dictionary with them. * * Usage: * ::tcl::clock::GetDateFields seconds tzdata changeover * * Parameters: * seconds - Time expressed in seconds from the Posix epoch. - * tzdata - Time zone data of the time zone in which time is to + * tzdata - Time zone data of the time zone in which time is to * be expressed. * changeover - Julian Day Number at which the current locale adopted * the Gregorian calendar @@ -383,64 +387,71 @@ ClockGetdatefieldsObjCmd( Tcl_Obj* CONST * literals = data->literals; int changeover; - /* Check params */ + /* + * Check params. + */ if (objc != 4) { Tcl_WrongNumArgs(interp, 1, objv, "seconds tzdata changeover"); return TCL_ERROR; } if (Tcl_GetWideIntFromObj(interp, objv[1], &(fields.seconds)) != TCL_OK - || Tcl_GetIntFromObj(interp, objv[3], &changeover) != TCL_OK) { + || Tcl_GetIntFromObj(interp, objv[3], &changeover) != TCL_OK) { return TCL_ERROR; } - /* Convert UTC time to local */ + /* + * Convert UTC time to local. + */ if (ConvertUTCToLocal(interp, &fields, objv[2], changeover) != TCL_OK) { return TCL_ERROR; } - /* Extract Julian day */ + /* + * Extract Julian day. + */ fields.julianDay = (int) ((fields.localSeconds + JULIAN_SEC_POSIX_EPOCH) - / SECONDS_PER_DAY); + / SECONDS_PER_DAY); - /* Convert to Julian or Gregorian calendar */ + /* + * Convert to Julian or Gregorian calendar. + */ GetGregorianEraYearDay(&fields, changeover); GetMonthDay(&fields); GetYearWeekDay(&fields, changeover); dict = Tcl_NewDictObj(); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_LOCALSECONDS], - Tcl_NewWideIntObj(fields.localSeconds)); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_SECONDS], - Tcl_NewWideIntObj(fields.seconds)); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_TZNAME], - fields.tzName); + Tcl_DictObjPut(NULL, dict, literals[LIT_LOCALSECONDS], + Tcl_NewWideIntObj(fields.localSeconds)); + Tcl_DictObjPut(NULL, dict, literals[LIT_SECONDS], + Tcl_NewWideIntObj(fields.seconds)); + Tcl_DictObjPut(NULL, dict, literals[LIT_TZNAME], fields.tzName); Tcl_DecrRefCount(fields.tzName); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_TZOFFSET], - Tcl_NewIntObj(fields.tzOffset)); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_JULIANDAY], - Tcl_NewIntObj(fields.julianDay)); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_GREGORIAN], - Tcl_NewIntObj(fields.gregorian)); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_ERA], - literals[fields.era ? LIT_BCE : LIT_CE]); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_YEAR], - Tcl_NewIntObj(fields.year)); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_DAYOFYEAR], - Tcl_NewIntObj(fields.dayOfYear)); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_MONTH], - Tcl_NewIntObj(fields.month)); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_DAYOFMONTH], - Tcl_NewIntObj(fields.dayOfMonth)); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_ISO8601YEAR], - Tcl_NewIntObj(fields.iso8601Year)); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_ISO8601WEEK], - Tcl_NewIntObj(fields.iso8601Week)); - Tcl_DictObjPut((Tcl_Interp*) NULL, dict, literals[LIT_DAYOFWEEK], - Tcl_NewIntObj(fields.dayOfWeek)); + Tcl_DictObjPut(NULL, dict, literals[LIT_TZOFFSET], + Tcl_NewIntObj(fields.tzOffset)); + Tcl_DictObjPut(NULL, dict, literals[LIT_JULIANDAY], + Tcl_NewIntObj(fields.julianDay)); + Tcl_DictObjPut(NULL, dict, literals[LIT_GREGORIAN], + Tcl_NewIntObj(fields.gregorian)); + Tcl_DictObjPut(NULL, dict, literals[LIT_ERA], + literals[fields.era ? LIT_BCE : LIT_CE]); + Tcl_DictObjPut(NULL, dict, literals[LIT_YEAR], + Tcl_NewIntObj(fields.year)); + Tcl_DictObjPut(NULL, dict, literals[LIT_DAYOFYEAR], + Tcl_NewIntObj(fields.dayOfYear)); + Tcl_DictObjPut(NULL, dict, literals[LIT_MONTH], + Tcl_NewIntObj(fields.month)); + Tcl_DictObjPut(NULL, dict, literals[LIT_DAYOFMONTH], + Tcl_NewIntObj(fields.dayOfMonth)); + Tcl_DictObjPut(NULL, dict, literals[LIT_ISO8601YEAR], + Tcl_NewIntObj(fields.iso8601Year)); + Tcl_DictObjPut(NULL, dict, literals[LIT_ISO8601WEEK], + Tcl_NewIntObj(fields.iso8601Week)); + Tcl_DictObjPut(NULL, dict, literals[LIT_DAYOFWEEK], + Tcl_NewIntObj(fields.dayOfWeek)); Tcl_SetObjResult(interp, dict); return TCL_OK; @@ -451,8 +462,8 @@ ClockGetdatefieldsObjCmd( * * ClockGetjuliandayfromerayearmonthdayObjCmd -- * - * Tcl command that converts a time from era-year-month-day to - * a Julian Day Number. + * Tcl command that converts a time from era-year-month-day to a Julian + * Day Number. * * Parameters: * dict - Dictionary that contains 'era', 'year', 'month' and @@ -484,7 +495,9 @@ ClockGetjuliandayfromerayearmonthdayObjCmd ( int status; int era = 0; - /* Check params */ + /* + * Check params. + */ if (objc != 3) { Tcl_WrongNumArgs(interp, 1, objv, "dict changeover"); @@ -492,27 +505,32 @@ ClockGetjuliandayfromerayearmonthdayObjCmd ( } dict = objv[1]; if (Tcl_DictObjGet(interp, dict, literals[LIT_ERA], &fieldPtr) != TCL_OK - || Tcl_GetIndexFromObj(interp, fieldPtr, eras, "era", TCL_EXACT, - &era) != TCL_OK - || Tcl_DictObjGet(interp, dict, literals[LIT_YEAR], - &fieldPtr) != TCL_OK - || Tcl_GetIntFromObj(interp, fieldPtr, &(fields.year)) != TCL_OK - || Tcl_DictObjGet(interp, dict, literals[LIT_MONTH], - &fieldPtr) != TCL_OK - || Tcl_GetIntFromObj(interp, fieldPtr, &(fields.month)) != TCL_OK - || Tcl_DictObjGet(interp, dict, literals[LIT_DAYOFMONTH], - &fieldPtr) != TCL_OK - || Tcl_GetIntFromObj(interp, fieldPtr, &(fields.dayOfMonth)) != TCL_OK - || Tcl_GetIntFromObj(interp, objv[2], &changeover) != TCL_OK) { + || Tcl_GetIndexFromObj(interp, fieldPtr, eras, "era", TCL_EXACT, + &era) != TCL_OK + || Tcl_DictObjGet(interp, dict, literals[LIT_YEAR], + &fieldPtr) != TCL_OK + || Tcl_GetIntFromObj(interp, fieldPtr, &(fields.year)) != TCL_OK + || Tcl_DictObjGet(interp, dict, literals[LIT_MONTH], + &fieldPtr) != TCL_OK + || Tcl_GetIntFromObj(interp, fieldPtr, &(fields.month)) != TCL_OK + || Tcl_DictObjGet(interp, dict, literals[LIT_DAYOFMONTH], + &fieldPtr) != TCL_OK + || Tcl_GetIntFromObj(interp, fieldPtr, + &(fields.dayOfMonth)) != TCL_OK + || Tcl_GetIntFromObj(interp, objv[2], &changeover) != TCL_OK) { return TCL_ERROR; } fields.era = era; - /* Get Julian day */ + /* + * Get Julian day. + */ GetJulianDayFromEraYearMonthDay(&fields, changeover); - /* Store Julian day in the dictionary - copy on write */ + /* + * Store Julian day in the dictionary - copy on write. + */ if (Tcl_IsShared(dict)) { dict = Tcl_DuplicateObj(dict); @@ -520,7 +538,7 @@ ClockGetjuliandayfromerayearmonthdayObjCmd ( copied = 1; } status = Tcl_DictObjPut(interp, dict, literals[LIT_JULIANDAY], - Tcl_NewIntObj(fields.julianDay)); + Tcl_NewIntObj(fields.julianDay)); if (status == TCL_OK) { Tcl_SetObjResult(interp, dict); } @@ -528,25 +546,25 @@ ClockGetjuliandayfromerayearmonthdayObjCmd ( Tcl_DecrRefCount(dict); } return status; -} +} /* *---------------------------------------------------------------------- * * ClockGetjuliandayfromerayearweekdayObjCmd -- * - * Tcl command that converts a time from the ISO calendar to - * a Julian Day Number. + * Tcl command that converts a time from the ISO calendar to a Julian Day + * Number. * * Parameters: - * dict - Dictionary that contains 'era', 'iso8601Year', 'iso8601Week' + * dict - Dictionary that contains 'era', 'iso8601Year', 'iso8601Week' * and 'dayOfWeek' keys. * changeover - Julian Day of changeover to the Gregorian calendar * * Results: * Result is either TCL_OK, with the interpreter result being the - * dictionary augmented with a 'julianDay' key, or TCL_ERROR, - * with the result being an error message. + * dictionary augmented with a 'julianDay' key, or TCL_ERROR, with the + * result being an error message. * *---------------------------------------------------------------------- */ @@ -568,7 +586,9 @@ ClockGetjuliandayfromerayearweekdayObjCmd ( int status; int era = 0; - /* Check params */ + /* + * Check params. + */ if (objc != 3) { Tcl_WrongNumArgs(interp, 1, objv, "dict changeover"); @@ -576,27 +596,34 @@ ClockGetjuliandayfromerayearweekdayObjCmd ( } dict = objv[1]; if (Tcl_DictObjGet(interp, dict, literals[LIT_ERA], &fieldPtr) != TCL_OK - || Tcl_GetIndexFromObj(interp, fieldPtr, eras, "era", TCL_EXACT, - &era) != TCL_OK - || Tcl_DictObjGet(interp, dict, literals[LIT_ISO8601YEAR], - &fieldPtr) != TCL_OK - || Tcl_GetIntFromObj(interp, fieldPtr, &(fields.iso8601Year)) != TCL_OK - || Tcl_DictObjGet(interp, dict, literals[LIT_ISO8601WEEK], - &fieldPtr) != TCL_OK - || Tcl_GetIntFromObj(interp, fieldPtr, &(fields.iso8601Week)) != TCL_OK - || Tcl_DictObjGet(interp, dict, literals[LIT_DAYOFWEEK], - &fieldPtr) != TCL_OK - || Tcl_GetIntFromObj(interp, fieldPtr, &(fields.dayOfWeek)) != TCL_OK - || Tcl_GetIntFromObj(interp, objv[2], &changeover) != TCL_OK) { + || Tcl_GetIndexFromObj(interp, fieldPtr, eras, "era", TCL_EXACT, + &era) != TCL_OK + || Tcl_DictObjGet(interp, dict, literals[LIT_ISO8601YEAR], + &fieldPtr) != TCL_OK + || Tcl_GetIntFromObj(interp, fieldPtr, + &(fields.iso8601Year)) != TCL_OK + || Tcl_DictObjGet(interp, dict, literals[LIT_ISO8601WEEK], + &fieldPtr) != TCL_OK + || Tcl_GetIntFromObj(interp, fieldPtr, + &(fields.iso8601Week)) != TCL_OK + || Tcl_DictObjGet(interp, dict, literals[LIT_DAYOFWEEK], + &fieldPtr) != TCL_OK + || Tcl_GetIntFromObj(interp, fieldPtr, + &(fields.dayOfWeek)) != TCL_OK + || Tcl_GetIntFromObj(interp, objv[2], &changeover) != TCL_OK) { return TCL_ERROR; } fields.era = era; - /* Get Julian day */ + /* + * Get Julian day. + */ GetJulianDayFromEraYearWeekDay(&fields, changeover); - /* Store Julian day in the dictionary - copy on write */ + /* + * Store Julian day in the dictionary - copy on write. + */ if (Tcl_IsShared(dict)) { dict = Tcl_DuplicateObj(dict); @@ -604,7 +631,7 @@ ClockGetjuliandayfromerayearweekdayObjCmd ( copied = 1; } status = Tcl_DictObjPut(interp, dict, literals[LIT_JULIANDAY], - Tcl_NewIntObj(fields.julianDay)); + Tcl_NewIntObj(fields.julianDay)); if (status == TCL_OK) { Tcl_SetObjResult(interp, dict); } @@ -612,22 +639,22 @@ ClockGetjuliandayfromerayearweekdayObjCmd ( Tcl_DecrRefCount(dict); } return status; -} +} /* *---------------------------------------------------------------------- * * ConvertLocalToUTC -- * - * Converts a time (in a TclDateFields structure) from the - * local wall clock to UTC. + * Converts a time (in a TclDateFields structure) from the local wall + * clock to UTC. * * Results: * Returns a standard Tcl result. * * Side effects: - * Populates the 'seconds' field if successful; stores an error - * message in the interpreter result on failure. + * Populates the 'seconds' field if successful; stores an error message + * in the interpreter result on failure. * *---------------------------------------------------------------------- */ @@ -642,13 +669,15 @@ ConvertLocalToUTC( int rowc; /* Number of rows in tzdata */ Tcl_Obj** rowv; /* Pointers to the rows */ - /* unpack the tz data */ + /* + * Unpack the tz data. + */ if (Tcl_ListObjGetElements(interp, tzdata, &rowc, &rowv) != TCL_OK) { return TCL_ERROR; } - /* + /* * Special case: If the time zone is :localtime, the tzdata will be empty. * Use 'mktime' to convert the time to local */ @@ -665,18 +694,19 @@ ConvertLocalToUTC( * * ConvertLocalToUTCUsingTable -- * - * Converts a time (in a TclDateFields structure) from local time - * in a given time zone to UTC. + * Converts a time (in a TclDateFields structure) from local time in a + * given time zone to UTC. * * Results: * Returns a standard Tcl result. * * Side effects: - * Stores an error message in the interpreter if an error occurs; - * if successful, stores the 'seconds' field in 'fields. + * Stores an error message in the interpreter if an error occurs; if + * successful, stores the 'seconds' field in 'fields. * *---------------------------------------------------------------------- */ + static int ConvertLocalToUTCUsingTable( Tcl_Interp* interp, /* Tcl interpreter */ @@ -693,13 +723,13 @@ ConvertLocalToUTCUsingTable( int found; /* - * Perform an initial lookup assuming that local == UTC, and locate - * the last time conversion prior to that time. Get the offset from - * that row, and look up again. Continue until we find an offset - * that we found before. This definition, rather than "the same offset" - * ensures that we don't enter an endless loop, as would otherwise happen - * when trying to convert a non-existent time such as 02:30 during - * the US Spring Daylight Saving Time transition. + * Perform an initial lookup assuming that local == UTC, and locate the + * last time conversion prior to that time. Get the offset from that row, + * and look up again. Continue until we find an offset that we found + * before. This definition, rather than "the same offset" ensures that we + * don't enter an endless loop, as would otherwise happen when trying to + * convert a non-existent time such as 02:30 during the US Spring Daylight + * Saving Time transition. */ found = 0; @@ -708,9 +738,10 @@ ConvertLocalToUTCUsingTable( while (!found) { row = LookupLastTransition(interp, fields->seconds, rowc, rowv); if ((row == NULL) - || (Tcl_ListObjGetElements(interp, row, &cellc, &cellv) != TCL_OK) - || (Tcl_GetIntFromObj(interp, cellv[1], &(fields->tzOffset)) - != TCL_OK)) { + || Tcl_ListObjGetElements(interp, row, &cellc, + &cellv) != TCL_OK + || Tcl_GetIntFromObj(interp, cellv[1], + &(fields->tzOffset)) != TCL_OK) { return TCL_ERROR; } found = 0; @@ -718,7 +749,7 @@ ConvertLocalToUTCUsingTable( if (have[i] == fields->tzOffset) { found = 1; break; - } + } } if (!found) { if (nHave == 8) { @@ -739,15 +770,15 @@ ConvertLocalToUTCUsingTable( * * ConvertLocalToUTCUsingC -- * - * Converts a time from local wall clock to UTC when the local - * time zone cannot be determined. Uses 'mktime' to do the job. + * Converts a time from local wall clock to UTC when the local time zone + * cannot be determined. Uses 'mktime' to do the job. * * Results: * Returns a standard Tcl result. * * Side effects: - * Stores an error message in the interpreter if an error occurs; - * if successful, stores the 'seconds' field in 'fields. + * Stores an error message in the interpreter if an error occurs; if + * successful, stores the 'seconds' field in 'fields. * *---------------------------------------------------------------------- */ @@ -762,14 +793,18 @@ ConvertLocalToUTCUsingC( int localErrno; int secondOfDay; - /* Convert the given time to a date */ + /* + * Convert the given time to a date. + */ fields->julianDay = (int) ((fields->localSeconds + JULIAN_SEC_POSIX_EPOCH) - / SECONDS_PER_DAY); + / SECONDS_PER_DAY); GetGregorianEraYearDay(fields, changeover); GetMonthDay(fields); - - /* Convert the date/time to a 'struct tm' */ + + /* + * Convert the date/time to a 'struct tm'. + */ timeVal.tm_year = fields->year - 1900; timeVal.tm_mon = fields->month - 1; @@ -785,9 +820,9 @@ ConvertLocalToUTCUsingC( timeVal.tm_wday = -1; timeVal.tm_yday = -1; - /* - * Get local time. It is rumored that mktime is not thread safe - * on some platforms, so seize a mutex before attempting this. + /* + * Get local time. It is rumored that mktime is not thread safe on some + * platforms, so seize a mutex before attempting this. */ TzsetIfNecessary(); @@ -797,13 +832,15 @@ ConvertLocalToUTCUsingC( localErrno = errno; Tcl_MutexUnlock(&clockMutex); - /* If conversion fails, report an error */ + /* + * If conversion fails, report an error. + */ if (localErrno != 0 - || (fields->seconds == -1 && timeVal.tm_yday == -1)) { - Tcl_SetObjResult(interp, - Tcl_NewStringObj( "time value too large/small to " - "represent", -1)); + || (fields->seconds == -1 && timeVal.tm_yday == -1)) { + Tcl_SetObjResult(interp, + Tcl_NewStringObj("time value too large/small to represent", + -1)); return TCL_ERROR; } return TCL_OK; @@ -814,8 +851,7 @@ ConvertLocalToUTCUsingC( * * ConvertUTCToLocal -- * - * Converts a time (in a TclDateFields structure) from UTC to - * local time. + * Converts a time (in a TclDateFields structure) from UTC to local time. * * Results: * Returns a standard Tcl result. @@ -836,13 +872,15 @@ ConvertUTCToLocal( int rowc; /* Number of rows in tzdata */ Tcl_Obj** rowv; /* Pointers to the rows */ - /* unpack the tz data */ + /* + * Unpack the tz data. + */ if (Tcl_ListObjGetElements(interp, tzdata, &rowc, &rowv) != TCL_OK) { return TCL_ERROR; } - /* + /* * Special case: If the time zone is :localtime, the tzdata will be empty. * Use 'localtime' to convert the time to local */ @@ -852,7 +890,6 @@ ConvertUTCToLocal( } else { return ConvertUTCToLocalUsingTable(interp, fields, rowc, rowv); } - } /* @@ -867,8 +904,8 @@ ConvertUTCToLocal( * * Side effects: * On success, fills fields->tzName, fields->tzOffset and - * fields->localSeconds. On failure, places an error message in - * the interpreter result. + * fields->localSeconds. On failure, places an error message in the + * interpreter result. * *---------------------------------------------------------------------- */ @@ -881,22 +918,24 @@ ConvertUTCToLocalUsingTable( * (>= 1) */ Tcl_Obj *CONST rowv[] /* Rows of the conversion table */ ) { - Tcl_Obj* row; /* Row containing the current information */ - int cellc; /* Count of cells in the row (must be 4) */ - Tcl_Obj** cellv; /* Pointers to the cells */ + int cellc; /* Count of cells in the row (must be 4) */ + Tcl_Obj** cellv; /* Pointers to the cells */ - /* Look up the nearest transition time */ + /* + * Look up the nearest transition time. + */ row = LookupLastTransition(interp, fields->seconds, rowc, rowv); - if (row == NULL - || (Tcl_ListObjGetElements(interp, row, &cellc, &cellv) != TCL_OK) - || (Tcl_GetIntFromObj(interp, cellv[1], &(fields->tzOffset)) - != TCL_OK)) { + if (row == NULL || + Tcl_ListObjGetElements(interp, row, &cellc, &cellv) != TCL_OK || + Tcl_GetIntFromObj(interp,cellv[1],&(fields->tzOffset)) != TCL_OK) { return TCL_ERROR; } - /* Convert the time */ + /* + * Convert the time. + */ fields->tzName = cellv[3]; Tcl_IncrRefCount(fields->tzName); @@ -909,39 +948,39 @@ ConvertUTCToLocalUsingTable( * * ConvertUTCToLocalUsingC -- * - * Converts UTC to localtime in cases where the local time zone is - * not determinable, using the C 'localtime' function to do it. + * Converts UTC to localtime in cases where the local time zone is not + * determinable, using the C 'localtime' function to do it. * * Results: * Returns a standard Tcl result. * * Side effects: * On success, fills fields->tzName, fields->tzOffset and - * fields->localSeconds. On failure, places an error message in - * the interpreter result. + * fields->localSeconds. On failure, places an error message in the + * interpreter result. * *---------------------------------------------------------------------- */ - + static int ConvertUTCToLocalUsingC( Tcl_Interp* interp, /* Tcl interpreter */ TclDateFields* fields, /* Time to convert, with 'seconds' filled in */ int changeover /* Julian Day of the Gregorian transition */ ) { - time_t tock; struct tm* timeVal; /* Time after conversion */ int diff; /* Time zone diff local-Greenwich */ char buffer[8]; /* Buffer for time zone name */ - /* Use 'localtime' to determine local year, month, day, time of day. */ + /* + * Use 'localtime' to determine local year, month, day, time of day. + */ tock = (time_t) fields->seconds; if ((Tcl_WideInt) tock != fields->seconds) { Tcl_AppendResult(interp, - "number too large to represent as a Posix time", - NULL); + "number too large to represent as a Posix time", NULL); Tcl_SetErrorCode(interp, "CLOCK", "argTooLarge", NULL); return TCL_ERROR; } @@ -949,13 +988,15 @@ ConvertUTCToLocalUsingC( timeVal = ThreadSafeLocalTime(&tock); if (timeVal == NULL) { Tcl_AppendResult(interp, - "localtime failed (clock value may be too ", - "large/small to represent)", NULL); + "localtime failed (clock value may be too ", + "large/small to represent)", NULL); Tcl_SetErrorCode(interp, "CLOCK", "localtimeFailed", NULL); return TCL_ERROR; } - /* Fill in the date in 'fields' and use it to derive Julian Day */ + /* + * Fill in the date in 'fields' and use it to derive Julian Day. + */ fields->era = CE; fields->year = timeVal->tm_year + 1900; @@ -963,14 +1004,17 @@ ConvertUTCToLocalUsingC( fields->dayOfMonth = timeVal->tm_mday; GetJulianDayFromEraYearMonthDay(fields, changeover); - /* Convert that value to seconds */ - + /* + * Convert that value to seconds. + */ + fields->localSeconds = (((fields->julianDay * (Tcl_WideInt) 24 - + timeVal->tm_hour) * 60 - + timeVal->tm_min) * 60 - + timeVal->tm_sec) - JULIAN_SEC_POSIX_EPOCH; - - /* Determine a time zone offset and name; just use +hhmm for the name */ + + timeVal->tm_hour) * 60 + timeVal->tm_min) * 60 + + timeVal->tm_sec) - JULIAN_SEC_POSIX_EPOCH; + + /* + * Determine a time zone offset and name; just use +hhmm for the name. + */ diff = (int) (fields->localSeconds - fields->seconds); fields->tzOffset = diff; @@ -997,8 +1041,8 @@ ConvertUTCToLocalUsingC( * * LookupLastTransition -- * - * Given a UTC time and a tzdata array, looks up the last - * transition on or before the given time. + * Given a UTC time and a tzdata array, looks up the last transition on + * or before the given time. * * Results: * Returns a pointer to the row, or NULL if an error occurs. @@ -1018,21 +1062,25 @@ LookupLastTransition( Tcl_Obj* compObj; Tcl_WideInt compVal; - /* Examine the first row to make sure we're in bounds */ + /* + * Examine the first row to make sure we're in bounds. + */ if (Tcl_ListObjIndex(interp, rowv[0], 0, &compObj) != TCL_OK - || Tcl_GetWideIntFromObj(interp, compObj, &compVal) != TCL_OK) { + || Tcl_GetWideIntFromObj(interp, compObj, &compVal) != TCL_OK) { return NULL; } - /* - * Bizarre case - first row doesn't begin at MIN_WIDE_INT. - * Return it anyway. + + /* + * Bizarre case - first row doesn't begin at MIN_WIDE_INT. Return it + * anyway. */ + if (tick < compVal) { return rowv[0]; } - /* + /* * Binary-search to find the transition. */ @@ -1040,8 +1088,9 @@ LookupLastTransition( u = rowc-1; while (l < u) { int m = (l + u + 1) / 2; - if (Tcl_ListObjIndex(interp, rowv[m], 0, &compObj) != TCL_OK - || Tcl_GetWideIntFromObj(interp, compObj, &compVal) != TCL_OK) { + + if (Tcl_ListObjIndex(interp, rowv[m], 0, &compObj) != TCL_OK || + Tcl_GetWideIntFromObj(interp, compObj, &compVal) != TCL_OK) { return NULL; } if (tick >= compVal) { @@ -1051,7 +1100,6 @@ LookupLastTransition( } } return rowv[l]; - } /* @@ -1059,15 +1107,15 @@ LookupLastTransition( * * GetYearWeekDay -- * - * Given a date with Julian Calendar Day, compute the year, week, - * and day in the ISO8601 calendar. + * Given a date with Julian Calendar Day, compute the year, week, and day + * in the ISO8601 calendar. * * Results: * None. * * Side effects: - * Stores 'iso8601Year', 'iso8601Week' and 'dayOfWeek' in - * the date fields. + * Stores 'iso8601Year', 'iso8601Week' and 'dayOfWeek' in the date + * fields. * *---------------------------------------------------------------------- */ @@ -1081,8 +1129,8 @@ GetYearWeekDay( TclDateFields temp; int dayOfFiscalYear; - /* - * Find the given date, minus three days, plus one year. That date's + /* + * Find the given date, minus three days, plus one year. That date's * iso8601 year is an upper bound on the ISO8601 year of the given date. */ @@ -1097,10 +1145,10 @@ GetYearWeekDay( temp.dayOfWeek = 1; GetJulianDayFromEraYearWeekDay(&temp, changeover); - /* - * temp.julianDay is now the start of an ISO8601 year, either the - * one corresponding to the given date, or the one after. If we guessed - * high, move one year earlier + /* + * temp.julianDay is now the start of an ISO8601 year, either the one + * corresponding to the given date, or the one after. If we guessed high, + * move one year earlier */ if (fields->julianDay < temp.julianDay) { @@ -1126,10 +1174,9 @@ GetYearWeekDay( * * GetGregorianEraYearDay -- * - * Given a Julian Day Number, extracts the year and day of the - * year and puts them into TclDateFields, along with the era - * (BCE or CE) and a flag indicating whether the date is Gregorian - * or Julian. + * Given a Julian Day Number, extracts the year and day of the year and + * puts them into TclDateFields, along with the era (BCE or CE) and a + * flag indicating whether the date is Gregorian or Julian. * * Results: * None. @@ -1151,15 +1198,16 @@ GetGregorianEraYearDay( int n; if (jday >= changeover) { - - /* Gregorian calendar */ + /* + * Gregorian calendar. + */ fields->gregorian = 1; year = 1; - /* + /* * n = Number of 400-year cycles since 1 January, 1 CE in the - * proleptic Gregorian calendar. day = remaining days. + * proleptic Gregorian calendar. day = remaining days. */ day = jday - JDAY_1_JAN_1_CE_GREGORIAN; @@ -1175,15 +1223,19 @@ GetGregorianEraYearDay( n = day / ONE_CENTURY_GREGORIAN; day %= ONE_CENTURY_GREGORIAN; if (n > 3) { - /* 31 December in the last year of a 400-year cycle */ + /* + * 31 December in the last year of a 400-year cycle. + */ + n = 3; day += ONE_CENTURY_GREGORIAN; } year += 100 * n; } else { - - /* Julian calendar */ + /* + * Julian calendar. + */ fields->gregorian = 0; year = 1; @@ -1191,24 +1243,33 @@ GetGregorianEraYearDay( } - /* n = number of 4-year cycles; days = remaining days */ + /* + * n = number of 4-year cycles; days = remaining days. + */ n = day / FOUR_YEARS; day %= 1461; year += 4 * n; - /* n = number of years; days = remaining days */ + /* + * n = number of years; days = remaining days. + */ n = day / ONE_YEAR; day %= ONE_YEAR; if (n > 3) { - /* 31 December of a leap year */ + /* + * 31 December of a leap year. + */ + n = 3; day += 365; } year += n; - /* store era/year/day back into fields */ + /* + * store era/year/day back into fields. + */ if (year < 0) { fields->era = BCE; @@ -1218,7 +1279,6 @@ GetGregorianEraYearDay( fields->year = year; } fields->dayOfYear = day + 1; - } /* @@ -1244,6 +1304,7 @@ GetMonthDay( int day = fields->dayOfYear; int month; const int* h = hath[IsGregorianLeapYear(fields)]; + for (month = 0; month < 12 && day > h[month]; ++month) { day -= h[month]; } @@ -1256,8 +1317,8 @@ GetMonthDay( * * GetJulianDayFromEraYearWeekDay -- * - * Given a TclDateFields structure containing era, ISO8601 year, - * ISO8601 week, and day of week, computes the Julian Day Number. + * Given a TclDateFields structure containing era, ISO8601 year, ISO8601 + * week, and day of week, computes the Julian Day Number. * * Results: * None. @@ -1271,14 +1332,15 @@ GetMonthDay( static void GetJulianDayFromEraYearWeekDay( TclDateFields* fields, /* Date to convert */ - int changeover /* Julian Day Number of the - * Gregorian transition */ + int changeover /* Julian Day Number of the Gregorian + * transition */ ) { + int firstMonday; /* Julian day number of week 1, day 1 in the + * given year */ - int firstMonday; /* Julian day number of week 1, day 1 - * in the given year */ - - /* Find January 4 in the ISO8601 year, which will always be in week 1 */ + /* + * Find January 4 in the ISO8601 year, which will always be in week 1. + */ TclDateFields firstWeek; firstWeek.era = fields->era; @@ -1287,15 +1349,18 @@ GetJulianDayFromEraYearWeekDay( firstWeek.dayOfMonth = 4; GetJulianDayFromEraYearMonthDay(&firstWeek, changeover); - /* Find Monday of week 1. */ + /* + * Find Monday of week 1. + */ firstMonday = WeekdayOnOrBefore(1, firstWeek.julianDay); - /* Advance to the given week and day */ + /* + * Advance to the given week and day. + */ fields->julianDay = firstMonday + 7 * (fields->iso8601Week - 1) - + fields->dayOfWeek - 1; - + + fields->dayOfWeek - 1; } /* @@ -1303,8 +1368,8 @@ GetJulianDayFromEraYearWeekDay( * * GetJulianDayFromEraYearMonthDay -- * - * Given era, year, month, and dayOfMonth (in TclDateFields), and - * the Gregorian transition date, computes the Julian Day Number. + * Given era, year, month, and dayOfMonth (in TclDateFields), and the + * Gregorian transition date, computes the Julian Day Number. * * Results: * None. @@ -1330,7 +1395,9 @@ GetJulianDayFromEraYearMonthDay( year = fields->year; } - /* Reduce month modulo 12 */ + /* + * Reduce month modulo 12. + */ month = fields->month; mm1 = month - 1; @@ -1343,8 +1410,10 @@ GetJulianDayFromEraYearMonthDay( year += q; month = r + 1; ym1 = year - 1; - - /* Adjust the year after reducing the month */ + + /* + * Adjust the year after reducing the month. + */ fields->gregorian = 1; if (year < 1) { @@ -1355,30 +1424,31 @@ GetJulianDayFromEraYearMonthDay( fields->year = year; } - /* Try an initial conversion in the Gregorian calendar */ + /* + * Try an initial conversion in the Gregorian calendar. + */ fields->julianDay = JDAY_1_JAN_1_CE_GREGORIAN - 1 - + fields->dayOfMonth - + daysInPriorMonths[IsGregorianLeapYear(fields)][month - 1] - + (ONE_YEAR * ym1) - + (ym1 / 4) - - (ym1 / 100) - + (ym1 / 400); - - /* - * If the resulting date is before the Gregorian changeover, convert - * in the Julian calendar instead. + + fields->dayOfMonth + + daysInPriorMonths[IsGregorianLeapYear(fields)][month - 1] + + (ONE_YEAR * ym1) + + (ym1 / 4) + - (ym1 / 100) + + (ym1 / 400); + + /* + * If the resulting date is before the Gregorian changeover, convert in + * the Julian calendar instead. */ if (fields->julianDay < changeover) { fields->gregorian = 0; fields->julianDay = JDAY_1_JAN_1_CE_JULIAN - 1 - + fields->dayOfMonth - + daysInPriorMonths[year%4 == 0][month - 1] - + (365 * ym1) - + (ym1 / 4); + + fields->dayOfMonth + + daysInPriorMonths[year%4 == 0][month - 1] + + (365 * ym1) + + (ym1 / 4); } - } /* @@ -1386,8 +1456,8 @@ GetJulianDayFromEraYearMonthDay( * * IsGregorianLeapYear -- * - * Tests whether a given year is a leap year, in either Julian - * or Gregorian calendar. + * Tests whether a given year is a leap year, in either Julian or + * Gregorian calendar. * * Results: * Returns 1 for a leap year, 0 otherwise. @@ -1400,6 +1470,7 @@ IsGregorianLeapYear( TclDateFields* fields /* Date to test */ ) { int year; + if (fields->era == BCE) { year = 1 - fields->year; } else { @@ -1423,8 +1494,8 @@ IsGregorianLeapYear( * * WeekdayOnOrBefore -- * - * Finds the Julian Day Number of a given day of the week that - * falls on or before a given date, expressed as Julian Day Number. + * Finds the Julian Day Number of a given day of the week that falls on + * or before a given date, expressed as Julian Day Number. * * Results: * Returns the Julian Day Number @@ -1455,10 +1526,9 @@ WeekdayOnOrBefore( * NAME - Name of the environment variable desired * * Results: - * Returns a standard Tcl result. Returns an error if the - * variable does not exist, with a message left in the interpreter. - * Returns TCL_OK and the value of the variable if the variable - * does exist, + * Returns a standard Tcl result. Returns an error if the variable does + * not exist, with a message left in the interpreter. Returns TCL_OK and + * the value of the variable if the variable does exist, * *---------------------------------------------------------------------- */ @@ -1470,7 +1540,6 @@ ClockGetenvObjCmd( int objc, Tcl_Obj *CONST objv[]) { - CONST char* varName; CONST char* varValue; @@ -1526,7 +1595,7 @@ ThreadSafeLocalTime( Tcl_MutexUnlock(&clockMutex); return NULL; } else { - memcpy((VOID *) tmPtr, (VOID *) localtime(timePtr), sizeof(struct tm)); + memcpy((void *) tmPtr, (void *) localtime(timePtr), sizeof(struct tm)); Tcl_MutexUnlock(&clockMutex); } #endif @@ -1767,11 +1836,12 @@ TzsetIfNecessary(void) */ static void -ClockDeleteCmdProc(ClientData clientData) - /* Opaque pointer to the client data */ +ClockDeleteCmdProc( + ClientData clientData) /* Opaque pointer to the client data */ { ClockClientData *data = (ClockClientData*) clientData; int i; + --(data->refCount); if (data->refCount == 0) { for (i = 0; i < LIT__END; ++i) { |