You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to configure its data-representations to be as close as possible as the original processing done by MongoBD, and as compact as possible.
Here is my configuration:
@SingletonpublicclassJsr310AdditionalCodecsProviderimplementsCodecProvider {
// As date objects to be able to compare them and have an ISO and smaller representationprivatestaticfinalLocalDateTimeCodecLOCAL_DATE_TIME_CODEC = newLocalDateTimeCodec();
// As strings to have an ISO and smaller representationprivatestaticfinalZoneOffsetAsStringCodecZONE_OFFSET_CODEC = newZoneOffsetAsStringCodec();
// As strings to have a smaller representationprivatestaticfinalZoneIdAsStringCodecZONE_ID_CODEC = newZoneIdAsStringCodec();
@Overridepublic <T> Codec<T> get(Class<T> clazz, CodecRegistryregistry) {
if (clazz.equals(ZonedDateTime.class)) {
return (Codec<T>) newZonedDateTimeAsDocumentCodec(LOCAL_DATE_TIME_CODEC, ZONE_OFFSET_CODEC, ZONE_ID_CODEC);
} elseif (clazz.equals(OffsetDateTime.class)) {
return (Codec<T>) newOffsetDateTimeAsDocumentCodec(LOCAL_DATE_TIME_CODEC, ZONE_OFFSET_CODEC);
} elseif (clazz.equals(ZoneOffset.class)) {
return (Codec<T>) ZONE_OFFSET_CODEC;
} elseif (clazz.equals(ZoneId.class)) {
return (Codec<T>) ZONE_ID_CODEC;
}
returnnull; // Other default codecs are fine to us
}
}
The goal is to store OffsetDateTime & ZonedDateTime as close as possible as the default MongoDB codecs: as $date fields to be able to compare them, with just one or two additional fields to be able to restore the original offset, lost with the default MongoDB converter, as well as the original zone, non-standard, and thus also lost.
Unfortunately, the dateTime fields are stored as LocalDate with a "Z" appended, making the date objects not comparable.
Example of stored document: the two OffsetDateTimes are at the same moment, created on my machine that is currently GMT+1 (will be GMT+2 in 6 months when DST kicks in):
So, as long as you create documents on the same machine, or on machines with the same offset AND that do not honor Daylight Saving Time, then the created dates are comparable.
That's a lot of ifs :)
And semantically speaking, if we store a "Z"-date in database, we expect it to be UTC, like MongoDB does by default.
We would also expect Instant.parse(offsetDateTime2.dateTime).atOffset(ZoneOffset.of(offsetDateTime2.zone)) to restore the original OffsetDateTime: it currently does not.
I'd like both fields above to have theirs dateTime to "$date": "2022-03-02T14:15:44.555Z".
Did I use the library wrong? Is there a possible way to do this?
If not, is it possible to create alternate ZonedDateTimeAsDocumentCodec & OffsetDateTimeAsDocumentCodec classes that do not use a LocalDateTimeCodec or that convert them to UTC first?
Thanks a lot.
The text was updated successfully, but these errors were encountered:
Hello,
First, thanks for this wonderful library!
I'd like to configure its data-representations to be as close as possible as the original processing done by MongoBD, and as compact as possible.
Here is my configuration:
The goal is to store OffsetDateTime & ZonedDateTime as close as possible as the default MongoDB codecs: as $date fields to be able to compare them, with just one or two additional fields to be able to restore the original offset, lost with the default MongoDB converter, as well as the original zone, non-standard, and thus also lost.
Unfortunately, the dateTime fields are stored as LocalDate with a "Z" appended, making the date objects not comparable.
Example of stored document: the two OffsetDateTimes are at the same moment, created on my machine that is currently GMT+1 (will be GMT+2 in 6 months when DST kicks in):
So, as long as you create documents on the same machine, or on machines with the same offset AND that do not honor Daylight Saving Time, then the created dates are comparable.
That's a lot of ifs :)
And semantically speaking, if we store a "Z"-date in database, we expect it to be UTC, like MongoDB does by default.
We would also expect
Instant.parse(offsetDateTime2.dateTime).atOffset(ZoneOffset.of(offsetDateTime2.zone))
to restore the original OffsetDateTime: it currently does not.I'd like both fields above to have theirs dateTime to "$date": "2022-03-02T14:15:44.555Z".
Did I use the library wrong? Is there a possible way to do this?
If not, is it possible to create alternate ZonedDateTimeAsDocumentCodec & OffsetDateTimeAsDocumentCodec classes that do not use a LocalDateTimeCodec or that convert them to UTC first?
Thanks a lot.
The text was updated successfully, but these errors were encountered: