-
Notifications
You must be signed in to change notification settings - Fork 18
Test for date conversion in multiple timezone #1514
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
base: master
Are you sure you want to change the base?
Conversation
WalkthroughA test method was renamed and enhanced to verify date conversion behavior across multiple timezones by iterating through different TimeZone settings, ensuring consistent conversion and reverse conversion operations, with proper restoration of the original timezone. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 1
📜 Review details
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/test/java/org/commcare/util/DateRangeUtilsTest.java
🧰 Additional context used
🧬 Code graph analysis (1)
src/test/java/org/commcare/util/DateRangeUtilsTest.java (1)
src/main/java/org/commcare/util/DateRangeUtils.java (1)
DateRangeUtils(12-81)
🪛 GitHub Actions: CommCare Core Build
src/test/java/org/commcare/util/DateRangeUtilsTest.java
[error] 29-29: JUnit ComparisonFailure in testDateConversionInMultipleTimezones. 710 tests completed, 1 failed during Gradle test task.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: task-list-completed
🔇 Additional comments (4)
src/test/java/org/commcare/util/DateRangeUtilsTest.java (4)
8-8: LGTM! Necessary import for timezone testing.The TimeZone import is correctly added to support the multi-timezone test functionality.
13-14: LGTM! Good timezone coverage for the test.The method rename clearly reflects the expanded test scope, and the timezone selection covers diverse geographic locations (UTC, US East/West coast, Europe, and Asia) which will effectively surface timezone-dependent behavior.
35-37: LGTM! Proper cleanup in finally block.The finally block ensures the timezone is always restored even if assertions fail, which is the correct practice for tests that modify global state. Once the
originalTzcapture is moved outside the loop, this cleanup will work correctly.
19-34: Test correctly surfaces the timezone issue in the date range conversion logic.The test logic is sound and properly validates both forward and reverse conversion across timezones. The diagnostic output (lines 24-27) provides helpful debugging context. This test will correctly fail in non-UTC timezones, surfacing the root cause.
The underlying issue is in
DateRangeUtils.getTimeFromDateOffsettingTz()(line 39-40), which applies a double timezone adjustment:SimpleDateFormatparses the input date string according to the default timezone, and thengetTimeFromDateOffsettingTz()subtracts the timezone offset again using the deprecatedDate.getTimezoneOffset()method. This double adjustment causes date corruption across different timezones.
| for (String tzId : timezones) { | ||
| TimeZone originalTz = TimeZone.getDefault(); |
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.
Critical: Move originalTz capture outside the loop.
The originalTz variable is captured inside the loop, which means it will be overwritten on each iteration after the first. After the first iteration, the "original" timezone being restored will actually be the previous test's timezone, not the actual system default. This could lead to cascading failures or incorrect test behavior.
🔎 Proposed fix
- for (String tzId : timezones) {
- TimeZone originalTz = TimeZone.getDefault();
+ TimeZone originalTz = TimeZone.getDefault();
+ for (String tzId : timezones) {
try {
TimeZone.setDefault(TimeZone.getTimeZone(tzId));📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| for (String tzId : timezones) { | |
| TimeZone originalTz = TimeZone.getDefault(); | |
| TimeZone originalTz = TimeZone.getDefault(); | |
| for (String tzId : timezones) { | |
| try { | |
| TimeZone.setDefault(TimeZone.getTimeZone(tzId)); |
🤖 Prompt for AI Agents
In src/test/java/org/commcare/util/DateRangeUtilsTest.java around lines 16 to
17, the TimeZone originalTz is captured inside the for-loop so it gets
overwritten each iteration; move the capture of TimeZone originalTz =
TimeZone.getDefault() to immediately before the loop so it reflects the true
system default, then inside the loop set the test TimeZone and restore
TimeZone.setDefault(originalTz) after each iteration (and/or finally) to ensure
each test resets to the real original timezone.
Product Description
Mostly creating this to surface issue with Date widget conversion code.
[Test fails at the moment and PR only aims to surface the issue and should not be merged]
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.