diff --git a/libs/libc/time/lib_localtime.c b/libs/libc/time/lib_localtime.c index 5ff25f406e6..76f4fc8b86d 100644 --- a/libs/libc/time/lib_localtime.c +++ b/libs/libc/time/lib_localtime.c @@ -98,7 +98,7 @@ #define DAYSPERNYEAR 365 #define DAYSPERLYEAR 366 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR) -#define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY) +#define SECSPERDAY ((int_fast32_t)SECSPERHOUR * HOURSPERDAY) #define MONSPERYEAR 12 #define TM_SUNDAY 0 @@ -145,15 +145,15 @@ #define isleap_sum(a, b) isleap((a) % 400 + (b) % 400) #define GRANDPARENTED "Local time zone must be set--see zic manual page" -#define TYPE_BIT(type) (sizeof (type) * CHAR_BIT) -#define TYPE_SIGNED(type) (((type) -1) < 0) +#define TYPE_BIT(type) (sizeof(type) * CHAR_BIT) +#define TYPE_SIGNED(type) (((type)-1) < 0) #define YEARSPERREPEAT 400 /* years before a Gregorian repeat */ /* The Gregorian year averages 365.2425 days, which is 31556952 seconds. */ #define AVGSECSPERYEAR 31556952L -#define SECSPERREPEAT ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR) +#define SECSPERREPEAT ((int_fast64_t)YEARSPERREPEAT * (int_fast64_t)AVGSECSPERYEAR) #define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */ #define TZ_ABBR_MAX_LEN 16 @@ -306,14 +306,14 @@ struct rule_s /* The minimum and maximum finite time values. */ -static time_t const g_min_timet = +static const time_t g_min_timet = (TYPE_SIGNED(time_t) - ? (time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1) + ? (time_t)-1 << (CHAR_BIT * sizeof(time_t) - 1) : 0); -static time_t const g_max_timet = +static const time_t g_max_timet = (TYPE_SIGNED(time_t) - ? - (~ 0 < 0) - ((time_t) -1 << (CHAR_BIT * sizeof (time_t) - 1)) + ? - (~ 0 < 0) - ((time_t)-1 << (CHAR_BIT * sizeof(time_t) - 1)) : -1); static const char g_wildabbr[] = WILDABBR; @@ -364,9 +364,9 @@ FAR char *tzname[2] = static int_fast32_t detzcode(FAR const char *codep); static int_fast64_t detzcode64(FAR const char *codep); -static int differ_by_repeat(time_t t1, time_t t0); +static int differ_by_repeat(time_t t1, time_t t0); static FAR const char *getzname(FAR const char *strp); -static FAR const char *getqzname(FAR const char *strp, const int delim); +static FAR const char *getqzname(FAR const char *strp, int delim); static FAR const char *getnum(FAR const char *strp, FAR int *nump, int min, int max); static FAR const char *getsecs(FAR const char *strp, @@ -375,16 +375,16 @@ static FAR const char *getoffset(FAR const char *strp, FAR int_fast32_t *offsetp); static FAR const char *getrule(FAR const char *strp, FAR struct rule_s *rulep); -static void gmtload(struct state_s *sp); -static FAR struct tm *gmtsub(FAR const time_t * timep, int_fast32_t offset, +static void gmtload(FAR struct state_s *sp); +static FAR struct tm *gmtsub(FAR const time_t *timep, int_fast32_t offset, FAR struct tm *tmp); -static FAR struct tm *localsub(FAR const time_t * timep, int_fast32_t offset, +static FAR struct tm *localsub(FAR const time_t *timep, int_fast32_t offset, FAR struct tm *tmp); static int increment_overflow(FAR int *number, int delta); static int leaps_thru_end_of(int y); -static int increment_overflow32(FAR int_fast32_t * number, int delta); -static int increment_overflow_time(time_t * t, int_fast32_t delta); -static int normalize_overflow32(FAR int_fast32_t * tensptr, +static int increment_overflow32(FAR int_fast32_t *number, int delta); +static int increment_overflow_time(FAR time_t *t, int_fast32_t delta); +static int normalize_overflow32(FAR int_fast32_t *tensptr, FAR int *unitsptr, int base); static int normalize_overflow(FAR int *tensptr, FAR int *unitsptr, int base); @@ -401,7 +401,7 @@ static time_t time2sub(FAR struct tm *tmp, FAR struct tm *(*funcp)(FAR const time_t *, int_fast32_t, FAR struct tm *), int_fast32_t offset, FAR int *okayp, int do_norm_secs); -static FAR struct tm *timesub(FAR const time_t * timep, int_fast32_t offset, +static FAR struct tm *timesub(FAR const time_t *timep, int_fast32_t offset, FAR const struct state_s *sp, FAR struct tm *tmp); static int tmcomp(FAR const struct tm *atmp, FAR const struct tm *btmp); static int_fast32_t transtime(int year, FAR const struct rule_s *rulep, @@ -438,7 +438,7 @@ static void tz_semgive(FAR sem_t *sem) DEBUGVERIFY(_SEM_POST(sem)); } -static int_fast32_t detzcode(FAR const char *const codep) +static int_fast32_t detzcode(FAR const char *codep) { int_fast32_t result; int i; @@ -452,7 +452,7 @@ static int_fast32_t detzcode(FAR const char *const codep) return result; } -static int_fast64_t detzcode64(FAR const char *const codep) +static int_fast64_t detzcode64(FAR const char *codep) { int_fast64_t result; int i; @@ -482,14 +482,14 @@ static void settzname(void) for (i = 0; i < sp->typecnt; ++i) { - const struct ttinfo_s *const ttisp = &sp->ttis[i]; + FAR const struct ttinfo_s *const ttisp = &sp->ttis[i]; tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_abbrind]; } for (i = 0; i < sp->timecnt; ++i) { - const struct ttinfo_s *const ttisp = &sp->ttis[sp->types[i]]; + FAR const struct ttinfo_s *const ttisp = &sp->ttis[sp->types[i]]; tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_abbrind]; } @@ -508,8 +508,8 @@ static void settzname(void) for (i = 0; i < sp->typecnt; ++i) { - const struct ttinfo_s *const ttisp = &sp->ttis[i]; - char *cp = &sp->chars[ttisp->tt_abbrind]; + FAR const struct ttinfo_s *const ttisp = &sp->ttis[i]; + FAR char *cp = &sp->chars[ttisp->tt_abbrind]; if (strlen(cp) > TZ_ABBR_MAX_LEN && strcmp(cp, GRANDPARENTED) != 0) { @@ -518,7 +518,7 @@ static void settzname(void) } } -static int differ_by_repeat(const time_t t1, const time_t t0) +static int differ_by_repeat(time_t t1, time_t t0) { if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS) { @@ -529,7 +529,7 @@ static int differ_by_repeat(const time_t t1, const time_t t0) } static int tzload(FAR const char *name, - FAR struct state_s *const sp, const int doextend) + FAR struct state_s *sp, int doextend) { FAR const char *p; int i; @@ -539,7 +539,7 @@ static int tzload(FAR const char *name, typedef union { - struct tzhead_s tzhead_s; + struct tzhead_s tzhead; char buf[2 * sizeof(struct tzhead_s) + 2 * sizeof *sp + 4 * TZ_MAX_TIMES]; @@ -565,10 +565,10 @@ static int tzload(FAR const char *name, } u; }; - char *fullname; - u_t *up; + FAR char *fullname; + FAR u_t *up; int doaccess; - union local_storage *lsp; + FAR union local_storage *lsp; lsp = lib_malloc(sizeof *lsp); if (!lsp) @@ -642,14 +642,14 @@ static int tzload(FAR const char *name, int ttisgmtcnt; int timecnt; - ttisstdcnt = (int)detzcode(up->tzhead_s.tzh_ttisstdcnt); - ttisgmtcnt = (int)detzcode(up->tzhead_s.tzh_ttisgmtcnt); - sp->leapcnt = (int)detzcode(up->tzhead_s.tzh_leapcnt); - sp->timecnt = (int)detzcode(up->tzhead_s.tzh_timecnt); - sp->typecnt = (int)detzcode(up->tzhead_s.tzh_typecnt); - sp->charcnt = (int)detzcode(up->tzhead_s.tzh_charcnt); + ttisstdcnt = (int)detzcode(up->tzhead.tzh_ttisstdcnt); + ttisgmtcnt = (int)detzcode(up->tzhead.tzh_ttisgmtcnt); + sp->leapcnt = (int)detzcode(up->tzhead.tzh_leapcnt); + sp->timecnt = (int)detzcode(up->tzhead.tzh_timecnt); + sp->typecnt = (int)detzcode(up->tzhead.tzh_typecnt); + sp->charcnt = (int)detzcode(up->tzhead.tzh_charcnt); - p = up->tzhead_s.tzh_charcnt + sizeof up->tzhead_s.tzh_charcnt; + p = up->tzhead.tzh_charcnt + sizeof up->tzhead.tzh_charcnt; if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || @@ -715,7 +715,7 @@ static int tzload(FAR const char *name, sp->timecnt = timecnt; for (i = 0; i < sp->typecnt; ++i) { - struct ttinfo_s *ttisp; + FAR struct ttinfo_s *ttisp; ttisp = &sp->ttis[i]; ttisp->tt_gmtoff = detzcode(p); @@ -741,7 +741,7 @@ static int tzload(FAR const char *name, sp->chars[i] = '\0'; /* ensure '\0' at end */ for (i = 0; i < sp->leapcnt; ++i) { - struct lsinfo_s *lsisp; + FAR struct lsinfo_s *lsisp; lsisp = &sp->lsis[i]; lsisp->ls_trans = (stored == 4) ? detzcode(p) : detzcode64(p); @@ -752,7 +752,7 @@ static int tzload(FAR const char *name, for (i = 0; i < sp->typecnt; ++i) { - struct ttinfo_s *ttisp; + FAR struct ttinfo_s *ttisp; ttisp = &sp->ttis[i]; if (ttisstdcnt == 0) @@ -771,7 +771,7 @@ static int tzload(FAR const char *name, for (i = 0; i < sp->typecnt; ++i) { - struct ttinfo_s *ttisp; + FAR struct ttinfo_s *ttisp; ttisp = &sp->ttis[i]; if (ttisgmtcnt == 0) @@ -790,7 +790,7 @@ static int tzload(FAR const char *name, /* If this is an old file, we're done. */ - if (up->tzhead_s.tzh_version[0] == '\0') + if (up->tzhead.tzh_version[0] == '\0') { break; } @@ -923,16 +923,15 @@ static int tzload(FAR const char *name, } sp->defaulttype = i; - lib_free(up); + lib_free(lsp); return 0; oops: - lib_free(up); + lib_free(lsp); return -1; } -static int typesequiv(FAR const struct state_s *const sp, const int a, - const int b) +static int typesequiv(FAR const struct state_s *sp, int a, int b) { int result; @@ -942,8 +941,9 @@ static int typesequiv(FAR const struct state_s *const sp, const int a, } else { - const struct ttinfo_s *ap = &sp->ttis[a]; - const struct ttinfo_s *bp = &sp->ttis[b]; + FAR const struct ttinfo_s *ap = &sp->ttis[a]; + FAR const struct ttinfo_s *bp = &sp->ttis[b]; + result = ap->tt_gmtoff == bp->tt_gmtoff && ap->tt_isdst == bp->tt_isdst && ap->tt_ttisstd == bp->tt_ttisstd && @@ -980,7 +980,7 @@ static FAR const char *getzname(FAR const char *strp) * We don't do any checking here; checking is done later in common-case code. */ -static FAR const char *getqzname(FAR const char *strp, const int delim) +static FAR const char *getqzname(FAR const char *strp, int delim) { int c; @@ -998,8 +998,8 @@ static FAR const char *getqzname(FAR const char *strp, const int delim) * of the number. */ -static FAR const char *getnum(FAR const char *strp, FAR int *const nump, - const int min, const int max) +static FAR const char *getnum(FAR const char *strp, FAR int *nump, + int min, int max) { char c; int num; @@ -1039,7 +1039,7 @@ static FAR const char *getnum(FAR const char *strp, FAR int *const nump, */ static FAR const char *getsecs(FAR const char *strp, - FAR int_fast32_t * const secsp) + FAR int_fast32_t *secsp) { int num; @@ -1055,7 +1055,7 @@ static FAR const char *getsecs(FAR const char *strp, return NULL; } - *secsp = num * (int_fast32_t) SECSPERHOUR; + *secsp = num * (int_fast32_t)SECSPERHOUR; if (*strp == ':') { ++strp; @@ -1092,7 +1092,7 @@ static FAR const char *getsecs(FAR const char *strp, */ static FAR const char *getoffset(FAR const char *strp, - FAR int_fast32_t * const offsetp) + FAR int_fast32_t *offsetp) { int neg = 0; @@ -1127,7 +1127,7 @@ static FAR const char *getoffset(FAR const char *strp, */ static FAR const char *getrule(FAR const char *strp, - FAR struct rule_s *const rulep) + FAR struct rule_s *rulep) { if (*strp == 'J') { @@ -1203,14 +1203,19 @@ static FAR const char *getrule(FAR const char *strp, * effect, calculate the year-relative time that rule takes effect. */ -static int_fast32_t transtime(const int year, - FAR const struct rule_s *const rulep, - const int_fast32_t offset) +static int_fast32_t transtime(int year, + FAR const struct rule_s *rulep, + int_fast32_t offset) { int leapyear; int_fast32_t value; int i; - int d, m1, yy0, yy1, yy2, dow; + int d; + int m1; + int yy0; + int yy1; + int yy2; + int dow; value = 0; leapyear = isleap(year); @@ -1305,8 +1310,8 @@ static int_fast32_t transtime(const int year, * appropriate. */ -static int tzparse(FAR const char *name, FAR struct state_s *const sp, - const int lastditch) +static int tzparse(FAR const char *name, FAR struct state_s *sp, + int lastditch) { FAR const char *stdname; FAR const char *dstname; @@ -1314,7 +1319,7 @@ static int tzparse(FAR const char *name, FAR struct state_s *const sp, size_t dstlen; int_fast32_t stdoffset; int_fast32_t dstoffset; - char *cp; + FAR char *cp; int load_result; static struct ttinfo_s zttinfo; @@ -1631,7 +1636,7 @@ static int tzparse(FAR const char *name, FAR struct state_s *const sp, sp->charcnt += dstlen + 1; } - if ((size_t) sp->charcnt > sizeof sp->chars) + if ((size_t)sp->charcnt > sizeof sp->chars) { return -1; } @@ -1649,7 +1654,7 @@ static int tzparse(FAR const char *name, FAR struct state_s *const sp, return 0; } -static void gmtload(FAR struct state_s *const sp) +static void gmtload(FAR struct state_s *sp) { if (tzload(GMT, sp, TRUE) != 0) { @@ -1691,13 +1696,13 @@ static void tzsetwall(void) * The unused offset argument is for the benefit of mktime variants. */ -static struct tm *localsub(FAR const time_t * const timep, - const int_fast32_t offset, struct tm *const tmp) +static FAR struct tm *localsub(FAR const time_t *timep, + int_fast32_t offset, FAR struct tm *tmp) { FAR struct state_s *sp; - const struct ttinfo_s *ttisp; + FAR const struct ttinfo_s *ttisp; int i; - struct tm *result; + FAR struct tm *result; const time_t t = *timep; sp = g_lcl_ptr; @@ -1807,8 +1812,8 @@ static struct tm *localsub(FAR const time_t * const timep, /* gmtsub is to gmtime as localsub is to localtime */ -static struct tm *gmtsub(FAR const time_t * const timep, - const int_fast32_t offset, struct tm *const tmp) +static FAR struct tm *gmtsub(FAR const time_t *timep, + int_fast32_t offset, FAR struct tm *tmp) { tz_semtake(&g_gmt_sem); @@ -1831,18 +1836,18 @@ static struct tm *gmtsub(FAR const time_t * const timep, * where, to make the math easy, the answer for year zero is defined as zero. */ -static int leaps_thru_end_of(const int y) +static int leaps_thru_end_of(int y) { return (y >= 0) ? (y / 4 - y / 100 + y / 400) : -(leaps_thru_end_of(-(y + 1)) + 1); } -static struct tm *timesub(FAR const time_t * const timep, - const int_fast32_t offset, - FAR const struct state_s *const sp, - struct tm *const tmp) +static FAR struct tm *timesub(FAR const time_t *timep, + int_fast32_t offset, + FAR const struct state_s *sp, + FAR struct tm *tmp) { - const struct lsinfo_s *lp; + FAR const struct lsinfo_s *lp; time_t tdays; int idays; /* unsigned would be so 2003 */ int_fast32_t rem; @@ -1912,7 +1917,7 @@ static struct tm *timesub(FAR const time_t * const timep, } leapdays = leaps_thru_end_of(newy - 1) - leaps_thru_end_of(y - 1); - tdays -= ((time_t) newy - y) * DAYSPERNYEAR; + tdays -= ((time_t)newy - y) * DAYSPERNYEAR; tdays -= leapdays; y = newy; } @@ -2013,9 +2018,9 @@ static struct tm *timesub(FAR const time_t * const timep, /* Normalize logic courtesy Paul Eggert */ -static int increment_overflow(FAR int *const ip, int j) +static int increment_overflow(FAR int *ip, int j) { - int const i = *ip; + const int i = *ip; /* If i >= 0 there can only be overflow if i + j > INT_MAX * or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow. @@ -2032,9 +2037,9 @@ static int increment_overflow(FAR int *const ip, int j) return FALSE; } -static int increment_overflow32(FAR int_fast32_t * const lp, int const m) +static int increment_overflow32(FAR int_fast32_t *lp, int m) { - int_fast32_t const l = *lp; + const int_fast32_t l = *lp; if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l)) { @@ -2045,7 +2050,7 @@ static int increment_overflow32(FAR int_fast32_t * const lp, int const m) return FALSE; } -static int increment_overflow_time(time_t * tp, int_fast32_t j) +static int increment_overflow_time(FAR time_t *tp, int_fast32_t j) { /* This is like * 'if (! (g_min_timet <= *tp + j && *tp + j <= g_max_timet)) ...', @@ -2063,8 +2068,8 @@ static int increment_overflow_time(time_t * tp, int_fast32_t j) return FALSE; } -static int normalize_overflow(FAR int *const tensptr, - FAR int *const unitsptr, const int base) +static int normalize_overflow(FAR int *tensptr, + FAR int *unitsptr, int base) { int tensdelta; @@ -2074,8 +2079,8 @@ static int normalize_overflow(FAR int *const tensptr, return increment_overflow(tensptr, tensdelta); } -static int normalize_overflow32(FAR int_fast32_t * const tensptr, - FAR int *const unitsptr, const int base) +static int normalize_overflow32(FAR int_fast32_t *tensptr, + FAR int *unitsptr, int base) { int tensdelta; @@ -2105,15 +2110,16 @@ static int tmcomp(FAR const struct tm *atmp, FAR const struct tm *btmp) return result; } -static time_t time2sub(struct tm *const tmp, - FAR struct tm *(*const funcp)(FAR const time_t *, - int_fast32_t, struct tm *), - const int_fast32_t offset, FAR int *const okayp, - const int do_norm_secs) +static time_t time2sub(FAR struct tm *tmp, + FAR struct tm *(*funcp)(FAR const time_t *, + int_fast32_t, FAR struct tm *), + int_fast32_t offset, FAR int *okayp, + int do_norm_secs) { FAR const struct state_s *sp; int dir; - int i, j; + int i; + int j; int saved_seconds; int_fast32_t li; time_t lo; @@ -2121,7 +2127,8 @@ static time_t time2sub(struct tm *const tmp, int_fast32_t y; time_t newt; time_t t; - struct tm yourtm, mytm; + struct tm yourtm; + struct tm mytm; *okayp = FALSE; yourtm = *tmp; @@ -2395,10 +2402,10 @@ label: return t; } -static time_t time2(FAR struct tm *const tmp, - FAR struct tm *(*const funcp)(FAR const time_t *, - int_fast32_t, struct tm *), - const int_fast32_t offset, FAR int *const okayp) +static time_t time2(FAR struct tm *tmp, + FAR struct tm *(*funcp)(FAR const time_t *, + int_fast32_t, FAR struct tm *), + int_fast32_t offset, FAR int *okayp) { time_t t; @@ -2411,15 +2418,17 @@ static time_t time2(FAR struct tm *const tmp, return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE); } -static time_t time1(FAR struct tm *const tmp, - FAR struct tm *(*const funcp)(FAR const time_t *, +static time_t time1(FAR struct tm *tmp, + FAR struct tm *(*funcp)(FAR const time_t *, int_fast32_t, FAR struct tm *), - const int_fast32_t offset) + int_fast32_t offset) { time_t t; FAR const struct state_s *sp; - int samei, otheri; - int sameind, otherind; + int samei; + int otheri; + int sameind; + int otherind; int i; int nseen; char seen[TZ_MAX_TYPES]; @@ -2573,7 +2582,7 @@ out: tz_semgive(&g_lcl_sem); } -FAR struct tm *localtime(FAR const time_t * const timep) +FAR struct tm *localtime(FAR const time_t *timep) { tzset(); return localsub(timep, 0L, &g_tm); @@ -2581,25 +2590,25 @@ FAR struct tm *localtime(FAR const time_t * const timep) /* Re-entrant version of localtime */ -FAR struct tm *localtime_r(FAR const time_t * const timep, struct tm *tmp) +FAR struct tm *localtime_r(FAR const time_t *timep, FAR struct tm *tmp) { tzset(); return localsub(timep, 0L, tmp); } -FAR struct tm *gmtime(FAR const time_t * const timep) +FAR struct tm *gmtime(FAR const time_t *timep) { return gmtsub(timep, 0L, &g_tm); } /* Re-entrant version of gmtime */ -FAR struct tm *gmtime_r(FAR const time_t * const timep, struct tm *tmp) +FAR struct tm *gmtime_r(FAR const time_t *timep, FAR struct tm *tmp) { return gmtsub(timep, 0L, tmp); } -time_t mktime(struct tm * const tmp) +time_t mktime(FAR struct tm *tmp) { tzset(); return time1(tmp, localsub, 0L);