iceberg-cpp
Loading...
Searching...
No Matches
temporal_test_helper.h
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
22#include <chrono>
23#include <cstdint>
24
25namespace iceberg {
26
27using namespace std::chrono; // NOLINT
28
29struct DateParts {
30 int32_t year{0};
31 uint8_t month{0};
32 uint8_t day{0};
33};
34
35struct TimeParts {
36 int32_t hour{0};
37 int32_t minute{0};
38 int32_t second{0};
39 int32_t microsecond{0};
40};
41
43 int32_t year{0};
44 uint8_t month{0};
45 uint8_t day{0};
46 int32_t hour{0};
47 int32_t minute{0};
48 int32_t second{0};
49 int32_t microsecond{0};
50 // e.g. -480 for PST (UTC-8:00), +480 for Asia/Shanghai (UTC+8:00)
51 int32_t tz_offset_minutes{0};
52};
53
55 int32_t year{0};
56 uint8_t month{0};
57 uint8_t day{0};
58 int32_t hour{0};
59 int32_t minute{0};
60 int32_t second{0};
61 int32_t nanosecond{0};
62 // e.g. -480 for PST (UTC-8:00), +480 for Asia/Shanghai (UTC+8:00)
63 int32_t tz_offset_minutes{0};
64};
65
67 static constexpr auto kEpochDays = sys_days(year{1970} / January / 1);
68
69 public:
71 static int32_t CreateDate(const DateParts& parts) {
72 return static_cast<int32_t>(
73 (sys_days(year{parts.year} / month{parts.month} / day{parts.day}) - kEpochDays)
74 .count());
75 }
76
78 static int64_t CreateTime(const TimeParts& parts) {
79 return duration_cast<microseconds>(hours(parts.hour) + minutes(parts.minute) +
80 seconds(parts.second) +
81 microseconds(parts.microsecond))
82 .count();
83 }
84
86 static int64_t CreateTimestamp(const TimestampParts& parts) {
87 year_month_day ymd{year{parts.year}, month{parts.month}, day{parts.day}};
88 auto tp = sys_time<microseconds>{(sys_days(ymd) + hours{parts.hour} +
89 minutes{parts.minute} + seconds{parts.second} +
90 microseconds{parts.microsecond})
91 .time_since_epoch()};
92 return tp.time_since_epoch().count();
93 }
94
96 static int64_t CreateTimestampTz(const TimestampParts& parts) {
97 year_month_day ymd{year{parts.year}, month{parts.month}, day{parts.day}};
98 auto tp = sys_time<microseconds>{(sys_days(ymd) + hours{parts.hour} +
99 minutes{parts.minute} + seconds{parts.second} +
100 microseconds{parts.microsecond} -
101 minutes{parts.tz_offset_minutes})
102 .time_since_epoch()};
103 return tp.time_since_epoch().count();
104 }
105
107 static int64_t CreateTimestampNanos(const TimestampNanosParts& parts) {
108 year_month_day ymd{year{parts.year}, month{parts.month}, day{parts.day}};
109 auto tp =
110 sys_time<nanoseconds>{(sys_days(ymd) + hours{parts.hour} + minutes{parts.minute} +
111 seconds{parts.second} + nanoseconds{parts.nanosecond})
112 .time_since_epoch()};
113 return tp.time_since_epoch().count();
114 }
115
117 static int64_t CreateTimestampTzNanos(const TimestampNanosParts& parts) {
118 year_month_day ymd{year{parts.year}, month{parts.month}, day{parts.day}};
119 auto tp =
120 sys_time<nanoseconds>{(sys_days(ymd) + hours{parts.hour} + minutes{parts.minute} +
121 seconds{parts.second} + nanoseconds{parts.nanosecond} -
122 minutes{parts.tz_offset_minutes})
123 .time_since_epoch()};
124 return tp.time_since_epoch().count();
125 }
126};
127
128} // namespace iceberg
Definition temporal_test_helper.h:66
static int64_t CreateTimestamp(const TimestampParts &parts)
Construct a timestamp, microsecond precision, without timezone.
Definition temporal_test_helper.h:86
static int64_t CreateTimestampNanos(const TimestampNanosParts &parts)
Construct a timestamp, nanosecond precision, without timezone.
Definition temporal_test_helper.h:107
static int64_t CreateTimestampTz(const TimestampParts &parts)
Construct a timestamp, microsecond precision, with timezone.
Definition temporal_test_helper.h:96
static int64_t CreateTime(const TimeParts &parts)
Construct a time-of-day, microsecond precision, without date, timezone.
Definition temporal_test_helper.h:78
static int32_t CreateDate(const DateParts &parts)
Construct a Calendar date without timezone or time.
Definition temporal_test_helper.h:71
static int64_t CreateTimestampTzNanos(const TimestampNanosParts &parts)
Construct a timestamp, nanosecond precision, with timezone.
Definition temporal_test_helper.h:117
Definition temporal_test_helper.h:29
Definition temporal_test_helper.h:35
Definition temporal_test_helper.h:54
Definition temporal_test_helper.h:42