MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / EncodeTimestampToJson

Function EncodeTimestampToJson

internal/time.cc:174–192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

172}
173
174absl::StatusOr<std::string> EncodeTimestampToJson(absl::Time timestamp) {
175 // Adapted from protobuf time_util.
176 static constexpr absl::string_view kTimestampFormat = "%E4Y-%m-%dT%H:%M:%S";
177 CEL_RETURN_IF_ERROR(ValidateTimestamp(timestamp));
178 // Handle nanos and the seconds separately to match proto JSON format.
179 absl::Time unix_seconds =
180 absl::FromUnixSeconds(absl::ToUnixSeconds(timestamp));
181 int64_t n = (timestamp - unix_seconds) / absl::Nanoseconds(1);
182
183 std::string result =
184 absl::FormatTime(kTimestampFormat, unix_seconds, absl::UTCTimeZone());
185
186 if (n > 0) {
187 absl::StrAppend(&result, ".", FormatNanos(n));
188 }
189
190 absl::StrAppend(&result, "Z");
191 return result;
192}
193
194std::string DebugStringTimestamp(absl::Time timestamp) {
195 return RawFormatTimestamp(timestamp);

Callers 2

TESTFunction · 0.85

Calls 2

ValidateTimestampFunction · 0.85
FormatNanosFunction · 0.85

Tested by 1

TESTFunction · 0.68