Skip to content

Commit

Permalink
Merge pull request sebres/tclclockmod#17 from sebres/simplify-yy-flags
Browse files Browse the repository at this point in the history
Simplifying info structure, yacc rules and flags
  • Loading branch information
sebres committed Jul 15, 2019
2 parents 925378f + 31ef272 commit 828ad35
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 178 deletions.
58 changes: 25 additions & 33 deletions generic/tclClock.c
Original file line number Diff line number Diff line change
Expand Up @@ -3794,14 +3794,14 @@ ClockValidDate(
}

/* first year (used later in hath / daysInPriorMonths) */
if ((info->flags & (CLF_YEAR|CLF_ISO8601YEAR)) || yyHaveDate) {
if ((info->flags & (CLF_YEAR|CLF_ISO8601YEAR))) {
if ((info->flags & CLF_ISO8601YEAR)) {
if ( yydate.iso8601Year < dataPtr->validMinYear
|| yydate.iso8601Year > dataPtr->validMaxYear ) {
errMsg = "invalid iso year"; errCode = "iso year"; goto error;
}
}
if ((info->flags & CLF_YEAR) || yyHaveDate) {
if (info->flags & CLF_YEAR) {
if ( yyYear < dataPtr->validMinYear
|| yyYear > dataPtr->validMaxYear ) {
errMsg = "invalid year"; errCode = "year"; goto error;
Expand All @@ -3817,15 +3817,13 @@ ClockValidDate(
}
}
/* and month (used later in hath) */
if ((info->flags & CLF_MONTH) || yyHaveDate) {
info->flags |= CLF_MONTH;
if (info->flags & CLF_MONTH) {
if ( yyMonth < 1 || yyMonth > 12 ) {
errMsg = "invalid month"; errCode = "month"; goto error;
}
}
/* day of month */
if ((info->flags & CLF_DAYOFMONTH) || (yyHaveDate || yyHaveDay)) {
info->flags |= CLF_DAYOFMONTH;
if (info->flags & (CLF_DAYOFMONTH|CLF_DAYOFWEEK)) {
if ( yyDay < 1 || yyDay > 31 ) {
errMsg = "invalid day"; errCode = "day"; goto error;
}
Expand All @@ -3837,7 +3835,7 @@ ClockValidDate(
}
}
}
if ((info->flags & CLF_DAYOFYEAR)) {
if (info->flags & CLF_DAYOFYEAR) {
if ( yydate.dayOfYear < 1
|| yydate.dayOfYear > daysInPriorMonths[IsGregorianLeapYear(&yydate)][12] ) {
errMsg = "invalid day of year"; errCode = "day of year"; goto error;
Expand All @@ -3857,7 +3855,7 @@ ClockValidDate(
}
}

if ((info->flags & CLF_TIME) || yyHaveTime) {
if (info->flags & CLF_TIME) {
/* hour */
if ( yyHour < 0 || yyHour > ((yyMeridian == MER24) ? 23 : 12) ) {
errMsg = "invalid time (hour)"; errCode = "hour"; goto error;
Expand All @@ -3884,7 +3882,7 @@ ClockValidDate(

/* time, regarding the modifications by the time-zone (looks for given time
* in between DST-time hole, so does not exist in this time-zone) */
if (((info->flags & CLF_TIME) || yyHaveTime)) {
if (info->flags & CLF_TIME) {
/*
* we don't need to do the backwards time-conversion (UTC to local) and
* compare results, because the after conversion (local to UTC) we
Expand Down Expand Up @@ -3971,17 +3969,14 @@ ClockFreeScan(
* midnight.
*/

if (yyHaveDate) {
if (info->flags & CLF_YEAR) {
if (yyYear < 100) {
if (yyYear >= dataPtr->yearOfCenturySwitch) {
yyYear -= 100;
}
yyYear += dataPtr->currentYearCentury;
}
yydate.era = CE;
if (yyHaveTime == 0) {
yyHaveTime = -1;
}
info->flags |= CLF_ASSEMBLE_JULIANDAY|CLF_ASSEMBLE_SECONDS;
}

Expand All @@ -3990,7 +3985,7 @@ ClockFreeScan(
* zone indicator of +-hhmm and setup this time zone.
*/

if (yyHaveZone) {
if (info->flags & CLF_ZONE) {
Tcl_Obj *tzObjStor = NULL;
int minEast = -yyTimezone;
int dstFlag = 1 - yyDSTmode;
Expand Down Expand Up @@ -4024,20 +4019,20 @@ ClockFreeScan(
* Assemble date, time, zone into seconds-from-epoch
*/

if (yyHaveTime == -1) {
if ((info->flags & (CLF_TIME|CLF_HAVEDATE)) == CLF_HAVEDATE) {
yySecondOfDay = 0;
info->flags |= CLF_ASSEMBLE_SECONDS;
}
else
if (yyHaveTime) {
if (info->flags & CLF_TIME) {
yySecondOfDay = ToSeconds(yyHour, yyMinutes,
yySeconds, yyMeridian);
info->flags |= CLF_ASSEMBLE_SECONDS;
}
else
if ( (yyHaveDay && !yyHaveDate)
|| yyHaveOrdinalMonth
|| ( yyHaveRel
if ( (info->flags & (CLF_DAYOFWEEK|CLF_HAVEDATE)) == CLF_DAYOFWEEK
|| (info->flags & CLF_ORDINALMONTH)
|| ( (info->flags & CLF_RELCONV)
&& ( yyRelMonth != 0
|| yyRelDay != 0 ) )
) {
Expand Down Expand Up @@ -4090,7 +4085,7 @@ ClockCalcRelTime(
*/
repeat_rel:

if (yyHaveRel) {
if (info->flags & CLF_RELCONV) {

/*
* Relative conversion normally possible in UTC time only, because
Expand Down Expand Up @@ -4162,14 +4157,14 @@ ClockCalcRelTime(
}
}

yyHaveRel = 0;
info->flags &= ~CLF_RELCONV;
}

/*
* Do relative (ordinal) month
*/

if (yyHaveOrdinalMonth) {
if (info->flags & CLF_ORDINALMONTH) {
int monthDiff;

/* if needed extract year, month, etc. again */
Expand All @@ -4195,12 +4190,10 @@ ClockCalcRelTime(
}

/* process it further via relative times */
yyHaveRel++;
yyYear += yyMonthOrdinalIncr;
yyRelMonth += monthDiff;
yyHaveOrdinalMonth = 0;

info->flags |= CLF_ASSEMBLE_JULIANDAY|CLF_ASSEMBLE_SECONDS;
info->flags &= ~CLF_ORDINALMONTH;
info->flags |= CLF_RELCONV|CLF_ASSEMBLE_JULIANDAY|CLF_ASSEMBLE_SECONDS;

goto repeat_rel;
}
Expand All @@ -4209,12 +4202,11 @@ ClockCalcRelTime(
* Do relative weekday
*/

if (yyHaveDay && !yyHaveDate) {
if ((info->flags & (CLF_DAYOFWEEK|CLF_HAVEDATE)) == CLF_DAYOFWEEK) {

/* restore scanned day of week */
if (info->flags & CLF_DAYOFWEEK) {
yyDayOfWeek = prevDayOfWeek;
}
yyDayOfWeek = prevDayOfWeek;

/* if needed assemble julianDay now */
if (info->flags & CLF_ASSEMBLE_JULIANDAY) {
GetJulianDayFromEraYearMonthDay(&yydate, GREGORIAN_CHANGE_DATE);
Expand Down Expand Up @@ -4420,7 +4412,7 @@ ClockAddObjCmd(
* correct date info, because the date may be changed,
* so refresh it now */

if ( yyHaveRel
if ( (info->flags & CLF_RELCONV)
&& ( unitIndex == CLC_ADD_WEEKDAYS
/* some months can be shorter as another */
|| yyRelMonth || yyRelDay
Expand All @@ -4435,7 +4427,7 @@ ClockAddObjCmd(
}

/* process increment by offset + unit */
yyHaveRel++;
info->flags |= CLF_RELCONV;
switch (unitIndex) {
case CLC_ADD_YEARS:
yyRelMonth += offs * 12;
Expand Down Expand Up @@ -4472,7 +4464,7 @@ ClockAddObjCmd(
* Do relative times (if not yet already processed interim):
*/

if (yyHaveRel) {
if (info->flags & CLF_RELCONV) {
if (ClockCalcRelTime(info, &opts) != TCL_OK) {
goto done;
}
Expand Down
Loading

0 comments on commit 828ad35

Please sign in to comment.