Coverage Report

Created: 2025-11-11 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cctz/src/time_zone_fixed.cc
Line
Count
Source
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 cctz {
24
25
namespace {
26
27
// The prefix used for the internal names of fixed-offset zones.
28
const char kFixedZonePrefix[] = "Fixed/UTC";
29
30
const char kDigits[] = "0123456789";
31
32
693
char* Format02d(char* p, int v) {
33
693
  *p++ = kDigits[(v / 10) % 10];
34
693
  *p++ = kDigits[v % 10];
35
693
  return p;
36
693
}
37
38
1.63k
int Parse02d(const char* p) {
39
1.63k
  if (const char* ap = std::strchr(kDigits, *p)) {
40
1.63k
    int v = static_cast<int>(ap - kDigits);
41
1.63k
    if (const char* bp = std::strchr(kDigits, *++p)) {
42
1.61k
      return (v * 10) + static_cast<int>(bp - kDigits);
43
1.61k
    }
44
1.63k
  }
45
20
  return -1;
46
1.63k
}
47
48
}  // namespace
49
50
9.41k
bool FixedOffsetFromName(const std::string& name, seconds* offset) {
51
9.41k
  if (name == "UTC" || name == "UTC0") {
52
264
    *offset = seconds::zero();
53
264
    return true;
54
264
  }
55
56
9.15k
  const std::size_t prefix_len = sizeof(kFixedZonePrefix) - 1;
57
9.15k
  const char* const ep = kFixedZonePrefix + prefix_len;
58
9.15k
  if (name.size() != prefix_len + 9)  // <prefix>+99:99:99
59
8.45k
    return false;
60
696
  if (!std::equal(kFixedZonePrefix, ep, name.begin()))
61
67
    return false;
62
629
  const char* np = name.data() + prefix_len;
63
629
  if (np[0] != '+' && np[0] != '-')
64
34
    return false;
65
595
  if (np[3] != ':' || np[6] != ':')  // see note below about large offsets
66
42
    return false;
67
68
553
  int hours = Parse02d(np + 1);
69
553
  if (hours == -1) return false;
70
547
  int mins = Parse02d(np + 4);
71
547
  if (mins == -1) return false;
72
539
  int secs = Parse02d(np + 7);
73
539
  if (secs == -1) return false;
74
75
533
  secs += ((hours * 60) + mins) * 60;
76
533
  if (secs > 24 * 60 * 60) return false;  // outside supported offset range
77
514
  *offset = seconds(secs * (np[0] == '-' ? -1 : 1));  // "-" means west
78
514
  return true;
79
533
}
80
81
232
std::string FixedOffsetToName(const seconds& offset) {
82
232
  if (offset == seconds::zero()) return "UTC";
83
231
  if (offset < std::chrono::hours(-24) || offset > std::chrono::hours(24)) {
84
    // We don't support fixed-offset zones more than 24 hours
85
    // away from UTC to avoid complications in rendering such
86
    // offsets and to (somewhat) limit the total number of zones.
87
0
    return "UTC";
88
0
  }
89
231
  int offset_seconds = static_cast<int>(offset.count());
90
231
  const char sign = (offset_seconds < 0 ? '-' : '+');
91
231
  int offset_minutes = offset_seconds / 60;
92
231
  offset_seconds %= 60;
93
231
  if (sign == '-') {
94
120
    if (offset_seconds > 0) {
95
0
      offset_seconds -= 60;
96
0
      offset_minutes += 1;
97
0
    }
98
120
    offset_seconds = -offset_seconds;
99
120
    offset_minutes = -offset_minutes;
100
120
  }
101
231
  int offset_hours = offset_minutes / 60;
102
231
  offset_minutes %= 60;
103
231
  const std::size_t prefix_len = sizeof(kFixedZonePrefix) - 1;
104
231
  char buf[prefix_len + sizeof("-24:00:00")];
105
231
  char* ep = std::copy_n(kFixedZonePrefix, prefix_len, buf);
106
231
  *ep++ = sign;
107
231
  ep = Format02d(ep, offset_hours);
108
231
  *ep++ = ':';
109
231
  ep = Format02d(ep, offset_minutes);
110
231
  *ep++ = ':';
111
231
  ep = Format02d(ep, offset_seconds);
112
231
  *ep++ = '\0';
113
231
  assert(ep == buf + sizeof(buf));
114
231
  return buf;
115
231
}
116
117
232
std::string FixedOffsetToAbbr(const seconds& offset) {
118
232
  std::string abbr = FixedOffsetToName(offset);
119
232
  const std::size_t prefix_len = sizeof(kFixedZonePrefix) - 1;
120
232
  if (abbr.size() == prefix_len + 9) {         // <prefix>+99:99:99
121
231
    abbr.erase(0, prefix_len);                 // +99:99:99
122
231
    abbr.erase(6, 1);                          // +99:9999
123
231
    abbr.erase(3, 1);                          // +999999
124
231
    if (abbr[5] == '0' && abbr[6] == '0') {    // +999900
125
32
      abbr.erase(5, 2);                        // +9999
126
32
      if (abbr[3] == '0' && abbr[4] == '0') {  // +9900
127
2
        abbr.erase(3, 2);                      // +99
128
2
      }
129
32
    }
130
231
  }
131
232
  return abbr;
132
232
}
133
134
}  // namespace cctz