rok commented on code in PR #12865: URL: https://github.com/apache/arrow/pull/12865#discussion_r2308733742
########## cpp/src/arrow/compute/kernels/temporal_internal.h: ########## @@ -40,15 +40,65 @@ using arrow_vendored::date::year_month_day; using arrow_vendored::date::zoned_time; using std::chrono::duration_cast; +// https://howardhinnant.github.io/date/tz.html#Examples +using ArrowTimeZone = std::variant<const time_zone*, OffsetZone>; + +template <class Duration, class Func> +auto ApplyTimeZone(const ArrowTimeZone& tz, sys_time<Duration> st, Func&& func) + -> decltype(func(zoned_time<Duration>{})) { + return std::visit( + [&](auto&& zone) { + if constexpr (std::is_pointer_v<std::decay_t<decltype(zone)> >) { + return func(zoned_time<Duration>{zone, st}); + } else { + return func(zoned_time<Duration, const OffsetZone*>{&zone, st}); + } + }, + tz); +} + +template <class Duration, class Func> +auto ApplyTimeZone(const ArrowTimeZone& tz, local_time<Duration> lt, + std::optional<choose> c, Func&& func) + -> decltype(func(zoned_time<Duration>{})) { + return std::visit( + [&](auto&& zone) { + if constexpr (std::is_pointer_v<std::decay_t<decltype(zone)> >) { + return c.has_value() ? func(zoned_time<Duration>{zone, lt, c.value()}) + : func(zoned_time<Duration>{zone, lt}); + } else { + return func(zoned_time<Duration, const OffsetZone*>{&zone, lt}); + } + }, + tz); +} + inline int64_t GetQuarter(const year_month_day& ymd) { return static_cast<int64_t>((static_cast<uint32_t>(ymd.month()) - 1) / 3); } -static inline Result<const time_zone*> LocateZone(const std::string& timezone) { +static inline Result<ArrowTimeZone> LocateZone(const std::string& timezone) { + if (timezone[0] == '+' || timezone[0] == '-') { + // Valid offset strings have to have 4 digits and a sign prefix. + // Valid examples: +01:23 and -0123, invalid examples: 1:23, 123, 0123, 01:23. + std::chrono::minutes zone_offset; + if (timezone.length() == 6 && + !arrow::internal::detail::ParseHH_MM(timezone.substr(1).c_str(), &zone_offset)) { + return Status::Invalid("Cannot locate or parse timezone '", timezone, "'"); + } + if (timezone.length() == 5 && + !arrow::internal::detail::ParseHHMM(timezone.substr(1).c_str(), &zone_offset)) { + return Status::Invalid("Cannot locate or parse timezone '", timezone, "'"); + } Review Comment: Changed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
