Coverage Report

Created: 2026-09-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/common/values/timestamp_value.h
Line
Count
Source
1
// Copyright 2023 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
// IWYU pragma: private, include "common/value.h"
16
// IWYU pragma: friend "common/value.h"
17
18
#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_TIMESTAMP_VALUE_H_
19
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_TIMESTAMP_VALUE_H_
20
21
#include <ostream>
22
#include <string>
23
24
#include "absl/base/attributes.h"
25
#include "absl/base/nullability.h"
26
#include "absl/log/absl_check.h"
27
#include "absl/status/status.h"
28
#include "absl/status/statusor.h"
29
#include "absl/strings/string_view.h"
30
#include "absl/time/time.h"
31
#include "absl/utility/utility.h"
32
#include "common/type.h"
33
#include "common/value_kind.h"
34
#include "common/values/values.h"
35
#include "internal/time.h"
36
#include "google/protobuf/arena.h"
37
#include "google/protobuf/descriptor.h"
38
#include "google/protobuf/io/zero_copy_stream.h"
39
#include "google/protobuf/message.h"
40
41
namespace cel {
42
43
class Value;
44
class TimestampValue;
45
46
TimestampValue UnsafeTimestampValue(absl::Time value);
47
absl::StatusOr<TimestampValue> SafeTimestampValue(absl::Time value);
48
49
// `TimestampValue` represents values of the primitive `timestamp` type.
50
class TimestampValue final
51
    : private common_internal::ValueMixin<TimestampValue> {
52
 public:
53
  static constexpr ValueKind kKind = ValueKind::kTimestamp;
54
55
  // Constructs a `TimestampValue` from an `absl::Time`.
56
  //
57
  // DCHECK-fails if the value is not in the supported range.
58
  //
59
  // Prefer using `SafeTimestampValue` or `UnsafeTimestampValue` if the caller
60
  // has already validated the value.
61
  explicit TimestampValue(absl::Time value) noexcept
62
0
      : TimestampValue(absl::in_place, value) {
63
0
    ABSL_DCHECK_OK(internal::ValidateTimestamp(value));
64
0
  }
65
66
  TimestampValue() = default;
67
  TimestampValue(const TimestampValue&) = default;
68
  TimestampValue(TimestampValue&&) = default;
69
  TimestampValue& operator=(const TimestampValue&) = default;
70
  TimestampValue& operator=(TimestampValue&&) = default;
71
72
0
  ValueKind kind() const { return kKind; }
73
74
0
  absl::string_view GetTypeName() const { return TimestampType::kName; }
75
76
  std::string DebugString() const;
77
78
  // See Value::SerializeTo().
79
  absl::Status SerializeTo(
80
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
81
      google::protobuf::MessageFactory* absl_nonnull message_factory,
82
      google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const;
83
84
  // See Value::ConvertToJson().
85
  absl::Status ConvertToJson(
86
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
87
      google::protobuf::MessageFactory* absl_nonnull message_factory,
88
      google::protobuf::Message* absl_nonnull json) const;
89
90
  absl::Status Equal(const Value& other,
91
                     const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
92
                     google::protobuf::MessageFactory* absl_nonnull message_factory,
93
                     google::protobuf::Arena* absl_nonnull arena,
94
                     Value* absl_nonnull result) const;
95
  using ValueMixin::Equal;
96
97
0
  bool IsZeroValue() const { return ToTime() == absl::UnixEpoch(); }
98
99
  ABSL_DEPRECATED("Use ToTime()")
100
621
  absl::Time NativeValue() const { return static_cast<absl::Time>(*this); }
101
102
  ABSL_DEPRECATED("Use ToTime()")
103
  // NOLINTNEXTLINE(google-explicit-constructor)
104
621
  operator absl::Time() const noexcept { return value_; }
105
106
2.98k
  absl::Time ToTime() const { return value_; }
107
108
0
  friend void swap(TimestampValue& lhs, TimestampValue& rhs) noexcept {
109
0
    using std::swap;
110
0
    swap(lhs.value_, rhs.value_);
111
0
  }
112
113
0
  friend bool operator==(TimestampValue lhs, TimestampValue rhs) {
114
0
    return lhs.value_ == rhs.value_;
115
0
  }
116
117
0
  friend bool operator<(const TimestampValue& lhs, const TimestampValue& rhs) {
118
0
    return lhs.value_ < rhs.value_;
119
0
  }
120
121
 private:
122
  friend class common_internal::ValueMixin<TimestampValue>;
123
  friend TimestampValue UnsafeTimestampValue(absl::Time value);
124
125
15.4k
  TimestampValue(absl::in_place_t, absl::Time value) : value_(value) {}
126
127
  absl::Time value_ = absl::UnixEpoch();
128
};
129
130
15.4k
inline TimestampValue UnsafeTimestampValue(absl::Time value) {
131
15.4k
  return TimestampValue(absl::in_place, value);
132
15.4k
}
133
134
0
inline absl::StatusOr<TimestampValue> SafeTimestampValue(absl::Time value) {
135
0
  absl::Status status = internal::ValidateTimestamp(value);
136
0
  if (!status.ok()) {
137
0
    return status;
138
0
  }
139
0
  return UnsafeTimestampValue(value);
140
0
}
141
142
0
inline bool operator!=(TimestampValue lhs, TimestampValue rhs) {
143
0
  return !operator==(lhs, rhs);
144
0
}
145
146
0
inline std::ostream& operator<<(std::ostream& out, TimestampValue value) {
147
0
  return out << value.DebugString();
148
0
}
149
150
}  // namespace cel
151
152
#endif  // THIRD_PARTY_CEL_CPP_COMMON_VALUES_TIMESTAMP_VALUE_H_