diff options
Diffstat (limited to 'absl/time/internal/cctz/src/time_zone_fixed.cc')
-rw-r--r-- | absl/time/internal/cctz/src/time_zone_fixed.cc | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/absl/time/internal/cctz/src/time_zone_fixed.cc b/absl/time/internal/cctz/src/time_zone_fixed.cc index 4b608f68..070abd26 100644 --- a/absl/time/internal/cctz/src/time_zone_fixed.cc +++ b/absl/time/internal/cctz/src/time_zone_fixed.cc @@ -15,13 +15,13 @@ #include "time_zone_fixed.h" #include <algorithm> +#include <cassert> #include <chrono> -#include <cstdio> #include <cstring> #include <string> namespace absl { -inline namespace lts_2018_06_20 { +inline namespace lts_2018_12_18 { namespace time_internal { namespace cctz { @@ -30,8 +30,15 @@ namespace { // The prefix used for the internal names of fixed-offset zones. const char kFixedOffsetPrefix[] = "Fixed/UTC"; +const char kDigits[] = "0123456789"; + +char* Format02d(char* p, int v) { + *p++ = kDigits[(v / 10) % 10]; + *p++ = kDigits[v % 10]; + return p; +} + int Parse02d(const char* p) { - static const char kDigits[] = "0123456789"; if (const char* ap = std::strchr(kDigits, *p)) { int v = static_cast<int>(ap - kDigits); if (const char* bp = std::strchr(kDigits, *++p)) { @@ -43,9 +50,9 @@ int Parse02d(const char* p) { } // namespace -bool FixedOffsetFromName(const std::string& name, sys_seconds* offset) { +bool FixedOffsetFromName(const std::string& name, seconds* offset) { if (name.compare(0, std::string::npos, "UTC", 3) == 0) { - *offset = sys_seconds::zero(); + *offset = seconds::zero(); return true; } @@ -70,12 +77,12 @@ bool FixedOffsetFromName(const std::string& name, sys_seconds* offset) { secs += ((hours * 60) + mins) * 60; if (secs > 24 * 60 * 60) return false; // outside supported offset range - *offset = sys_seconds(secs * (np[0] == '-' ? -1 : 1)); // "-" means west + *offset = seconds(secs * (np[0] == '-' ? -1 : 1)); // "-" means west return true; } -std::string FixedOffsetToName(const sys_seconds& offset) { - if (offset == sys_seconds::zero()) return "UTC"; +std::string FixedOffsetToName(const seconds& offset) { + if (offset == seconds::zero()) return "UTC"; if (offset < std::chrono::hours(-24) || offset > std::chrono::hours(24)) { // We don't support fixed-offset zones more than 24 hours // away from UTC to avoid complications in rendering such @@ -96,13 +103,21 @@ std::string FixedOffsetToName(const sys_seconds& offset) { } int hours = minutes / 60; minutes %= 60; - char buf[sizeof(kFixedOffsetPrefix) + sizeof("-24:00:00")]; - snprintf(buf, sizeof(buf), "%s%c%02d:%02d:%02d", - kFixedOffsetPrefix, sign, hours, minutes, seconds); + char buf[sizeof(kFixedOffsetPrefix) - 1 + sizeof("-24:00:00")]; + std::strcpy(buf, kFixedOffsetPrefix); + char* ep = buf + sizeof(kFixedOffsetPrefix) - 1; + *ep++ = sign; + ep = Format02d(ep, hours); + *ep++ = ':'; + ep = Format02d(ep, minutes); + *ep++ = ':'; + ep = Format02d(ep, seconds); + *ep++ = '\0'; + assert(ep == buf + sizeof(buf)); return buf; } -std::string FixedOffsetToAbbr(const sys_seconds& offset) { +std::string FixedOffsetToAbbr(const seconds& offset) { std::string abbr = FixedOffsetToName(offset); const std::size_t prefix_len = sizeof(kFixedOffsetPrefix) - 1; if (abbr.size() == prefix_len + 9) { // <prefix>+99:99:99 @@ -121,5 +136,5 @@ std::string FixedOffsetToAbbr(const sys_seconds& offset) { } // namespace cctz } // namespace time_internal -} // inline namespace lts_2018_06_20 +} // inline namespace lts_2018_12_18 } // namespace absl |