24 namespace time_internal {
30 const char kFixedOffsetPrefix[] =
"Fixed/UTC";
32 const char kDigits[] =
"0123456789";
34 char* Format02d(
char* p,
int v) {
35 *p++ = kDigits[(v / 10) % 10];
36 *p++ = kDigits[v % 10];
40 int Parse02d(
const char* p) {
41 if (
const char* ap = std::strchr(kDigits, *p)) {
42 int v =
static_cast<int>(ap - kDigits);
43 if (
const char* bp = std::strchr(kDigits, *++p)) {
44 return (v * 10) +
static_cast<int>(bp - kDigits);
53 if (name.compare(0, std::string::npos,
"UTC", 3) == 0) {
54 *offset = seconds::zero();
58 const std::size_t prefix_len =
sizeof(kFixedOffsetPrefix) - 1;
59 const char*
const ep = kFixedOffsetPrefix + prefix_len;
60 if (name.size() != prefix_len + 9)
62 if (!std::equal(kFixedOffsetPrefix, ep, name.begin()))
64 const char* np = name.data() + prefix_len;
65 if (np[0] !=
'+' && np[0] !=
'-')
67 if (np[3] !=
':' || np[6] !=
':')
70 int hours = Parse02d(np + 1);
71 if (hours == -1)
return false;
72 int mins = Parse02d(np + 4);
73 if (mins == -1)
return false;
74 int secs = Parse02d(np + 7);
75 if (secs == -1)
return false;
77 secs += ((hours * 60) + mins) * 60;
78 if (secs > 24 * 60 * 60)
return false;
79 *offset =
seconds(secs * (np[0] ==
'-' ? -1 : 1));
84 if (offset == seconds::zero())
return "UTC";
85 if (offset < std::chrono::hours(-24) || offset > std::chrono::hours(24)) {
91 int seconds =
static_cast<int>(offset.count());
92 const char sign = (seconds < 0 ?
'-' :
'+');
93 int minutes = seconds / 60;
103 int hours = minutes / 60;
105 char buf[
sizeof(kFixedOffsetPrefix) - 1 +
sizeof(
"-24:00:00")];
106 std::strcpy(buf, kFixedOffsetPrefix);
107 char* ep = buf +
sizeof(kFixedOffsetPrefix) - 1;
109 ep = Format02d(ep, hours);
111 ep = Format02d(ep, minutes);
113 ep = Format02d(ep, seconds);
115 assert(ep == buf +
sizeof(buf));
121 const std::size_t prefix_len =
sizeof(kFixedOffsetPrefix) - 1;
122 if (abbr.size() == prefix_len + 9) {
123 abbr.erase(0, prefix_len);
126 if (abbr[5] ==
'0' && abbr[6] ==
'0') {
128 if (abbr[3] ==
'0' && abbr[4] ==
'0') {
std::string FixedOffsetToAbbr(const seconds &offset)
std::chrono::duration< std::int_fast64_t > seconds
std::string FixedOffsetToName(const seconds &offset)
bool FixedOffsetFromName(const std::string &name, seconds *offset)