| 172 | } |
| 173 | |
| 174 | absl::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 | |
| 194 | std::string DebugStringTimestamp(absl::Time timestamp) { |
| 195 | return RawFormatTimestamp(timestamp); |