chaokunyang commented on code in PR #3789: URL: https://github.com/apache/fory/pull/3789#discussion_r3489863736
########## cpp/fory/type/temporal.h: ########## @@ -0,0 +1,139 @@ +/* + * 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 <chrono> +#include <cstddef> +#include <cstdint> +#include <functional> + +namespace fory { + +/// Duration: absolute length of time as nanoseconds +class Duration { +public: + Duration() : ns_(0) {} + + explicit Duration(std::chrono::nanoseconds ns) : ns_(ns) {} + + std::chrono::nanoseconds to_chrono() const { return ns_; } + + int64_t count() const { return ns_.count(); } + + bool operator==(const Duration &other) const { return ns_ == other.ns_; } + + bool operator!=(const Duration &other) const { return !(*this == other); } + + bool operator<(const Duration &other) const { return ns_ < other.ns_; } + +private: + std::chrono::nanoseconds ns_; +}; + +/// Timestamp: point in time as nanoseconds since Unix epoch (Jan 1, 1970 UTC) +class Timestamp { +public: + using ChronoType = std::chrono::time_point<std::chrono::system_clock, + std::chrono::nanoseconds>; + Timestamp() : tp_() {} + + explicit Timestamp(ChronoType tp) : tp_(tp) {} + + explicit Timestamp(std::chrono::nanoseconds ns) : tp_(ns) {} + + ChronoType to_chrono() const { return tp_; } + + std::chrono::nanoseconds time_since_epoch() const { + return tp_.time_since_epoch(); + } + + bool operator==(const Timestamp &other) const { return tp_ == other.tp_; } + + bool operator!=(const Timestamp &other) const { return !(*this == other); } + + bool operator<(const Timestamp &other) const { return tp_ < other.tp_; } + +private: + ChronoType tp_; +}; + +/// Date: naive date without timezone as days since Unix epoch +class Date { +public: + Date() : days_since_epoch_(0) {} + explicit Date(int32_t days) : days_since_epoch_(days) {} + + int32_t days_since_epoch() const { return days_since_epoch_; } + + bool operator==(const Date &other) const { + return days_since_epoch_ == other.days_since_epoch_; + } + + bool operator!=(const Date &other) const { return !(*this == other); } + + bool operator<(const Date &other) const { + return days_since_epoch_ < other.days_since_epoch_; + } + +private: + int32_t days_since_epoch_; // Days since Jan 1, 1970 UTC Review Comment: Could we use int64_t instead? -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
