morningman commented on a change in pull request #1598: Time zone related be function URL: https://github.com/apache/incubator-doris/pull/1598#discussion_r311497048
########## File path: be/src/runtime/timestamp_value.h ########## @@ -0,0 +1,174 @@ +// 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. + +#ifndef DORIS_BE_RUNTIME_TIMESTAMP_VALUE_H +#define DORIS_BE_RUNTIME_TIMESTAMP_VALUE_H + +#include <stdint.h> +#include <iostream> +#include <cstddef> +#include <sstream> + +#include "exprs/timestamp_functions.h" +#include "udf/udf.h" +#include "util/hash_util.hpp" + +namespace doris { + +// Functions to load and access the timestamp database. +class TimezoneDatabase { +public: + TimezoneDatabase(); + + ~TimezoneDatabase(); + + static void init() { + TimezoneDatabase(); + } + + static boost::local_time::time_zone_ptr find_timezone(const std::string &tz); + +private: + static const char *_s_timezone_database_str; + static boost::local_time::tz_database _s_tz_database; +}; + +class TimestampValue { +public: + TimestampValue() { } + + TimestampValue(int64_t timestamp) { + val = timestamp; + } + + bool from_date_time_value(const DateTimeValue& tv, std::string timezone) { + boost::local_time::time_zone_ptr local_time_zone = TimezoneDatabase::find_timezone(timezone); + if (local_time_zone == nullptr) { + return false; + } + + std::stringstream ss; + ss << tv; + boost::posix_time::ptime pt = boost::posix_time::time_from_string(ss.str()); + boost::local_time::local_date_time lt(pt.date(), pt.time_of_day(), local_time_zone, + boost::local_time::local_date_time::NOT_DATE_TIME_ON_ERROR); + + boost::posix_time::ptime utc_ptime = lt.utc_time(); + boost::posix_time::ptime utc_start(boost::gregorian::date(1970, 1, 1)); + boost::posix_time::time_duration dur = utc_ptime - utc_start; + val = dur.total_milliseconds(); + return true; + } + + bool to_datetime_value(DateTimeValue& dt_val, std::string timezone) { + boost::local_time::time_zone_ptr local_time_zone = TimezoneDatabase::find_timezone(timezone); + if (local_time_zone == nullptr) { + return false; + } + boost::local_time::local_date_time lt(boost::posix_time::from_time_t(val), local_time_zone); + boost::posix_time::ptime locat_ptime = lt.local_time(); + + dt_val.set_type(TIME_DATETIME); + dt_val.from_olap_datetime( + locat_ptime.date().year() * 10000000000 + + locat_ptime.date().month() * 100000000 + + locat_ptime.date().day() * 1000000 + + locat_ptime.time_of_day().hours() * 10000 + + locat_ptime.time_of_day().minutes() * 100 + + locat_ptime.time_of_day().seconds()); + return true; + } + + std::string to_datetime_string(std::string timezone) { + boost::local_time::time_zone_ptr local_time_zone = TimezoneDatabase::find_timezone(timezone); + if (local_time_zone == nullptr) { + return ""; + } + boost::local_time::local_date_time lt(boost::posix_time::from_time_t(val), local_time_zone); + boost::posix_time::ptime ret_ptime = lt.local_time(); + + std::stringstream ss; + ss << std::setw(4) << std::setfill('0') << ret_ptime.date().year() << "-" + << std::setw(2) << std::setfill('0') << ret_ptime.date().month().as_number() << "-" + << std::setw(2) << std::setfill('0') << ret_ptime.date().day() << " " + << std::setw(2) << std::setfill('0') << ret_ptime.time_of_day().hours() << ":" + << std::setw(2) << std::setfill('0') << ret_ptime.time_of_day().minutes() << ":" + << std::setw(2) << std::setfill('0') << ret_ptime.time_of_day().seconds(); + return ss.str(); + } + + void to_datetime_val(doris_udf::DateTimeVal *tv) const { + boost::posix_time::ptime p = boost::posix_time::from_time_t(val / 1000); + int _year = p.date().year(); Review comment: Your code style is not compatible with Doris C++ code style. private member should starts with `_`. local variables should not start with `_` ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
