Coverage Report

Created: 2026-03-25 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cctz/src/time_zone_posix.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_posix.h"
16
17
#include <cstddef>
18
#include <cstring>
19
#include <limits>
20
#include <string>
21
22
namespace cctz {
23
24
namespace {
25
26
const char kDigits[] = "0123456789";
27
28
358
const char* ParseInt(const char* p, int min, int max, int* vp) {
29
358
  int value = 0;
30
358
  const char* op = p;
31
358
  const int kMaxInt = std::numeric_limits<int>::max();
32
759
  for (; const char* dp = strchr(kDigits, *p); ++p) {
33
488
    int d = static_cast<int>(dp - kDigits);
34
488
    if (d >= 10) break;  // '\0'
35
401
    if (value > kMaxInt / 10) return nullptr;
36
401
    value *= 10;
37
401
    if (value > kMaxInt - d) return nullptr;
38
401
    value += d;
39
401
  }
40
358
  if (p == op || value < min || value > max) return nullptr;
41
358
  *vp = value;
42
358
  return p;
43
358
}
44
45
// abbr = <.*?> | [^-+,\d]{3,}
46
125
const char* ParseAbbr(const char* p, std::string* abbr) {
47
125
  const char* op = p;
48
125
  if (*p == '<') {  // special zoneinfo <...> form
49
12
    while (*++p != '>') {
50
9
      if (*p == '\0') return nullptr;
51
9
    }
52
3
    abbr->assign(op + 1, static_cast<std::size_t>(p - op) - 1);
53
3
    return ++p;
54
3
  }
55
524
  while (*p != '\0') {
56
524
    if (strchr("-+,", *p)) break;
57
470
    if (strchr(kDigits, *p)) break;
58
402
    ++p;
59
402
  }
60
122
  if (p - op < 3) return nullptr;
61
122
  abbr->assign(op, static_cast<std::size_t>(p - op));
62
122
  return p;
63
122
}
64
65
// offset = [+|-]hh[:mm[:ss]] (aggregated into single seconds value)
66
const char* ParseOffset(const char* p, int min_hour, int max_hour, int sign,
67
130
                        std::int_fast32_t* offset) {
68
130
  if (p == nullptr) return nullptr;
69
130
  if (*p == '+' || *p == '-') {
70
19
    if (*p++ == '-') sign = -sign;
71
19
  }
72
130
  int hours = 0;
73
130
  int minutes = 0;
74
130
  int seconds = 0;
75
76
130
  p = ParseInt(p, min_hour, max_hour, &hours);
77
130
  if (p == nullptr) return nullptr;
78
130
  if (*p == ':') {
79
0
    p = ParseInt(p + 1, 0, 59, &minutes);
80
0
    if (p == nullptr) return nullptr;
81
0
    if (*p == ':') {
82
0
      p = ParseInt(p + 1, 0, 59, &seconds);
83
0
      if (p == nullptr) return nullptr;
84
0
    }
85
0
  }
86
130
  *offset = sign * ((((hours * 60) + minutes) * 60) + seconds);
87
130
  return p;
88
130
}
89
90
// datetime = ( Jn | n | Mm.w.d ) [ / offset ]
91
76
const char* ParseDateTime(const char* p, PosixTransition* res) {
92
76
  if (p != nullptr && *p == ',') {
93
76
    if (*++p == 'M') {
94
76
      int month = 0;
95
76
      if ((p = ParseInt(p + 1, 1, 12, &month)) != nullptr && *p == '.') {
96
76
        int week = 0;
97
76
        if ((p = ParseInt(p + 1, 1, 5, &week)) != nullptr && *p == '.') {
98
76
          int weekday = 0;
99
76
          if ((p = ParseInt(p + 1, 0, 6, &weekday)) != nullptr) {
100
76
            res->date.fmt = PosixTransition::M;
101
76
            res->date.m.month = static_cast<std::int_fast8_t>(month);
102
76
            res->date.m.week = static_cast<std::int_fast8_t>(week);
103
76
            res->date.m.weekday = static_cast<std::int_fast8_t>(weekday);
104
76
          }
105
76
        }
106
76
      }
107
76
    } else if (*p == 'J') {
108
0
      int day = 0;
109
0
      if ((p = ParseInt(p + 1, 1, 365, &day)) != nullptr) {
110
0
        res->date.fmt = PosixTransition::J;
111
0
        res->date.j.day = static_cast<std::int_fast16_t>(day);
112
0
      }
113
0
    } else {
114
0
      int day = 0;
115
0
      if ((p = ParseInt(p, 0, 365, &day)) != nullptr) {
116
0
        res->date.fmt = PosixTransition::N;
117
0
        res->date.n.day = static_cast<std::int_fast16_t>(day);
118
0
      }
119
0
    }
120
76
  }
121
76
  if (p != nullptr) {
122
76
    res->time.offset = 2 * 60 * 60;  // default offset is 02:00:00
123
76
    if (*p == '/') p = ParseOffset(p + 1, -167, 167, 1, &res->time.offset);
124
76
  }
125
76
  return p;
126
76
}
127
128
}  // namespace
129
130
// spec = std offset [ dst [ offset ] , datetime , datetime ]
131
87
bool ParsePosixSpec(const std::string& spec, PosixTimeZone* res) {
132
87
  const char* p = spec.c_str();
133
87
  if (*p == ':') return false;
134
135
87
  p = ParseAbbr(p, &res->std_abbr);
136
87
  p = ParseOffset(p, 0, 24, -1, &res->std_offset);
137
87
  if (p == nullptr) return false;
138
87
  if (*p == '\0') return true;
139
140
38
  p = ParseAbbr(p, &res->dst_abbr);
141
38
  if (p == nullptr) return false;
142
38
  res->dst_offset = res->std_offset + (60 * 60);  // default
143
38
  if (*p != ',') p = ParseOffset(p, 0, 24, -1, &res->dst_offset);
144
145
38
  p = ParseDateTime(p, &res->dst_start);
146
38
  p = ParseDateTime(p, &res->dst_end);
147
148
38
  return p != nullptr && *p == '\0';
149
38
}
150
151
}  // namespace cctz