Donny9 commented on code in PR #6783: URL: https://github.com/apache/incubator-nuttx/pull/6783#discussion_r998799660
########## libs/libc/time/lib_localtime.c: ########## @@ -412,40 +423,119 @@ static int typesequiv(FAR const struct state_s *sp, int a, int b); static int tzload(FAR const char *name, FAR struct state_s *sp, int doextend); static int tzparse(FAR const char *name, FAR struct state_s *sp, - int lastditch); + FAR struct state_s *basep); /**************************************************************************** * Private Functions ****************************************************************************/ +/* Initialize *S to a value based on UTOFF, ISDST, and DESIGIDX. */ + +static void init_ttinfo(FAR struct ttinfo_s *s, int_fast32_t utoff, + bool isdst, int desigidx) +{ + s->tt_utoff = utoff; + s->tt_isdst = isdst; + s->tt_desigidx = desigidx; + s->tt_ttisstd = false; + s->tt_ttisut = false; +} + +/* Return true if SP's time type I does not specify local time. */ + +static int ttunspecified(FAR struct state_s const *sp, int i) Review Comment: Done! ########## libs/libc/time/lib_localtime.c: ########## @@ -412,40 +423,119 @@ static int typesequiv(FAR const struct state_s *sp, int a, int b); static int tzload(FAR const char *name, FAR struct state_s *sp, int doextend); static int tzparse(FAR const char *name, FAR struct state_s *sp, - int lastditch); + FAR struct state_s *basep); /**************************************************************************** * Private Functions ****************************************************************************/ +/* Initialize *S to a value based on UTOFF, ISDST, and DESIGIDX. */ + +static void init_ttinfo(FAR struct ttinfo_s *s, int_fast32_t utoff, + bool isdst, int desigidx) +{ + s->tt_utoff = utoff; + s->tt_isdst = isdst; + s->tt_desigidx = desigidx; + s->tt_ttisstd = false; + s->tt_ttisut = false; +} + +/* Return true if SP's time type I does not specify local time. */ + +static int ttunspecified(FAR struct state_s const *sp, int i) +{ + FAR char const *abbr = &sp->chars[sp->ttis[i].tt_desigidx]; + + /* memcmp is likely faster than strcmp, and is safe due to CHARS_EXTRA. */ + + return memcmp(abbr, UNSPEC, sizeof UNSPEC) == 0; +} + static int_fast32_t detzcode(FAR const char *codep) { int_fast32_t result; + int_fast32_t one = 1; + int_fast32_t halfmaxval = one << (32 - 2); + int_fast32_t maxval = halfmaxval - 1 + halfmaxval; + int_fast32_t minval = -1 - maxval; int i; - result = (codep[0] & 0x80) ? -1 : 0; - for (i = 0; i < 4; ++i) + result = codep[0] & 0x7f; + for (i = 1; i < 4; ++i) { result = (result << 8) | (codep[i] & 0xff); } + if (codep[0] & 0x80) + { + /* Do two's-complement negation even on non-two's-complement machines. + * If the result would be minval - 1, return minval. + */ + + result -= !TWOS_COMPLEMENT(int_fast32_t) && result != 0; + result += minval; + } + return result; } static int_fast64_t detzcode64(FAR const char *codep) { int_fast64_t result; + int_fast64_t one = 1; + int_fast64_t halfmaxval = one << (64 - 2); + int_fast64_t maxval = halfmaxval - 1 + halfmaxval; + int_fast64_t minval = -TWOS_COMPLEMENT(int_fast64_t) - maxval; int i; - result = (codep[0] & 0x80) ? -1 : 0; - for (i = 0; i < 8; ++i) + result = codep[0] & 0x7f; + for (i = 1; i < 8; ++i) { - result = (result * 256) | (codep[i] & 0xff); + result = (result << 8) | (codep[i] & 0xff); + } + + if (codep[0] & 0x80) + { + /* Do two's-complement negation even on non-two's-complement machines. + * If the result would be minval - 1, return minval. + */ + + result -= !TWOS_COMPLEMENT(int_fast64_t) && result != 0; + result += minval; } return result; } +static void scrub_abbrs(struct state_s *sp) +{ + int i; + + /* First, replace bogus characters. */ + + for (i = 0; i < sp->charcnt; ++i) + { + if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL) + { + sp->chars[i] = TZ_ABBR_ERR_CHAR; + } + } + + /* Second, truncate long abbreviations. */ + + for (i = 0; i < sp->typecnt; ++i) + { + FAR const struct ttinfo_s *const ttisp = &sp->ttis[i]; Review Comment: Done! ########## libs/libc/time/lib_localtime.c: ########## @@ -464,64 +554,48 @@ static void settzname(void) { FAR const struct ttinfo_s *const ttisp = &sp->ttis[i]; - tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_abbrind]; + tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_desigidx]; } for (i = 0; i < sp->timecnt; ++i) { FAR const struct ttinfo_s *const ttisp = &sp->ttis[sp->types[i]]; - tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_abbrind]; - } - - /* Finally, scrub the abbreviations. First, replace bogus characters. */ - - for (i = 0; i < sp->charcnt; ++i) - { - if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL) - { - sp->chars[i] = TZ_ABBR_ERR_CHAR; - } + tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_desigidx]; } +} - /* Second, truncate long abbreviations. */ +static int_fast32_t leapcorr(FAR struct state_s const *sp, time_t t) Review Comment: Done! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org