📄 tcldate.c
字号:
* represented by the date, then multiply by the number of seconds/day. */ for (Julian = Day - 1, i = 0; i < Month; i++) Julian += DaysInMonth[i]; if (Year >= EPOCH) { for (i = EPOCH; i < Year; i++) Julian += 365 + IsLeapYear(i); } else { for (i = Year; i < EPOCH; i++) Julian -= 365 + IsLeapYear(i); } Julian *= SECSPERDAY; /* Add the timezone offset ?? */ Julian += TclDateTimezone * 60L; /* Add the number of seconds represented by the time component */ if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0) return -1; Julian += tod; /* Perform a preliminary DST compensation ?? */ if (DSTmode == DSTon || (DSTmode == DSTmaybe && TclpGetDate((TclpTime_t)&Julian, 0)->tm_isdst)) Julian -= 60 * 60; *TimePtr = Julian; return 0;}static time_tDSTcorrect(Start, Future) time_t Start; time_t Future;{ time_t StartDay; time_t FutureDay; StartDay = (TclpGetDate((TclpTime_t)&Start, 0)->tm_hour + 1) % 24; FutureDay = (TclpGetDate((TclpTime_t)&Future, 0)->tm_hour + 1) % 24; return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;}static time_tNamedDay(Start, DayOrdinal, DayNumber) time_t Start; time_t DayOrdinal; time_t DayNumber;{ struct tm *tm; time_t now; now = Start; tm = TclpGetDate((TclpTime_t)&now, 0); now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7); now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1); return DSTcorrect(Start, now);}static time_tNamedMonth(Start, MonthOrdinal, MonthNumber) time_t Start; time_t MonthOrdinal; time_t MonthNumber;{ struct tm *tm; time_t now; int result; now = Start; tm = TclpGetDate((TclpTime_t)&now, 0); /* To compute the next n'th month, we use this alg: * add n to year value * if currentMonth < requestedMonth decrement year value by 1 (so that * doing next february from january gives us february of the current year) * set day to 1, time to 0 */ tm->tm_year += MonthOrdinal; if (tm->tm_mon < MonthNumber - 1) { tm->tm_year--; } result = Convert(MonthNumber, (time_t) 1, tm->tm_year + TM_YEAR_BASE, (time_t) 0, (time_t) 0, (time_t) 0, MER24, DSTmaybe, &now); if (result < 0) { return 0; } return DSTcorrect(Start, now);}static intRelativeMonth(Start, RelMonth, TimePtr) time_t Start; time_t RelMonth; time_t *TimePtr;{ struct tm *tm; time_t Month; time_t Year; time_t Julian; int result; if (RelMonth == 0) { *TimePtr = 0; return 0; } tm = TclpGetDate((TclpTime_t)&Start, 0); Month = 12 * (tm->tm_year + TM_YEAR_BASE) + tm->tm_mon + RelMonth; Year = Month / 12; Month = Month % 12 + 1; result = Convert(Month, (time_t) tm->tm_mday, Year, (time_t) tm->tm_hour, (time_t) tm->tm_min, (time_t) tm->tm_sec, MER24, DSTmaybe, &Julian); /* * The Julian time returned above is behind by one day, if "month" * or "year" is used to specify relative time and the GMT flag is true. * This problem occurs only when the current time is closer to * midnight, the difference being not more than its time difference * with GMT. For example, in US/Pacific time zone, the problem occurs * whenever the current time is between midnight to 8:00am or 7:00amDST. * See Bug# 413397 for more details and sample script. * To resolve this bug, we simply add the number of seconds corresponding * to timezone difference with GMT to Julian time, if GMT flag is true. */ if (TclDateTimezone == 0) { Julian += TclpGetTimeZone((unsigned long) Start) * 60L; } /* * The following iteration takes into account the case were we jump * into a "short month". Far example, "one month from Jan 31" will * fail because there is no Feb 31. The code below will reduce the * day and try converting the date until we succed or the date equals * 28 (which always works unless the date is bad in another way). */ while ((result != 0) && (tm->tm_mday > 28)) { tm->tm_mday--; result = Convert(Month, (time_t) tm->tm_mday, Year, (time_t) tm->tm_hour, (time_t) tm->tm_min, (time_t) tm->tm_sec, MER24, DSTmaybe, &Julian); } if (result != 0) { return -1; } *TimePtr = DSTcorrect(Start, Julian); return 0;}/* *----------------------------------------------------------------------------- * * RelativeDay -- * * Given a starting time and a number of days before or after, compute the * DST corrected difference between those dates. * * Results: * 1 or -1 indicating success or failure. * * Side effects: * Fills TimePtr with the computed value. * *----------------------------------------------------------------------------- */static intRelativeDay(Start, RelDay, TimePtr) time_t Start; time_t RelDay; time_t *TimePtr;{ time_t new; new = Start + (RelDay * 60 * 60 * 24); *TimePtr = DSTcorrect(Start, new); return 1;}static intLookupWord(buff) char *buff;{ register char *p; register char *q; register TABLE *tp; int i; int abbrev; /* * Make it lowercase. */ Tcl_UtfToLower(buff); if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) { TclDatelval.Meridian = MERam; return tMERIDIAN; } if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) { TclDatelval.Meridian = MERpm; return tMERIDIAN; } /* * See if we have an abbreviation for a month. */ if (strlen(buff) == 3) { abbrev = 1; } else if (strlen(buff) == 4 && buff[3] == '.') { abbrev = 1; buff[3] = '\0'; } else { abbrev = 0; } for (tp = MonthDayTable; tp->name; tp++) { if (abbrev) { if (strncmp(buff, tp->name, 3) == 0) { TclDatelval.Number = tp->value; return tp->type; } } else if (strcmp(buff, tp->name) == 0) { TclDatelval.Number = tp->value; return tp->type; } } for (tp = TimezoneTable; tp->name; tp++) { if (strcmp(buff, tp->name) == 0) { TclDatelval.Number = tp->value; return tp->type; } } for (tp = UnitsTable; tp->name; tp++) { if (strcmp(buff, tp->name) == 0) { TclDatelval.Number = tp->value; return tp->type; } } /* * Strip off any plural and try the units table again. */ i = strlen(buff) - 1; if (buff[i] == 's') { buff[i] = '\0'; for (tp = UnitsTable; tp->name; tp++) { if (strcmp(buff, tp->name) == 0) { TclDatelval.Number = tp->value; return tp->type; } } } for (tp = OtherTable; tp->name; tp++) { if (strcmp(buff, tp->name) == 0) { TclDatelval.Number = tp->value; return tp->type; } } /* * Military timezones. */ if (buff[1] == '\0' && !(*buff & 0x80) && isalpha(UCHAR(*buff))) { /* INTL: ISO only */ for (tp = MilitaryTable; tp->name; tp++) { if (strcmp(buff, tp->name) == 0) { TclDatelval.Number = tp->value; return tp->type; } } } /* * Drop out any periods and try the timezone table again. */ for (i = 0, p = q = buff; *q; q++) if (*q != '.') { *p++ = *q; } else { i++; } *p = '\0'; if (i) { for (tp = TimezoneTable; tp->name; tp++) { if (strcmp(buff, tp->name) == 0) { TclDatelval.Number = tp->value; return tp->type; } } } return tID;}static intTclDatelex(){ register char c; register char *p; char buff[20]; int Count; for ( ; ; ) { while (isspace(UCHAR(*TclDateInput))) { TclDateInput++; } if (isdigit(UCHAR(c = *TclDateInput))) { /* INTL: digit */ /* convert the string into a number; count the number of digits */ Count = 0; for (TclDatelval.Number = 0; isdigit(UCHAR(c = *TclDateInput++)); ) { /* INTL: digit */ TclDatelval.Number = 10 * TclDatelval.Number + c - '0'; Count++; } TclDateInput--; /* A number with 6 or more digits is considered an ISO 8601 base */ if (Count >= 6) { return tISOBASE; } else { return tUNUMBER; } } if (!(c & 0x80) && isalpha(UCHAR(c))) { /* INTL: ISO only. */ for (p = buff; isalpha(UCHAR(c = *TclDateInput++)) /* INTL: ISO only. */ || c == '.'; ) { if (p < &buff[sizeof buff - 1]) { *p++ = c; } } *p = '\0'; TclDateInput--; return LookupWord(buff); } if (c != '(') { return *TclDateInput++; } Count = 0; do { c = *TclDateInput++; if (c == '\0') { return c; } else if (c == '(') { Count++; } else if (c == ')') { Count--; } } while (Count > 0); }}/* * Specify zone is of -50000 to force GMT. (This allows BST to work). */intTclGetDate(p, now, zone, timePtr) char *p; unsigned long now; long zone; unsigned long *timePtr;{ struct tm *tm; time_t Start; time_t Time; time_t tod; int thisyear; TclDateInput = p; /* now has to be cast to a time_t for 64bit compliance */ Start = now; tm = TclpGetDate((TclpTime_t) &Start, 0); thisyear = tm->tm_year + TM_YEAR_BASE; TclDateYear = thisyear; TclDateMonth = tm->tm_mon + 1; TclDateDay = tm->tm_mday; TclDateTimezone = zone; if (zone == -50000) { TclDateDSTmode = DSToff; /* assume GMT */ TclDateTimezone = 0; } else { TclDateDSTmode = DSTmaybe; } TclDateHour = 0; TclDateMinutes = 0; TclDateSeconds = 0; TclDateMeridian = MER24; TclDateRelSeconds = 0; TclDateRelMonth = 0; TclDateRelDay = 0; TclDateRelPointer = NULL; TclDateHaveDate = 0; TclDateHaveDay = 0; TclDateHaveOrdinalMonth = 0; TclDateHaveRel = 0; TclDateHaveTime = 0; TclDateHaveZone = 0; if (TclDateparse() || TclDateHaveTime > 1 || TclDateHaveZone > 1 || TclDateHaveDate > 1 || TclDateHaveDay > 1 || TclDateHaveOrdinalMonth > 1) { return -1; } if (TclDateHaveDate || TclDateHaveTime || TclDateHaveDay) { if (TclDateYear < 0) { TclDateYear = -TclDateYear; } /* * The following line handles years that are specified using * only two digits. The line of code below implements a policy * defined by the X/Open workgroup on the millinium rollover. * Note: some of those dates may not actually be valid on some * platforms. The POSIX standard startes that the dates 70-99 * shall refer to 1970-1999 and 00-38 shall refer to 2000-2038. * This later definition should work on all platforms. */ if (TclDateYear < 100) { if (TclDateYear >= 69) { TclDateYear += 1900; } else { TclDateYear += 2000; } } if (Convert(TclDateMonth, TclDateDay, TclDateYear, TclDateHour, TclDateMinutes, TclDateSeconds, TclDateMeridian, TclDateDSTmode, &Start) < 0) { return -1; } } else { Start = now; if (!TclDateHaveRel) { Start -= ((tm->tm_hour * 60L * 60L) + tm->tm_min * 60L) + tm->tm_sec; } } Start += TclDateRelSeconds; if (RelativeMonth(Start, TclDateRelMonth, &Time) < 0) { return -1; } Start += Time; if (RelativeDay(Start, TclDateRelDay, &Time) < 0) { return -1; } Start += Time; if (TclDateHaveDay && !TclDateHaveDate) { tod = NamedDay(Start, TclDateDayOrdinal, TclDateDayNumber); Start += tod; } if (TclDateHaveOrdinalMonth) { tod = NamedMonth(Start, TclDateMonthOrdinal, TclDateMonth); Start += tod; } *timePtr = Start; return 0;}static CONST TclDatetabelem TclDateexca[] ={
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -