| 1 |
// chrono::tzdb -*- C++ -*- |
| 2 |
|
| 3 |
// Copyright The GNU Toolchain Authors. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// Under Section 7 of GPL version 3, you are granted additional |
| 17 |
// permissions described in the GCC Runtime Library Exception, version |
| 18 |
// 3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
// You should have received a copy of the GNU General Public License and |
| 21 |
// a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
// <http://www.gnu.org/licenses/>. |
| 24 |
|
| 25 |
// The -Wabi warnings in this file are all for non-exported symbols. |
| 26 |
#pragma GCC diagnostic ignored "-Wabi" |
| 27 |
|
| 28 |
// In the usual dual-abi build, std::chrono::tzdb is only defined for cxx11. |
| 29 |
#define _GLIBCXX_USE_CXX11_ABI 1 |
| 30 |
|
| 31 |
#include <chrono> |
| 32 |
#include <fstream> // ifstream |
| 33 |
#include <sstream> // istringstream |
| 34 |
#include <algorithm> // ranges::upper_bound, ranges::lower_bound, ranges::sort |
| 35 |
#include <atomic> // atomic<T*>, atomic<int> |
| 36 |
#include <memory> // atomic<shared_ptr<T>> |
| 37 |
#include <mutex> // mutex |
| 38 |
#include <iomanip> // quoted |
| 39 |
#if defined __GTHREADS && ! defined _GLIBCXX_HAS_GTHREADS |
| 40 |
# include <ext/concurrence.h> // __gnu_cxx::__mutex |
| 41 |
#endif |
| 42 |
|
| 43 |
#if defined(_GLIBCXX_HAVE_READLINK) && defined(_GLIBCXX_HAVE_UNISTD_H) |
| 44 |
# include <unistd.h> // readlink |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifdef _AIX |
| 48 |
# include <cstdlib> // getenv |
| 49 |
#endif |
| 50 |
|
| 51 |
#if defined __GTHREADS && ATOMIC_POINTER_LOCK_FREE == 2 |
| 52 |
# define USE_ATOMIC_LIST_HEAD 1 |
| 53 |
// TODO benchmark atomic<shared_ptr<>> vs mutex. |
| 54 |
# define USE_ATOMIC_SHARED_PTR 1 |
| 55 |
#else |
| 56 |
# define USE_ATOMIC_LIST_HEAD 0 |
| 57 |
# define USE_ATOMIC_SHARED_PTR 0 |
| 58 |
#endif |
| 59 |
|
| 60 |
#if USE_ATOMIC_SHARED_PTR && ! USE_ATOMIC_LIST_HEAD |
| 61 |
# error Unsupported combination |
| 62 |
#endif |
| 63 |
|
| 64 |
#if ! __cpp_constinit |
| 65 |
# if __has_cpp_attribute(clang::require_constant_initialization) |
| 66 |
# define constinit [[clang::require_constant_initialization]] |
| 67 |
# else // YOLO |
| 68 |
# define constinit |
| 69 |
# endif |
| 70 |
#endif |
| 71 |
|
| 72 |
namespace __gnu_cxx |
| 73 |
{ |
| 74 |
#ifdef _AIX |
| 75 |
// Cannot override weak symbols on AIX. |
| 76 |
const char* (*zoneinfo_dir_override)() = nullptr; |
| 77 |
#else |
| 78 |
[[gnu::weak]] const char* zoneinfo_dir_override(); |
| 79 |
|
| 80 |
#if defined(__APPLE__) || defined(__hpux__) \ |
| 81 |
|| (defined(__VXWORKS__) && !defined(__RTP__)) |
| 82 |
// Need a weak definition for Mach-O et al. |
| 83 |
[[gnu::weak]] const char* zoneinfo_dir_override() |
| 84 |
{ |
| 85 |
#ifdef _GLIBCXX_ZONEINFO_DIR |
| 86 |
return _GLIBCXX_ZONEINFO_DIR; |
| 87 |
#else |
| 88 |
return nullptr; |
| 89 |
#endif |
| 90 |
} |
| 91 |
#endif |
| 92 |
#endif |
| 93 |
} |
| 94 |
|
| 95 |
#if ! defined _GLIBCXX_ZONEINFO_DIR && ! defined _GLIBCXX_STATIC_TZDATA |
| 96 |
# define TZDB_DISABLED |
| 97 |
[[noreturn]] void __throw_disabled() |
| 98 |
{ |
| 99 |
std::__throw_runtime_error("tzdb: support for loading tzdata is disabled"); |
| 100 |
} |
| 101 |
#endif |
| 102 |
|
| 103 |
namespace std::chrono |
| 104 |
{ |
| 105 |
namespace |
| 106 |
{ |
| 107 |
#ifndef __GTHREADS |
| 108 |
// Dummy no-op mutex type for single-threaded targets. |
| 109 |
struct mutex { void lock() { } void unlock() { } }; |
| 110 |
#elif ! defined _GLIBCXX_HAS_GTHREADS |
| 111 |
// Use __gnu_cxx::__mutex if std::mutex isn't available. |
| 112 |
using mutex = __gnu_cxx::__mutex; |
| 113 |
# if ! USE_ATOMIC_SHARED_PTR && defined __GTHREAD_MUTEX_INIT |
| 114 |
# error "TODO: __gnu_cxx::__mutex can't be initialized with 'constinit'" |
| 115 |
# endif |
| 116 |
#endif |
| 117 |
|
| 118 |
#if ! USE_ATOMIC_SHARED_PTR |
| 119 |
inline mutex& list_mutex() |
| 120 |
{ |
| 121 |
#ifdef __GTHREAD_MUTEX_INIT |
| 122 |
constinit static mutex m; |
| 123 |
#else |
| 124 |
// Cannot use a constinit mutex, so use a local static. |
| 125 |
alignas(mutex) constinit static char buf[sizeof(mutex)]; |
| 126 |
static mutex& m = *::new(buf) mutex(); |
| 127 |
#endif |
| 128 |
return m; |
| 129 |
} |
| 130 |
#endif // ! USE_ATOMIC_SHARED_PTR |
| 131 |
|
| 132 |
struct Rule; |
| 133 |
} |
| 134 |
|
| 135 |
// The tzdb list is a singly-linked list of _Node objects, using shared_ptr |
| 136 |
// for the links. Iterators into the list share ownership of the nodes. |
| 137 |
// Each _Node contains a tzdb and a vector<Rule> with the rule set. |
| 138 |
struct tzdb_list::_Node |
| 139 |
{ |
| 140 |
shared_ptr<_Node> next; |
| 141 |
tzdb db; |
| 142 |
#ifndef TZDB_DISABLED |
| 143 |
vector<Rule> rules; |
| 144 |
#endif |
| 145 |
|
| 146 |
// The following static members are here because making them members |
| 147 |
// of this type gives them access to the private members of time_zone |
| 148 |
// and tzdb, without needing them declared in the <chrono> header. |
| 149 |
|
| 150 |
// The tzdb_list singleton. This doesn't contain the actual linked list, |
| 151 |
// but it has member functions that give access to it. |
| 152 |
static tzdb_list _S_the_list; |
| 153 |
|
| 154 |
#if USE_ATOMIC_SHARED_PTR |
| 155 |
using head_ptr = atomic<shared_ptr<_Node>>; |
| 156 |
#else |
| 157 |
// Non-atomic, list_mutex must be locked to access it. |
| 158 |
using head_ptr = shared_ptr<_Node>; |
| 159 |
#endif |
| 160 |
// This is the owning reference to the first tzdb in the list. |
| 161 |
static head_ptr _S_head_owner; |
| 162 |
|
| 163 |
#if USE_ATOMIC_LIST_HEAD |
| 164 |
// Lock-free access to the head of the list. |
| 165 |
static atomic<_Node*> _S_head_cache; |
| 166 |
|
| 167 |
static _Node* |
| 168 |
_S_list_head(memory_order ord) noexcept |
| 169 |
{ return _S_head_cache.load(ord); } |
| 170 |
|
| 171 |
static void |
| 172 |
_S_cache_list_head(_Node* new_head) noexcept |
| 173 |
{ _S_head_cache = new_head; } |
| 174 |
#else |
| 175 |
static _Node* |
| 176 |
_S_list_head(memory_order) |
| 177 |
{ |
| 178 |
lock_guard<mutex> l(list_mutex()); |
| 179 |
return _S_head_owner.get(); |
| 180 |
} |
| 181 |
|
| 182 |
static void |
| 183 |
_S_cache_list_head(_Node*) noexcept |
| 184 |
{ } |
| 185 |
#endif |
| 186 |
|
| 187 |
static const tzdb& _S_init_tzdb(); |
| 188 |
static const tzdb& _S_replace_head(shared_ptr<_Node>, shared_ptr<_Node>); |
| 189 |
|
| 190 |
static pair<vector<leap_second>, bool> _S_read_leap_seconds(); |
| 191 |
}; |
| 192 |
|
| 193 |
// Implementation of the private constructor used for the singleton object. |
| 194 |
constexpr tzdb_list::tzdb_list(nullptr_t) { } |
| 195 |
|
| 196 |
#pragma GCC diagnostic ignored "-Wprio-ctor-dtor" |
| 197 |
|
| 198 |
[[gnu::init_priority(98)]] |
| 199 |
constinit tzdb_list tzdb_list::_Node::_S_the_list(nullptr); |
| 200 |
|
| 201 |
[[gnu::init_priority(98)]] |
| 202 |
constinit tzdb_list::_Node::head_ptr tzdb_list::_Node::_S_head_owner{nullptr}; |
| 203 |
|
| 204 |
#if USE_ATOMIC_LIST_HEAD |
| 205 |
[[gnu::init_priority(98)]] |
| 206 |
constinit atomic<tzdb_list::_Node*> tzdb_list::_Node::_S_head_cache{nullptr}; |
| 207 |
#endif |
| 208 |
|
| 209 |
// The data structures defined in this file (Rule, on_day, at_time etc.) |
| 210 |
// are used to represent the information parsed from the tzdata.zi file |
| 211 |
// described at https://man7.org/linux/man-pages/man8/zic.8.html#FILES |
| 212 |
|
| 213 |
// N.B. Most stream extraction operations for time zones, rules etc. |
| 214 |
// assume that setting failbit will throw an exception, so individual |
| 215 |
// input operations are not always checked for success. |
| 216 |
|
| 217 |
#ifndef TZDB_DISABLED |
| 218 |
namespace |
| 219 |
{ |
| 220 |
// Used for reading a possibly-quoted string from a stream. |
| 221 |
struct quoted |
| 222 |
{ |
| 223 |
string& str; |
| 224 |
|
| 225 |
friend istream& operator>>(istream& in, quoted&& q) |
| 226 |
{ |
| 227 |
if (ws(in).peek() == '"') |
| 228 |
in >> std::quoted(q.str); |
| 229 |
else |
| 230 |
in >> q.str; |
| 231 |
return in; |
| 232 |
} |
| 233 |
}; |
| 234 |
|
| 235 |
// 32-bit version of chrono::seconds for offsets in the range [-24h,24h]. |
| 236 |
// Care must be taken to avoid overflow when using this in arithmetic. |
| 237 |
// For example, if sys_days::rep is also 32-bit then the result of |
| 238 |
// sys_days(1850y/January/1) + sec32_t(0) will be incorrect. |
| 239 |
using sec32_t = duration<int_least32_t>; |
| 240 |
|
| 241 |
// A time relative to midnight, as measured by the indicator clock. |
| 242 |
struct at_time |
| 243 |
{ |
| 244 |
sec32_t time{}; |
| 245 |
enum Indicator : unsigned char { Wall, Universal, Standard, Daylight }; |
| 246 |
Indicator indicator = Wall; |
| 247 |
|
| 248 |
static pair<Indicator, bool> is_indicator(int c) noexcept |
| 249 |
{ |
| 250 |
switch (c) |
| 251 |
{ |
| 252 |
case 's': |
| 253 |
return {Standard, true}; |
| 254 |
case 'u': |
| 255 |
case 'g': |
| 256 |
case 'z': |
| 257 |
return {Universal, true}; |
| 258 |
case 'w': |
| 259 |
return {Wall, true}; |
| 260 |
case 'd': |
| 261 |
return {Daylight, true}; |
| 262 |
default: |
| 263 |
return {Wall, false}; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
// Checks if the next character in the stream is an indicator. |
| 268 |
// If not, indic is unchanged. Callers should set a default first. |
| 269 |
friend istream& operator>>(istream& in, Indicator& indic) |
| 270 |
{ |
| 271 |
if (!in.eof()) |
| 272 |
{ |
| 273 |
auto [val, yes] = at_time::is_indicator(in.peek()); |
| 274 |
if (yes) |
| 275 |
{ |
| 276 |
in.ignore(1); |
| 277 |
indic = val; |
| 278 |
} |
| 279 |
} |
| 280 |
return in; |
| 281 |
} |
| 282 |
|
| 283 |
friend istream& operator>>(istream& in, at_time& at); |
| 284 |
}; |
| 285 |
|
| 286 |
// Wrapper for chrono::month that can be extracted from an istream |
| 287 |
// as an abbreviated month name. |
| 288 |
// The month name can be any unambiguous portion of a month name, |
| 289 |
// e.g. "S" (September) or "Ja" (January), but not "Ju" (June/July). |
| 290 |
struct abbrev_month |
| 291 |
{ |
| 292 |
month m; |
| 293 |
|
| 294 |
friend istream& operator>>(istream& in, abbrev_month& am); |
| 295 |
}; |
| 296 |
|
| 297 |
// The IN and ON fields of a RULE record, e.g. "March lastSunday". |
| 298 |
struct on_day |
| 299 |
{ |
| 300 |
using rep = uint_least16_t; |
| 301 |
// Equivalent to Kind, chrono::month, chrono::day, chrono::weekday, |
| 302 |
// but half the size. |
| 303 |
enum Kind : rep { DayOfMonth=0, LastWeekday, LessEq, GreaterEq }; |
| 304 |
Kind kind : 2 = DayOfMonth; |
| 305 |
rep month : 4 = 0; // [1,12] |
| 306 |
rep day_of_month : 5 = 0; // [1,31] |
| 307 |
rep day_of_week : 3 = 0; // [0,6] |
| 308 |
|
| 309 |
chrono::month |
| 310 |
get_month() const noexcept |
| 311 |
{ return chrono::month{month}; } |
| 312 |
|
| 313 |
chrono::day |
| 314 |
get_day() const noexcept |
| 315 |
{ return chrono::day{day_of_month}; } |
| 316 |
|
| 317 |
chrono::month_day |
| 318 |
get_month_day() const noexcept |
| 319 |
{ return chrono::month_day{get_month(), get_day()}; } |
| 320 |
|
| 321 |
bool ok() const noexcept |
| 322 |
{ |
| 323 |
switch (kind) |
| 324 |
{ |
| 325 |
case DayOfMonth: |
| 326 |
return day_of_month != 0; |
| 327 |
case LastWeekday: |
| 328 |
return day_of_week != 7 && day_of_month == 0; |
| 329 |
case LessEq: |
| 330 |
case GreaterEq: |
| 331 |
return day_of_week != 7 && day_of_month != 0; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
// Convert date like "last Sunday in June" or "Sunday <= June 30" |
| 336 |
// to a specific date in the given year. |
| 337 |
year_month_day pin(year y) const |
| 338 |
{ |
| 339 |
year_month_day ymd; |
| 340 |
if (kind == LastWeekday) // Last Sunday in June |
| 341 |
{ |
| 342 |
month_weekday_last mwdl{get_month(), |
| 343 |
weekday_last{weekday{day_of_week}}}; |
| 344 |
ymd = year_month_day{sys_days{y/mwdl}}; |
| 345 |
} |
| 346 |
else if (kind != DayOfMonth) // Sunday <= June 30 or Sunday >= June 30 |
| 347 |
{ |
| 348 |
sys_days date{y/get_month_day()}; |
| 349 |
days diff; |
| 350 |
if (kind == LessEq) |
| 351 |
diff = -(weekday{date} - weekday{day_of_week}); |
| 352 |
else |
| 353 |
diff = weekday{day_of_week} - weekday{date}; |
| 354 |
// XXX need to handle underflow/overflow to another year? |
| 355 |
ymd = year_month_day{date + diff}; |
| 356 |
} |
| 357 |
else |
| 358 |
ymd = year_month_day{y, get_month(), get_day()}; |
| 359 |
return ymd; |
| 360 |
} |
| 361 |
|
| 362 |
friend istream& operator>>(istream&, on_day&); |
| 363 |
}; |
| 364 |
|
| 365 |
// Wrapper for two chrono::year values, which reads the FROM and TO |
| 366 |
// fields of a Rule line. The FROM field is a year and TO is a year or |
| 367 |
// one of the keywords "maximum" or "only" (or an abbreviation of those). |
| 368 |
// For backwards compatibility, the keyword "minimum" is recognized |
| 369 |
// for FROM and interpreted as 1900. |
| 370 |
struct years_from_to |
| 371 |
{ |
| 372 |
year& from; |
| 373 |
year& to; |
| 374 |
|
| 375 |
friend istream& operator>>(istream& in, years_from_to&& yy) |
| 376 |
{ |
| 377 |
string s; |
| 378 |
auto c = ws(in).peek(); |
| 379 |
if (c == 'm') [[unlikely]] // keyword "minimum" |
| 380 |
{ |
| 381 |
in >> s; // extract the rest of the word |
| 382 |
yy.from = year(1900); |
| 383 |
} |
| 384 |
else if (int num = 0; in >> num) [[likely]] |
| 385 |
yy.from = year{num}; |
| 386 |
|
| 387 |
c = ws(in).peek(); |
| 388 |
if (c == 'm') // keyword "maximum" |
| 389 |
{ |
| 390 |
in >> s; // extract the rest of the word |
| 391 |
yy.to = year::max(); |
| 392 |
} |
| 393 |
else if (c == 'o') // keyword "only" |
| 394 |
{ |
| 395 |
in >> s; // extract the rest of the word |
| 396 |
yy.to = yy.from; |
| 397 |
} |
| 398 |
else if (int num = 0; in >> num) |
| 399 |
yy.to = year{num}; |
| 400 |
|
| 401 |
return in; |
| 402 |
} |
| 403 |
}; |
| 404 |
|
| 405 |
bool |
| 406 |
select_std_or_dst_abbrev(string& abbrev, minutes save) |
| 407 |
{ |
| 408 |
if (size_t pos = abbrev.find('/'); pos != string::npos) |
| 409 |
{ |
| 410 |
// Select one of "STD/DST" for standard or daylight. |
| 411 |
if (save == 0min) |
| 412 |
abbrev.erase(pos); |
| 413 |
else |
| 414 |
abbrev.erase(0, pos + 1); |
| 415 |
return true; |
| 416 |
} |
| 417 |
return false; |
| 418 |
} |
| 419 |
|
| 420 |
// Set the sys_info::abbrev string by expanding any placeholders. |
| 421 |
void |
| 422 |
format_abbrev_str(sys_info& info, string_view letters = {}) |
| 423 |
{ |
| 424 |
if (size_t pos = info.abbrev.find('%'); pos != string::npos) |
| 425 |
{ |
| 426 |
if (info.abbrev[pos + 1] == 's') |
| 427 |
{ |
| 428 |
// Expand "%s" to the variable part, given by Rule::letters. |
| 429 |
if (letters == "-") |
| 430 |
info.abbrev.erase(pos, 2); |
| 431 |
else |
| 432 |
info.abbrev.replace(pos, 2, letters); |
| 433 |
} |
| 434 |
else if (info.abbrev[pos + 1] == 'z') |
| 435 |
{ |
| 436 |
// Expand "%z" to the UT offset as +/-hh, +/-hhmm, or +/-hhmmss. |
| 437 |
hh_mm_ss<seconds> t(info.offset); |
| 438 |
string z(1, "+-"[t.is_negative()]); |
| 439 |
long val = t.hours().count(); |
| 440 |
int digits = 2; |
| 441 |
if (int m = t.minutes().count()) |
| 442 |
{ |
| 443 |
digits = 4; |
| 444 |
val *= 100; |
| 445 |
val += m; |
| 446 |
if (int s = t.seconds().count()) |
| 447 |
{ |
| 448 |
digits = 6; |
| 449 |
val *= 100; |
| 450 |
val += s; |
| 451 |
} |
| 452 |
} |
| 453 |
auto sval = std::to_string(val); |
| 454 |
z += string(digits - sval.size(), '0'); |
| 455 |
z += sval; |
| 456 |
info.abbrev.replace(pos, 2, z); |
| 457 |
} |
| 458 |
} |
| 459 |
else |
| 460 |
select_std_or_dst_abbrev(info.abbrev, info.save); |
| 461 |
} |
| 462 |
|
| 463 |
// A time zone information record. |
| 464 |
// Zone NAME STDOFF RULES FORMAT [UNTIL] |
| 465 |
// Zone Asia/Amman 2:00 Jordan EE%sT 2017 Oct 27 01:00 |
| 466 |
// Will be lazily expanded into sys_info objects as needed. |
| 467 |
struct ZoneInfo |
| 468 |
{ |
| 469 |
ZoneInfo() = default; |
| 470 |
|
| 471 |
ZoneInfo(sys_info&& info) |
| 472 |
: m_buf(std::move(info.abbrev)), m_expanded(true), m_save(info.save), |
| 473 |
m_offset(info.offset), m_until(info.end) |
| 474 |
{ } |
| 475 |
|
| 476 |
ZoneInfo(const pair<sys_info, string_view>& info) |
| 477 |
: m_expanded(true), m_save(info.first.save), m_offset(info.first.offset), |
| 478 |
m_until(info.first.end) |
| 479 |
{ |
| 480 |
if (info.second.size()) |
| 481 |
{ |
| 482 |
m_buf = info.second; // LETTERS field |
| 483 |
m_buf += ' '; |
| 484 |
m_pos = m_buf.size(); |
| 485 |
} |
| 486 |
m_buf += info.first.abbrev; |
| 487 |
} |
| 488 |
|
| 489 |
// STDOFF: Seconds from UTC during standard time. |
| 490 |
seconds |
| 491 |
offset() const noexcept { return m_offset; } |
| 492 |
|
| 493 |
// RULES: The name of the rules that apply for this period. |
| 494 |
string_view |
| 495 |
rules() const noexcept |
| 496 |
{ |
| 497 |
string_view r; |
| 498 |
if (m_pos != 0) |
| 499 |
r = {m_buf.data(), m_pos - 1u}; |
| 500 |
return r; |
| 501 |
} |
| 502 |
|
| 503 |
// FORMAT: The name of the time zone (might contain %s or %z). |
| 504 |
string_view |
| 505 |
format() const noexcept |
| 506 |
{ return {m_buf.data() + m_pos, m_buf.size() - m_pos}; } |
| 507 |
|
| 508 |
// UNTIL: The time when this info no longer applies. |
| 509 |
sys_seconds |
| 510 |
until() const noexcept { return m_until; } |
| 511 |
|
| 512 |
friend istream& operator>>(istream&, ZoneInfo&); |
| 513 |
|
| 514 |
bool |
| 515 |
expanded() const noexcept |
| 516 |
{ return m_expanded; } |
| 517 |
|
| 518 |
// For an expanded ZoneInfo this returns the LETTERS that apply to the |
| 519 |
// **next** sys_info after this one. |
| 520 |
string_view |
| 521 |
next_letters() const noexcept |
| 522 |
{ return m_expanded ? rules() : string_view{}; } |
| 523 |
|
| 524 |
|
| 525 |
bool |
| 526 |
to(sys_info& info) const |
| 527 |
{ |
| 528 |
// If this object references a named Rule then we can't populate |
| 529 |
// a sys_info yet. |
| 530 |
if (!m_expanded) |
| 531 |
return false; |
| 532 |
|
| 533 |
info.end = until(); |
| 534 |
info.offset = offset(); |
| 535 |
info.save = minutes(m_save); |
| 536 |
info.abbrev = format(); |
| 537 |
format_abbrev_str(info); // expand %z |
| 538 |
return true; |
| 539 |
} |
| 540 |
|
| 541 |
private: |
| 542 |
friend class time_zone; |
| 543 |
|
| 544 |
void |
| 545 |
set_abbrev(string abbrev) |
| 546 |
{ |
| 547 |
m_buf = std::move(abbrev); |
| 548 |
m_pos = 0; |
| 549 |
m_expanded = true; |
| 550 |
} |
| 551 |
|
| 552 |
void |
| 553 |
set_rules_and_format(const string& rls, const string& fmt) |
| 554 |
{ |
| 555 |
// Both strings are typically short enough to fit in one SSO string. |
| 556 |
// As of tzdata 2022f the maximum is 14 chars, e.g. "AU +0845/+0945". |
| 557 |
m_buf.reserve(rls.size() + 1 + fmt.size()); |
| 558 |
m_buf = rls; |
| 559 |
m_buf += ' '; |
| 560 |
m_buf += fmt; |
| 561 |
m_pos = rls.size() + 1; |
| 562 |
} |
| 563 |
|
| 564 |
string m_buf; // rules() + ' ' + format() OR letters + ' ' + format() |
| 565 |
uint_least16_t m_pos : 15 = 0; // offset of format() in m_buf |
| 566 |
uint_least16_t m_expanded : 1 = 0; |
| 567 |
duration<int_least16_t, ratio<60>> m_save{}; |
| 568 |
sec32_t m_offset{}; |
| 569 |
sys_seconds m_until{}; |
| 570 |
|
| 571 |
#if 0 |
| 572 |
// Consider making this class more compact, e.g. |
| 573 |
int_least64_t offset_seconds : 18; |
| 574 |
int_least64_t until_sys_seconds : 46; |
| 575 |
uint_least32_t save_minutes : 12; |
| 576 |
uint_least32_t pos : 20; |
| 577 |
string buf; // abbrev OR "rules format" |
| 578 |
#endif |
| 579 |
}; |
| 580 |
|
| 581 |
// A RULE record from the tzdata.zi timezone info file. |
| 582 |
struct Rule |
| 583 |
{ |
| 584 |
// This allows on_day to reuse padding of at_time. |
| 585 |
// This keeps the size to 8 bytes and the alignment to 4 bytes. |
| 586 |
struct datetime : at_time { on_day day; }; |
| 587 |
|
| 588 |
// TODO combining name+letters into a single string (like in ZoneInfo) |
| 589 |
// would save sizeof(string) and make Rule fit in a single cacheline. |
| 590 |
// Or don't store name in the Rule, and replace vector<Rule> with |
| 591 |
// vector<pair<string,vector<Rule>>> i.e. map-like structure. |
| 592 |
|
| 593 |
string name; // the name of the rule set |
| 594 |
year from{}; // first year in which the rule applies |
| 595 |
year to{}; // final year in which the rule applies |
| 596 |
datetime when; // the day and time on which the rule takes effect |
| 597 |
sec32_t save{}; // amount of time to be added when the rule is in effect |
| 598 |
string letters; // variable part of TZ abbreviations when rule in effect |
| 599 |
|
| 600 |
// Combine y + this->when + offset to give a UTC timestamp. |
| 601 |
sys_seconds |
| 602 |
start_time(year y, seconds offset) const |
| 603 |
{ |
| 604 |
auto time = when.time; |
| 605 |
if (when.indicator == at_time::Wall |
| 606 |
|| when.indicator == at_time::Standard) |
| 607 |
time -= offset; // Convert local time to sys time. |
| 608 |
return sys_days(when.day.pin(y)) + time; |
| 609 |
} |
| 610 |
|
| 611 |
friend istream& operator>>(istream& in, Rule& rule) |
| 612 |
{ |
| 613 |
string str; |
| 614 |
|
| 615 |
// Rule NAME FROM TO TYPE IN ON AT SAVE LETTER/S |
| 616 |
|
| 617 |
in >> quoted(rule.name) >> years_from_to{rule.from, rule.to}; |
| 618 |
|
| 619 |
if (char type; in >> type && type != '-') |
| 620 |
in.setstate(ios::failbit); |
| 621 |
in >> rule.when.day >> static_cast<at_time&>(rule.when); |
| 622 |
at_time save_time; |
| 623 |
save_time.indicator = at_time::Wall; // not valid for this field |
| 624 |
in >> save_time; |
| 625 |
if (save_time.indicator != at_time::Wall) |
| 626 |
{ |
| 627 |
// We don't actually store the save_time.indicator, because we |
| 628 |
// assume that it's always deducible from the offset value. |
| 629 |
auto expected = save_time.time == 0s |
| 630 |
? at_time::Standard |
| 631 |
: at_time::Daylight; |
| 632 |
__glibcxx_assert(save_time.indicator == expected); |
| 633 |
} |
| 634 |
|
| 635 |
rule.save = save_time.time; |
| 636 |
|
| 637 |
in >> rule.letters; |
| 638 |
return in; |
| 639 |
} |
| 640 |
|
| 641 |
#ifdef _GLIBCXX_ASSERTIONS |
| 642 |
friend ostream& operator<<(ostream& out, const Rule& r) |
| 643 |
{ |
| 644 |
out << "Rule " << r.name << ' ' << (int)r.from << ' ' << (int)r.to |
| 645 |
<< ' ' << r.when.day.get_month() << ' '; |
| 646 |
switch (r.when.day.kind) |
| 647 |
{ |
| 648 |
case on_day::DayOfMonth: |
| 649 |
out << (unsigned)r.when.day.get_day(); |
| 650 |
break; |
| 651 |
case on_day::LastWeekday: |
| 652 |
out << "last" << weekday(r.when.day.day_of_week); |
| 653 |
break; |
| 654 |
case on_day::LessEq: |
| 655 |
out << weekday(r.when.day.day_of_week) << " <= " |
| 656 |
<< r.when.day.day_of_month; |
| 657 |
break; |
| 658 |
case on_day::GreaterEq: |
| 659 |
out << weekday(r.when.day.day_of_week) << " >= " |
| 660 |
<< r.when.day.day_of_month; |
| 661 |
break; |
| 662 |
} |
| 663 |
out << ' ' << hh_mm_ss(r.when.time) << "wusd"[r.when.indicator]; |
| 664 |
out << ' ' << r.save.count(); |
| 665 |
if (!r.letters.empty()) |
| 666 |
out << ' ' << r.letters; |
| 667 |
else |
| 668 |
out << " -"; |
| 669 |
return out; |
| 670 |
} |
| 671 |
#endif |
| 672 |
}; |
| 673 |
} // namespace |
| 674 |
#endif // TZDB_DISABLED |
| 675 |
|
| 676 |
// Private constructor used by reload_tzdb() to create time_zone objects. |
| 677 |
time_zone::time_zone(unique_ptr<_Impl> __p) : _M_impl(std::move(__p)) { } |
| 678 |
|
| 679 |
time_zone::~time_zone() = default; |
| 680 |
|
| 681 |
// The opaque pimpl class stored in a time_zone object. |
| 682 |
struct time_zone::_Impl |
| 683 |
{ |
| 684 |
#ifndef TZDB_DISABLED |
| 685 |
explicit |
| 686 |
_Impl(weak_ptr<tzdb_list::_Node> node) : node(std::move(node)) { } |
| 687 |
|
| 688 |
vector<ZoneInfo> infos; // Definitions of the time zone's transitions. |
| 689 |
|
| 690 |
// Non-owning reference back to the tzdb that owns this time_zone. |
| 691 |
// Needed to access the list of rules for the time zones. |
| 692 |
weak_ptr<tzdb_list::_Node> node; |
| 693 |
|
| 694 |
// In the simple case, we don't actual keep count. No concurrent access |
| 695 |
// to the infos vector is possible, even if all infos are expanded. |
| 696 |
template<typename _Tp> |
| 697 |
struct RulesCounter |
| 698 |
{ |
| 699 |
// Called for each rule-based ZoneInfo added to the infos vector. |
| 700 |
// Called when the time_zone::_Impl is created, so no concurrent calls. |
| 701 |
void increment() { } |
| 702 |
// Called when a rule-based ZoneInfo is expanded. |
| 703 |
// The caller must have called lock() for exclusive access to infos. |
| 704 |
void decrement() { } |
| 705 |
|
| 706 |
// Use a mutex to synchronize all access to the infos vector. |
| 707 |
mutex infos_mutex; |
| 708 |
|
| 709 |
void lock() { infos_mutex.lock(); } |
| 710 |
void unlock() { infos_mutex.unlock(); } |
| 711 |
}; |
| 712 |
|
| 713 |
#if defined __GTHREADS && __cpp_lib_atomic_wait |
| 714 |
// Atomic count of unexpanded ZoneInfo objects in the infos vector. |
| 715 |
// Concurrent access is allowed when all objects have been expanded. |
| 716 |
// Only use an atomic counter if it would not require libatomic, |
| 717 |
// because we don't want libstdc++.so to depend on libatomic. |
| 718 |
template<typename _Tp> requires _Tp::is_always_lock_free |
| 719 |
struct RulesCounter<_Tp> |
| 720 |
{ |
| 721 |
_Tp counter{0}; |
| 722 |
|
| 723 |
void |
| 724 |
increment() |
| 725 |
{ counter.fetch_add(1, memory_order::relaxed); } |
| 726 |
|
| 727 |
void |
| 728 |
decrement() |
| 729 |
{ |
| 730 |
// The current thread holds the lock, so the counter is negative |
| 731 |
// and so we need to increment it to decrement it! |
| 732 |
// If the count reaches zero then there are no more unexpanded infos, |
| 733 |
// so notify all waiting threads that they can access the infos. |
| 734 |
// We must do this here, because unlock() is a no-op if counter==0. |
| 735 |
if (++counter == 0) |
| 736 |
counter.notify_all(); |
| 737 |
} |
| 738 |
|
| 739 |
void |
| 740 |
lock() |
| 741 |
{ |
| 742 |
// If counter is zero then concurrent access is allowed, so lock() |
| 743 |
// and unlock() are no-ops and multiple threads can "lock" at once. |
| 744 |
// If counter is non-zero then the contents of the infos vector might |
| 745 |
// need to be changed, so only one thread is allowed to access it. |
| 746 |
for (auto c = counter.load(memory_order::relaxed); c != 0;) |
| 747 |
{ |
| 748 |
// Setting counter to negative means this thread has the lock. |
| 749 |
if (c > 0 && counter.compare_exchange_strong(c, -c)) |
| 750 |
return; |
| 751 |
|
| 752 |
if (c < 0) |
| 753 |
{ |
| 754 |
// Counter is negative, another thread already has the lock. |
| 755 |
counter.wait(c); |
| 756 |
c = counter.load(); |
| 757 |
} |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
void |
| 762 |
unlock() |
| 763 |
{ |
| 764 |
if (auto c = counter.load(memory_order::relaxed); c < 0) |
| 765 |
{ |
| 766 |
counter.store(-c, memory_order::release); |
| 767 |
counter.notify_one(); |
| 768 |
} |
| 769 |
} |
| 770 |
}; |
| 771 |
#endif // __GTHREADS && __cpp_lib_atomic_wait |
| 772 |
|
| 773 |
#if __cpp_lib_atomic_lock_free_type_aliases |
| 774 |
RulesCounter<atomic_signed_lock_free> rules_counter; |
| 775 |
#else |
| 776 |
RulesCounter<void> rules_counter; |
| 777 |
#endif |
| 778 |
|
| 779 |
#else // TZDB_DISABLED |
| 780 |
_Impl(weak_ptr<tzdb_list::_Node>) { } |
| 781 |
struct { |
| 782 |
sys_info info; |
| 783 |
void push_back(sys_info i) { info = std::move(i); } |
| 784 |
} infos; |
| 785 |
#endif // TZDB_DISABLED |
| 786 |
}; |
| 787 |
|
| 788 |
// Implementation of std::chrono::time_zone::get_info(const sys_time<D>&) |
| 789 |
sys_info |
| 790 |
time_zone::_M_get_sys_info(sys_seconds tp) const |
| 791 |
{ |
| 792 |
#ifndef TZDB_DISABLED |
| 793 |
// This gives us access to the node->rules vector, but also ensures |
| 794 |
// that the tzdb node won't get erased while we're still using it. |
| 795 |
const auto node = _M_impl->node.lock(); |
| 796 |
auto& infos = _M_impl->infos; |
| 797 |
|
| 798 |
// Prevent concurrent access to _M_impl->infos if it might need to change. |
| 799 |
lock_guard lock(_M_impl->rules_counter); |
| 800 |
|
| 801 |
// Find the transition info for the time point. |
| 802 |
auto i = ranges::upper_bound(infos, tp, ranges::less{}, &ZoneInfo::until); |
| 803 |
|
| 804 |
if (i == infos.end()) |
| 805 |
{ |
| 806 |
if (infos.empty()) |
| 807 |
__throw_runtime_error("std::chrono::time_zone::get_info: invalid data"); |
| 808 |
tp = (--i)->until(); |
| 809 |
} |
| 810 |
|
| 811 |
sys_info info; |
| 812 |
|
| 813 |
if (i == infos.begin()) |
| 814 |
info.begin = sys_days(year::min()/January/1); |
| 815 |
else |
| 816 |
info.begin = i[-1].until(); |
| 817 |
|
| 818 |
if (i->to(info)) // We already know a sys_info for this time. |
| 819 |
return info; |
| 820 |
|
| 821 |
// Otherwise, we have a ZoneInfo object that describes the applicable |
| 822 |
// transitions in terms of a set of named rules that vary by year. |
| 823 |
// Replace that rules-based ZoneInfo object with a sequence of one or more |
| 824 |
// objects that map directly to a sys_info value. |
| 825 |
const ZoneInfo& ri = *i; |
| 826 |
|
| 827 |
// Find the rules named by ri.rules() |
| 828 |
auto rules = ranges::equal_range(node->rules, ri.rules(), |
| 829 |
ranges::less{}, &Rule::name); |
| 830 |
|
| 831 |
if (ranges::empty(rules)) |
| 832 |
__throw_runtime_error("std::chrono::time_zone::get_info: invalid data"); |
| 833 |
|
| 834 |
vector<pair<sys_info, string_view>> new_infos; |
| 835 |
if (int n = ceil<years>(tp - info.begin).count()) |
| 836 |
new_infos.reserve(std::min(100, n * 2)); |
| 837 |
long result_index = -1; |
| 838 |
int num_after = 4; // number of sys_info to generate past tp. |
| 839 |
|
| 840 |
// The following initial values for info.offset, info.save, and letters |
| 841 |
// are only valid if the first sys_info we are generating uses the time |
| 842 |
// zone's standard time, because daylight time would need non-zero offset. |
| 843 |
// This is true by construction, because this function always tries to |
| 844 |
// finish so that the last ZoneInfo object expanded is for daylight time. |
| 845 |
// This means that i[-1] is either an expanded ZoneInfo for a DST sys_info |
| 846 |
// or is an unexpanded (rule-based) ZoneInfo for a different rule, and |
| 847 |
// rule changes always occur between periods of standard time. |
| 848 |
info.offset = ri.offset(); |
| 849 |
info.save = 0min; |
| 850 |
// XXX The ri.until() time point should be |
| 851 |
// "interpreted using the rules in effect just before the transition" |
| 852 |
info.end = ri.until(); |
| 853 |
info.abbrev = ri.format(); |
| 854 |
|
| 855 |
string_view letters; |
| 856 |
if (i != infos.begin() && i[-1].expanded()) |
| 857 |
letters = i[-1].next_letters(); |
| 858 |
|
| 859 |
if (letters.empty()) |
| 860 |
{ |
| 861 |
sys_seconds t = info.begin - seconds(1); |
| 862 |
const year_month_day date(chrono::floor<days>(t)); |
| 863 |
|
| 864 |
// Try to find a Rule active before this time, to get initial |
| 865 |
// SAVE and LETTERS values. There may not be a Rule for the period |
| 866 |
// before the first DST transition, so find the earliest DST->STD |
| 867 |
// transition and use the LETTERS from that. |
| 868 |
const Rule* active_rule = nullptr; |
| 869 |
sys_seconds active_rule_start = sys_seconds::min(); |
| 870 |
const Rule* first_std = nullptr; |
| 871 |
for (const auto& rule : rules) |
| 872 |
{ |
| 873 |
if (rule.save == minutes(0)) |
| 874 |
{ |
| 875 |
if (!first_std) |
| 876 |
first_std = &rule; |
| 877 |
else if (rule.from < first_std->from) |
| 878 |
first_std = &rule; |
| 879 |
else if (rule.from == first_std->from) |
| 880 |
{ |
| 881 |
if (rule.start_time(rule.from, {}) |
| 882 |
< first_std->start_time(first_std->from, {})) |
| 883 |
first_std = &rule; |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
year y = date.year(); |
| 888 |
|
| 889 |
if (y > rule.to) // rule no longer applies at time t |
| 890 |
continue; |
| 891 |
if (y < rule.from) // rule doesn't apply yet at time t |
| 892 |
continue; |
| 893 |
|
| 894 |
sys_seconds rule_start; |
| 895 |
|
| 896 |
seconds offset{}; // appropriate for at_time::Universal |
| 897 |
if (rule.when.indicator == at_time::Wall) |
| 898 |
offset = info.offset; |
| 899 |
else if (rule.when.indicator == at_time::Standard) |
| 900 |
offset = ri.offset(); |
| 901 |
|
| 902 |
// Time the rule takes effect this year: |
| 903 |
rule_start = rule.start_time(y, offset); |
| 904 |
|
| 905 |
if (rule_start >= t && rule.from < y) |
| 906 |
{ |
| 907 |
// Try this rule in the previous year. |
| 908 |
rule_start = rule.start_time(--y, offset); |
| 909 |
} |
| 910 |
|
| 911 |
if (active_rule_start < rule_start && rule_start < t) |
| 912 |
{ |
| 913 |
active_rule_start = rule_start; |
| 914 |
active_rule = &rule; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
if (active_rule) |
| 919 |
{ |
| 920 |
info.offset = ri.offset() + active_rule->save; |
| 921 |
info.save = chrono::duration_cast<minutes>(active_rule->save); |
| 922 |
letters = active_rule->letters; |
| 923 |
} |
| 924 |
else if (first_std) |
| 925 |
letters = first_std->letters; |
| 926 |
} |
| 927 |
|
| 928 |
const Rule* curr_rule = nullptr; |
| 929 |
|
| 930 |
while (info.begin < info.end && num_after > 0) |
| 931 |
{ |
| 932 |
sys_seconds t = info.begin; |
| 933 |
const year_month_day date(chrono::floor<days>(t)); |
| 934 |
const Rule* next_rule = nullptr; |
| 935 |
|
| 936 |
// Check every rule to find the next transition after t. |
| 937 |
for (const auto& rule : rules) |
| 938 |
{ |
| 939 |
if (&rule == curr_rule) // don't bother checking this one again |
| 940 |
continue; |
| 941 |
|
| 942 |
if (date.year() > rule.to) // rule no longer applies at time t |
| 943 |
continue; |
| 944 |
|
| 945 |
sys_seconds rule_start; |
| 946 |
|
| 947 |
seconds offset{}; // appropriate for at_time::Universal |
| 948 |
if (rule.when.indicator == at_time::Wall) |
| 949 |
offset = info.offset; |
| 950 |
else if (rule.when.indicator == at_time::Standard) |
| 951 |
offset = ri.offset(); |
| 952 |
|
| 953 |
if (date.year() < rule.from) // rule doesn't apply yet at time t |
| 954 |
{ |
| 955 |
// Find first transition for this rule: |
| 956 |
rule_start = rule.start_time(rule.from, offset); |
| 957 |
} |
| 958 |
else // rule applies in the year that contains time t |
| 959 |
{ |
| 960 |
year y = date.year(); |
| 961 |
// Time the rule takes effect this year: |
| 962 |
rule_start = rule.start_time(y, offset); |
| 963 |
|
| 964 |
if (rule_start < t && rule.to > y) |
| 965 |
{ |
| 966 |
// Try this rule in the following year. |
| 967 |
rule_start = rule.start_time(++y, offset); |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
if (t < rule_start && rule_start < info.end) |
| 972 |
{ |
| 973 |
if (rule_start - t < days(1)) // XXX shouldn't be needed! |
| 974 |
continue; |
| 975 |
|
| 976 |
// Found a closer transition than the previous info.end. |
| 977 |
info.end = rule_start; |
| 978 |
next_rule = &rule; |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
format_abbrev_str(info, letters); |
| 983 |
|
| 984 |
bool merged = false; |
| 985 |
#if 0 |
| 986 |
if (!new_infos.empty()) |
| 987 |
{ |
| 988 |
auto& back = new_infos.back(); |
| 989 |
if (back.offset == info.offset && back.abbrev == info.abbrev |
| 990 |
&& back.save == info.save) |
| 991 |
{ |
| 992 |
// This is a continuation of the previous sys_info. |
| 993 |
back.end = info.end; |
| 994 |
merged = true; |
| 995 |
} |
| 996 |
} |
| 997 |
#endif |
| 998 |
|
| 999 |
if (next_rule) |
| 1000 |
letters = next_rule->letters; |
| 1001 |
else |
| 1002 |
letters = {}; |
| 1003 |
|
| 1004 |
if (!merged) |
| 1005 |
new_infos.emplace_back(info, letters); |
| 1006 |
|
| 1007 |
if (info.begin <= tp && tp < info.end) // Found the result. |
| 1008 |
result_index = new_infos.size() - 1; |
| 1009 |
else if (result_index >= 0 && !merged) |
| 1010 |
{ |
| 1011 |
// Finish on a DST sys_info if possible, so that if we resume |
| 1012 |
// generating sys_info objects after this time point, save=0 |
| 1013 |
// should be correct for the next sys_info. |
| 1014 |
if (num_after > 1 || info.save != 0min) |
| 1015 |
--num_after; |
| 1016 |
} |
| 1017 |
|
| 1018 |
info.begin = info.end; |
| 1019 |
if (next_rule) |
| 1020 |
{ |
| 1021 |
info.end = ri.until(); |
| 1022 |
info.offset = ri.offset() + next_rule->save; |
| 1023 |
info.save = duration_cast<minutes>(next_rule->save); |
| 1024 |
info.abbrev = ri.format(); |
| 1025 |
letters = next_rule->letters; |
| 1026 |
curr_rule = next_rule; |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
if (new_infos.empty() || result_index < 0) |
| 1031 |
__throw_runtime_error("time_zone::get_info"); |
| 1032 |
|
| 1033 |
info = new_infos[result_index].first; |
| 1034 |
|
| 1035 |
if (new_infos.back().first.end < ri.until()) |
| 1036 |
{ |
| 1037 |
// Insert the new sys_info objects but don't remove the rules_info. |
| 1038 |
infos.insert(i, new_infos.begin(), new_infos.end()); |
| 1039 |
} |
| 1040 |
else |
| 1041 |
{ |
| 1042 |
// Replace the rules_info at *i with the sys_info objects in new_infos. |
| 1043 |
|
| 1044 |
// First note the index of *i because we will invalidate i. |
| 1045 |
result_index = i - infos.begin(); |
| 1046 |
// Insert everything except new_infos.front() at the end of infos: |
| 1047 |
i = infos.insert(infos.end(), new_infos.begin() + 1, new_infos.end()); |
| 1048 |
// Then rotate those new elements into place: |
| 1049 |
std::rotate(infos.begin() + result_index + 1, i, infos.end()); |
| 1050 |
// Then replace the original rules_info object with new_infos.front(): |
| 1051 |
infos[result_index] = ZoneInfo(new_infos.front()); |
| 1052 |
// Decrement count of rule-based infos (might also release lock). |
| 1053 |
_M_impl->rules_counter.decrement(); |
| 1054 |
} |
| 1055 |
return info; |
| 1056 |
#else |
| 1057 |
return _M_impl->infos.info; |
| 1058 |
#endif // TZDB_DISABLED |
| 1059 |
} |
| 1060 |
|
| 1061 |
// Implementation of std::chrono::time_zone::get_info(const local_time<D>&) |
| 1062 |
local_info |
| 1063 |
time_zone::_M_get_local_info(local_seconds tp) const |
| 1064 |
{ |
| 1065 |
local_info info{}; |
| 1066 |
#ifndef TZDB_DISABLED |
| 1067 |
const auto node = _M_impl->node.lock(); |
| 1068 |
|
| 1069 |
// Get sys_info assuming no offset between local time and UTC: |
| 1070 |
info.first = _M_get_sys_info(sys_seconds(tp.time_since_epoch())); |
| 1071 |
|
| 1072 |
// Convert to UTC using the real offset: |
| 1073 |
sys_seconds st(tp.time_since_epoch() - info.first.offset); |
| 1074 |
|
| 1075 |
if ((st - info.first.begin) <= days(1)) |
| 1076 |
{ |
| 1077 |
sys_info prev = _M_get_sys_info(info.first.begin - 1s); |
| 1078 |
sys_seconds prevst(tp.time_since_epoch() - prev.offset); |
| 1079 |
if (st < info.first.begin) |
| 1080 |
{ |
| 1081 |
if (prevst < info.first.begin) |
| 1082 |
{ |
| 1083 |
// tp is a unique local time, prev is the correct sys_info. |
| 1084 |
info.first = prev; |
| 1085 |
} |
| 1086 |
else |
| 1087 |
{ |
| 1088 |
// The local time is nonexistent, falling within a clock change |
| 1089 |
// e.g. there is no 1:30am if DST moves clock from 1am to 2am. |
| 1090 |
__glibcxx_assert(prev.offset < info.first.offset); // start DST |
| 1091 |
info.result = local_info::nonexistent; |
| 1092 |
info.second = info.first; |
| 1093 |
info.first = prev; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
else if (prevst < info.first.begin) |
| 1097 |
{ |
| 1098 |
// The local time is ambiguous, referring to two possible UTC times |
| 1099 |
// e.g. 1:30am happens twice if clock moves back from 2am to 1am. |
| 1100 |
__glibcxx_assert(prev.offset > info.first.offset); // DST ended |
| 1101 |
info.result = local_info::ambiguous; |
| 1102 |
info.second = info.first; |
| 1103 |
info.first = prev; |
| 1104 |
} |
| 1105 |
// else tp is a unique local time, info.first is the correct sys_info. |
| 1106 |
} |
| 1107 |
else if ((info.first.end - st) <= days(1)) |
| 1108 |
{ |
| 1109 |
sys_info next = _M_get_sys_info(info.first.end); |
| 1110 |
sys_seconds nextst(tp.time_since_epoch() - next.offset); |
| 1111 |
if (st >= info.first.end) |
| 1112 |
{ |
| 1113 |
if (nextst >= info.first.end) |
| 1114 |
{ |
| 1115 |
// tp is a unique local time, next is the correct sys_info. |
| 1116 |
info.first = next; |
| 1117 |
} |
| 1118 |
else |
| 1119 |
{ |
| 1120 |
info.result = local_info::nonexistent; |
| 1121 |
info.second = next; |
| 1122 |
} |
| 1123 |
} |
| 1124 |
else if (nextst >= info.first.end) |
| 1125 |
{ |
| 1126 |
info.result = local_info::ambiguous; |
| 1127 |
info.second = next; |
| 1128 |
} |
| 1129 |
// else tp is a unique local time, info.first is the correct sys_info. |
| 1130 |
} |
| 1131 |
#else |
| 1132 |
info.first = _M_impl->infos.info; |
| 1133 |
#endif // TZDB_DISABLED |
| 1134 |
return info; |
| 1135 |
} |
| 1136 |
|
| 1137 |
#ifndef TZDB_DISABLED |
| 1138 |
namespace |
| 1139 |
{ |
| 1140 |
// If a zoneinfo directory is defined (either when the library was built, |
| 1141 |
// or via the zoneinfo_dir_override function) then append filename to it. |
| 1142 |
// The filename should have a leading '/' as one is not added explicitly. |
| 1143 |
string |
| 1144 |
zoneinfo_file(string_view filename) |
| 1145 |
{ |
| 1146 |
string path; |
| 1147 |
#pragma GCC diagnostic push |
| 1148 |
#pragma GCC diagnostic ignored "-Waddress" |
| 1149 |
if (__gnu_cxx::zoneinfo_dir_override) |
| 1150 |
{ |
| 1151 |
if (auto override_dir = __gnu_cxx::zoneinfo_dir_override()) |
| 1152 |
path = override_dir; |
| 1153 |
#pragma GCC diagnostic pop |
| 1154 |
} |
| 1155 |
#ifdef _GLIBCXX_ZONEINFO_DIR |
| 1156 |
else |
| 1157 |
path = _GLIBCXX_ZONEINFO_DIR; |
| 1158 |
#endif |
| 1159 |
if (!path.empty()) |
| 1160 |
path.append(filename); |
| 1161 |
return path; |
| 1162 |
} |
| 1163 |
|
| 1164 |
// N.B. Leading slash as required by zoneinfo_file function. |
| 1165 |
const string_view tzdata_file = "/tzdata.zi"; |
| 1166 |
const string_view leaps_file = "/leapseconds"; |
| 1167 |
|
| 1168 |
#ifdef _GLIBCXX_STATIC_TZDATA |
| 1169 |
// Static copy of tzdata.zi embedded in the library as tzdata_chars[] |
| 1170 |
#include "tzdata.zi.h" |
| 1171 |
#endif |
| 1172 |
|
| 1173 |
// An istream type that can read from a file or from a string. |
| 1174 |
struct tzdata_stream : istream |
| 1175 |
{ |
| 1176 |
// std::spanbuf not available until C++23 |
| 1177 |
struct ispanbuf : streambuf |
| 1178 |
{ |
| 1179 |
ispanbuf() : streambuf() |
| 1180 |
{ |
| 1181 |
#ifdef _GLIBCXX_STATIC_TZDATA |
| 1182 |
char* p = const_cast<char*>(tzdata_chars); |
| 1183 |
this->setg(p, p, p + std::size(tzdata_chars) - 1); |
| 1184 |
#endif |
| 1185 |
} |
| 1186 |
|
| 1187 |
// N.B. seekoff and seekpos not overridden, not currently needed. |
| 1188 |
}; |
| 1189 |
|
| 1190 |
union { |
| 1191 |
filebuf fb; |
| 1192 |
ispanbuf sb; |
| 1193 |
}; |
| 1194 |
|
| 1195 |
tzdata_stream() : istream(nullptr) |
| 1196 |
{ |
| 1197 |
if (string path = zoneinfo_file(tzdata_file); !path.empty()) |
| 1198 |
{ |
| 1199 |
filebuf fbuf; |
| 1200 |
if (fbuf.open(path, std::ios::in)) |
| 1201 |
{ |
| 1202 |
std::construct_at(&fb, std::move(fbuf)); |
| 1203 |
this->init(&fb); |
| 1204 |
return; |
| 1205 |
} |
| 1206 |
} |
| 1207 |
std::construct_at(&sb); |
| 1208 |
this->init(&sb); |
| 1209 |
} |
| 1210 |
|
| 1211 |
~tzdata_stream() { std::destroy_at(this->rdbuf()); } // use virtual dtor |
| 1212 |
|
| 1213 |
bool using_static_data() const { return this->rdbuf() == &sb; } |
| 1214 |
}; |
| 1215 |
} |
| 1216 |
#endif // TZDB_DISABLED |
| 1217 |
|
| 1218 |
// Return leap_second values, and a bool indicating whether the values are |
| 1219 |
// current (true), or potentially out of date (false). |
| 1220 |
pair<vector<leap_second>, bool> |
| 1221 |
tzdb_list::_Node::_S_read_leap_seconds() |
| 1222 |
{ |
| 1223 |
// This list is valid until at least 2026-12-28 00:00:00 UTC. |
| 1224 |
constexpr auto expires = sys_days{2026y/12/28}; |
| 1225 |
vector<leap_second> leaps |
| 1226 |
{ |
| 1227 |
(leap_second) 78796800, // 1 Jul 1972 |
| 1228 |
(leap_second) 94694400, // 1 Jan 1973 |
| 1229 |
(leap_second) 126230400, // 1 Jan 1974 |
| 1230 |
(leap_second) 157766400, // 1 Jan 1975 |
| 1231 |
(leap_second) 189302400, // 1 Jan 1976 |
| 1232 |
(leap_second) 220924800, // 1 Jan 1977 |
| 1233 |
(leap_second) 252460800, // 1 Jan 1978 |
| 1234 |
(leap_second) 283996800, // 1 Jan 1979 |
| 1235 |
(leap_second) 315532800, // 1 Jan 1980 |
| 1236 |
(leap_second) 362793600, // 1 Jul 1981 |
| 1237 |
(leap_second) 394329600, // 1 Jul 1982 |
| 1238 |
(leap_second) 425865600, // 1 Jul 1983 |
| 1239 |
(leap_second) 489024000, // 1 Jul 1985 |
| 1240 |
(leap_second) 567993600, // 1 Jan 1988 |
| 1241 |
(leap_second) 631152000, // 1 Jan 1990 |
| 1242 |
(leap_second) 662688000, // 1 Jan 1991 |
| 1243 |
(leap_second) 709948800, // 1 Jul 1992 |
| 1244 |
(leap_second) 741484800, // 1 Jul 1993 |
| 1245 |
(leap_second) 773020800, // 1 Jul 1994 |
| 1246 |
(leap_second) 820454400, // 1 Jan 1996 |
| 1247 |
(leap_second) 867715200, // 1 Jul 1997 |
| 1248 |
(leap_second) 915148800, // 1 Jan 1999 |
| 1249 |
(leap_second)1136073600, // 1 Jan 2006 |
| 1250 |
(leap_second)1230768000, // 1 Jan 2009 |
| 1251 |
(leap_second)1341100800, // 1 Jul 2012 |
| 1252 |
(leap_second)1435708800, // 1 Jul 2015 |
| 1253 |
(leap_second)1483228800, // 1 Jan 2017 |
| 1254 |
}; |
| 1255 |
|
| 1256 |
#if 0 |
| 1257 |
// This optimization isn't valid if the file has additional leap seconds |
| 1258 |
// defined since the library was compiled, but the system clock has been |
| 1259 |
// set to a time before the hardcoded expiration date. |
| 1260 |
if (system_clock::now() < expires) |
| 1261 |
return {std::move(leaps), true}; |
| 1262 |
#endif |
| 1263 |
|
| 1264 |
#ifndef TZDB_DISABLED |
| 1265 |
if (ifstream ls{zoneinfo_file(leaps_file)}) |
| 1266 |
{ |
| 1267 |
auto exp_year = year_month_day(expires).year(); |
| 1268 |
std::string s, w; |
| 1269 |
s.reserve(80); // Avoid later reallocations. |
| 1270 |
while (std::getline(ls, s)) |
| 1271 |
{ |
| 1272 |
// Leap YEAR MONTH DAY HH:MM:SS CORR R/S |
| 1273 |
|
| 1274 |
if (!s.starts_with("Leap")) |
| 1275 |
continue; |
| 1276 |
istringstream li(std::move(s)); |
| 1277 |
li.exceptions(ios::failbit); |
| 1278 |
li.ignore(4); |
| 1279 |
unsigned yval; |
| 1280 |
if (li >> yval) |
| 1281 |
{ |
| 1282 |
if (year y(yval); y >= exp_year) // Only process new entries. |
| 1283 |
{ |
| 1284 |
li >> w; |
| 1285 |
char m = w[0]; |
| 1286 |
if (m != 'J' && m != 'D') |
| 1287 |
return {std::move(leaps), false}; |
| 1288 |
|
| 1289 |
const int is_december = m == 'D'; |
| 1290 |
year_month_day ymd{y, month(6 + 6 * is_december), |
| 1291 |
day(30 + is_december)}; |
| 1292 |
sys_seconds secs(sys_days(ymd) + days(1)); |
| 1293 |
li >> w >> w >> m; |
| 1294 |
|
| 1295 |
if (m != '+' && m != '-') |
| 1296 |
return {std::move(leaps), false}; |
| 1297 |
|
| 1298 |
seconds::rep val = secs.time_since_epoch().count(); |
| 1299 |
if (m == '-') [[unlikely]] |
| 1300 |
val = -(val - 1); // -ve leap second happens at 23:59:59 |
| 1301 |
|
| 1302 |
if (leap_second ls{val}; ls > leaps.back()) |
| 1303 |
leaps.push_back(ls); |
| 1304 |
} |
| 1305 |
} |
| 1306 |
s = std::move(li).str(); // return storage to s |
| 1307 |
} |
| 1308 |
return {std::move(leaps), true}; |
| 1309 |
} |
| 1310 |
#endif |
| 1311 |
return {std::move(leaps), false}; |
| 1312 |
} |
| 1313 |
|
| 1314 |
#ifndef TZDB_DISABLED |
| 1315 |
namespace |
| 1316 |
{ |
| 1317 |
// Read the version number from a tzdata.zi file. |
| 1318 |
string |
| 1319 |
remote_version(istream& zif) |
| 1320 |
{ |
| 1321 |
char hash; |
| 1322 |
string label; |
| 1323 |
string version; |
| 1324 |
if (zif >> hash >> label >> version) |
| 1325 |
if (hash == '#' && label == "version") |
| 1326 |
return version; |
| 1327 |
#if 0 // Ignore these files, because we're not using them anyway. |
| 1328 |
#if defined __NetBSD__ |
| 1329 |
if (string ver; ifstream(zoneinfo_file("/TZDATA_VERSION")) >> ver) |
| 1330 |
return ver; |
| 1331 |
#elif defined __APPLE__ |
| 1332 |
// The standard install on macOS has no tzdata.zi, but we can find the |
| 1333 |
// version from +VERSION. |
| 1334 |
if (string ver; ifstream(zoneinfo_file("/+VERSION")) >> ver) |
| 1335 |
return ver; |
| 1336 |
#endif |
| 1337 |
#endif |
| 1338 |
__throw_runtime_error("tzdb: no version found in tzdata.zi"); |
| 1339 |
} |
| 1340 |
} |
| 1341 |
#endif |
| 1342 |
|
| 1343 |
// Definition of std::chrono::remote_version() |
| 1344 |
string remote_version() |
| 1345 |
{ |
| 1346 |
#ifndef TZDB_DISABLED |
| 1347 |
tzdata_stream zif; |
| 1348 |
return remote_version(zif); |
| 1349 |
#else |
| 1350 |
__throw_disabled(); |
| 1351 |
#endif |
| 1352 |
} |
| 1353 |
|
| 1354 |
// Used by chrono::reload_tzdb() to add a new node to the front of the list. |
| 1355 |
const tzdb& |
| 1356 |
tzdb_list::_Node::_S_replace_head(shared_ptr<_Node> curr [[maybe_unused]], |
| 1357 |
shared_ptr<_Node> new_head) |
| 1358 |
{ |
| 1359 |
_Node* new_head_ptr = new_head.get(); |
| 1360 |
#if USE_ATOMIC_SHARED_PTR |
| 1361 |
new_head_ptr->next = curr; |
| 1362 |
while (!_S_head_owner.compare_exchange_strong(curr, new_head)) |
| 1363 |
{ |
| 1364 |
if (curr->db.version == new_head_ptr->db.version) |
| 1365 |
return curr->db; |
| 1366 |
new_head_ptr->next = curr; |
| 1367 |
} |
| 1368 |
// XXX small window here where _S_head_cache still points to previous tzdb. |
| 1369 |
#else |
| 1370 |
lock_guard<mutex> l(list_mutex()); |
| 1371 |
if (const _Node* h = _S_head_owner.get()) |
| 1372 |
{ |
| 1373 |
if (h->db.version == new_head_ptr->db.version) |
| 1374 |
return h->db; |
| 1375 |
new_head_ptr->next = _S_head_owner; |
| 1376 |
} |
| 1377 |
_S_head_owner = std::move(new_head); |
| 1378 |
#endif |
| 1379 |
_S_cache_list_head(new_head_ptr); |
| 1380 |
return new_head_ptr->db; |
| 1381 |
} |
| 1382 |
|
| 1383 |
// Called to populate the list for the first time. If reload_tzdb() fails, |
| 1384 |
// it creates a tzdb that only contains the UTC and GMT time zones. |
| 1385 |
const tzdb& |
| 1386 |
tzdb_list::_Node::_S_init_tzdb() |
| 1387 |
{ |
| 1388 |
#ifndef TZDB_DISABLED |
| 1389 |
__try |
| 1390 |
{ |
| 1391 |
return reload_tzdb(); |
| 1392 |
} |
| 1393 |
__catch (const std::exception&) |
| 1394 |
#endif |
| 1395 |
{ |
| 1396 |
auto [leaps, ok] = _S_read_leap_seconds(); |
| 1397 |
|
| 1398 |
using Node = tzdb_list::_Node; |
| 1399 |
auto node = std::make_shared<tzdb_list::_Node>(); |
| 1400 |
node->db.version = "ersatz"; |
| 1401 |
node->db.leap_seconds = std::move(leaps); |
| 1402 |
node->db.zones.reserve(2); |
| 1403 |
node->db.links.reserve(7); |
| 1404 |
|
| 1405 |
time_zone zone(nullptr); |
| 1406 |
time_zone_link link(nullptr); |
| 1407 |
sys_info info{sys_seconds::min(), sys_seconds::max(), 0s, 0min, ""}; |
| 1408 |
|
| 1409 |
zone._M_impl = std::make_unique<time_zone::_Impl>(node); |
| 1410 |
zone._M_name = "Etc/UTC"; |
| 1411 |
info.abbrev = "UTC"; |
| 1412 |
zone._M_impl->infos.push_back(std::move(info)); |
| 1413 |
|
| 1414 |
link._M_target = zone._M_name; |
| 1415 |
link._M_name = "UTC"; |
| 1416 |
node->db.links.push_back(std::move(link)); |
| 1417 |
for (auto name : {"Etc/UCT", "Etc/Universal", "Etc/Zulu"}) |
| 1418 |
{ |
| 1419 |
link._M_target = zone._M_name; |
| 1420 |
link._M_name = name; |
| 1421 |
node->db.links.push_back(std::move(link)); |
| 1422 |
link._M_target = zone._M_name; |
| 1423 |
link._M_name = name + 4; |
| 1424 |
node->db.links.push_back(std::move(link)); |
| 1425 |
} |
| 1426 |
node->db.zones.emplace_back(std::move(zone)); |
| 1427 |
|
| 1428 |
zone._M_impl = std::make_unique<time_zone::_Impl>(node); |
| 1429 |
zone._M_name = "Etc/GMT"; |
| 1430 |
info.abbrev = "GMT"; |
| 1431 |
zone._M_impl->infos.push_back(std::move(info)); |
| 1432 |
|
| 1433 |
link._M_target = zone._M_name; |
| 1434 |
link._M_name = "GMT"; |
| 1435 |
node->db.links.push_back(std::move(link)); |
| 1436 |
for (auto name : {"Etc/GMT+0", "Etc/GMT-0", "Etc/GMT0", "Etc/Greenwich"}) |
| 1437 |
{ |
| 1438 |
link._M_target = zone._M_name; |
| 1439 |
link._M_name = name; |
| 1440 |
node->db.links.push_back(std::move(link)); |
| 1441 |
link._M_target = zone._M_name; |
| 1442 |
link._M_name = name + 4; |
| 1443 |
node->db.links.push_back(std::move(link)); |
| 1444 |
} |
| 1445 |
node->db.zones.emplace_back(std::move(zone)); |
| 1446 |
|
| 1447 |
ranges::sort(node->db.zones); |
| 1448 |
ranges::sort(node->db.links); |
| 1449 |
return Node::_S_replace_head(nullptr, std::move(node)); |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
// There are only three ways for users to access the tzdb list. |
| 1454 |
// get_tzdb_list() returns a reference to the list itself. |
| 1455 |
// get_tzdb() returns a reference to the front of the list. |
| 1456 |
// reload_tzdb() returns a reference to the (possibly new) front of the list. |
| 1457 |
// Those are the only functions that need to check whether the list has |
| 1458 |
// been populated already. |
| 1459 |
|
| 1460 |
// Implementation of std::chrono::get_tzdb_list() |
| 1461 |
tzdb_list& |
| 1462 |
get_tzdb_list() |
| 1463 |
{ |
| 1464 |
using Node = tzdb_list::_Node; |
| 1465 |
if (Node::_S_list_head(memory_order::acquire) == nullptr) [[unlikely]] |
| 1466 |
Node::_S_init_tzdb(); // populates list |
| 1467 |
return Node::_S_the_list; |
| 1468 |
} |
| 1469 |
|
| 1470 |
// Implementation of std::chrono::get_tzdb() |
| 1471 |
const tzdb& |
| 1472 |
get_tzdb() |
| 1473 |
{ |
| 1474 |
using Node = tzdb_list::_Node; |
| 1475 |
if (auto* __p = Node::_S_list_head(memory_order::acquire)) [[likely]] |
| 1476 |
return __p->db; |
| 1477 |
return Node::_S_init_tzdb(); // populates list |
| 1478 |
} |
| 1479 |
|
| 1480 |
// Implementation of std::chrono::reload_tzdb() |
| 1481 |
const tzdb& |
| 1482 |
reload_tzdb() |
| 1483 |
{ |
| 1484 |
#ifndef TZDB_DISABLED |
| 1485 |
using Node = tzdb_list::_Node; |
| 1486 |
|
| 1487 |
tzdata_stream zif; |
| 1488 |
const string version = remote_version(zif); |
| 1489 |
|
| 1490 |
#if USE_ATOMIC_SHARED_PTR |
| 1491 |
auto head = Node::_S_head_owner.load(memory_order::acquire); |
| 1492 |
if (head != nullptr && head->db.version == version) |
| 1493 |
return head->db; |
| 1494 |
#else |
| 1495 |
if (Node::_S_list_head(memory_order::relaxed) != nullptr) [[likely]] |
| 1496 |
{ |
| 1497 |
lock_guard<mutex> l(list_mutex()); |
| 1498 |
const tzdb& current = Node::_S_head_owner->db; |
| 1499 |
if (current.version == version) |
| 1500 |
return current; |
| 1501 |
} |
| 1502 |
shared_ptr<Node> head; // Passed as unused arg to _S_replace_head. |
| 1503 |
#endif |
| 1504 |
|
| 1505 |
auto [leaps, leaps_ok] = Node::_S_read_leap_seconds(); |
| 1506 |
if (!leaps_ok && !zif.using_static_data()) |
| 1507 |
__throw_runtime_error("tzdb: cannot parse leapseconds file"); |
| 1508 |
|
| 1509 |
auto node = std::make_shared<Node>(); |
| 1510 |
node->db.version = std::move(version); |
| 1511 |
node->db.leap_seconds = std::move(leaps); |
| 1512 |
|
| 1513 |
string line, type; |
| 1514 |
line.reserve(80); // Maximum allowed line is 511 but much less in practice. |
| 1515 |
istringstream is; |
| 1516 |
is.exceptions(ios::failbit); |
| 1517 |
int lineno = 0; |
| 1518 |
while (std::getline(zif, line)) |
| 1519 |
{ |
| 1520 |
++lineno; |
| 1521 |
if (line.empty()) |
| 1522 |
continue; |
| 1523 |
is.str(std::move(line)); |
| 1524 |
is.clear(); |
| 1525 |
ws(is); |
| 1526 |
int c = is.eof() ? '#' : is.peek(); |
| 1527 |
__try |
| 1528 |
{ |
| 1529 |
switch (c) |
| 1530 |
{ |
| 1531 |
case '#': |
| 1532 |
break; |
| 1533 |
case 'R': |
| 1534 |
{ |
| 1535 |
// Rule NAME FROM TO TYPE IN ON AT SAVE LETTER/S |
| 1536 |
is >> type; // extract the "Rule" or "R" marker |
| 1537 |
Rule rule; |
| 1538 |
is >> rule; |
| 1539 |
node->rules.push_back(std::move(rule)); |
| 1540 |
break; |
| 1541 |
} |
| 1542 |
case 'L': |
| 1543 |
{ |
| 1544 |
// Link TARGET LINK-NAME |
| 1545 |
is >> type; // extract the "Link" or "L" marker |
| 1546 |
time_zone_link link(nullptr); |
| 1547 |
is >> quoted(link._M_target) >> quoted(link._M_name); |
| 1548 |
node->db.links.push_back(std::move(link)); |
| 1549 |
break; |
| 1550 |
} |
| 1551 |
case 'Z': |
| 1552 |
{ |
| 1553 |
// Zone NAME STDOFF RULES FORMAT [UNTIL] |
| 1554 |
is >> type; // extract the "Zone" or "Z" marker |
| 1555 |
time_zone tz(std::make_unique<time_zone::_Impl>(node)); |
| 1556 |
is >> quoted(tz._M_name); |
| 1557 |
node->db.zones.push_back(time_zone(std::move(tz))); |
| 1558 |
[[fallthrough]]; // Use default case to parse rest of line ... |
| 1559 |
} |
| 1560 |
default: // Continuation of the previous Zone line. |
| 1561 |
{ |
| 1562 |
// STDOFF RULES FORMAT [UNTIL] |
| 1563 |
if (type[0] != 'Z') |
| 1564 |
is.setstate(ios::failbit); |
| 1565 |
|
| 1566 |
auto& impl = *node->db.zones.back()._M_impl; |
| 1567 |
ZoneInfo& info = impl.infos.emplace_back(); |
| 1568 |
is >> info; |
| 1569 |
|
| 1570 |
// Keep count of ZoneInfo objects that refer to named Rules. |
| 1571 |
if (!info.rules().empty()) |
| 1572 |
impl.rules_counter.increment(); |
| 1573 |
} |
| 1574 |
} |
| 1575 |
} |
| 1576 |
__catch (const ios::failure&) |
| 1577 |
{ |
| 1578 |
ostringstream ss; |
| 1579 |
ss << "std::chrono::reload_tzdb: parse error at line " << lineno |
| 1580 |
<< ": " << std::move(is).str(); |
| 1581 |
__throw_runtime_error(std::move(ss).str().c_str()); |
| 1582 |
} |
| 1583 |
|
| 1584 |
line = std::move(is).str(); // return storage to line |
| 1585 |
} |
| 1586 |
|
| 1587 |
ranges::sort(node->db.zones, {}, &time_zone::name); |
| 1588 |
ranges::sort(node->db.links, {}, &time_zone_link::name); |
| 1589 |
ranges::stable_sort(node->rules, {}, &Rule::name); |
| 1590 |
|
| 1591 |
return Node::_S_replace_head(std::move(head), std::move(node)); |
| 1592 |
#else |
| 1593 |
__throw_disabled(); |
| 1594 |
#endif // TZDB_DISABLED |
| 1595 |
} |
| 1596 |
|
| 1597 |
// Any call to tzdb_list::front() or tzdb_list::begin() must follow |
| 1598 |
// a call to get_tzdb_list() so the list has already been populated. |
| 1599 |
|
| 1600 |
// Implementation of std::chrono::tzdb_list::front(). |
| 1601 |
const tzdb& |
| 1602 |
tzdb_list::front() const noexcept |
| 1603 |
{ |
| 1604 |
return _Node::_S_list_head(memory_order::seq_cst)->db; |
| 1605 |
} |
| 1606 |
|
| 1607 |
// Implementation of std::chrono::tzdb_list::begin(). |
| 1608 |
auto |
| 1609 |
tzdb_list::begin() const noexcept |
| 1610 |
-> const_iterator |
| 1611 |
{ |
| 1612 |
#if USE_ATOMIC_SHARED_PTR |
| 1613 |
return const_iterator{_Node::_S_head_owner.load()}; |
| 1614 |
#else |
| 1615 |
lock_guard<mutex> l(list_mutex()); |
| 1616 |
return const_iterator{_Node::_S_head_owner}; |
| 1617 |
#endif |
| 1618 |
} |
| 1619 |
|
| 1620 |
// Implementation of std::chrono::tzdb_list::erase_after(const_iterator). |
| 1621 |
auto |
| 1622 |
tzdb_list::erase_after(const_iterator p) |
| 1623 |
-> const_iterator |
| 1624 |
{ |
| 1625 |
if (p._M_node) [[likely]] |
| 1626 |
{ |
| 1627 |
#if ! USE_ATOMIC_SHARED_PTR |
| 1628 |
lock_guard<mutex> l(list_mutex()); |
| 1629 |
#endif |
| 1630 |
if (auto next = p._M_node->next) [[likely]] |
| 1631 |
return const_iterator{p._M_node->next = std::move(next->next)}; |
| 1632 |
} |
| 1633 |
|
| 1634 |
// This is undefined, but let's be kind: |
| 1635 |
std::__throw_logic_error("std::tzdb_list::erase_after: iterator is not " |
| 1636 |
"dereferenceable"); |
| 1637 |
} |
| 1638 |
|
| 1639 |
// Private constructor for tzdb_list::const_iterator. |
| 1640 |
// Only used within this file, so can be inline. |
| 1641 |
inline |
| 1642 |
tzdb_list:: |
| 1643 |
const_iterator::const_iterator(const shared_ptr<_Node>& __p) noexcept |
| 1644 |
: _M_node(__p) |
| 1645 |
{ } |
| 1646 |
|
| 1647 |
// Implementation of std::chrono::tzdb_list::const_iterator::operator*(). |
| 1648 |
auto |
| 1649 |
tzdb_list::const_iterator::operator*() const noexcept |
| 1650 |
-> reference |
| 1651 |
{ |
| 1652 |
return _M_node->db; |
| 1653 |
} |
| 1654 |
|
| 1655 |
// Implementation of std::chrono::tzdb_list::const_iterator::operator++(). |
| 1656 |
auto |
| 1657 |
tzdb_list::const_iterator::operator++() |
| 1658 |
-> const_iterator& |
| 1659 |
{ |
| 1660 |
auto cur = std::move(_M_node); |
| 1661 |
_M_node = cur->next; |
| 1662 |
return *this; |
| 1663 |
} |
| 1664 |
|
| 1665 |
// Implementation of std::chrono::tzdb_list::const_iterator::operator++(int). |
| 1666 |
auto |
| 1667 |
tzdb_list::const_iterator::operator++(int) |
| 1668 |
-> const_iterator |
| 1669 |
{ |
| 1670 |
auto tmp = std::move(*this); |
| 1671 |
_M_node = tmp._M_node->next; |
| 1672 |
return tmp; |
| 1673 |
} |
| 1674 |
|
| 1675 |
namespace |
| 1676 |
{ |
| 1677 |
const time_zone* |
| 1678 |
do_locate_zone(const vector<time_zone>& zones, |
| 1679 |
const vector<time_zone_link>& links, |
| 1680 |
string_view tz_name) |
| 1681 |
{ |
| 1682 |
// Lambda mangling changed between -fabi-version=2 and -fabi-version=18 |
| 1683 |
auto search = []<class Vec>(const Vec& v, string_view name) { |
| 1684 |
auto pos = ranges::lower_bound(v, name, {}, &Vec::value_type::name); |
| 1685 |
auto ptr = pos.base(); |
| 1686 |
if (pos == v.end() || pos->name() != name) |
| 1687 |
ptr = nullptr; |
| 1688 |
return ptr; |
| 1689 |
}; |
| 1690 |
|
| 1691 |
// Search zones first. |
| 1692 |
if (auto tz = search(zones, tz_name)) |
| 1693 |
return tz; |
| 1694 |
|
| 1695 |
// Search links second. |
| 1696 |
if (auto tz_l = search(links, tz_name)) |
| 1697 |
{ |
| 1698 |
// Handle the common case of a link that has a zone as the target. |
| 1699 |
if (auto tz = search(zones, tz_l->target())) [[likely]] |
| 1700 |
return tz; |
| 1701 |
|
| 1702 |
// Either tz_l->target() doesn't exist, or we have a chain of links. |
| 1703 |
// Use Floyd's cycle-finding algorithm to avoid infinite loops, |
| 1704 |
// at the cost of extra lookups. In the common case we expect a |
| 1705 |
// chain of links to be short so the loop won't run many times. |
| 1706 |
// In particular, the duplicate lookups to move the tortoise |
| 1707 |
// never happen unless the chain has four or more links. |
| 1708 |
// When a chain contains a cycle we do multiple duplicate lookups, |
| 1709 |
// but that case should never happen with correct tzdata.zi, |
| 1710 |
// so there's no need to optimize cycle detection. |
| 1711 |
|
| 1712 |
const time_zone_link* tortoise = tz_l; |
| 1713 |
const time_zone_link* hare = search(links, tz_l->target()); |
| 1714 |
while (hare) |
| 1715 |
{ |
| 1716 |
// Chains should be short, so first check if it ends here: |
| 1717 |
if (auto tz = search(zones, hare->target())) [[likely]] |
| 1718 |
return tz; |
| 1719 |
|
| 1720 |
// Otherwise follow the chain: |
| 1721 |
hare = search(links, hare->target()); |
| 1722 |
if (!hare) |
| 1723 |
break; |
| 1724 |
|
| 1725 |
// Again, first check if the chain ends at a zone here: |
| 1726 |
if (auto tz = search(zones, hare->target())) [[likely]] |
| 1727 |
return tz; |
| 1728 |
|
| 1729 |
// Follow the chain again: |
| 1730 |
hare = search(links, hare->target()); |
| 1731 |
|
| 1732 |
if (hare == tortoise) |
| 1733 |
{ |
| 1734 |
string_view err = "std::chrono::tzdb: link cycle: "; |
| 1735 |
string str; |
| 1736 |
str.reserve(err.size() + tz_name.size()); |
| 1737 |
str += err; |
| 1738 |
str += tz_name; |
| 1739 |
__throw_runtime_error(str.c_str()); |
| 1740 |
} |
| 1741 |
// Plod along the chain one step: |
| 1742 |
tortoise = search(links, tortoise->target()); |
| 1743 |
} |
| 1744 |
} |
| 1745 |
|
| 1746 |
return nullptr; // not found |
| 1747 |
} |
| 1748 |
} // namespace |
| 1749 |
|
| 1750 |
// Implementation of std::chrono::tzdb::locate_zone(string_view). |
| 1751 |
const time_zone* |
| 1752 |
tzdb::locate_zone(string_view tz_name) const |
| 1753 |
{ |
| 1754 |
if (auto tz = do_locate_zone(zones, links, tz_name)) |
| 1755 |
return tz; |
| 1756 |
string_view err = "std::chrono::tzdb: cannot locate zone: "; |
| 1757 |
string str; |
| 1758 |
str.reserve(err.size() + tz_name.size()); |
| 1759 |
str += err; |
| 1760 |
str += tz_name; |
| 1761 |
__throw_runtime_error(str.c_str()); |
| 1762 |
} |
| 1763 |
|
| 1764 |
// Implementation of std::chrono::tzdb::current_zone(). |
| 1765 |
const time_zone* |
| 1766 |
tzdb::current_zone() const |
| 1767 |
{ |
| 1768 |
// TODO cache this function's result? |
| 1769 |
// Could check the modification time of /etc/localtime, and not re-read |
| 1770 |
// it if it hasn't changed. reload_tzdb() could clear the cache too, |
| 1771 |
// to have a way to force a re-read. |
| 1772 |
|
| 1773 |
#ifndef _AIX |
| 1774 |
#if defined(_GLIBCXX_HAVE_READLINK) && defined(_GLIBCXX_HAVE_UNISTD_H) |
| 1775 |
string_view str; |
| 1776 |
char buf[128]; // strlen("../usr/share/zoneinfo/...") is usually < 55 |
| 1777 |
string dynbuf; |
| 1778 |
// /etc/localtime should be a symlink that ends with a zone name, |
| 1779 |
// e.g. /etc/localtime -> /usr/share/zoneinfo/Europe/London |
| 1780 |
// https://www.freedesktop.org/software/systemd/man/latest/localtime.html |
| 1781 |
// This should work on GNU/Linux, macOS, NetBSD, and OpenBSD. |
| 1782 |
// Some FreeBSD systems also use a symlink for /etc/localtime. |
| 1783 |
// Use readlink directly to avoid std::filesystem overhead. |
| 1784 |
if (auto n = ::readlink("/etc/localtime", buf, sizeof(buf)); n > 0) |
| 1785 |
{ |
| 1786 |
if (static_cast<size_t>(n) < sizeof(buf)) |
| 1787 |
str = string_view(buf, n); |
| 1788 |
else [[unlikely]] |
| 1789 |
{ |
| 1790 |
// We read the symlink but it didn't fit in buf[], use dynbuf. |
| 1791 |
do |
| 1792 |
{ |
| 1793 |
n *= 2; |
| 1794 |
dynbuf.__resize_and_overwrite(n, [](char* p, size_t len) { |
| 1795 |
auto n2 = ::readlink("/etc/localtime", p, len); |
| 1796 |
if (n2 == -1) // symlink removed or replaced by file?! |
| 1797 |
__throw_runtime_error("tzdb: error reading /etc/localtime"); |
| 1798 |
const size_t r = n2; |
| 1799 |
return r < len ? r : 0; |
| 1800 |
}); |
| 1801 |
} |
| 1802 |
while (dynbuf.empty()); |
| 1803 |
str = dynbuf; |
| 1804 |
} |
| 1805 |
} |
| 1806 |
|
| 1807 |
if (!str.empty()) |
| 1808 |
{ |
| 1809 |
// Remove any redundant slashes so we can match zone names. |
| 1810 |
// e.g. /usr/share/zoneinfo/Europe//London is a valid symlink, |
| 1811 |
// but won't match against "Europe/London". |
| 1812 |
if (auto pos = str.rfind("//"); pos != str.npos) [[unlikely]] |
| 1813 |
{ |
| 1814 |
if (str.data() != dynbuf.data()) |
| 1815 |
dynbuf = str; |
| 1816 |
string::size_type spos = pos; |
| 1817 |
do |
| 1818 |
{ |
| 1819 |
dynbuf.erase(spos, 1); |
| 1820 |
spos = dynbuf.rfind("//", spos); |
| 1821 |
} |
| 1822 |
while (spos != dynbuf.npos); |
| 1823 |
str = dynbuf; |
| 1824 |
} |
| 1825 |
|
| 1826 |
// Check the trailing components of the path against known zone names. |
| 1827 |
// Valid IANA times zones can have one, two, or three parts, e.g. |
| 1828 |
// "UTC", "Europe/London", and "America/Indiana/Indianapolis". |
| 1829 |
// Custom tzdata.zi files could in theory use four or more parts. |
| 1830 |
|
| 1831 |
auto pos = str.rfind('/'); |
| 1832 |
while (pos != str.npos && pos != 0) |
| 1833 |
{ |
| 1834 |
if (auto tz = do_locate_zone(this->zones, this->links, |
| 1835 |
str.substr(pos + 1))) |
| 1836 |
return tz; |
| 1837 |
pos = str.rfind('/', pos - 1); |
| 1838 |
} |
| 1839 |
// If we didn't match yet, try once more so that we will match |
| 1840 |
// a symlink to a relative path such as "Europe/London" |
| 1841 |
// or symlink to an absolute path such as "/Europe/London". |
| 1842 |
// Both cases seem unlikely because it would require either |
| 1843 |
// /etc/Europe or /Europe to be a directory (or a symlink to one) |
| 1844 |
// containing the TZif files, but it's theoretically possible. |
| 1845 |
// If pos==npos then pos+1 wraps to 0 and we use the whole string. |
| 1846 |
// If pos==0 then substr(1) discards the leading slash. |
| 1847 |
if (auto tz = do_locate_zone(this->zones, this->links, |
| 1848 |
str.substr(pos + 1))) |
| 1849 |
return tz; |
| 1850 |
} |
| 1851 |
#endif |
| 1852 |
// Otherwise, look for a file naming the time zone. |
| 1853 |
string_view files[] { |
| 1854 |
"/etc/timezone", // Debian derivates |
| 1855 |
"/var/db/zoneinfo", // FreeBSD |
| 1856 |
}; |
| 1857 |
for (auto f : files) |
| 1858 |
{ |
| 1859 |
std::ifstream tzf{string{f}}; |
| 1860 |
if (std::string name; std::getline(tzf, name)) |
| 1861 |
if (auto tz = do_locate_zone(this->zones, this->links, name)) |
| 1862 |
return tz; |
| 1863 |
} |
| 1864 |
|
| 1865 |
if (ifstream tzf{"/etc/sysconfig/clock"}) |
| 1866 |
{ |
| 1867 |
string line; |
| 1868 |
// Old versions of Suse use TIMEZONE. Old versions of RHEL use ZONE. |
| 1869 |
const string_view keys[] = { "TIMEZONE=" , "ZONE=" }; |
| 1870 |
while (std::getline(tzf, line)) |
| 1871 |
for (string_view key : keys) |
| 1872 |
if (line.starts_with(key)) |
| 1873 |
{ |
| 1874 |
string_view name = line; |
| 1875 |
name.remove_prefix(key.size()); |
| 1876 |
if (name.size() != 0 && name.front() == '"') |
| 1877 |
{ |
| 1878 |
name.remove_prefix(1); |
| 1879 |
if (auto pos = name.find('"'); pos != name.npos) |
| 1880 |
name = name.substr(0, pos); |
| 1881 |
} |
| 1882 |
if (auto tz = do_locate_zone(this->zones, this->links, name)) |
| 1883 |
return tz; |
| 1884 |
} |
| 1885 |
} |
| 1886 |
|
| 1887 |
// FIXME: For DragonFly BSD /etc/localtime is a copy of one of the |
| 1888 |
// zone files in /usr/share/zoneinfo so we need to compare its contents |
| 1889 |
// to each one until we find a match. |
| 1890 |
|
| 1891 |
#else // _AIX |
| 1892 |
// AIX stores current zone in $TZ in /etc/environment but the value |
| 1893 |
// is typically a POSIX time zone name, not IANA zone. |
| 1894 |
// https://developer.ibm.com/articles/au-aix-posix/ |
| 1895 |
// https://www.ibm.com/support/pages/managing-time-zone-variable-posix |
| 1896 |
if (const char* env = std::getenv("TZ")) |
| 1897 |
{ |
| 1898 |
// This will fail unless TZ contains an IANA time zone name. |
| 1899 |
if (auto tz = do_locate_zone(this->zones, this->links, env)) |
| 1900 |
return tz; |
| 1901 |
} |
| 1902 |
#endif |
| 1903 |
|
| 1904 |
// Default to UTC. |
| 1905 |
if (auto tz = do_locate_zone(this->zones, this->links, "UTC")) |
| 1906 |
return tz; |
| 1907 |
|
| 1908 |
__throw_runtime_error("tzdb: cannot determine current zone"); |
| 1909 |
} |
| 1910 |
|
| 1911 |
// Implementation of std::chrono::locate_zone(string_view) |
| 1912 |
// TODO define this inline in the header instead? |
| 1913 |
const time_zone* |
| 1914 |
locate_zone(string_view tz_name) |
| 1915 |
{ |
| 1916 |
// Use begin() so the tzdb cannot be erased while this operation runs. |
| 1917 |
return get_tzdb_list().begin()->locate_zone(tz_name); |
| 1918 |
} |
| 1919 |
|
| 1920 |
// Implementation of std::chrono::current_zone() |
| 1921 |
// TODO define this inline in the header instead? |
| 1922 |
const time_zone* |
| 1923 |
current_zone() |
| 1924 |
{ |
| 1925 |
// Use begin() so the tzdb cannot be erased while this operation runs. |
| 1926 |
return get_tzdb_list().begin()->current_zone(); |
| 1927 |
} |
| 1928 |
|
| 1929 |
#ifndef TZDB_DISABLED |
| 1930 |
namespace |
| 1931 |
{ |
| 1932 |
istream& operator>>(istream& in, abbrev_month& am) |
| 1933 |
{ |
| 1934 |
string s; |
| 1935 |
in >> s; |
| 1936 |
switch (s[0]) |
| 1937 |
{ |
| 1938 |
case 'J': |
| 1939 |
switch (s[1]) |
| 1940 |
{ |
| 1941 |
case 'a': |
| 1942 |
am.m = January; |
| 1943 |
return in; |
| 1944 |
case 'u': |
| 1945 |
switch (s[2]) |
| 1946 |
{ |
| 1947 |
case 'n': |
| 1948 |
am.m = June; |
| 1949 |
return in; |
| 1950 |
case 'l': |
| 1951 |
am.m = July; |
| 1952 |
return in; |
| 1953 |
} |
| 1954 |
break; |
| 1955 |
} |
| 1956 |
break; |
| 1957 |
case 'F': |
| 1958 |
am.m = February; |
| 1959 |
return in; |
| 1960 |
case 'M': |
| 1961 |
if (s[1] == 'a') [[likely]] |
| 1962 |
switch (s[2]) |
| 1963 |
{ |
| 1964 |
case 'r': |
| 1965 |
am.m = March; |
| 1966 |
return in; |
| 1967 |
case 'y': |
| 1968 |
am.m = May; |
| 1969 |
return in; |
| 1970 |
} |
| 1971 |
break; |
| 1972 |
case 'A': |
| 1973 |
switch (s[1]) |
| 1974 |
{ |
| 1975 |
case 'p': |
| 1976 |
am.m = April; |
| 1977 |
return in; |
| 1978 |
case 'u': |
| 1979 |
am.m = August; |
| 1980 |
return in; |
| 1981 |
} |
| 1982 |
break; |
| 1983 |
case 'S': |
| 1984 |
am.m = September; |
| 1985 |
return in; |
| 1986 |
case 'O': |
| 1987 |
am.m = October; |
| 1988 |
return in; |
| 1989 |
case 'N': |
| 1990 |
am.m = November; |
| 1991 |
return in; |
| 1992 |
case 'D': |
| 1993 |
am.m = December; |
| 1994 |
return in; |
| 1995 |
} |
| 1996 |
in.setstate(ios::failbit); |
| 1997 |
return in; |
| 1998 |
} |
| 1999 |
|
| 2000 |
// Wrapper for chrono::weekday that can be extracted from an istream |
| 2001 |
// as an abbreviated weekday name. |
| 2002 |
// The weekday name can be any unambiguous portion of a weekday name, |
| 2003 |
// e.g. "M" (Monday) or "Su" (Sunday), but not "T" (Tuesday/Thursday). |
| 2004 |
struct abbrev_weekday |
| 2005 |
{ |
| 2006 |
weekday wd; |
| 2007 |
|
| 2008 |
friend istream& operator>>(istream& in, abbrev_weekday& aw) |
| 2009 |
{ |
| 2010 |
// Do not read a whole word from the stream, in some cases |
| 2011 |
// the weekday is only part of a larger word like "Sun<=25". |
| 2012 |
// Just peek at one char at a time. |
| 2013 |
switch (in.peek()) |
| 2014 |
{ |
| 2015 |
case 'M': |
| 2016 |
aw.wd = Monday; |
| 2017 |
break; |
| 2018 |
case 'T': |
| 2019 |
in.ignore(1); // Discard the 'T' |
| 2020 |
switch (in.peek()) |
| 2021 |
{ |
| 2022 |
case 'u': |
| 2023 |
aw.wd = Tuesday; |
| 2024 |
break; |
| 2025 |
case 'h': |
| 2026 |
aw.wd = Thursday; |
| 2027 |
break; |
| 2028 |
default: |
| 2029 |
in.setstate(ios::failbit); |
| 2030 |
} |
| 2031 |
break; |
| 2032 |
case 'W': |
| 2033 |
aw.wd = Wednesday; |
| 2034 |
break; |
| 2035 |
case 'F': |
| 2036 |
aw.wd = Friday; |
| 2037 |
break; |
| 2038 |
case 'S': |
| 2039 |
in.ignore(1); // Discard the 'S' |
| 2040 |
switch (in.peek()) |
| 2041 |
{ |
| 2042 |
case 'a': |
| 2043 |
aw.wd = Saturday; |
| 2044 |
break; |
| 2045 |
case 'u': |
| 2046 |
aw.wd = Sunday; |
| 2047 |
break; |
| 2048 |
default: |
| 2049 |
in.setstate(ios::failbit); |
| 2050 |
} |
| 2051 |
break; |
| 2052 |
default: |
| 2053 |
in.setstate(ios::failbit); |
| 2054 |
} |
| 2055 |
in.ignore(1); // Discard whichever char we just looked at. |
| 2056 |
|
| 2057 |
// Discard any remaining chars from weekday, e.g. "onday". |
| 2058 |
string_view day_chars = "ondayesritu"; |
| 2059 |
auto is_day_char = [&day_chars](int c) { |
| 2060 |
return c != char_traits<char>::eof() |
| 2061 |
&& day_chars.find((char)c) != day_chars.npos; |
| 2062 |
}; |
| 2063 |
while (is_day_char(in.peek())) |
| 2064 |
in.ignore(1); |
| 2065 |
|
| 2066 |
return in; |
| 2067 |
} |
| 2068 |
}; |
| 2069 |
|
| 2070 |
istream& operator>>(istream& in, on_day& to) |
| 2071 |
{ |
| 2072 |
on_day on{}; |
| 2073 |
abbrev_month m{}; |
| 2074 |
in >> m; |
| 2075 |
on.month = static_cast<unsigned>(m.m); |
| 2076 |
int c = ws(in).peek(); |
| 2077 |
if ('0' <= c && c <= '9') |
| 2078 |
{ |
| 2079 |
on.kind = on_day::DayOfMonth; |
| 2080 |
unsigned d; |
| 2081 |
in >> d; |
| 2082 |
if (d <= 31) [[likely]] |
| 2083 |
{ |
| 2084 |
on.day_of_month = d; |
| 2085 |
to = on; |
| 2086 |
return in; |
| 2087 |
} |
| 2088 |
} |
| 2089 |
else if (c == 'l') // lastSunday, lastWed, ... |
| 2090 |
{ |
| 2091 |
in.ignore(4); |
| 2092 |
if (abbrev_weekday w{}; in >> w) [[likely]] |
| 2093 |
{ |
| 2094 |
on.kind = on_day::LastWeekday; |
| 2095 |
on.day_of_week = w.wd.c_encoding(); |
| 2096 |
to = on; |
| 2097 |
return in; |
| 2098 |
} |
| 2099 |
} |
| 2100 |
else |
| 2101 |
{ |
| 2102 |
abbrev_weekday w; |
| 2103 |
in >> w; |
| 2104 |
if (auto c = in.get(); c == '<' || c == '>') |
| 2105 |
{ |
| 2106 |
if (in.get() == '=') |
| 2107 |
{ |
| 2108 |
on.kind = c == '<' ? on_day::LessEq : on_day::GreaterEq; |
| 2109 |
on.day_of_week = w.wd.c_encoding(); |
| 2110 |
unsigned d; |
| 2111 |
in >> d; |
| 2112 |
if (d <= 31) [[likely]] |
| 2113 |
{ |
| 2114 |
on.day_of_month = d; |
| 2115 |
to = on; |
| 2116 |
return in; |
| 2117 |
} |
| 2118 |
} |
| 2119 |
} |
| 2120 |
} |
| 2121 |
in.setstate(ios::failbit); |
| 2122 |
return in; |
| 2123 |
} |
| 2124 |
|
| 2125 |
istream& operator>>(istream& in, at_time& at) |
| 2126 |
{ |
| 2127 |
int sign = 1; |
| 2128 |
if (ws(in).peek() == '-') |
| 2129 |
{ |
| 2130 |
in.ignore(1); |
| 2131 |
if (auto [val, yes] = at_time::is_indicator(in.peek()); |
| 2132 |
in.eof() || yes) |
| 2133 |
{ |
| 2134 |
in.ignore(1); |
| 2135 |
at.time = 0s; |
| 2136 |
at.indicator = val; |
| 2137 |
return in; |
| 2138 |
} |
| 2139 |
sign = -1; |
| 2140 |
} |
| 2141 |
int i; |
| 2142 |
in >> i; |
| 2143 |
hours h{i}; |
| 2144 |
minutes m{}; |
| 2145 |
seconds s{}; |
| 2146 |
if (!in.eof() && in.peek() == ':') |
| 2147 |
{ |
| 2148 |
in.ignore(1); // discard the colon. |
| 2149 |
in >> i; |
| 2150 |
m = minutes{i}; |
| 2151 |
if (!in.eof() && in.peek() == ':') |
| 2152 |
{ |
| 2153 |
in.ignore(1); // discard the colon. |
| 2154 |
in >> i; |
| 2155 |
if (!in.eof() && in.peek() == '.') |
| 2156 |
{ |
| 2157 |
double frac; |
| 2158 |
in >> frac; |
| 2159 |
// zic(8) rounds to nearest second, rounding ties to even. |
| 2160 |
s = chrono::round<seconds>(duration<double>(i + frac)); |
| 2161 |
} |
| 2162 |
else |
| 2163 |
s = seconds{i}; |
| 2164 |
} |
| 2165 |
} |
| 2166 |
if (in >> at.indicator) |
| 2167 |
at.time = sign * (h + m + s); |
| 2168 |
return in; |
| 2169 |
} |
| 2170 |
|
| 2171 |
// Test whether the RULES field of a Zone line is a valid Rule name. |
| 2172 |
inline bool |
| 2173 |
is_rule_name(string_view rules) noexcept |
| 2174 |
{ |
| 2175 |
// The NAME field of a Rule line must start with a character that is |
| 2176 |
// neither an ASCII digit nor '-' nor '+'. |
| 2177 |
if (('0' <= rules[0] && rules[0] <= '9') || rules[0] == '-') |
| 2178 |
return false; |
| 2179 |
// However, some older tzdata.zi files (e.g. in tzdata-2018e-3.el6 RPM) |
| 2180 |
// used "+" as a Rule name, so we need to handle that special case. |
| 2181 |
if (rules[0] == '+') |
| 2182 |
return rules.size() == 1; // "+" is a rule name, "+1" is not. |
| 2183 |
// Everything else is the name of a Rule. |
| 2184 |
return true; |
| 2185 |
} |
| 2186 |
|
| 2187 |
istream& operator>>(istream& in, ZoneInfo& inf) |
| 2188 |
{ |
| 2189 |
// STDOFF RULES FORMAT [UNTIL] |
| 2190 |
at_time off; |
| 2191 |
string rules; |
| 2192 |
string fmt; |
| 2193 |
|
| 2194 |
in >> off >> quoted{rules} >> fmt; |
| 2195 |
inf.m_offset = off.time; |
| 2196 |
if (is_rule_name(rules)) |
| 2197 |
{ |
| 2198 |
// `rules` refers to a named Rule which describes transitions. |
| 2199 |
inf.set_rules_and_format(rules, fmt); |
| 2200 |
} |
| 2201 |
else |
| 2202 |
{ |
| 2203 |
if (rules == "-") |
| 2204 |
{ |
| 2205 |
// Standard time always applies, no DST. |
| 2206 |
} |
| 2207 |
else |
| 2208 |
{ |
| 2209 |
// `rules` specifies the difference from standard time, |
| 2210 |
// e.g., "-2:30" |
| 2211 |
at_time rules_time; |
| 2212 |
istringstream in2(std::move(rules)); |
| 2213 |
in2 >> rules_time; |
| 2214 |
inf.m_save = duration_cast<minutes>(rules_time.time); |
| 2215 |
// If the FORMAT is "STD/DST" then we can choose the right one |
| 2216 |
// now, so that we store a shorter string. |
| 2217 |
select_std_or_dst_abbrev(fmt, inf.m_save); |
| 2218 |
} |
| 2219 |
inf.set_abbrev(std::move(fmt)); |
| 2220 |
} |
| 2221 |
|
| 2222 |
// YEAR [MONTH [DAY [TIME]]] |
| 2223 |
ios::iostate ex = in.exceptions(); |
| 2224 |
in.exceptions(ios::goodbit); // Don't throw ios::failure if YEAR absent. |
| 2225 |
if (int y = int(year::max()); in >> y) |
| 2226 |
{ |
| 2227 |
abbrev_month m{January}; |
| 2228 |
int d = 1; |
| 2229 |
at_time t{}; |
| 2230 |
// XXX DAY should support ON format, e.g. lastSun or Sun>=8 |
| 2231 |
in >> m >> d >> t; |
| 2232 |
inf.m_until = sys_days(year(y)/m.m/day(d)) + seconds(t.time); |
| 2233 |
if (t.indicator != at_time::Universal) |
| 2234 |
{ // UNTIL uses "the rules in effect just before the transition" |
| 2235 |
// adjust by STDOFF |
| 2236 |
inf.m_until -= seconds(inf.m_offset); |
| 2237 |
if (t.indicator != at_time::Standard) |
| 2238 |
{ |
| 2239 |
if (inf.m_expanded) // Not a named Rule, SAVE is known now. |
| 2240 |
inf.m_until -= inf.m_save; |
| 2241 |
// else Named Rule, SAVE is unknown. FIXME: PR 116110 |
| 2242 |
} |
| 2243 |
} |
| 2244 |
} |
| 2245 |
else |
| 2246 |
inf.m_until = sys_days(year::max()/December/31); |
| 2247 |
|
| 2248 |
in.clear(in.rdstate() & ios::eofbit); |
| 2249 |
in.exceptions(ex); |
| 2250 |
if (!in.eof()) |
| 2251 |
// Not actually necessary, as we're only parsing a single line: |
| 2252 |
in.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 2253 |
return in; |
| 2254 |
} |
| 2255 |
} // namespace |
| 2256 |
#endif // TZDB_DISABLED |
| 2257 |
} // namespace std::chrono |