diff --git a/src/main/kotlin/org/wfanet/measurement/common/ProtoUtils.kt b/src/main/kotlin/org/wfanet/measurement/common/ProtoUtils.kt index fa75f64d0..8df143e2b 100644 --- a/src/main/kotlin/org/wfanet/measurement/common/ProtoUtils.kt +++ b/src/main/kotlin/org/wfanet/measurement/common/ProtoUtils.kt @@ -26,12 +26,14 @@ import com.google.protobuf.TypeRegistry import com.google.protobuf.duration import com.google.protobuf.timestamp import com.google.protobuf.util.JsonFormat +import com.google.type.Date import com.google.type.Interval import com.google.type.interval import java.io.File import java.time.Clock import java.time.Duration import java.time.Instant +import java.time.ZoneOffset /** Converts a protobuf [MessageOrBuilder] into its canonical JSON representation. */ fun MessageOrBuilder.toJson(): String { @@ -129,6 +131,16 @@ fun Message.Builder.mergeFromTextProto(textProto: Readable, typeRegistry: TypeRe TextFormat.Parser.newBuilder().setTypeRegistry(typeRegistry).build().merge(textProto, this) } +/** Converts this [Timestamp] to a [Date]. */ +fun Timestamp.toDateUtc(): Date { + return toInstant().atZone(ZoneOffset.UTC).toLocalDate().toProtoDate() +} + +/** Converts this [Date] to a [Timestamp]. */ +fun Date.atStartOfDayUtc(): Timestamp { + return toLocalDate().atStartOfDay().toInstant(ZoneOffset.UTC).toProtoTime() +} + @Suppress("UNCHECKED_CAST") // Safe per Message contract. fun parseTextProto( textProto: Readable,