Skip to content

Commit

Permalink
update vendored/datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Freist committed Jul 11, 2024
1 parent edfa343 commit bfe0792
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 55 deletions.
2 changes: 1 addition & 1 deletion cpp/src/arrow/vendored/datetime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ copies or substantial portions of the Software.
Sources for datetime are adapted from Howard Hinnant's date library
(https://github.com/HowardHinnant/date).

Sources are taken from changeset cc4685a21e4a4fdae707ad1233c61bbaff241f93
Sources are taken from changeset 1ead6715dec030d340a316c927c877a3c4e5a00c
of the above project.

The following changes are made:
Expand Down
30 changes: 19 additions & 11 deletions cpp/src/arrow/vendored/datetime/date.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@
# pragma warning(disable : 4127)
#endif

namespace arrow_vendored
{
namespace date
{

Expand Down Expand Up @@ -4230,7 +4228,7 @@ inline
std::basic_ostream<CharT, Traits>&
operator<<(std::basic_ostream<CharT, Traits>& os, const local_time<Duration>& ut)
{
return (os << sys_time<Duration>{ut.time_since_epoch()});
return (date::operator<<(os, sys_time<Duration>{ut.time_since_epoch()}));
}

namespace detail
Expand Down Expand Up @@ -6353,7 +6351,10 @@ read_signed(std::basic_istream<CharT, Traits>& is, unsigned m = 1, unsigned M =
if (('0' <= c && c <= '9') || c == '-' || c == '+')
{
if (c == '-' || c == '+')
{
(void)is.get();
--M;
}
auto x = static_cast<int>(read_unsigned(is, std::max(m, 1u), M));
if (!is.fail())
{
Expand Down Expand Up @@ -6526,7 +6527,14 @@ read(std::basic_istream<CharT, Traits>& is, int a0, Args&& ...args)
*e++ = static_cast<CharT>(CharT(u % 10) + CharT{'0'});
u /= 10;
} while (u > 0);
#if defined(__GNUC__) && __GNUC__ >= 11
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#endif
std::reverse(buf, e);
#if defined(__GNUC__) && __GNUC__ >= 11
#pragma GCC diagnostic pop
#endif
for (auto p = buf; p != e && is.rdstate() == std::ios::goodbit; ++p)
read(is, *p);
}
Expand Down Expand Up @@ -6592,7 +6600,7 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,

CONSTDATA int not_a_year = numeric_limits<short>::min();
CONSTDATA int not_a_2digit_year = 100;
CONSTDATA int not_a_century = not_a_year / 100;
CONSTDATA int not_a_century = numeric_limits<int>::min();
CONSTDATA int not_a_month = 0;
CONSTDATA int not_a_day = 0;
CONSTDATA int not_a_hour = numeric_limits<int>::min();
Expand Down Expand Up @@ -7519,7 +7527,12 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
{
auto c = static_cast<char>(Traits::to_char_type(ic));
if (c == '-')
{
neg = true;
(void)is.get();
}
else if (c == '+')
(void)is.get();
}
if (modified == CharT{})
{
Expand Down Expand Up @@ -7735,9 +7748,7 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
year_month_day ymd_trial = sys_days(year{Y}/January/Sunday[1]) +
weeks{U-1} +
(weekday{static_cast<unsigned>(wd)} - Sunday);
if (Y == not_a_year)
Y = static_cast<int>(ymd_trial.year());
else if (year{Y} != ymd_trial.year())
if (year{Y} != ymd_trial.year())
goto broken;
if (m == not_a_month)
m = static_cast<int>(static_cast<unsigned>(ymd_trial.month()));
Expand All @@ -7754,9 +7765,7 @@ from_stream(std::basic_istream<CharT, Traits>& is, const CharT* fmt,
year_month_day ymd_trial = sys_days(year{Y}/January/Monday[1]) +
weeks{W-1} +
(weekday{static_cast<unsigned>(wd)} - Monday);
if (Y == not_a_year)
Y = static_cast<int>(ymd_trial.year());
else if (year{Y} != ymd_trial.year())
if (year{Y} != ymd_trial.year())
goto broken;
if (m == not_a_month)
m = static_cast<int>(static_cast<unsigned>(ymd_trial.month()));
Expand Down Expand Up @@ -8224,7 +8233,6 @@ operator<<(std::basic_ostream<CharT, Traits>& os,
}

} // namespace date
} // namespace arrow_vendored

#ifdef _MSC_VER
# pragma warning(pop)
Expand Down
3 changes: 0 additions & 3 deletions cpp/src/arrow/vendored/datetime/ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
# if TARGET_OS_IPHONE
# include <string>

namespace arrow_vendored
{
namespace date
{
namespace iOSUtils
Expand All @@ -44,7 +42,6 @@

} // namespace iOSUtils
} // namespace date
} // namespace arrow_vendored

# endif // TARGET_OS_IPHONE
#else // !__APPLE__
Expand Down
5 changes: 1 addition & 4 deletions cpp/src/arrow/vendored/datetime/ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// SOFTWARE.
//

#include "ios.h"
#include "date/ios.h"

#if TARGET_OS_IPHONE

Expand All @@ -47,8 +47,6 @@
#define TAR_SIZE_POSITION 124
#define TAR_SIZE_SIZE 12

namespace arrow_vendored
{
namespace date
{
namespace iOSUtils
Expand Down Expand Up @@ -335,6 +333,5 @@ bool writeFile(const std::string &tzdataPath, const std::string &fileName,

} // namespace iOSUtils
} // namespace date
} // namespace arrow_vendored

#endif // TARGET_OS_IPHONE
109 changes: 89 additions & 20 deletions cpp/src/arrow/vendored/datetime/tz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
// been invented (that would involve another several millennia of evolution).
// We did not mean to shout.

// NOTE(ARROW): This is required so that symbols are properly exported from the DLL
#include "visibility.h"


#ifdef _WIN32
// windows.h will be included directly and indirectly (e.g. by curl).
// We need to define these macros to prevent windows.h bringing in
Expand Down Expand Up @@ -87,15 +83,19 @@
# include <windows.h>
#endif // _WIN32

#include "tz_private.h"
#include "date/tz_private.h"

#ifdef __APPLE__
# include "ios.h"
# include "date/ios.h"
#else
# define TARGET_OS_IPHONE 0
# define TARGET_OS_SIMULATOR 0
#endif

#if defined(ANDROID) || defined(__ANDROID__)
#include <sys/system_properties.h>
#endif

#if USE_OS_TZDB
# include <dirent.h>
#endif
Expand All @@ -118,7 +118,7 @@
#include <vector>
#include <sys/stat.h>

// unistd.h is used on some platforms as part of the means to get
// unistd.h is used on some platforms as part of the the means to get
// the current time zone. On Win32 windows.h provides a means to do it.
// gcc/mingw supports unistd.h on Win32 but MSVC does not.

Expand Down Expand Up @@ -384,8 +384,7 @@ class file_streambuf
};

#endif // !USE_OS_TZDB
namespace arrow_vendored
{

namespace date
{
// +---------------------+
Expand Down Expand Up @@ -2709,7 +2708,8 @@ operator<<(std::ostream& os, const time_zone& z)
os.width(8);
os << s.format_ << " ";
os << s.until_year_ << ' ' << s.until_date_;
os << " " << s.until_utc_ << " UTC";
os << " ";
date::operator<<(os, s.until_utc_) << " UTC";
os << " " << s.until_std_ << " STD";
os << " " << s.until_loc_;
os << " " << make_time(s.initial_save_);
Expand All @@ -2734,8 +2734,7 @@ operator<<(std::ostream& os, const time_zone& z)
std::ostream&
operator<<(std::ostream& os, const leap_second& x)
{
using namespace date;
return os << x.date_ << " +";
return date::operator<<(os, x.date_) << " +";
}

#if USE_OS_TZDB
Expand Down Expand Up @@ -3716,13 +3715,78 @@ get_tzdb()
return get_tzdb_list().front();
}

namespace {

class recursion_limiter
{
unsigned depth_ = 0;
unsigned limit_;

class restore_recursion_depth;

public:
recursion_limiter(recursion_limiter const&) = delete;
recursion_limiter& operator=(recursion_limiter const&) = delete;

explicit constexpr recursion_limiter(unsigned limit) noexcept;

restore_recursion_depth count();
};

class recursion_limiter::restore_recursion_depth
{
recursion_limiter* rc_;

public:
~restore_recursion_depth();
restore_recursion_depth(restore_recursion_depth&&) = default;

explicit restore_recursion_depth(recursion_limiter* rc) noexcept;
};

inline
recursion_limiter::restore_recursion_depth::~restore_recursion_depth()
{
--(rc_->depth_);
}

inline
recursion_limiter::restore_recursion_depth::restore_recursion_depth(recursion_limiter* rc)
noexcept
: rc_{rc}
{}

inline
constexpr
recursion_limiter::recursion_limiter(unsigned limit) noexcept
: limit_{limit}
{
}

inline
recursion_limiter::restore_recursion_depth
recursion_limiter::count()
{
++depth_;
if (depth_ > limit_)
throw std::runtime_error("recursion limit of " +
std::to_string(limit_) + " exceeded");
return restore_recursion_depth{this};
}

} // unnamed namespace

const time_zone*
#if HAS_STRING_VIEW
tzdb::locate_zone(std::string_view tz_name) const
#else
tzdb::locate_zone(const std::string& tz_name) const
#endif
{
// If a link-to-link chain exceeds this limit, give up
thread_local recursion_limiter rc{10};
auto restore_count = rc.count();

auto zi = std::lower_bound(zones.begin(), zones.end(), tz_name,
#if HAS_STRING_VIEW
[](const time_zone& z, const std::string_view& nm)
Expand All @@ -3746,13 +3810,7 @@ tzdb::locate_zone(const std::string& tz_name) const
});
if (li != links.end() && li->name() == tz_name)
{
zi = std::lower_bound(zones.begin(), zones.end(), li->target(),
[](const time_zone& z, const std::string& nm)
{
return z.name() < nm;
});
if (zi != zones.end() && zi->name() == li->target())
return &*zi;
return locate_zone(li->target());
}
#endif // !USE_OS_TZDB
throw std::runtime_error(std::string(tz_name) + " not found in timezone database");
Expand Down Expand Up @@ -4038,6 +4096,18 @@ tzdb::current_zone() const
if (!result.empty())
return locate_zone(result);
#endif
// Fall through to try other means.
}
{
// On Android, it is not possible to use file based approach either,
// we have to ask the value of `persist.sys.timezone` system property
#if defined(ANDROID) || defined(__ANDROID__)
char sys_timezone[PROP_VALUE_MAX];
if (__system_property_get("persist.sys.timezone", sys_timezone) > 0)
{
return locate_zone(sys_timezone);
}
#endif // defined(ANDROID) || defined(__ANDROID__)
// Fall through to try other means.
}
{
Expand Down Expand Up @@ -4072,7 +4142,6 @@ current_zone()
}

} // namespace date
} // namespace arrow_vendored

