Skip to content

Commit

Permalink
Simplify some ALL_STATE and PCTS ifdefs.
Browse files Browse the repository at this point in the history
* Makefile (GCC_DEBUG_FLAGS): Add -Wno-address and -Wno-cast-qual.
Otherwise, GCC 4.8.1 complains about some of the following changes.
* localtime.c (wildabbr): Now const.  This is a tiny bit more
efficient, POSIX allows it, and the ALL_STATE simplifications
below make use of it.  All uses changed to cast if needed.
(settzname, tzload, localsub, gmtsub, timesub, time2sub, time1):
Protect less code with ALL_STATE; this is simpler and doesn't
change behavior (or, these days, efficiency).
(time1): Simplify the PCTS ifdef, and explain what PCTS is.
  • Loading branch information
eggert committed Jan 18, 2014
1 parent 7257f94 commit 8dbbeb5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ GCC_DEBUG_FLAGS = -Dlint -g3 -O3 -fno-common -fstrict-aliasing \
-Wbad-function-cast -Wcast-align -Wcast-qual \
-Wformat=2 -Winit-self \
-Wmissing-declarations -Wmissing-noreturn -Wmissing-prototypes \
-Wnested-externs \
-Wnested-externs -Wno-address -Wno-cast-qual \
-Wno-format-nonliteral -Wno-sign-compare -Wno-sign-conversion \
-Wno-type-limits \
-Wno-unused-parameter -Woverlength-strings -Wpointer-arith \
Expand Down
53 changes: 12 additions & 41 deletions localtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
#define WILDABBR " "
#endif /* !defined WILDABBR */

static char wildabbr[] = WILDABBR;
static const char wildabbr[] = WILDABBR;

static const char gmt[] = "GMT";

Expand Down Expand Up @@ -201,8 +201,8 @@ static int lcl_is_set;
static int gmt_is_set;

char * tzname[2] = {
wildabbr,
wildabbr
(char *) wildabbr,
(char *) wildabbr
};

/*
Expand Down Expand Up @@ -254,21 +254,18 @@ settzname(void)
register struct state * const sp = lclptr;
register int i;

tzname[0] = wildabbr;
tzname[1] = wildabbr;
tzname[0] = tzname[1] = (char *) wildabbr;
#ifdef USG_COMPAT
daylight = 0;
timezone = 0;
#endif /* defined USG_COMPAT */
#ifdef ALTZONE
altzone = 0;
#endif /* defined ALTZONE */
#ifdef ALL_STATE
if (sp == NULL) {
tzname[0] = tzname[1] = gmt;
tzname[0] = tzname[1] = (char *) gmt;
return;
}
#endif /* defined ALL_STATE */
/*
** And to get the latest zone names into tzname. . .
*/
Expand Down Expand Up @@ -347,10 +344,8 @@ tzload(register const char *name, register struct state *const sp,

sp->goback = sp->goahead = FALSE;

#ifdef ALL_STATE
if (up == NULL)
return -1;
#endif

if (name == NULL && (name = TZDEFAULT) == NULL)
goto oops;
Expand Down Expand Up @@ -1268,10 +1263,8 @@ localsub(const time_t *const timep, const int_fast32_t offset,
const time_t t = *timep;

sp = lclptr;
#ifdef ALL_STATE
if (sp == NULL)
return gmtsub(timep, offset, tmp);
#endif /* defined ALL_STATE */
if ((sp->goback && t < sp->ats[0]) ||
(sp->goahead && t > sp->ats[sp->timecnt - 1])) {
time_t newt = t;
Expand Down Expand Up @@ -1366,8 +1359,8 @@ gmtsub(const time_t *const timep, const int_fast32_t offset,
gmt_is_set = TRUE;
#ifdef ALL_STATE
gmtptr = malloc(sizeof *gmtptr);
if (gmtptr != NULL)
#endif /* defined ALL_STATE */
if (gmtptr != NULL)
gmtload(gmtptr);
}
result = timesub(timep, offset, gmtptr, tmp);
Expand All @@ -1377,18 +1370,7 @@ gmtsub(const time_t *const timep, const int_fast32_t offset,
** "UT+xxxx" or "UT-xxxx" if offset is non-zero,
** but this is no time for a treasure hunt.
*/
if (offset != 0)
tmp->TM_ZONE = wildabbr;
else {
#ifdef ALL_STATE
if (gmtptr == NULL)
tmp->TM_ZONE = gmt;
else tmp->TM_ZONE = gmtptr->chars;
#endif /* defined ALL_STATE */
#ifndef ALL_STATE
tmp->TM_ZONE = gmtptr->chars;
#endif /* State Farm */
}
tmp->TM_ZONE = offset ? wildabbr : gmtptr ? gmtptr->chars : gmt;
#endif /* defined TM_ZONE */
return result;
}
Expand Down Expand Up @@ -1448,12 +1430,7 @@ timesub(const time_t *const timep, const int_fast32_t offset,

corr = 0;
hit = 0;
#ifdef ALL_STATE
i = (sp == NULL) ? 0 : sp->leapcnt;
#endif /* defined ALL_STATE */
#ifndef ALL_STATE
i = sp->leapcnt;
#endif /* State Farm */
while (--i >= 0) {
lp = &sp->lsis[i];
if (*timep >= lp->ls_trans) {
Expand Down Expand Up @@ -1827,10 +1804,8 @@ time2sub(struct tm *const tmp,
*/
sp = (const struct state *)
((funcp == localsub) ? lclptr : gmtptr);
#ifdef ALL_STATE
if (sp == NULL)
return WRONG;
#endif /* defined ALL_STATE */
for (i = sp->typecnt - 1; i >= 0; --i) {
if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
continue;
Expand Down Expand Up @@ -1903,17 +1878,15 @@ time1(struct tm *const tmp,
if (tmp->tm_isdst > 1)
tmp->tm_isdst = 1;
t = time2(tmp, funcp, offset, &okay);
#ifdef PCTS
/*
** PCTS code courtesy Grant Sullivan.
*/
if (okay)
return t;
if (tmp->tm_isdst < 0)
#ifdef PCTS
/*
** POSIX Conformance Test Suite code courtesy Grant Sullivan.
*/
tmp->tm_isdst = 0; /* reset to std and try again */
#endif /* defined PCTS */
#ifndef PCTS
if (okay || tmp->tm_isdst < 0)
#else
return t;
#endif /* !defined PCTS */
/*
Expand All @@ -1923,10 +1896,8 @@ time1(struct tm *const tmp,
** type they need.
*/
sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
#ifdef ALL_STATE
if (sp == NULL)
return WRONG;
#endif /* defined ALL_STATE */
for (i = 0; i < sp->typecnt; ++i)
seen[i] = FALSE;
nseen = 0;
Expand Down

0 comments on commit 8dbbeb5

Please sign in to comment.