|
|
|
@ -36,8 +36,24 @@
|
|
|
|
|
// Posix::time_zone tz{"EST5EDT,M3.2.0,M11.1.0"};
|
|
|
|
|
// zoned_time<system_clock::duration, Posix::time_zone> zt{tz, system_clock::now()};
|
|
|
|
|
//
|
|
|
|
|
// If the rule set is missing (everything starting with ','), then the rule is that the
|
|
|
|
|
// alternate offset is never enabled.
|
|
|
|
|
// In C++17 CTAD simplifies this to:
|
|
|
|
|
//
|
|
|
|
|
// Posix::time_zone tz{"EST5EDT,M3.2.0,M11.1.0"};
|
|
|
|
|
// zoned_time zt{tz, system_clock::now()};
|
|
|
|
|
//
|
|
|
|
|
// Extension to the Posix rules to allow a constant daylight saving offset:
|
|
|
|
|
//
|
|
|
|
|
// If the rule set is missing (everything starting with ','), then
|
|
|
|
|
// there must be exactly one abbreviation (std or daylight) with
|
|
|
|
|
// length 3 or greater, and that will be used as the constant offset. If
|
|
|
|
|
// there are two, the std abbreviation is silently set to "", and the
|
|
|
|
|
// result is constant daylight saving. If there are zero abbreviations
|
|
|
|
|
// with no rule set, an exception is thrown.
|
|
|
|
|
//
|
|
|
|
|
// Example:
|
|
|
|
|
// "EST5" yields a constant offset of -5h with 0h save and "EST abbreviation.
|
|
|
|
|
// "5EDT" yields a constant offset of -4h with 1h save and "EDT" abbreviation.
|
|
|
|
|
// "EST5EDT" and "5EDT4" are both equal to "5EDT".
|
|
|
|
|
//
|
|
|
|
|
// Note, Posix-style time zones are not recommended for all of the reasons described here:
|
|
|
|
|
// https://stackoverflow.com/tags/timezone/info
|
|
|
|
@ -46,6 +62,7 @@
|
|
|
|
|
// have to have Posix time zones, you're welcome to use this one.
|
|
|
|
|
|
|
|
|
|
#include "date/tz.h"
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cctype>
|
|
|
|
|
#include <ostream>
|
|
|
|
|
#include <string>
|
|
|
|
@ -322,6 +339,7 @@ time_zone::get_next_end(date::year y) const
|
|
|
|
|
return date::sys_seconds{(end_rule_(++y) - (offset_ + save_)).time_since_epoch()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
date::sys_info
|
|
|
|
|
time_zone::contant_offset() const
|
|
|
|
|
{
|
|
|
|
@ -331,11 +349,22 @@ time_zone::contant_offset() const
|
|
|
|
|
using date::January;
|
|
|
|
|
using date::December;
|
|
|
|
|
using date::last;
|
|
|
|
|
using std::chrono::minutes;
|
|
|
|
|
sys_info r;
|
|
|
|
|
r.begin = sys_days{year::min()/January/1};
|
|
|
|
|
r.end = sys_days{year::max()/December/last};
|
|
|
|
|
r.abbrev = std_abbrev_;
|
|
|
|
|
r.offset = offset_;
|
|
|
|
|
if (std_abbrev_.size() > 0)
|
|
|
|
|
{
|
|
|
|
|
r.abbrev = std_abbrev_;
|
|
|
|
|
r.offset = offset_;
|
|
|
|
|
r.save = {};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
r.abbrev = dst_abbrev_;
|
|
|
|
|
r.offset = offset_ + save_;
|
|
|
|
|
r.save = date::ceil<minutes>(save_);
|
|
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -346,11 +375,14 @@ time_zone::time_zone(const detail::string_t& s)
|
|
|
|
|
using detail::read_signed_time;
|
|
|
|
|
using detail::throw_invalid;
|
|
|
|
|
auto i = read_name(s, 0, std_abbrev_);
|
|
|
|
|
auto std_name_i = i;
|
|
|
|
|
auto abbrev_name_i = i;
|
|
|
|
|
i = read_signed_time(s, i, offset_);
|
|
|
|
|
offset_ = -offset_;
|
|
|
|
|
if (i != s.size())
|
|
|
|
|
{
|
|
|
|
|
i = read_name(s, i, dst_abbrev_);
|
|
|
|
|
abbrev_name_i = i;
|
|
|
|
|
if (i != s.size())
|
|
|
|
|
{
|
|
|
|
|
if (s[i] != ',')
|
|
|
|
@ -373,6 +405,32 @@ time_zone::time_zone(const detail::string_t& s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (start_rule_.ok())
|
|
|
|
|
{
|
|
|
|
|
if (std_abbrev_.size() < 3)
|
|
|
|
|
throw_invalid(s, std_name_i, "Zone with rules must have a std"
|
|
|
|
|
" abbreviation of length 3 or greater");
|
|
|
|
|
if (dst_abbrev_.size() < 3)
|
|
|
|
|
throw_invalid(s, abbrev_name_i, "Zone with rules must have a daylight"
|
|
|
|
|
" abbreviation of length 3 or greater");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (dst_abbrev_.size() >= 3)
|
|
|
|
|
{
|
|
|
|
|
std_abbrev_.clear();
|
|
|
|
|
}
|
|
|
|
|
else if (std_abbrev_.size() < 3)
|
|
|
|
|
{
|
|
|
|
|
throw_invalid(s, std_name_i, "Zone must have at least one abbreviation"
|
|
|
|
|
" of length 3 or greater");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dst_abbrev_.clear();
|
|
|
|
|
save_ = {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class Duration>
|
|
|
|
@ -395,6 +453,10 @@ time_zone::get_info(date::sys_time<Duration> st) const
|
|
|
|
|
if (start_rule_.ok())
|
|
|
|
|
{
|
|
|
|
|
auto y = year_month_day{floor<days>(st)}.year();
|
|
|
|
|
if (st >= get_next_start(y))
|
|
|
|
|
++y;
|
|
|
|
|
else if (st < get_prev_end(y))
|
|
|
|
|
--y;
|
|
|
|
|
auto start = get_start(y);
|
|
|
|
|
auto end = get_end(y);
|
|
|
|
|
if (start <= end) // (northern hemisphere)
|
|
|
|
@ -448,6 +510,7 @@ time_zone::get_info(date::sys_time<Duration> st) const
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
r = contant_offset();
|
|
|
|
|
assert(r.begin <= st && st < r.end);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -589,7 +652,18 @@ time_zone::name() const
|
|
|
|
|
{
|
|
|
|
|
using namespace date;
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
auto nm = std_abbrev_;
|
|
|
|
|
auto print_abbrev = [](std::string const& nm)
|
|
|
|
|
{
|
|
|
|
|
if (std::any_of(nm.begin(), nm.end(),
|
|
|
|
|
[](char c)
|
|
|
|
|
{
|
|
|
|
|
return !std::isalpha(c);
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
return '<' + nm + '>';
|
|
|
|
|
}
|
|
|
|
|
return nm;
|
|
|
|
|
};
|
|
|
|
|
auto print_offset = [](seconds off)
|
|
|
|
|
{
|
|
|
|
|
std::string nm;
|
|
|
|
@ -613,10 +687,11 @@ time_zone::name() const
|
|
|
|
|
}
|
|
|
|
|
return nm;
|
|
|
|
|
};
|
|
|
|
|
auto nm = print_abbrev(std_abbrev_);
|
|
|
|
|
nm += print_offset(offset_);
|
|
|
|
|
if (!dst_abbrev_.empty())
|
|
|
|
|
{
|
|
|
|
|
nm += dst_abbrev_;
|
|
|
|
|
nm += print_abbrev(dst_abbrev_);
|
|
|
|
|
if (save_ != hours{1})
|
|
|
|
|
nm += print_offset(offset_+save_);
|
|
|
|
|
if (start_rule_.ok())
|
|
|
|
@ -678,6 +753,8 @@ read_date(const string_t& s, unsigned i, rule& r)
|
|
|
|
|
++i;
|
|
|
|
|
unsigned n;
|
|
|
|
|
i = read_unsigned(s, i, 3, n, "Expected to find the Julian day [1, 365]");
|
|
|
|
|
if (!(1 <= n && n <= 365))
|
|
|
|
|
throw_invalid(s, i-1, "Expected Julian day to be in the range [1, 365]");
|
|
|
|
|
r.mode_ = rule::J;
|
|
|
|
|
r.n_ = n;
|
|
|
|
|
}
|
|
|
|
@ -686,16 +763,22 @@ read_date(const string_t& s, unsigned i, rule& r)
|
|
|
|
|
++i;
|
|
|
|
|
unsigned m;
|
|
|
|
|
i = read_unsigned(s, i, 2, m, "Expected to find month [1, 12]");
|
|
|
|
|
if (!(1 <= m && m <= 12))
|
|
|
|
|
throw_invalid(s, i-1, "Expected month to be in the range [1, 12]");
|
|
|
|
|
if (i == s.size() || s[i] != '.')
|
|
|
|
|
throw_invalid(s, i, "Expected '.' after month");
|
|
|
|
|
++i;
|
|
|
|
|
unsigned n;
|
|
|
|
|
i = read_unsigned(s, i, 1, n, "Expected to find week number [1, 5]");
|
|
|
|
|
if (!(1 <= n && n <= 5))
|
|
|
|
|
throw_invalid(s, i-1, "Expected week number to be in the range [1, 5]");
|
|
|
|
|
if (i == s.size() || s[i] != '.')
|
|
|
|
|
throw_invalid(s, i, "Expected '.' after weekday index");
|
|
|
|
|
++i;
|
|
|
|
|
unsigned wd;
|
|
|
|
|
i = read_unsigned(s, i, 1, wd, "Expected to find day of week [0, 6]");
|
|
|
|
|
if (wd > 6)
|
|
|
|
|
throw_invalid(s, i-1, "Expected day of week to be in the range [0, 6]");
|
|
|
|
|
r.mode_ = rule::M;
|
|
|
|
|
r.m_ = month{m};
|
|
|
|
|
r.wd_ = weekday{wd};
|
|
|
|
@ -705,6 +788,8 @@ read_date(const string_t& s, unsigned i, rule& r)
|
|
|
|
|
{
|
|
|
|
|
unsigned n;
|
|
|
|
|
i = read_unsigned(s, i, 3, n);
|
|
|
|
|
if (n > 365)
|
|
|
|
|
throw_invalid(s, i-1, "Expected Julian day to be in the range [0, 365]");
|
|
|
|
|
r.mode_ = rule::N;
|
|
|
|
|
r.n_ = n;
|
|
|
|
|
}
|
|
|
|
@ -749,8 +834,6 @@ read_name(const string_t& s, unsigned i, std::string& name)
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (name.size() < 3)
|
|
|
|
|
throw_invalid(s, i, "Found name to be shorter than 3 characters");
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -786,16 +869,22 @@ read_unsigned_time(const string_t& s, unsigned i, std::chrono::seconds& t)
|
|
|
|
|
throw_invalid(s, i, "Expected to read unsigned time, but found end of string");
|
|
|
|
|
unsigned x;
|
|
|
|
|
i = read_unsigned(s, i, 2, x, "Expected to find hours [0, 24]");
|
|
|
|
|
if (x > 24)
|
|
|
|
|
throw_invalid(s, i-1, "Expected hours to be in the range [0, 24]");
|
|
|
|
|
t = hours{x};
|
|
|
|
|
if (i != s.size() && s[i] == ':')
|
|
|
|
|
{
|
|
|
|
|
++i;
|
|
|
|
|
i = read_unsigned(s, i, 2, x, "Expected to find minutes [0, 59]");
|
|
|
|
|
if (x > 59)
|
|
|
|
|
throw_invalid(s, i-1, "Expected minutes to be in the range [0, 59]");
|
|
|
|
|
t += minutes{x};
|
|
|
|
|
if (i != s.size() && s[i] == ':')
|
|
|
|
|
{
|
|
|
|
|
++i;
|
|
|
|
|
i = read_unsigned(s, i, 2, x, "Expected to find seconds [0, 59]");
|
|
|
|
|
if (x > 59)
|
|
|
|
|
throw_invalid(s, i-1, "Expected seconds to be in the range [0, 59]");
|
|
|
|
|
t += seconds{x};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|