So after some searching, its been suggested to use an int64
epoch.
This is all well and great, but when interacting with my model, I would like to interact with actual LocalDate
objects, so what are strategies for handling this?
The two strategies I can think of are:
What is the common practice here?
I went with creating a generic solution for all dates/times:
message Timestamp {
int64 seconds = 1;
int32 nanos = 2;
}
With the following converters:
public static Timestamp fromLocalDate(LocalDate localDate) {
Instant instant = localDate.atStartOfDay().toInstant(ZoneOffset.UTC);
return Timestamp.newBuilder()
.setSeconds(instant.getEpochSecond())
.setNanos(instant.getNano())
.build();
}
public static LocalDate toLocalDate(Timestamp timestamp) {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos()), ZoneId.of("UTC"))
.toLocalDate();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With