Skip to content

Commit

Permalink
For USE_OS_TZDB, look for leap seconds in the files
Browse files Browse the repository at this point in the history
  leapseconds and leap-seconds.list
  • Loading branch information
HowardHinnant committed Sep 11, 2020
1 parent 5e18488 commit ba99134
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 74 deletions.
19 changes: 0 additions & 19 deletions include/date/tz.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,6 @@ static_assert(HAS_REMOTE_API == 0 ? AUTO_DOWNLOAD == 0 : true,
# ifdef _WIN32
# error "USE_OS_TZDB can not be used on Windows"
# endif
# ifndef MISSING_LEAP_SECONDS
# ifdef __APPLE__
# define MISSING_LEAP_SECONDS 1
# else
# define MISSING_LEAP_SECONDS 0
# endif
# endif
#else
# define MISSING_LEAP_SECONDS 0
#endif

#ifndef HAS_DEDUCTION_GUIDES
Expand Down Expand Up @@ -995,8 +986,6 @@ inline bool operator>=(const time_zone_link& x, const time_zone_link& y) {return

#endif // !USE_OS_TZDB

#if !MISSING_LEAP_SECONDS

class leap_second
{
private:
Expand Down Expand Up @@ -1120,8 +1109,6 @@ operator>=(const sys_time<Duration>& x, const leap_second& y)

using leap = leap_second;

#endif // !MISSING_LEAP_SECONDS

#ifdef _WIN32

namespace detail
Expand Down Expand Up @@ -1167,9 +1154,7 @@ struct tzdb
#if !USE_OS_TZDB
std::vector<time_zone_link> links;
#endif
#if !MISSING_LEAP_SECONDS
std::vector<leap_second> leap_seconds;
#endif
#if !USE_OS_TZDB
std::vector<detail::Rule> rules;
#endif
Expand Down Expand Up @@ -1864,8 +1849,6 @@ operator<<(std::basic_ostream<CharT, Traits>& os, const zoned_time<Duration, Tim
return to_stream(os, fmt, t);
}

#if !MISSING_LEAP_SECONDS

class utc_clock
{
public:
Expand Down Expand Up @@ -2804,8 +2787,6 @@ to_gps_time(const tai_time<Duration>& t)
return gps_clock::from_utc(tai_clock::to_utc(t));
}

#endif // !MISSING_LEAP_SECONDS

} // namespace date

#endif // TZ_H
161 changes: 106 additions & 55 deletions src/tz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,32 @@ get_tzdb_list()
return tz_db;
}

static
std::string
parse3(std::istream& in)
{
std::string r(3, ' ');
ws(in);
r[0] = static_cast<char>(in.get());
r[1] = static_cast<char>(in.get());
r[2] = static_cast<char>(in.get());
return r;
}

static
unsigned
parse_month(std::istream& in)
{
CONSTDATA char*const month_names[] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
auto s = parse3(in);
auto m = std::find(std::begin(month_names), std::end(month_names), s) - month_names;
if (m >= std::end(month_names) - std::begin(month_names))
throw std::runtime_error("oops: bad month name: " + s);
return static_cast<unsigned>(++m);
}

#if !USE_OS_TZDB

#ifdef _WIN32
Expand Down Expand Up @@ -679,18 +705,6 @@ load_timezone_mappings_from_xml_file(const std::string& input_path)

// Parsing helpers

static
std::string
parse3(std::istream& in)
{
std::string r(3, ' ');
ws(in);
r[0] = static_cast<char>(in.get());
r[1] = static_cast<char>(in.get());
r[2] = static_cast<char>(in.get());
return r;
}

static
unsigned
parse_dow(std::istream& in)
Expand All @@ -704,20 +718,6 @@ parse_dow(std::istream& in)
return static_cast<unsigned>(dow);
}

static
unsigned
parse_month(std::istream& in)
{
CONSTDATA char*const month_names[] =
{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
auto s = parse3(in);
auto m = std::find(std::begin(month_names), std::end(month_names), s) - month_names;
if (m >= std::end(month_names) - std::begin(month_names))
throw std::runtime_error("oops: bad month name: " + s);
return static_cast<unsigned>(++m);
}

static
std::chrono::seconds
parse_unsigned_time(std::istream& in)
Expand Down Expand Up @@ -2221,15 +2221,11 @@ operator<<(std::ostream& os, const time_zone& z)
return os;
}

