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