iceberg-cpp
Loading...
Searching...
No Matches
temporal_util.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
24
25#include <chrono>
26#include <cstdint>
27#include <string_view>
28
30#include "iceberg/result.h"
31#include "iceberg/type_fwd.h"
32
33namespace iceberg::internal {
34
35inline constexpr int64_t kNanosPerMicro = 1000;
36inline constexpr int64_t kMicrosPerMilli = 1000;
37inline constexpr int64_t kMicrosPerSecond = 1000 * kMicrosPerMilli;
38inline constexpr int64_t kSecondsPerMinute = 60;
39inline constexpr int64_t kMinutesPerHour = 60;
40inline constexpr int64_t kHoursPerDay = 24;
41inline constexpr int64_t kSecondsPerHour = kMinutesPerHour * kSecondsPerMinute;
42inline constexpr int64_t kSecondsPerDay = kHoursPerDay * kSecondsPerHour;
43inline constexpr int64_t kMicrosPerDay = kSecondsPerDay * kMicrosPerSecond;
44inline constexpr int64_t kNanosPerMilli = kMicrosPerMilli * kNanosPerMicro;
45inline constexpr int64_t kNanosPerSecond = kMicrosPerSecond * kNanosPerMicro;
46inline constexpr int64_t kNanosPerDay = kMicrosPerDay * kNanosPerMicro;
47
48inline constexpr auto kEpochYmd = std::chrono::year{1970} / std::chrono::January / 1;
49inline constexpr auto kEpochDays = std::chrono::sys_days{kEpochYmd};
50
51} // namespace iceberg::internal
52
53namespace iceberg {
54
55class ICEBERG_EXPORT TemporalUtils {
56 public:
58 static int64_t NanosToMicros(int64_t nanos);
59
61 static Result<int64_t> MicrosToNanos(int64_t micros);
62
69 static Result<int32_t> ParseDay(std::string_view str);
70
79 static Result<int64_t> ParseTime(std::string_view str);
80
89 static Result<int64_t> ParseTimeNs(std::string_view str);
90
99 static Result<int64_t> ParseTimestamp(std::string_view str);
100
109 static Result<int64_t> ParseTimestampNs(std::string_view str);
110
119 static Result<int64_t> ParseTimestampWithZone(std::string_view str);
120
129 static Result<int64_t> ParseTimestampNsWithZone(std::string_view str);
130
145 static Result<bool> IsUtcOffset(std::string_view str);
146
148 static Result<Literal> ExtractYear(const Literal& literal);
149
151 static Result<Literal> ExtractMonth(const Literal& literal);
152
154 static Result<Literal> ExtractDay(const Literal& literal);
155
157 static Result<Literal> ExtractHour(const Literal& literal);
158};
159
160} // namespace iceberg
Literal is a literal value that is associated with a primitive type.
Definition literal.h:42
Definition temporal_util.h:55
static Result< bool > IsUtcOffset(std::string_view str)
Reports whether a timestamp-with-zone string carries a zero (UTC) offset.
static Result< int64_t > ParseTimestampWithZone(std::string_view str)
Parses a timestamp-with-zone string into microseconds since epoch (UTC).
static Result< int64_t > ParseTimestampNs(std::string_view str)
Parses a timestamp string into nanoseconds since epoch.
static Result< int64_t > ParseTime(std::string_view str)
Parses a time string into microseconds from midnight.
static Result< int64_t > MicrosToNanos(int64_t micros)
Convert microseconds since epoch to nanoseconds, failing on overflow.
static Result< int64_t > ParseTimeNs(std::string_view str)
Parses a time string into nanoseconds from midnight.
static Result< int64_t > ParseTimestampNsWithZone(std::string_view str)
Parses a timestamp-with-zone string into nanoseconds since epoch (UTC).
static int64_t NanosToMicros(int64_t nanos)
Convert nanoseconds since epoch to microseconds using floor division.
static Result< Literal > ExtractMonth(const Literal &literal)
Extract a date or timestamp month, as months from 1970-01-01.
static Result< Literal > ExtractHour(const Literal &literal)
Extract a timestamp hour, as hours from 1970-01-01 00:00:00.
static Result< Literal > ExtractYear(const Literal &literal)
Extract a date or timestamp year, as years from 1970.
static Result< Literal > ExtractDay(const Literal &literal)
Extract a date or timestamp day, as days from 1970-01-01.
static Result< int32_t > ParseDay(std::string_view str)
Parses a date string in "[+-]yyyy-MM-dd" format into days since epoch.
static Result< int64_t > ParseTimestamp(std::string_view str)
Parses a timestamp string into microseconds since epoch.
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
std::expected< T, E > Result
Result alias.
Definition result.h:88
Define Result, Status, and error helpers.