#if !MISSING_LEAP_SECONDS

leap_second::leap_second(const sys_seconds& s, detail::undocumented)
: date_(s)
{
}

#endif // !MISSING_LEAP_SECONDS

#else // !USE_OS_TZDB

time_zone::time_zone(const std::string& s, detail::undocumented)
Expand Down Expand Up @@ -2626,17 +2622,13 @@ operator<<(std::ostream& os, const time_zone& z)

#endif // !USE_OS_TZDB

#if !MISSING_LEAP_SECONDS

std::ostream&
operator<<(std::ostream& os, const leap_second& x)
{
using namespace date;
return os << x.date_ << " +";
}

#endif // !MISSING_LEAP_SECONDS

#if USE_OS_TZDB

# ifdef __APPLE__
Expand All @@ -2655,6 +2647,85 @@ get_version()
}
# endif

static
std::vector<leap_second>
find_read_and_leap_seconds()
{
std::ifstream in(get_tz_dir() + std::string(1, folder_delimiter) + "leapseconds",
std::ios_base::binary);
if (in)
{
std::vector<leap_second> leap_seconds;
std::string line;
while (in)
{
std::getline(in, line);
if (!line.empty() && line[0] != '#')
{
std::istringstream in(line);
in.exceptions(std::ios::failbit | std::ios::badbit);
std::string word;
in >> word;
if (word == "Leap")
{
int y, m, d;
in >> y;
m = static_cast<int>(parse_month(in));
in >> d;
leap_seconds.push_back(leap_second(sys_days{year{y}/m/d} + days{1},
detail::undocumented{}));
}
else
{
std::cerr << line << '\n';
}
}
}
return leap_seconds;
}
in.clear();
in.open(get_tz_dir() + std::string(1, folder_delimiter) + "leap-seconds.list",
std::ios_base::binary);
if (in)
{
std::vector<leap_second> leap_seconds;
std::string line;
const auto offset = sys_days{1970_y/1/1}-sys_days{1900_y/1/1};
while (in)
{
std::getline(in, line);
if (!line.empty() && line[0] != '#')
{
std::istringstream in(line);
in.exceptions(std::ios::failbit | std::ios::badbit);
using seconds = std::chrono::seconds;
seconds::rep s;
in >> s;
if (s == 2272060800)
continue;
leap_seconds.push_back(leap_second(sys_seconds{seconds{s}} - offset,
detail::undocumented{}));
}
}
return leap_seconds;
}
in.clear();
in.open(get_tz_dir() + std::string(1, folder_delimiter) + "right/UTC",
std::ios_base::binary);
if (in)
{
return load_just_leaps(in);
}
in.clear();
in.open(get_tz_dir() + std::string(1, folder_delimiter) + "UTC",
std::ios_base::binary);
if (in)
{
return load_just_leaps(in);
}
return {};
}

static
std::unique_ptr<tzdb>
init_tzdb()
Expand Down Expand Up @@ -2709,25 +2780,7 @@ init_tzdb()
}
db->zones.shrink_to_fit();
std::sort(db->zones.begin(), db->zones.end());
# if !MISSING_LEAP_SECONDS
std::ifstream in(get_tz_dir() + std::string(1, folder_delimiter) + "right/UTC",
std::ios_base::binary);
if (in)
{
in.exceptions(std::ios::failbit | std::ios::badbit);
db->leap_seconds = load_just_leaps(in);
}
else
{
in.clear();
in.open(get_tz_dir() + std::string(1, folder_delimiter) +
"UTC", std::ios_base::binary);
if (!in)
throw std::runtime_error("Unable to extract leap second information");
in.exceptions(std::ios::failbit | std::ios::badbit);
db->leap_seconds = load_just_leaps(in);
}
# endif // !MISSING_LEAP_SECONDS
db->leap_seconds = find_read_and_leap_seconds();
# ifdef __APPLE__
db->version = get_version();
# endif
Expand Down Expand Up @@ -3580,11 +3633,9 @@ operator<<(std::ostream& os, const tzdb& db)
os << "Version: " << db.version << "\n\n";
for (const auto& x : db.zones)
os << x << '\n';
#if !MISSING_LEAP_SECONDS
os << '\n';
for (const auto& x : db.leap_seconds)
os << x << '\n';
#endif // !MISSING_LEAP_SECONDS
return os;
}

Expand Down

0 comments on commit ba99134

Please sign in to comment.