MCPcopy Create free account
hub / github.com/eggert/tz / gethms

Function gethms

zic.c:2059–2115  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2057 if (wantcont)
2058 error(N_("expected continuation line not found"));
2059}
2060
2061/*
2062** Convert a string of one of the forms
2063** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
2064** into a number of seconds.
2065** A null string maps to zero.
2066** Call error with msgid and return zero on errors.
2067*/
2068
2069static zic_t
2070gethms(char const *string, char const *msgid)
2071{
2072 zic_t hh, r;
2073 int sign, mm = 0, ss = 0;
2074 char hhx, mmx, ssx, xr = '0', xs;
2075 int tenths = 0;
2076 bool ok = true;
2077
2078 if (string == NULL || *string == '\0')
2079 return 0;
2080 if (*string == '-') {
2081 sign = -1;
2082 ++string;
2083 } else sign = 1;
2084 switch (sscanf(string,
2085 "%"SCNdZIC"%c%d%c%d%c%1d%*[0]%c%*[0123456789]%c",
2086 &hh, &hhx, &mm, &mmx, &ss, &ssx, &tenths, &xr, &xs)) {
2087 default: ok = false; break;
2088 case 8:
2089 ok = is_digit(xr);
2090 ATTRIBUTE_FALLTHROUGH;
2091 case 7:
2092 ok &= ssx == '.';
2093 if (ok && noise)
2094 warning(N_("fractional seconds rejected by"
2095 " pre-2018 versions of zic"));
2096 ATTRIBUTE_FALLTHROUGH;
2097 case 5: ok &= mmx == ':'; ATTRIBUTE_FALLTHROUGH;
2098 case 3: ok &= hhx == ':'; ATTRIBUTE_FALLTHROUGH;
2099 case 1: break;
2100 }
2101 if (!ok || hh < 0 ||
2102 mm < 0 || mm >= MINSPERHOUR ||
2103 ss < 0 || ss > SECSPERMIN) {
2104 error(N_("%s: %s"), string, _(msgid));
2105 return 0;
2106 }
2107 ss += 5 + ((ss ^ 1) & (xr == '0')) <= tenths; /* Round to even. */
2108 if (noise && (hh > HOURSPERDAY ||
2109 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
2110 warning(N_("values over 24 hours"
2111 " not handled by pre-2007 versions of zic"));
2112 r = oadd(omul(hh, sign * SECSPERHOUR), sign * (mm * SECSPERMIN + ss));
2113
2114 /* Check that 4 * R fits in zic_t. This is more than enough
2115 for times of day and for UT offsets in TZif files, and it
2116 avoids later problems during arithmetic on these numbers,

Callers 4

getsaveFunction · 0.85
inzsubFunction · 0.85
getleapdatetimeFunction · 0.85
rulesubFunction · 0.85

Calls 5

warningFunction · 0.85
errorFunction · 0.85
oaddFunction · 0.85
omulFunction · 0.85
is_digitFunction · 0.70

Tested by

no test coverage detected