jvictorhuguenin commented on a change in pull request #10432: URL: https://github.com/apache/arrow/pull/10432#discussion_r813816522
########## File path: cpp/src/gandiva/convert_timezone_holder.h ########## @@ -0,0 +1,282 @@ +// 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 <memory> +#include <string> +#include <unordered_map> + +#include "arrow/vendored/datetime/date.h" +#include "arrow/vendored/datetime/tz.h" +#include "gandiva/function_holder.h" +#include "gandiva/node.h" +#include "gandiva/visibility.h" + +using arrow_vendored::date::local_time; +using arrow_vendored::date::locate_zone; +using arrow_vendored::date::make_zoned; +using arrow_vendored::date::sys_time; +using arrow_vendored::date::time_zone; +using std::chrono::milliseconds; + +namespace gandiva { + +class OffsetZone { + std::chrono::seconds offset_; + + public: + explicit OffsetZone(std::chrono::seconds offset) : offset_(offset) {} + + template <class Duration> + auto to_local(arrow_vendored::date::sys_time<Duration> tp) const { + using std::common_type_t; + using std::chrono::seconds; + using LT = local_time<std::common_type_t<Duration, seconds>>; + return LT((tp + offset_).time_since_epoch()); + } + + template <class Duration> + auto to_sys(arrow_vendored::date::local_time<Duration> tp) const { + using std::chrono::seconds; + using ST = sys_time<std::common_type_t<Duration, seconds>>; + return ST((tp - offset_).time_since_epoch()); + } +}; + +/// Function Holder for SQL 'CONVERT_TIMEZONE' +class GANDIVA_EXPORT ConvertTimezoneHolder : public FunctionHolder { + public: + ~ConvertTimezoneHolder() override = default; + + static Status Make(const FunctionNode& node, + std::shared_ptr<ConvertTimezoneHolder>* holder); + + static Status Make(const std::string& srcTz, const std::string& destTz, + std::shared_ptr<ConvertTimezoneHolder>* holder); + + /// Return the converted timestamp + int64_t convert(const int64_t src_timestamp) { + using std::chrono::seconds; + + if (dest_timezone != NULLPTR && src_timezone == NULLPTR) { + return dest_timezone + ->to_local(src_offset_tz->to_sys<milliseconds>( + local_time<milliseconds>(milliseconds(src_timestamp)))) + .time_since_epoch() + .count(); + } else if (dest_timezone == NULLPTR && src_timezone != NULLPTR) { + return dest_offset_tz + ->to_local(src_timezone->to_sys<milliseconds>( + local_time<milliseconds>(milliseconds(src_timestamp)))) + .time_since_epoch() + .count(); + } else if (dest_timezone == NULLPTR && src_timezone == NULLPTR) { + return dest_offset_tz + ->to_local(src_offset_tz->to_sys<milliseconds>( + local_time<milliseconds>(milliseconds(src_timestamp)))) + .time_since_epoch() + .count(); + } else { + return dest_timezone + ->to_local(src_timezone->to_sys<milliseconds>( + local_time<milliseconds>(milliseconds(src_timestamp)))) + .time_since_epoch() + .count(); + } + } + + // Tracks if the timezones given could be found in IANA Timezone DB. + bool ok = true; + + private: + explicit ConvertTimezoneHolder(const std::string& srcTz, const std::string& destTz) { + auto srcTz_abbrv_offset = abbrv_tz.find(srcTz); + auto destTz_abbrv_offset = abbrv_tz.find(destTz); + + try { + if (srcTz_abbrv_offset != abbrv_tz.end()) { + auto secs = convert_offset_to_seconds(srcTz_abbrv_offset->second); + src_offset_tz = std::make_shared<OffsetZone>(secs); + } else { + if (is_timezone_shift(srcTz) || + is_timezone_offset(const_cast<std::string&>(srcTz))) { Review comment: done -- 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]
