pitrou commented on code in PR #12865:
URL: https://github.com/apache/arrow/pull/12865#discussion_r2300450364


##########
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:
   This is really looking for a `switch`/`case` statement IMHO.



##########
cpp/src/arrow/util/date_internal.h:
##########
@@ -0,0 +1,84 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include "arrow/vendored/datetime.h"
+
+namespace arrow::internal {
+
+using arrow_vendored::date::choose;
+using arrow_vendored::date::days;
+using arrow_vendored::date::floor;
+using arrow_vendored::date::format;
+using arrow_vendored::date::local_days;
+using arrow_vendored::date::local_time;
+using arrow_vendored::date::locate_zone;
+using arrow_vendored::date::sys_days;
+using arrow_vendored::date::sys_info;
+using arrow_vendored::date::sys_seconds;
+using arrow_vendored::date::sys_time;
+using arrow_vendored::date::time_zone;
+using arrow_vendored::date::year_month_day;
+using arrow_vendored::date::zoned_time;
+using arrow_vendored::date::zoned_traits;
+using std::chrono::minutes;

Review Comment:
   Can also perhaps remove `using std::chrono::minutes`.



##########
cpp/src/arrow/compute/kernels/scalar_temporal_test.cc:
##########
@@ -1872,33 +1883,46 @@ TEST_F(ScalarTemporalTest, TestLocalTimestamp) {
                      times_seconds_precision, timestamp(u), 
expected_local_kolkata);
     CheckScalarUnary("local_timestamp", timestamp(u, "Pacific/Marquesas"),
                      times_seconds_precision, timestamp(u), 
expected_local_marquesas);
+    CheckScalarUnary("local_timestamp", timestamp(u, "-09:30"), 
times_seconds_precision,
+                     timestamp(u), expected_local_marquesas);
   }
 }
 
 TEST_F(ScalarTemporalTest, TestAssumeTimezone) {
   std::string timezone_utc = "UTC";
   std::string timezone_kolkata = "Asia/Kolkata";
   std::string timezone_us_central = "America/Chicago";
+  std::string timezone_tbilisi = "Asia/Tbilisi";
+  std::string timezone_tbilisi_offset = "+04:00";
   const char* times_utc = R"(["1970-01-01T00:00:00", null])";
   const char* times_kolkata = R"(["1970-01-01T05:30:00", null])";
+  const char* times_tbilisi = R"(["1970-01-01T04:00:00", null])";
   const char* times_us_central = R"(["1969-12-31T18:00:00", null])";
   auto options_utc = AssumeTimezoneOptions(timezone_utc);
   auto options_kolkata = AssumeTimezoneOptions(timezone_kolkata);
   auto options_us_central = AssumeTimezoneOptions(timezone_us_central);
+  auto options_tbilisi = AssumeTimezoneOptions(timezone_tbilisi);
+  auto options_tbilisi_offset = AssumeTimezoneOptions(timezone_tbilisi_offset);
   auto options_invalid = AssumeTimezoneOptions("Europe/Brusselsss");

Review Comment:
   Can we now test with several invalid timezones, including several offset 
zones?



##########
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});

Review Comment:
   ```suggestion
             // Offset zone conversion to/from UTC is always unambiguous
             // therefore `c` can be ignored.
             return func(zoned_time<Duration, const OffsetZone*>{&zone, lt});
   ```



##########
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] == '-') {

Review Comment:
   Let's perhaps move the function definition to a `.cc` since it's neither 
trivial nor performance-critical?



##########
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) {

Review Comment:
   Let's take a `std::string_view` perhaps?



##########
cpp/src/arrow/compute/kernels/scalar_temporal_test.cc:
##########
@@ -2140,6 +2174,13 @@ TEST_F(ScalarTemporalTest, StrftimeCLocale) {
                    utf8(), string_locale_specific, &options_locale_specific);
 }
 
+TEST_F(ScalarTemporalTest, StrftimeRoundtrip) {
+  auto options = StrftimeOptions("%Y-%m-%dT%H:%M:%S%z", "C");
+
+  CheckScalarUnary("strftime", timestamp(TimeUnit::SECOND, "+04:30"), 
times_with_offsets,
+                   utf8(), times_with_offsets, &options);

Review Comment:
   I'm curious: `times_with_offsets` and the timestamp type reference the same 
offset "+04:30", but what happens if they reference different offsets?
   
   (also what happens if the timestamp type references an invalid offset?)



##########
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:
   What if length is neither 5 or 6?



##########
cpp/src/arrow/compute/kernels/scalar_temporal_test.cc:
##########
@@ -772,32 +779,36 @@ TEST_F(ScalarTemporalTest, TestZoned1) {
                         {"iso_year": 2008, "iso_week": 52, "iso_day_of_week": 
6},
                         {"iso_year": 2008, "iso_week": 52, "iso_day_of_week": 
7},
                         {"iso_year": 2011, "iso_week": 52, "iso_day_of_week": 
6}, null])");
-  auto quarter = "[4, 1, 4, 2, 4, 4, 4, 4, 4, 1, 1, 4, 4, 4, 4, 4, null]";
-  auto hour = "[14, 13, 15, 18, 15, 16, 17, 18, 19, 21, 22, 23, 0, 14, 14, 15, 
null]";
-  auto minute = "[30, 53, 41, 3, 35, 40, 45, 50, 55, 0, 5, 10, 15, 30, 30, 32, 
null]";
+    auto quarter = "[4, 1, 4, 2, 4, 4, 4, 4, 4, 1, 1, 4, 4, 4, 4, 4, null]";
+    auto hour = "[14, 13, 15, 18, 15, 16, 17, 18, 19, 21, 22, 23, 0, 14, 14, 
15, null]";
+    auto minute = "[30, 53, 41, 3, 35, 40, 45, 50, 55, 0, 5, 10, 15, 30, 30, 
32, null]";
+    if (timezone == "-09:30") {
+      minute = "[30, 53, 29, 3, 35, 40, 45, 50, 55, 0, 5, 10, 15, 30, 30, 32, 
null]";

Review Comment:
   Hmm, why is the third value different from above? Can you add a comment 
explaining this?



##########
cpp/src/arrow/compute/kernels/temporal_internal.h:
##########
@@ -128,27 +183,30 @@ struct ZonedLocalizer {
 template <typename Duration>
 struct TimestampFormatter {
   const char* format;
-  const time_zone* tz;
+  const ArrowTimeZone tz_;

Review Comment:
   Other members don't have trailing underscores so why add one here?
   (or you can add trailing underscores to all of them if you prefer)



-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to