time_zone_fixed.cc
Go to the documentation of this file.
1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "time_zone_fixed.h"
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <chrono>
20 #include <cstring>
21 #include <string>
22 
23 namespace absl {
24 namespace time_internal {
25 namespace cctz {
26 
27 namespace {
28 
29 // The prefix used for the internal names of fixed-offset zones.
30 const char kFixedOffsetPrefix[] = "Fixed/UTC";
31 
32 const char kDigits[] = "0123456789";
33 
34 char* Format02d(char* p, int v) {
35  *p++ = kDigits[(v / 10) % 10];
36  *p++ = kDigits[v % 10];
37  return p;
38 }
39 
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);
45  }
46  }
47  return -1;
48 }
49 
50 } // namespace
51 
52 bool FixedOffsetFromName(const std::string& name, seconds* offset) {
53  if (name.compare(0, std::string::npos, "UTC", 3) == 0) {
54  *offset = seconds::zero();
55  return true;
56  }
57 
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) // <prefix>+99:99:99
61  return false;
62  if (!std::equal(kFixedOffsetPrefix, ep, name.begin()))
63  return false;
64  const char* np = name.data() + prefix_len;
65  if (np[0] != '+' && np[0] != '-')
66  return false;
67  if (np[3] != ':' || np[6] != ':') // see note below about large offsets
68  return false;
69 
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;
76 
77  secs += ((hours * 60) + mins) * 60;
78  if (secs > 24 * 60 * 60) return false; // outside supported offset range
79  *offset = seconds(secs * (np[0] == '-' ? -1 : 1)); // "-" means west
80  return true;
81 }
82 
83 std::string FixedOffsetToName(const seconds& offset) {
84  if (offset == seconds::zero()) return "UTC";
85  if (offset < std::chrono::hours(-24) || offset > std::chrono::hours(24)) {
86  // We don't support fixed-offset zones more than 24 hours
87  // away from UTC to avoid complications in rendering such
88  // offsets and to (somewhat) limit the total number of zones.
89  return "UTC";
90  }
91  int seconds = static_cast<int>(offset.count());
92  const char sign = (seconds < 0 ? '-' : '+');
93  int minutes = seconds / 60;
94  seconds %= 60;
95  if (sign == '-') {
96  if (seconds > 0) {
97  seconds -= 60;
98  minutes += 1;
99  }
100  seconds = -seconds;
101  minutes = -minutes;
102  }
103  int hours = minutes / 60;
104  minutes %= 60;
105  char buf[sizeof(kFixedOffsetPrefix) - 1 + sizeof("-24:00:00")];
106  std::strcpy(buf, kFixedOffsetPrefix);
107  char* ep = buf + sizeof(kFixedOffsetPrefix) - 1;
108  *ep++ = sign;
109  ep = Format02d(ep, hours);
110  *ep++ = ':';
111  ep = Format02d(ep, minutes);
112  *ep++ = ':';
113  ep = Format02d(ep, seconds);
114  *ep++ = '\0';
115  assert(ep == buf + sizeof(buf));
116  return buf;
117 }
118 
119 std::string FixedOffsetToAbbr(const seconds& offset) {
120  std::string abbr = FixedOffsetToName(offset);
121  const std::size_t prefix_len = sizeof(kFixedOffsetPrefix) - 1;
122  if (abbr.size() == prefix_len + 9) { // <prefix>+99:99:99
123  abbr.erase(0, prefix_len); // +99:99:99
124  abbr.erase(6, 1); // +99:9999
125  abbr.erase(3, 1); // +999999
126  if (abbr[5] == '0' && abbr[6] == '0') { // +999900
127  abbr.erase(5, 2); // +9999
128  if (abbr[3] == '0' && abbr[4] == '0') { // +9900
129  abbr.erase(3, 2); // +99
130  }
131  }
132  }
133  return abbr;
134 }
135 
136 } // namespace cctz
137 } // namespace time_internal
138 } // namespace absl
int v
Definition: variant_test.cc:81
const char * abbr
Definition: duration.cc:706
std::string FixedOffsetToAbbr(const seconds &offset)
char buf[N]
std::chrono::duration< std::int_fast64_t > seconds
Definition: time_zone.h:37
Definition: algorithm.h:29
char name[1]
Definition: mutex.cc:296
std::string FixedOffsetToName(const seconds &offset)
bool FixedOffsetFromName(const std::string &name, seconds *offset)


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:58