forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.cc
More file actions
198 lines (168 loc) · 6.86 KB
/
Copy pathtime.cc
File metadata and controls
198 lines (168 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2021 Google LLC
//
// Licensed 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
//
// https://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.
#include "internal/time.h"
#include <cstdint>
#include <string>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "internal/status_macros.h"
#include "google/protobuf/util/time_util.h"
namespace cel::internal {
namespace {
std::string RawFormatTimestamp(absl::Time timestamp) {
return absl::FormatTime("%Y-%m-%d%ET%H:%M:%E*SZ", timestamp,
absl::UTCTimeZone());
}
} // namespace
absl::Duration MaxDuration() {
// This currently supports a larger range then the current CEL spec. The
// intent is to widen the CEL spec to support the larger range and match
// google.protobuf.Duration from protocol buffer messages, which this
// implementation currently supports.
// TODO(google/cel-spec/issues/214): revisit
return absl::Seconds(google::protobuf::util::TimeUtil::kDurationMaxSeconds) +
absl::Nanoseconds(google::protobuf::util::TimeUtil::kDurationMaxNanoseconds);
}
absl::Duration MinDuration() {
// This currently supports a larger range then the current CEL spec. The
// intent is to widen the CEL spec to support the larger range and match
// google.protobuf.Duration from protocol buffer messages, which this
// implementation currently supports.
// TODO(google/cel-spec/issues/214): revisit
return absl::Seconds(google::protobuf::util::TimeUtil::kDurationMinSeconds) +
absl::Nanoseconds(google::protobuf::util::TimeUtil::kDurationMinNanoseconds);
}
absl::Time MaxTimestamp() {
return absl::UnixEpoch() +
absl::Seconds(google::protobuf::util::TimeUtil::kTimestampMaxSeconds) +
absl::Nanoseconds(google::protobuf::util::TimeUtil::kTimestampMaxNanoseconds);
}
absl::Time MinTimestamp() {
return absl::UnixEpoch() +
absl::Seconds(google::protobuf::util::TimeUtil::kTimestampMinSeconds) +
absl::Nanoseconds(google::protobuf::util::TimeUtil::kTimestampMinNanoseconds);
}
absl::Status ValidateDuration(absl::Duration duration) {
if (duration < MinDuration()) {
return absl::InvalidArgumentError(
absl::StrCat("Duration \"", absl::FormatDuration(duration),
"\" below minimum allowed duration \"",
absl::FormatDuration(MinDuration()), "\""));
}
if (duration > MaxDuration()) {
return absl::InvalidArgumentError(
absl::StrCat("Duration \"", absl::FormatDuration(duration),
"\" above maximum allowed duration \"",
absl::FormatDuration(MaxDuration()), "\""));
}
return absl::OkStatus();
}
absl::StatusOr<absl::Duration> ParseDuration(absl::string_view input) {
absl::Duration duration;
if (!absl::ParseDuration(input, &duration)) {
return absl::InvalidArgumentError("Failed to parse duration from string");
}
return duration;
}
absl::StatusOr<std::string> FormatDuration(absl::Duration duration) {
CEL_RETURN_IF_ERROR(ValidateDuration(duration));
return absl::FormatDuration(duration);
}
std::string DebugStringDuration(absl::Duration duration) {
return absl::FormatDuration(duration);
}
absl::Status ValidateTimestamp(absl::Time timestamp) {
if (timestamp < MinTimestamp()) {
return absl::InvalidArgumentError(
absl::StrCat("Timestamp \"", RawFormatTimestamp(timestamp),
"\" below minimum allowed timestamp \"",
RawFormatTimestamp(MinTimestamp()), "\""));
}
if (timestamp > MaxTimestamp()) {
return absl::InvalidArgumentError(
absl::StrCat("Timestamp \"", RawFormatTimestamp(timestamp),
"\" above maximum allowed timestamp \"",
RawFormatTimestamp(MaxTimestamp()), "\""));
}
return absl::OkStatus();
}
absl::StatusOr<absl::Time> ParseTimestamp(absl::string_view input) {
absl::Time timestamp;
std::string err;
if (!absl::ParseTime(absl::RFC3339_full, input, absl::UTCTimeZone(),
×tamp, &err)) {
return err.empty() ? absl::InvalidArgumentError(
"Failed to parse timestamp from string")
: absl::InvalidArgumentError(absl::StrCat(
"Failed to parse timestamp from string: ", err));
}
CEL_RETURN_IF_ERROR(ValidateTimestamp(timestamp));
return timestamp;
}
absl::StatusOr<std::string> FormatTimestamp(absl::Time timestamp) {
CEL_RETURN_IF_ERROR(ValidateTimestamp(timestamp));
return RawFormatTimestamp(timestamp);
}
std::string FormatNanos(int32_t nanos) {
constexpr int32_t kNanosPerMillisecond = 1000000;
constexpr int32_t kNanosPerMicrosecond = 1000;
if (nanos % kNanosPerMillisecond == 0) {
return absl::StrFormat("%03d", nanos / kNanosPerMillisecond);
} else if (nanos % kNanosPerMicrosecond == 0) {
return absl::StrFormat("%06d", nanos / kNanosPerMicrosecond);
}
return absl::StrFormat("%09d", nanos);
}
absl::StatusOr<std::string> EncodeDurationToJson(absl::Duration duration) {
// Adapted from protobuf time_util.
CEL_RETURN_IF_ERROR(ValidateDuration(duration));
std::string result;
int64_t seconds = absl::IDivDuration(duration, absl::Seconds(1), &duration);
int64_t nanos = absl::IDivDuration(duration, absl::Nanoseconds(1), &duration);
if (seconds < 0 || nanos < 0) {
result = "-";
seconds = -seconds;
nanos = -nanos;
}
absl::StrAppend(&result, seconds);
if (nanos != 0) {
absl::StrAppend(&result, ".", FormatNanos(nanos));
}
absl::StrAppend(&result, "s");
return result;
}
absl::StatusOr<std::string> EncodeTimestampToJson(absl::Time timestamp) {
// Adapted from protobuf time_util.
static constexpr absl::string_view kTimestampFormat = "%E4Y-%m-%dT%H:%M:%S";
CEL_RETURN_IF_ERROR(ValidateTimestamp(timestamp));
// Handle nanos and the seconds separately to match proto JSON format.
absl::Time unix_seconds =
absl::FromUnixSeconds(absl::ToUnixSeconds(timestamp));
int64_t n = (timestamp - unix_seconds) / absl::Nanoseconds(1);
std::string result =
absl::FormatTime(kTimestampFormat, unix_seconds, absl::UTCTimeZone());
if (n > 0) {
absl::StrAppend(&result, ".", FormatNanos(n));
}
absl::StrAppend(&result, "Z");
return result;
}
std::string DebugStringTimestamp(absl::Time timestamp) {
return RawFormatTimestamp(timestamp);
}
} // namespace cel::internal