-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LocalDate properties depend on the machine's timezone setting [DATAMONGO-2627] #3482
Comments
Mark Paluch commented
MongoCustomConversions.create(config -> config.useNativeDriverJavaTimeCodecs());
|
sothawo commented I know, that Mongo needs a ??Uses UTC zone offset.?? But this is apparently not the case. We implemented our own converters as a workaround which do use UTC: @Configuration
class MongoConfig {
@Bean
fun mongoCustomConversions() = MongoCustomConversions(listOf(LocalDateReadingConverter(), LocalDateWritingConverter()))
}
@WritingConverter
class LocalDateWritingConverter : Converter<LocalDate, Date> {
override fun convert(localDate: LocalDate): Date = Date(localDate.atStartOfDay(ZONEID_UTC).toEpochSecond() * 1000)
}
@ReadingConverter
class LocalDateReadingConverter : Converter<Date, LocalDate> {
override fun convert(date: Date): LocalDate = date.toInstant().atZone(ZONEID_UTC).toLocalDate()
}
private val ZONEID_UTC = ZoneId.of("UTC") |
sothawo commented I just read the last comment (https://jira.spring.io/browse/DATAMONGO-1026?focusedCommentId=188441&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-188441) from the linked issue: If this is not to be changed, then at least the documentation should clearly state, that the timezone of the machine where the Spring application is running is used for writing and reading this value |
Mark Paluch commented The UTC reference is related to the native codec. See also https://github.com/mongodb/mongo-java-driver/blob/master/bson/src/main/org/bson/codecs/jsr310/LocalDateCodec.java#L57 for how the MongoDB driver translates the value. I think it makes sense to make the local TimeZone setting for the Spring Converter more explicit to avoid further confusion |
sothawo commented Definitely the docs should be mor precise. And I agree with the local timezoe to be taken into consideration for all the temporal types except ??This class does not store or represent a time or time-zone. Instead, it is a description of the date, as used for birthdays.?? So I'd never expect this value to be dependent on a timezone, even it needs to be converted under the hood |
The issue is somewhat related to relational databases. That being said, we are required to inter-op with a driver-native type, hence timezones make an impact here. Looking at the docs (https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.custom-converters.xml), we already have documented the impact of timezones. Can you @sothawo check whether that's sufficient? |
The documentation you link to is alright, but the table at https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mapping-conversion has a footnote for |
I agree that the conversion should use an |
Thanks, @christophstrobl |
Almost forgot I had opend that issue 😁 |
…tie en prennant compte le timezone du serveur. (spring-projects/spring-data-mongodb#3482)
…tie en prennant compte le timezone du serveur. (spring-projects/spring-data-mongodb#3482)
…tie en prennant compte le timezone du serveur. (spring-projects/spring-data-mongodb#3482)
…tie en prennant compte le timezone du serveur. (spring-projects/spring-data-mongodb#3482)
sothawo opened DATAMONGO-2627 and commented
When storing an entity with a LocalDate property in a Spring Data MongoDB Repository, on reading the entity the property's value depends on the system timezone setting.
LocalDates have no notion of a timezone, they are just a combination of year/month/day , so the value should not change.
I have linked a GitHub repo with a small demo application, in short:
This stores a person with her birthday in the database with one timezone setting and reads the entity with a different timezone setting. The birthday is then one day off.
Affects: 3.0.4 (Neumann SR4)
Reference URL: https://github.com/sothawo/spring-data-mongodb-localdate
Issue Links:
The text was updated successfully, but these errors were encountered: