/proc/self/cwd/internal/proto_time_encoding.cc
Line | Count | Source |
1 | | // Copyright 2021 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "internal/proto_time_encoding.h" |
16 | | |
17 | | #include <string> |
18 | | |
19 | | #include "google/protobuf/duration.pb.h" |
20 | | #include "google/protobuf/timestamp.pb.h" |
21 | | #include "absl/status/status.h" |
22 | | #include "absl/strings/str_cat.h" |
23 | | #include "absl/time/time.h" |
24 | | #include "internal/status_macros.h" |
25 | | #include "internal/time.h" |
26 | | #include "google/protobuf/util/time_util.h" |
27 | | |
28 | | namespace cel::internal { |
29 | | |
30 | | namespace { |
31 | | |
32 | 0 | absl::Status Validate(absl::Time time) { |
33 | 0 | if (time < cel::internal::MinTimestamp()) { |
34 | 0 | return absl::InvalidArgumentError("time below min"); |
35 | 0 | } |
36 | | |
37 | 0 | if (time > cel::internal::MaxTimestamp()) { |
38 | 0 | return absl::InvalidArgumentError("time above max"); |
39 | 0 | } |
40 | 0 | return absl::OkStatus(); |
41 | 0 | } |
42 | | |
43 | 0 | absl::Status CelValidateDuration(absl::Duration duration) { |
44 | 0 | if (duration < cel::internal::MinDuration()) { |
45 | 0 | return absl::InvalidArgumentError("duration below min"); |
46 | 0 | } |
47 | | |
48 | 0 | if (duration > cel::internal::MaxDuration()) { |
49 | 0 | return absl::InvalidArgumentError("duration above max"); |
50 | 0 | } |
51 | 0 | return absl::OkStatus(); |
52 | 0 | } |
53 | | |
54 | | } // namespace |
55 | | |
56 | 0 | absl::Duration DecodeDuration(const google::protobuf::Duration& proto) { |
57 | 0 | return absl::Seconds(proto.seconds()) + absl::Nanoseconds(proto.nanos()); |
58 | 0 | } |
59 | | |
60 | 0 | absl::Time DecodeTime(const google::protobuf::Timestamp& proto) { |
61 | 0 | return absl::FromUnixSeconds(proto.seconds()) + |
62 | 0 | absl::Nanoseconds(proto.nanos()); |
63 | 0 | } |
64 | | |
65 | | absl::Status EncodeDuration(absl::Duration duration, |
66 | 0 | google::protobuf::Duration* proto) { |
67 | 0 | CEL_RETURN_IF_ERROR(CelValidateDuration(duration)); |
68 | | // s and n may both be negative, per the Duration proto spec. |
69 | 0 | const int64_t s = absl::IDivDuration(duration, absl::Seconds(1), &duration); |
70 | 0 | const int64_t n = |
71 | 0 | absl::IDivDuration(duration, absl::Nanoseconds(1), &duration); |
72 | 0 | proto->set_seconds(s); |
73 | 0 | proto->set_nanos(n); |
74 | 0 | return absl::OkStatus(); |
75 | 0 | } |
76 | | |
77 | 0 | absl::StatusOr<std::string> EncodeDurationToString(absl::Duration duration) { |
78 | 0 | google::protobuf::Duration d; |
79 | 0 | auto status = EncodeDuration(duration, &d); |
80 | 0 | if (!status.ok()) { |
81 | 0 | return status; |
82 | 0 | } |
83 | 0 | return google::protobuf::util::TimeUtil::ToString(d); |
84 | 0 | } |
85 | | |
86 | 0 | absl::Status EncodeTime(absl::Time time, google::protobuf::Timestamp* proto) { |
87 | 0 | CEL_RETURN_IF_ERROR(Validate(time)); |
88 | 0 | const int64_t s = absl::ToUnixSeconds(time); |
89 | 0 | proto->set_seconds(s); |
90 | 0 | proto->set_nanos((time - absl::FromUnixSeconds(s)) / absl::Nanoseconds(1)); |
91 | 0 | return absl::OkStatus(); |
92 | 0 | } |
93 | | |
94 | 0 | absl::StatusOr<std::string> EncodeTimeToString(absl::Time time) { |
95 | 0 | google::protobuf::Timestamp t; |
96 | 0 | auto status = EncodeTime(time, &t); |
97 | 0 | if (!status.ok()) { |
98 | 0 | return status; |
99 | 0 | } |
100 | 0 | return google::protobuf::util::TimeUtil::ToString(t); |
101 | 0 | } |
102 | | |
103 | | } // namespace cel::internal |