Create local and system time types for timezone functions.

* Add sys_time.
* Add sys_days.
* Add sys_seconds.
* Add local_time.
* Add local_days.
* Add local_seconds.
* Rename day_point to sys_days.
* Rename Zone to time_zone.
This commit is contained in:
Howard Hinnant 2016-04-21 16:08:06 -04:00
parent 95271f8337
commit 1e5d2fa8dd
8 changed files with 906 additions and 414 deletions

179
date.h
View File

@ -22,17 +22,23 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Our apologies. When the previous paragraph was written, lowercase had not yet
// been invented (that woud involve another several millennia of evolution).
// We did not mean to shout.
#include <chrono>
#include <climits>
#if !(__cplusplus >= 201402)
# include <cmath>
#endif
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <locale>
#include <ostream>
#include <ratio>
#include <stdexcept>
#include <type_traits>
namespace date
{
@ -81,7 +87,22 @@ using months = std::chrono::duration
// time_point
using day_point = std::chrono::time_point<std::chrono::system_clock, days>;
template <class Duration>
using sys_time = std::chrono::time_point<std::chrono::system_clock, Duration>;
using sys_days = sys_time<days>;
using sys_seconds = sys_time<std::chrono::seconds>;
struct local_t {};
template <class Duration>
using local_time = std::chrono::time_point<local_t, Duration>;
using local_seconds = local_time<std::chrono::seconds>;
using local_days = local_time<days>;
// deprecated:
using day_point = sys_days;
// types
@ -314,7 +335,8 @@ class weekday
public:
explicit CONSTCD11 weekday(unsigned wd) NOEXCEPT;
explicit weekday(int) = delete;
CONSTCD11 weekday(const day_point& dp) NOEXCEPT;
CONSTCD11 weekday(const sys_days& dp) NOEXCEPT;
CONSTCD11 weekday(const local_days& dp) NOEXCEPT;
weekday& operator++() NOEXCEPT;
weekday operator++(int) NOEXCEPT;
@ -525,7 +547,9 @@ public:
CONSTCD11 year_month_day(const date::year& y, const date::month& m,
const date::day& d) NOEXCEPT;
CONSTCD14 year_month_day(const year_month_day_last& ymdl) NOEXCEPT;
CONSTCD14 year_month_day(const day_point& dp) NOEXCEPT;
CONSTCD14 year_month_day(sys_days dp) NOEXCEPT;
CONSTCD14 year_month_day(local_days dp) NOEXCEPT;
year_month_day& operator+=(const months& m) NOEXCEPT;
year_month_day& operator-=(const months& m) NOEXCEPT;
@ -536,11 +560,13 @@ public:
CONSTCD11 date::month month() const NOEXCEPT;
CONSTCD11 date::day day() const NOEXCEPT;
CONSTCD14 operator day_point() const NOEXCEPT;
CONSTCD14 operator sys_days() const NOEXCEPT;
CONSTCD14 explicit operator local_days() const NOEXCEPT;
CONSTCD14 bool ok() const NOEXCEPT;
private:
static CONSTCD14 year_month_day from_day_point(const day_point& dp) NOEXCEPT;
static CONSTCD14 year_month_day from_days(days dp) NOEXCEPT;
CONSTCD14 days to_days() const NOEXCEPT;
};
CONSTCD11 bool operator==(const year_month_day& x, const year_month_day& y) NOEXCEPT;
@ -580,7 +606,8 @@ public:
CONSTCD11 date::month_day_last month_day_last() const NOEXCEPT;
CONSTCD14 date::day day() const NOEXCEPT;
CONSTCD14 operator day_point() const NOEXCEPT;
CONSTCD14 operator sys_days() const NOEXCEPT;
CONSTCD14 explicit operator local_days() const NOEXCEPT;
CONSTCD11 bool ok() const NOEXCEPT;
};
@ -634,7 +661,8 @@ class year_month_weekday
public:
CONSTCD11 year_month_weekday(const date::year& y, const date::month& m,
const date::weekday_indexed& wdi) NOEXCEPT;
CONSTCD14 year_month_weekday(const day_point& dp) NOEXCEPT;
CONSTCD14 year_month_weekday(const sys_days& dp) NOEXCEPT;
CONSTCD14 year_month_weekday(const local_days& dp) NOEXCEPT;
year_month_weekday& operator+=(const months& m) NOEXCEPT;
year_month_weekday& operator-=(const months& m) NOEXCEPT;
@ -647,11 +675,13 @@ public:
CONSTCD11 unsigned index() const NOEXCEPT;
CONSTCD11 date::weekday_indexed weekday_indexed() const NOEXCEPT;
CONSTCD14 operator day_point() const NOEXCEPT;
CONSTCD14 operator sys_days() const NOEXCEPT;
CONSTCD14 explicit operator local_days() const NOEXCEPT;
CONSTCD14 bool ok() const NOEXCEPT;
private:
static CONSTCD14 year_month_weekday from_day_point(const day_point& dp) NOEXCEPT;
static CONSTCD14 year_month_weekday from_days(days dp) NOEXCEPT;
CONSTCD14 days to_days() const NOEXCEPT;
};
CONSTCD11
@ -707,8 +737,12 @@ public:
CONSTCD11 date::weekday weekday() const NOEXCEPT;
CONSTCD11 date::weekday_last weekday_last() const NOEXCEPT;
CONSTCD14 operator day_point() const NOEXCEPT;
CONSTCD14 operator sys_days() const NOEXCEPT;
CONSTCD14 explicit operator local_days() const NOEXCEPT;
CONSTCD11 bool ok() const NOEXCEPT;
private:
CONSTCD14 days to_days() const NOEXCEPT;
};
CONSTCD11
@ -1431,7 +1465,13 @@ weekday::weekday(unsigned wd) NOEXCEPT
CONSTCD11
inline
weekday::weekday(const day_point& dp) NOEXCEPT
weekday::weekday(const sys_days& dp) NOEXCEPT
: wd_(weekday_from_days(dp.time_since_epoch().count()))
{}
CONSTCD11
inline
weekday::weekday(const local_days& dp) NOEXCEPT
: wd_(weekday_from_days(dp.time_since_epoch().count()))
{}
@ -2171,6 +2211,20 @@ year_month_day_last::day() const NOEXCEPT
d[static_cast<unsigned>(month()) - 1] : date::day{29};
}
CONSTCD14
inline
year_month_day_last::operator sys_days() const NOEXCEPT
{
return sys_days(year()/month()/day());
}
CONSTCD14
inline
year_month_day_last::operator local_days() const NOEXCEPT
{
return local_days(year()/month()/day());
}
CONSTCD11
inline
bool
@ -2305,8 +2359,14 @@ year_month_day::year_month_day(const year_month_day_last& ymdl) NOEXCEPT
CONSTCD14
inline
year_month_day::year_month_day(const day_point& dp) NOEXCEPT
: year_month_day(from_day_point(dp))
year_month_day::year_month_day(sys_days dp) NOEXCEPT
: year_month_day(from_days(dp.time_since_epoch()))
{}
CONSTCD14
inline
year_month_day::year_month_day(local_days dp) NOEXCEPT
: year_month_day(from_days(dp.time_since_epoch()))
{}
CONSTCD11 inline year year_month_day::year() const NOEXCEPT {return y_;}
@ -2347,7 +2407,8 @@ year_month_day::operator-=(const years& y) NOEXCEPT
CONSTCD14
inline
year_month_day::operator day_point() const NOEXCEPT
days
year_month_day::to_days() const NOEXCEPT
{
static_assert(std::numeric_limits<unsigned>::digits >= 18,
"This algorithm has not been ported to a 16 bit unsigned integer");
@ -2360,14 +2421,21 @@ year_month_day::operator day_point() const NOEXCEPT
auto const yoe = static_cast<unsigned>(y - era * 400); // [0, 399]
auto const doy = (153*(m > 2 ? m-3 : m+9) + 2)/5 + d-1; // [0, 365]
auto const doe = yoe * 365 + yoe/4 - yoe/100 + doy; // [0, 146096]
return day_point{days{era * 146097 + static_cast<int>(doe) - 719468}};
return days{era * 146097 + static_cast<int>(doe) - 719468};
}
CONSTCD14
inline
year_month_day_last::operator day_point() const NOEXCEPT
year_month_day::operator sys_days() const NOEXCEPT
{
return day_point(year()/month()/day());
return sys_days{to_days()};
}
CONSTCD14
inline
year_month_day::operator local_days() const NOEXCEPT
{
return local_days{to_days()};
}
CONSTCD14
@ -2449,17 +2517,17 @@ operator<<(std::ostream& os, const year_month_day& ymd)
CONSTCD14
inline
year_month_day
year_month_day::from_day_point(const day_point& dp) NOEXCEPT
year_month_day::from_days(days dp) NOEXCEPT
{
static_assert(std::numeric_limits<unsigned>::digits >= 18,
"This algorithm has not been ported to a 16 bit unsigned integer");
static_assert(std::numeric_limits<int>::digits >= 20,
"This algorithm has not been ported to a 16 bit signed integer");
auto const z = dp.time_since_epoch().count() + 719468;
auto const z = dp.count() + 719468;
auto const era = (z >= 0 ? z : z - 146096) / 146097;
auto const doe = static_cast<unsigned>(z - era * 146097); // [0, 146096]
auto const yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365; // [0, 399]
auto const y = static_cast<day_point::rep>(yoe) + era * 400;
auto const y = static_cast<days::rep>(yoe) + era * 400;
auto const doy = doe - (365*yoe + yoe/4 - yoe/100); // [0, 365]
auto const mp = (5*doy + 2)/153; // [0, 11]
auto const d = doy - (153*mp+2)/5 + 1; // [1, 31]
@ -2529,8 +2597,14 @@ year_month_weekday::year_month_weekday(const date::year& y, const date::month& m
CONSTCD14
inline
year_month_weekday::year_month_weekday(const day_point& dp) NOEXCEPT
: year_month_weekday(from_day_point(dp))
year_month_weekday::year_month_weekday(const sys_days& dp) NOEXCEPT
: year_month_weekday(from_days(dp.time_since_epoch()))
{}
CONSTCD14
inline
year_month_weekday::year_month_weekday(const local_days& dp) NOEXCEPT
: year_month_weekday(from_days(dp.time_since_epoch()))
{}
inline
@ -2594,10 +2668,16 @@ year_month_weekday::weekday_indexed() const NOEXCEPT
CONSTCD14
inline
year_month_weekday::operator day_point() const NOEXCEPT
year_month_weekday::operator sys_days() const NOEXCEPT
{
auto d = day_point(y_/m_/1);
return d + (wdi_.weekday() - date::weekday(d) + days{(wdi_.index()-1)*7});
return sys_days{to_days()};
}
CONSTCD14
inline
year_month_weekday::operator local_days() const NOEXCEPT
{
return local_days{to_days()};
}
CONSTCD14
@ -2616,13 +2696,24 @@ year_month_weekday::ok() const NOEXCEPT
CONSTCD14
inline
year_month_weekday
year_month_weekday::from_day_point(const day_point& dp) NOEXCEPT
year_month_weekday::from_days(days d) NOEXCEPT
{
sys_days dp{d};
auto const wd = date::weekday(dp);
auto const ymd = year_month_day(dp);
return {ymd.year(), ymd.month(), wd[(static_cast<unsigned>(ymd.day())-1)/7+1]};
}
CONSTCD14
inline
days
year_month_weekday::to_days() const NOEXCEPT
{
auto d = day_point(y_/m_/1);
return (d + (wdi_.weekday() - date::weekday(d) + days{(wdi_.index()-1)*7})
).time_since_epoch();
}
CONSTCD11
inline
bool
@ -2761,10 +2852,16 @@ year_month_weekday_last::weekday_last() const NOEXCEPT
CONSTCD14
inline
year_month_weekday_last::operator day_point() const NOEXCEPT
year_month_weekday_last::operator sys_days() const NOEXCEPT
{
auto const d = day_point(y_/m_/last);
return d - (date::weekday{d} - wdl_.weekday());
return sys_days{to_days()};
}
CONSTCD14
inline
year_month_weekday_last::operator local_days() const NOEXCEPT
{
return local_days{to_days()};
}
CONSTCD11
@ -2775,6 +2872,15 @@ year_month_weekday_last::ok() const NOEXCEPT
return y_.ok() && m_.ok() && wdl_.ok();
}
CONSTCD14
inline
days
year_month_weekday_last::to_days() const NOEXCEPT
{
auto const d = sys_days(y_/m_/last);
return (d - (date::weekday{d} - wdl_.weekday())).time_since_epoch();
}
CONSTCD11
inline
bool
@ -3723,8 +3829,7 @@ typename std::enable_if
std::ratio_less<typename Duration::period, days::period>::value
, std::ostream&
>::type
operator<<(std::ostream& os,
const std::chrono::time_point<std::chrono::system_clock, Duration>& tp)
operator<<(std::ostream& os, const sys_time<Duration>& tp)
{
auto const dp = floor<days>(tp);
return os << year_month_day(dp) << ' ' << make_time(tp-dp);
@ -3732,11 +3837,19 @@ operator<<(std::ostream& os,
inline
std::ostream&
operator<<(std::ostream& os, const day_point& dp)
operator<<(std::ostream& os, const sys_days& dp)
{
return os << year_month_day(dp);
}
template <class Duration>
inline
std::ostream&
operator<<(std::ostream& os, const local_time<Duration>& ut)
{
return os << sys_time<Duration>{ut.time_since_epoch()};
}
} // namespace date
#endif // DATE_H

View File

@ -38,7 +38,10 @@ using years = date::years;
// time_point
using day_point = date::day_point;
using sys_days = date::sys_days;
// deprecated:
using day_point = sys_days;
// types
@ -95,7 +98,7 @@ public:
explicit CONSTCD11 weekday(unsigned wd) NOEXCEPT;
CONSTCD11 weekday(date::weekday wd) NOEXCEPT;
explicit weekday(int) = delete;
CONSTCD11 weekday(const day_point& dp) NOEXCEPT;
CONSTCD11 weekday(const sys_days& dp) NOEXCEPT;
weekday& operator++() NOEXCEPT;
weekday operator++(int) NOEXCEPT;
@ -328,7 +331,7 @@ public:
CONSTCD11 iso_week::weeknum weeknum() const NOEXCEPT;
CONSTCD11 iso_week::weekday weekday() const NOEXCEPT;
CONSTCD14 operator day_point() const NOEXCEPT;
CONSTCD14 operator sys_days() const NOEXCEPT;
CONSTCD11 bool ok() const NOEXCEPT;
};
@ -357,7 +360,7 @@ public:
CONSTCD11 year_weeknum_weekday(const iso_week::year& y, const iso_week::weeknum& wn,
const iso_week::weekday& wd) NOEXCEPT;
CONSTCD14 year_weeknum_weekday(const year_lastweek_weekday& ylwwd) NOEXCEPT;
CONSTCD14 year_weeknum_weekday(const day_point& dp) NOEXCEPT;
CONSTCD14 year_weeknum_weekday(const sys_days& dp) NOEXCEPT;
year_weeknum_weekday& operator+=(const years& y) NOEXCEPT;
year_weeknum_weekday& operator-=(const years& y) NOEXCEPT;
@ -366,11 +369,11 @@ public:
CONSTCD11 iso_week::weeknum weeknum() const NOEXCEPT;
CONSTCD11 iso_week::weekday weekday() const NOEXCEPT;
CONSTCD14 operator day_point() const NOEXCEPT;
CONSTCD14 operator sys_days() const NOEXCEPT;
CONSTCD14 bool ok() const NOEXCEPT;
private:
static CONSTCD14 year_weeknum_weekday from_day_point(const day_point& dp) NOEXCEPT;
static CONSTCD14 year_weeknum_weekday from_day_point(const sys_days& dp) NOEXCEPT;
};
CONSTCD11 bool operator==(const year_weeknum_weekday& x, const year_weeknum_weekday& y) NOEXCEPT;
@ -431,7 +434,7 @@ weekday::weekday(date::weekday wd) NOEXCEPT
CONSTCD11
inline
weekday::weekday(const day_point& dp) NOEXCEPT
weekday::weekday(const sys_days& dp) NOEXCEPT
: wd_(weekday_from_days(dp.time_since_epoch().count()))
{}
@ -980,8 +983,8 @@ weeknum
year_lastweek::weeknum() const NOEXCEPT
{
const auto y = date::year{int{y_}};
const auto s0 = day_point{(y-years{1})/12/date::thu[date::last]};
const auto s1 = day_point{y/12/date::thu[date::last]};
const auto s0 = sys_days{(y-years{1})/12/date::thu[date::last]};
const auto s1 = sys_days{y/12/date::thu[date::last]};
return iso_week::weeknum(date::trunc<weeks>(s1-s0).count());
}
@ -1273,9 +1276,9 @@ CONSTCD11 inline weekday year_lastweek_weekday::weekday() const NOEXCEPT {return
CONSTCD14
inline
year_lastweek_weekday::operator day_point() const NOEXCEPT
year_lastweek_weekday::operator sys_days() const NOEXCEPT
{
return day_point{date::year{int{y_}}/date::dec/date::thu[date::last]} + (mon - thu)
return sys_days{date::year{int{y_}}/date::dec/date::thu[date::last]} + (mon - thu)
- (mon - wd_);
}
@ -1390,7 +1393,7 @@ year_weeknum_weekday::year_weeknum_weekday(const year_lastweek_weekday& ylwwd) N
CONSTCD14
inline
year_weeknum_weekday::year_weeknum_weekday(const day_point& dp) NOEXCEPT
year_weeknum_weekday::year_weeknum_weekday(const sys_days& dp) NOEXCEPT
: year_weeknum_weekday(from_day_point(dp))
{}
@ -1416,9 +1419,9 @@ CONSTCD11 inline weekday year_weeknum_weekday::weekday() const NOEXCEPT {return
CONSTCD14
inline
year_weeknum_weekday::operator day_point() const NOEXCEPT
year_weeknum_weekday::operator sys_days() const NOEXCEPT
{
return day_point{date::year{int{y_}-1}/date::dec/date::thu[date::last]}
return sys_days{date::year{int{y_}-1}/date::dec/date::thu[date::last]}
+ (date::mon - date::thu) + weeks{unsigned{wn_}-1} + (wd_ - mon);
}
@ -1433,15 +1436,15 @@ year_weeknum_weekday::ok() const NOEXCEPT
CONSTCD14
inline
year_weeknum_weekday
year_weeknum_weekday::from_day_point(const day_point& dp) NOEXCEPT
year_weeknum_weekday::from_day_point(const sys_days& dp) NOEXCEPT
{
const auto wd = iso_week::weekday{dp};
auto y = date::year_month_day{dp + days{3}}.year();
auto start = day_point{(y - date::years{1})/date::dec/date::thu[date::last]} + (mon-thu);
auto start = sys_days{(y - date::years{1})/date::dec/date::thu[date::last]} + (mon-thu);
if (dp < start)
{
--y;
start = day_point{(y - date::years{1})/date::dec/date::thu[date::last]} + (mon-thu);
start = sys_days{(y - date::years{1})/date::dec/date::thu[date::last]} + (mon-thu);
}
const auto wn = iso_week::weeknum(date::trunc<weeks>(dp - start).count() + 1);
return {iso_week::year(int{y}), wn, wd};

Binary file not shown.

View File

@ -2,38 +2,42 @@
#include <iostream>
void
test_info(const date::Zone* zone, const date::Info& info)
test_info(const date::time_zone* zone, const date::Info& info)
{
using namespace date;
using namespace std::chrono;
auto begin = info.begin;
auto end = info.end - microseconds{1};
auto mid = begin + (end - begin) /2 ;
auto mid = begin + (end - begin) /2;
using sys_microseconds = sys_time<microseconds>;
using zoned_microseconds = zoned_time<microseconds>;
using local_microseconds = local_time<microseconds>;
zoned_microseconds local{zone};
if (begin > day_point{jan/1/1700})
if (begin > sys_days{jan/1/1700})
{
auto local = zone->to_local(begin).first;
auto prev_local = zone->to_local(begin - seconds{1}).first;
if (prev_local < local - seconds{1})
auto prev_local = local;
local = begin;
prev_local = begin - seconds{1};
auto slocal = local.get_local_time();
auto plocal = prev_local.get_local_time();
if (plocal < slocal - seconds{1})
{
assert(zone->to_sys(local) == begin);
auto imaginary = prev_local + (local - seconds{1} - prev_local) / 2;
assert(sys_microseconds{local} == begin);
try
{
zone->to_sys(imaginary);
local = plocal + (slocal - seconds{1} - plocal) / 2;
assert(false);
}
catch (const nonexistent_local_time&)
{
}
}
else if (prev_local > local - seconds{1})
else if (plocal > slocal - seconds{1})
{
auto ambiguous = local - seconds{1} +
(prev_local - (local - seconds{1})) / 2;
try
{
zone->to_sys(ambiguous);
local = slocal - seconds{1} + (plocal - (slocal - seconds{1})) / 2;
assert(false);
}
catch (const ambiguous_local_time&)
@ -42,33 +46,34 @@ test_info(const date::Zone* zone, const date::Info& info)
}
}
auto local = zone->to_local(mid).first;
assert(zone->to_sys(local) == mid);
local = mid;
assert(sys_microseconds{local} == mid);
if (end < day_point{jan/1/3000})
if (end < sys_days{jan/1/3000})
{
auto local = zone->to_local(end).first;
auto next_local = zone->to_local(info.end).first;
if (next_local < local + microseconds{1})
local = end;
auto next_local = local;
next_local = info.end;
auto slocal = local.get_local_time();
auto nlocal = next_local.get_local_time();
if (nlocal < slocal + microseconds{1})
{
auto ambiguous = next_local + (local + microseconds{1} - next_local) / 2;
try
{
zone->to_sys(ambiguous);
local = nlocal + (slocal + microseconds{1} - nlocal) / 2;
assert(false);
}
catch (const ambiguous_local_time&)
{
}
}
else if (next_local > local + microseconds{1})
else if (nlocal > slocal + microseconds{1})
{
assert(zone->to_sys(local) == end);
auto imaginary = local + microseconds{1} +
(next_local - (local + microseconds{1})) / 2;
assert(sys_microseconds{local} == end);
try
{
zone->to_sys(imaginary);
local = slocal + microseconds{1} +
(nlocal - (slocal + microseconds{1})) / 2;
assert(false);
}
catch (const nonexistent_local_time&)
@ -96,8 +101,8 @@ main()
{
std::cout << name << '\n';
auto z = locate_zone(name);
auto begin = day_point(jan/1/year::min()) + 0s;
auto end = day_point(jan/1/2035) + 0s;
auto begin = sys_days(jan/1/year::min()) + 0s;
auto end = sys_days(jan/1/2035) + 0s;
auto info = z->get_info(begin, tz::utc);
std::cout << "Initially: ";
if (info.offset >= 0s)
@ -122,7 +127,7 @@ main()
auto dp = floor<days>(begin);
auto ymd = year_month_day(dp);
auto time = make_time(begin - dp);
std::cout << ymd << 'T' << time << "Z ";
std::cout << ymd << ' ' << time << "Z ";
if (info.offset >= 0s)
std::cout << '+';
std::cout << make_time(info.offset);

View File

@ -6,10 +6,10 @@ main()
{
using namespace std;
using namespace date;
static_assert( is_nothrow_destructible<Zone>{}, "");
static_assert(!is_default_constructible<Zone>{}, "");
static_assert(!is_copy_constructible<Zone>{}, "");
static_assert(!is_copy_assignable<Zone>{}, "");
static_assert( is_nothrow_move_constructible<Zone>{}, "");
static_assert( is_nothrow_move_assignable<Zone>{}, "");
static_assert( is_nothrow_destructible<time_zone>{}, "");
static_assert(!is_default_constructible<time_zone>{}, "");
static_assert(!is_copy_constructible<time_zone>{}, "");
static_assert(!is_copy_assignable<time_zone>{}, "");
static_assert( is_nothrow_move_constructible<time_zone>{}, "");
static_assert( is_nothrow_move_assignable<time_zone>{}, "");
}

123
tz.cpp
View File

@ -20,6 +20,10 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Our apologies. When the previous paragraph was written, lowercase had not yet
// been invented (that woud involve another several millennia of evolution).
// We did not mean to shout.
#include "tz_private.h"
@ -118,6 +122,8 @@ CONSTDATA auto max_year = date::year::max();
// Arbitrary day of the year that will be away from any limits.
// Used with year::min() and year::max().
CONSTDATA auto boring_day = date::aug/18;
CONSTDATA auto min_day = date::jan/1;
CONSTDATA auto max_day = date::dec/31;
// +-------------------+
// | End Configuration |
@ -549,7 +555,7 @@ parse_signed_time(std::istream& in)
// MonthDayTime
MonthDayTime::MonthDayTime(second_point tp, tz timezone)
MonthDayTime::MonthDayTime(local_seconds tp, tz timezone)
: zone_(timezone)
{
using namespace date;
@ -606,8 +612,8 @@ MonthDayTime::compare(date::year y, const MonthDayTime& x, date::year yx,
{
if (zone_ != x.zone_)
{
auto dp0 = to_day_point(y);
auto dp1 = x.to_day_point(yx);
auto dp0 = to_sys_days(y);
auto dp1 = x.to_sys_days(yx);
if (std::abs((dp0-dp1).count()) > 1)
return dp0 < dp1 ? -1 : 1;
if (zone_ == tz::local)
@ -642,7 +648,7 @@ MonthDayTime::compare(date::year y, const MonthDayTime& x, date::year yx,
return t0 < t1 ? -1 : t0 == t1 ? 0 : 1;
}
second_point
sys_seconds
MonthDayTime::to_sys(date::year y, std::chrono::seconds offset,
std::chrono::seconds save) const
{
@ -677,23 +683,23 @@ MonthDayTime::U::operator=(const pair& x)
return *this;
}
date::day_point
MonthDayTime::to_day_point(date::year y) const
date::sys_days
MonthDayTime::to_sys_days(date::year y) const
{
using namespace std::chrono;
using namespace date;
switch (type_)
{
case month_day:
return day_point(y/u.month_day_);
return sys_days(y/u.month_day_);
case month_last_dow:
return day_point(y/u.month_weekday_last_);
return sys_days(y/u.month_weekday_last_);
case lteq:
{
auto const x = y/u.month_day_weekday_.month_day_;
auto const wd1 = weekday(x);
auto const wd0 = u.month_day_weekday_.weekday_;
return day_point(x) - (wd1-wd0);
return sys_days(x) - (wd1-wd0);
}
case gteq:
break;
@ -701,13 +707,13 @@ MonthDayTime::to_day_point(date::year y) const
auto const x = y/u.month_day_weekday_.month_day_;
auto const wd1 = u.month_day_weekday_.weekday_;
auto const wd0 = weekday(x);
return day_point(x) + (wd1-wd0);
return sys_days(x) + (wd1-wd0);
}
second_point
sys_seconds
MonthDayTime::to_time_point(date::year y) const
{
return to_day_point(y) + h_ + m_ + s_;
return to_sys_days(y) + h_ + m_ + s_;
}
void
@ -721,7 +727,7 @@ MonthDayTime::canonicalize(date::year y)
return;
case month_last_dow:
{
auto const ymd = year_month_day(y/u.month_weekday_last_);
auto const ymd = year_month_day(sys_days{y/u.month_weekday_last_});
u.month_day_ = ymd.month()/ymd.day();
type_ = month_day;
return;
@ -731,7 +737,7 @@ MonthDayTime::canonicalize(date::year y)
auto const x = y/u.month_day_weekday_.month_day_;
auto const wd1 = weekday(x);
auto const wd0 = u.month_day_weekday_.weekday_;
auto const ymd = year_month_day(day_point(x) - (wd1-wd0));
auto const ymd = year_month_day(sys_days(x) - (wd1-wd0));
u.month_day_ = ymd.month()/ymd.day();
type_ = month_day;
return;
@ -741,7 +747,7 @@ MonthDayTime::canonicalize(date::year y)
auto const x = y/u.month_day_weekday_.month_day_;
auto const wd1 = u.month_day_weekday_.weekday_;
auto const wd0 = weekday(x);
auto const ymd = year_month_day(day_point(x) + (wd1-wd0));
auto const ymd = year_month_day(sys_days(x) + (wd1-wd0));
u.month_day_ = ymd.month()/ymd.day();
type_ = month_day;
return;
@ -1218,9 +1224,9 @@ Rule::split_overlaps(std::vector<Rule>& rules)
rules.shrink_to_fit();
}
// Zone
// time_zone
Zone::zonelet::~zonelet()
time_zone::zonelet::~zonelet()
{
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
using minutes = std::chrono::minutes;
@ -1232,14 +1238,14 @@ Zone::zonelet::~zonelet()
#endif
}
Zone::zonelet::zonelet()
time_zone::zonelet::zonelet()
{
#if !defined(_MSC_VER) || (_MSC_VER >= 1900)
::new(&u.rule_) std::string();
#endif
}
Zone::zonelet::zonelet(const zonelet& i)
time_zone::zonelet::zonelet(const zonelet& i)
: gmtoff_(i.gmtoff_)
, tag_(i.tag_)
, format_(i.format_)
@ -1266,7 +1272,7 @@ Zone::zonelet::zonelet(const zonelet& i)
#endif
}
Zone::Zone(const std::string& s)
time_zone::time_zone(const std::string& s)
#if LAZY_INIT
: adjusted_(new std::once_flag{})
#endif
@ -1290,7 +1296,7 @@ Zone::Zone(const std::string& s)
}
void
Zone::add(const std::string& s)
time_zone::add(const std::string& s)
{
try
{
@ -1310,7 +1316,7 @@ Zone::add(const std::string& s)
}
void
Zone::parse_info(std::istream& in)
time_zone::parse_info(std::istream& in)
{
using namespace date;
using namespace std::chrono;
@ -1326,7 +1332,7 @@ Zone::parse_info(std::istream& in)
if (in.eof() || in.peek() == '#')
{
zonelet.until_year_ = year::max();
zonelet.until_date_ = MonthDayTime(boring_day, tz::utc);
zonelet.until_date_ = MonthDayTime(max_day, tz::utc);
}
else
{
@ -1489,8 +1495,9 @@ find_rule_for_zone(const std::pair<const Rule*, const Rule*>& eqr,
static
std::pair<const Rule*, date::year>
find_rule_for_zone(const std::pair<const Rule*, const Rule*>& eqr,
const second_point& tp_utc, const second_point& tp_std,
const second_point& tp_loc)
const sys_seconds& tp_utc,
const local_seconds& tp_std,
const local_seconds& tp_loc)
{
using namespace std::chrono;
using namespace date;
@ -1508,10 +1515,10 @@ find_rule_for_zone(const std::pair<const Rule*, const Rule*>& eqr,
found = tp_utc < r->mdt().to_time_point(ry);
break;
case tz::standard:
found = tp_std < r->mdt().to_time_point(ry);
found = sys_seconds{tp_std.time_since_epoch()} < r->mdt().to_time_point(ry);
break;
case tz::local:
found = tp_loc < r->mdt().to_time_point(ry);
found = sys_seconds{tp_loc.time_since_epoch()} < r->mdt().to_time_point(ry);
break;
}
if (found)
@ -1536,7 +1543,7 @@ find_rule(const std::pair<const Rule*, date::year>& first_rule,
using namespace date;
auto r = first_rule.first;
auto ry = first_rule.second;
Info x{day_point(year::min()/boring_day), day_point(year::max()/boring_day),
Info x{sys_days(year::min()/min_day), sys_days(year::max()/max_day),
seconds{0}, initial_save, initial_abbrev};
while (r != nullptr)
{
@ -1569,7 +1576,7 @@ find_rule(const std::pair<const Rule*, date::year>& first_rule,
x.end = r->mdt().to_sys(ry, offset, x.save);
}
else
x.end = day_point(year::max()/boring_day);
x.end = sys_days(year::max()/max_day);
break;
}
x.save = r->save();
@ -1580,7 +1587,7 @@ find_rule(const std::pair<const Rule*, date::year>& first_rule,
}
void
Zone::adjust_infos(const std::vector<Rule>& rules)
time_zone::adjust_infos(const std::vector<Rule>& rules)
{
using namespace std::chrono;
using namespace date;
@ -1640,7 +1647,7 @@ Zone::adjust_infos(const std::vector<Rule>& rules)
final_save = z.last_rule_.first->save();
}
z.until_utc_ = z.until_date_.to_sys(z.until_year_, z.gmtoff_, final_save);
z.until_std_ = z.until_utc_ + z.gmtoff_;
z.until_std_ = local_seconds{z.until_utc_.time_since_epoch()} + z.gmtoff_;
z.until_loc_ = z.until_std_ + final_save;
if (z.tag_ == zonelet::has_rule)
@ -1751,7 +1758,7 @@ format_abbrev(std::string format, const std::string& variable, std::chrono::seco
}
Info
Zone::get_info(std::chrono::system_clock::time_point tp, tz timezone) const
time_zone::get_info_impl(sys_seconds tp, tz timezone) const
{
using namespace std::chrono;
using namespace date;
@ -1761,17 +1768,18 @@ Zone::get_info(std::chrono::system_clock::time_point tp, tz timezone) const
throw std::runtime_error("The year " + std::to_string(static_cast<int>(y)) +
" is out of range:[" + std::to_string(static_cast<int>(min_year)) + ", "
+ std::to_string(static_cast<int>(max_year)) + "]");
auto tps = floor<seconds>(tp);
#if LAZY_INIT
std::call_once(*adjusted_, [this]()
{
const_cast<Zone*>(this)->adjust_infos(get_tzdb().rules);
});
std::call_once(*adjusted_,
[this]()
{
const_cast<time_zone*>(this)->adjust_infos(get_tzdb().rules);
});
#endif
auto i = std::upper_bound(zonelets_.begin(), zonelets_.end(), tps,
[timezone](second_point t, const zonelet& zl)
auto i = std::upper_bound(zonelets_.begin(), zonelets_.end(), tp,
[timezone](sys_seconds t, const zonelet& zl)
{
return timezone == tz::utc ? t < zl.until_utc_ : t < zl.until_loc_;
return timezone == tz::utc ? t < zl.until_utc_ :
t < sys_seconds{zl.until_loc_.time_since_epoch()};
});
Info r{};
@ -1782,7 +1790,7 @@ Zone::get_info(std::chrono::system_clock::time_point tp, tz timezone) const
if (i != zonelets_.begin())
r.begin = i[-1].until_utc_;
else
r.begin = day_point(year::min()/boring_day);
r.begin = sys_days(year::min()/min_day);
r.end = i->until_utc_;
r.offset = i->gmtoff_ + i->u.save_;
r.save = i->u.save_;
@ -1792,15 +1800,15 @@ Zone::get_info(std::chrono::system_clock::time_point tp, tz timezone) const
if (i != zonelets_.begin())
r.begin = i[-1].until_utc_;
else
r.begin = day_point(year::min()/boring_day);
r.begin = sys_days(year::min()/min_day);
r.end = i->until_utc_;
r.offset = i->gmtoff_;
}
else
{
r = find_rule(i->first_rule_, i->last_rule_, y, i->gmtoff_,
MonthDayTime(tps, timezone), i->initial_save_,
i->initial_abbrev_);
MonthDayTime(local_seconds{tp.time_since_epoch()}, timezone),
i->initial_save_, i->initial_abbrev_);
r.offset = i->gmtoff_ + r.save;
if (i != zonelets_.begin() && r.begin < i[-1].until_utc_)
r.begin = i[-1].until_utc_;
@ -1814,7 +1822,7 @@ Zone::get_info(std::chrono::system_clock::time_point tp, tz timezone) const
}
std::ostream&
operator<<(std::ostream& os, const Zone& z)
operator<<(std::ostream& os, const time_zone& z)
{
using namespace date;
using namespace std::chrono;
@ -1822,10 +1830,11 @@ operator<<(std::ostream& os, const Zone& z)
os.fill(' ');
os.flags(std::ios::dec | std::ios::left);
#if LAZY_INIT
std::call_once(*z.adjusted_, [&z]()
{
const_cast<Zone&>(z).adjust_infos(get_tzdb().rules);
});
std::call_once(*z.adjusted_,
[&z]()
{
const_cast<time_zone&>(z).adjust_infos(get_tzdb().rules);
});
#endif
os.width(35);
os << z.name_;
@ -1837,7 +1846,7 @@ operator<<(std::ostream& os, const Zone& z)
os << ' ';
os << make_time(s.gmtoff_) << " ";
os.width(15);
if (s.tag_ != Zone::zonelet::has_save)
if (s.tag_ != time_zone::zonelet::has_save)
os << s.u.rule_;
else
{
@ -2120,7 +2129,7 @@ init_tzdb()
}
else if (word == "Zone")
{
db.zones.push_back(Zone(line));
db.zones.push_back(time_zone(line));
continue_zone = true;
}
else if (line[0] == '\t' && continue_zone)
@ -2182,12 +2191,12 @@ get_tzdb()
return ref;
}
const Zone*
const time_zone*
locate_zone(const std::string& tz_name)
{
const auto& db = get_tzdb();
auto zi = std::lower_bound(db.zones.begin(), db.zones.end(), tz_name,
[](const Zone& z, const std::string& nm)
[](const time_zone& z, const std::string& nm)
{
return z.name() < nm;
});
@ -2201,7 +2210,7 @@ locate_zone(const std::string& tz_name)
if (li != db.links.end() && li->name() == tz_name)
{
zi = std::lower_bound(db.zones.begin(), db.zones.end(), li->target(),
[](const Zone& z, const std::string& nm)
[](const time_zone& z, const std::string& nm)
{
return z.name() < nm;
});
@ -2215,7 +2224,7 @@ locate_zone(const std::string& tz_name)
#ifdef TZ_TEST
#ifdef _WIN32
const Zone*
const time_zone*
locate_native_zone(const std::string& native_tz_name)
{
std::string standard_tz_name;
@ -2305,7 +2314,7 @@ operator<<(std::ostream& os, const Info& r)
#ifdef _WIN32
const Zone*
const time_zone*
current_zone()
{
#if TIMEZONE_MAPPING
@ -2346,7 +2355,7 @@ current_zone()
#else // ! WIN32
const Zone*
const time_zone*
current_zone()
{
// On some versions of some linux distro's (e.g. Ubuntu),

874
tz.h

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
// The MIT License (MIT)
//
// Copyright (c) 2015 Howard Hinnant
// Copyright (c) 2015, 2016 Howard Hinnant
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@ -22,6 +22,10 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Our apologies. When the previous paragraph was written, lowercase had not yet
// been invented (that woud involve another several millennia of evolution).
// We did not mean to shout.
#include "tz.h"
@ -80,7 +84,7 @@ private:
public:
MonthDayTime() = default;
MonthDayTime(second_point tp, tz timezone);
MonthDayTime(local_seconds tp, tz timezone);
MonthDayTime(const date::month_day& md, tz timezone);
date::day day() const;
@ -89,11 +93,11 @@ public:
void canonicalize(date::year y);
second_point
sys_seconds
to_sys(date::year y, std::chrono::seconds offset, std::chrono::seconds save) const;
date::day_point to_day_point(date::year y) const;
sys_days to_sys_days(date::year y) const;
second_point to_time_point(date::year y) const;
sys_seconds to_time_point(date::year y) const;
int compare(date::year y, const MonthDayTime& x, date::year yx,
std::chrono::seconds offset, std::chrono::minutes prev_save) const;
@ -181,7 +185,7 @@ inline bool operator> (const std::string& x, const Rule& y) {return y < x;}
inline bool operator<=(const std::string& x, const Rule& y) {return !(y < x);}
inline bool operator>=(const std::string& x, const Rule& y) {return !(x < y);}
struct Zone::zonelet
struct time_zone::zonelet
{
enum tag {has_rule, has_save, is_empty};
@ -203,14 +207,14 @@ struct Zone::zonelet
U& operator=(const U&) = delete;
} u;
std::string format_;
date::year until_year_{0};
MonthDayTime until_date_;
second_point until_utc_;
second_point until_std_;
second_point until_loc_;
std::chrono::minutes initial_save_{};
std::string initial_abbrev_;
std::string format_;
date::year until_year_{0};
MonthDayTime until_date_;
sys_seconds until_utc_;
local_seconds until_std_;
local_seconds until_loc_;
std::chrono::minutes initial_save_{};
std::string initial_abbrev_;
std::pair<const Rule*, date::year> first_rule_{nullptr, date::year::min()};
std::pair<const Rule*, date::year> last_rule_{nullptr, date::year::max()};