#if defined(__GNUC__) && __GNUC__ < 5
# pragma GCC diagnostic pop
Expand Down
13 changes: 2 additions & 11 deletions cpp/src/arrow/vendored/datetime/tz.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@
// required. On Windows, the names are never "Standard" so mapping is always required.
// Technically any OS may use the mapping process but currently only Windows does use it.

// NOTE(ARROW): If this is not set, then the library will attempt to
// use libcurl to obtain a timezone database, and we probably do not want this.
#ifndef _WIN32
#define USE_OS_TZDB 1
#endif

#ifndef USE_OS_TZDB
# define USE_OS_TZDB 0
#endif
Expand Down Expand Up @@ -140,8 +134,6 @@ static_assert(HAS_REMOTE_API == 0 ? AUTO_DOWNLOAD == 0 : true,
# endif
#endif

namespace arrow_vendored
{
namespace date
{

Expand Down Expand Up @@ -239,8 +231,8 @@ nonexistent_local_time::make_msg(local_time<Duration> tp, const local_info& i)
<< i.first.abbrev << " and\n"
<< local_seconds{i.second.begin.time_since_epoch()} + i.second.offset << ' '
<< i.second.abbrev
<< " which are both equivalent to\n"
<< i.first.end << " UTC";
<< " which are both equivalent to\n";
date::operator<<(os, i.first.end) << " UTC";
return os.str();
}

Expand Down Expand Up @@ -2796,6 +2788,5 @@ to_gps_time(const tai_time<Duration>& t)
}

} // namespace date
} // namespace arrow_vendored

#endif // TZ_H
Loading

0 comments on commit bfe0792

Please sign in to comment.