From 77435397cb5e0aeb1a93bcdd948e2afc7553b951 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Mon, 8 Aug 2016 18:25:17 -0400 Subject: [PATCH] Handle fractional seconds and offsets better. --- tz.h | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/tz.h b/tz.h index a4334f4..403a59e 100644 --- a/tz.h +++ b/tz.h @@ -1600,10 +1600,35 @@ parse(std::basic_istream& is, } if (ratio_less>::value) { - double s; - is >> s; - if (!is.fail()) - subseconds = round(duration{s}); + CharT decimal_point = + use_facet>(is.getloc()).decimal_point(); + string buf; + while (true) + { + auto k = is.peek(); + if (Traits::eq_int_type(k, Traits::eof())) + break; + if (k == decimal_point) + { + buf += '.'; + is.get(); + } + else + { + auto c = static_cast(Traits::to_char_type(k)); + if (isdigit(c)) + { + buf += c; + is.get(); + } + else + { + break; + } + } + }; + if (!buf.empty()) + subseconds = round(duration{stod(buf)}); else err |= ios_base::failbit; } @@ -1628,16 +1653,25 @@ parse(std::basic_istream& is, if (!is.fail() && (sign == '+' || sign == '-')) { char h1, h0, m1, m0; - char colon = '\0'; + char colon = ':'; h1 = static_cast(is.get()); h0 = static_cast(is.get()); if (modified) - colon = static_cast(is.get()); + { + if (h0 == ':') + { + colon = h0; + h0 = h1; + h1 = '0'; + } + else + colon = static_cast(is.get()); + } m1 = static_cast(is.get()); m0 = static_cast(is.get()); if (!is.fail() && std::isdigit(h1) && std::isdigit(h0) && std::isdigit(m1) && std::isdigit(m0) - && (!modified || colon == ':')) + && colon == ':') { temp_offset = 10*hours{h1 - '0'} + hours{h0 - '0'} + 10*minutes{m1 - '0'} + minutes{m0 - '0'};