Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:27:10

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 #pragma once
0019 
0020 #include <chrono>
0021 #include <memory>
0022 #include <utility>
0023 
0024 #include "arrow/type_fwd.h"
0025 #include "arrow/util/visibility.h"
0026 
0027 namespace arrow {
0028 namespace util {
0029 
0030 enum DivideOrMultiply {
0031   MULTIPLY,
0032   DIVIDE,
0033 };
0034 
0035 ARROW_EXPORT
0036 std::pair<DivideOrMultiply, int64_t> GetTimestampConversion(TimeUnit::type in_unit,
0037                                                             TimeUnit::type out_unit);
0038 
0039 // Converts a Timestamp value into another Timestamp value.
0040 //
0041 // This function takes care of properly transforming from one unit to another.
0042 //
0043 // \param[in] in the input type. Must be TimestampType.
0044 // \param[in] out the output type. Must be TimestampType.
0045 // \param[in] value the input value.
0046 //
0047 // \return The converted value, or an error.
0048 ARROW_EXPORT Result<int64_t> ConvertTimestampValue(const std::shared_ptr<DataType>& in,
0049                                                    const std::shared_ptr<DataType>& out,
0050                                                    int64_t value);
0051 
0052 template <typename Visitor, typename... Args>
0053 decltype(std::declval<Visitor>()(std::chrono::seconds{}, std::declval<Args&&>()...))
0054 VisitDuration(TimeUnit::type unit, Visitor&& visitor, Args&&... args) {
0055   switch (unit) {
0056     default:
0057     case TimeUnit::SECOND:
0058       break;
0059     case TimeUnit::MILLI:
0060       return visitor(std::chrono::milliseconds{}, std::forward<Args>(args)...);
0061     case TimeUnit::MICRO:
0062       return visitor(std::chrono::microseconds{}, std::forward<Args>(args)...);
0063     case TimeUnit::NANO:
0064       return visitor(std::chrono::nanoseconds{}, std::forward<Args>(args)...);
0065   }
0066   return visitor(std::chrono::seconds{}, std::forward<Args>(args)...);
0067 }
0068 
0069 /// Convert a count of seconds to the corresponding count in a different TimeUnit
0070 struct CastSecondsToUnitImpl {
0071   template <typename Duration>
0072   int64_t operator()(Duration, int64_t seconds) {
0073     auto duration = std::chrono::duration_cast<Duration>(std::chrono::seconds{seconds});
0074     return static_cast<int64_t>(duration.count());
0075   }
0076 };
0077 
0078 inline int64_t CastSecondsToUnit(TimeUnit::type unit, int64_t seconds) {
0079   return VisitDuration(unit, CastSecondsToUnitImpl{}, seconds);
0080 }
0081 
0082 }  // namespace util
0083 }  // namespace arrow