-
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
Remove LocalDate and LocalDateTime converters #11381
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
context/src/test/groovy/io/micronaut/runtime/converters/TimeConverterRegistrarSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.micronaut.runtime.converters | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.core.convert.ConversionService | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
import java.text.DateFormat | ||
import java.text.SimpleDateFormat | ||
import java.time.Instant | ||
import java.time.LocalDate | ||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
import java.time.ZoneOffset | ||
import java.time.format.DateTimeFormatter | ||
|
||
class TimeConverterRegistrarSpec extends Specification { | ||
|
||
@AutoCleanup | ||
@Shared | ||
ApplicationContext context = ApplicationContext.run() | ||
|
||
static final String DATE_FORMAT = "yyyy-MM-dd" | ||
static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm" | ||
static final String TEST_TIME_ZONE = "America/New_York" | ||
|
||
void 'test converting LocalDate and LocalDateTime'() { | ||
given: | ||
def timeZone = TimeZone.default | ||
TimeZone.default = TimeZone.getTimeZone(TEST_TIME_ZONE) | ||
def conversionService = context.getBean(ConversionService) | ||
def localDate = LocalDate.of(2024, 1, 1) | ||
def localDateTime = LocalDateTime.of(2024, 1, 1, 2, 30) | ||
def localDateStr = createDateTimeFormatter(DATE_FORMAT).format(localDate) | ||
def localDateTimeStr = createDateTimeFormatter(DATE_TIME_FORMAT).format(localDateTime) | ||
when: | ||
def date = conversionService.convert(localDate, Date).orElse(null) | ||
def dateTime = conversionService.convert(localDateTime, Date).orElse(null) | ||
then: | ||
def dateStr = createDateFormat(DATE_FORMAT).format(date) | ||
dateStr == localDateStr | ||
def dateTimeStr = new SimpleDateFormat(DATE_TIME_FORMAT).format(dateTime) | ||
dateTimeStr == localDateTimeStr | ||
cleanup: | ||
TimeZone.default = timeZone | ||
} | ||
|
||
static DateTimeFormatter createDateTimeFormatter(String pattern) { | ||
return DateTimeFormatter.ofPattern(pattern) | ||
} | ||
|
||
static DateFormat createDateFormat(String pattern) { | ||
return new SimpleDateFormat(pattern) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is converting
LocaleDate
toDate
usingUTC
and micronaut-data is using default timezone when converting back so we are getting date diff issues. I believe this should use default timezone as well.There is also comment about removal of this conversion in Micronaut4. Can it be removed? If yes, then we can add converter needed in micronaut-data using default timezone and the issue would be fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yawkat Can we remove these conversions above, or change it to not use
UTC
but default timezone? It is causing issue in micronat-data explained in the description of this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree that this should be default TZ. Yes they should be removed, but using system TZ is worse.
Why does data need to convert these at all? java.sql.Date has actual well-defined conversions from and to LocalDate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
micronaut-data registers various conversions for dates and is always using timezones.
With this converter defined in micronaut-core, even if we register this in micronaut-data
we will get wrong date when converted back.
I removed these converters from micronaut-core, hopefully nothing will break.