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