-
Notifications
You must be signed in to change notification settings - Fork 8k
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
Fixed time format assertion failing due to daylight saving time #3459
Open
hermya
wants to merge
1
commit into
alibaba:1.8
Choose a base branch
from
hermya:fix-timezone-assertion
base: 1.8
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
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
With DSTSavings in mind, I think there should be three test cases:
|
hermya
force-pushed
the
fix-timezone-assertion
branch
from
October 20, 2024 20:21
4359a4e
to
e546ffc
Compare
The latest commit contains I tried it with sample code for different zones and times as follows: import java.sql.Date;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.stream.Collectors;
public class Main {
public static DateTimeFormatter getFormatterByZone(String zoneId) {
return DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
.withZone(ZoneId.of(zoneId));
}
public static void printZoneInformation(String zoneId) {
var tz = TimeZone.getTimeZone(zoneId);
System.out.println("Time zone: " + tz.getDisplayName() + ", RawOffset: " + tz.getRawOffset() + ", DST: " + tz.observesDaylightTime() + ", DSTSavings: " + tz.getDSTSavings());
}
public static long getOffsetForZone(String zoneId, long epoch) {
return TimeZone.getTimeZone(zoneId).getOffset(epoch);
}
public static void main(String[] args) {
// SAPST -> (UTC-05:00) Bogota, Lima, Quito No DST
// IST -> (UTC+05:30) India Standard Time No DST
// CDT -> (UTC-05:00) Central Daylight Time DST
long epochNotInDST = 1729382400000l;
long epochInDST = 1732060800000l;
// 1729382400000l -> 10/20/2024 00:00:00.000 Date not in DST
// 1732060800000l -> 11/20/2024 00:00:00.000 Date in DST
System.out.println("\nDate in DST: ");
printZoneInformation("Etc/GMT+0");
System.out.println(getFormatterByZone("Etc/GMT+0").format(Instant.ofEpochMilli(epochInDST - getOffsetForZone("Etc/GMT+0", epochInDST))));
printZoneInformation("America/Bogota");
System.out.println(getFormatterByZone("America/Bogota").format(Instant.ofEpochMilli(epochInDST - getOffsetForZone("America/Bogota", epochInDST))));
printZoneInformation("Asia/Kolkata");
System.out.println(getFormatterByZone("Asia/Kolkata").format(Instant.ofEpochMilli(epochInDST - getOffsetForZone("Asia/Kolkata", epochInDST))));
printZoneInformation("America/Chicago");
System.out.println(getFormatterByZone("America/Chicago").format(Instant.ofEpochMilli(epochInDST - getOffsetForZone("America/Chicago", epochInDST))));
System.out.println("\nDate not in DST: ");
printZoneInformation("Etc/GMT+0");
System.out.println(getFormatterByZone("Etc/GMT+0").format(Instant.ofEpochMilli(epochNotInDST - getOffsetForZone("Etc/GMT+0", epochNotInDST))));
printZoneInformation("America/Bogota");
System.out.println(getFormatterByZone("America/Bogota").format(Instant.ofEpochMilli(epochNotInDST - getOffsetForZone("America/Bogota", epochNotInDST))));
printZoneInformation("Asia/Kolkata");
System.out.println(getFormatterByZone("Asia/Kolkata").format(Instant.ofEpochMilli(epochNotInDST - getOffsetForZone("Asia/Kolkata", epochNotInDST))));
printZoneInformation("America/Chicago");
System.out.println(getFormatterByZone("America/Chicago").format(Instant.ofEpochMilli(epochNotInDST - getOffsetForZone("America/Chicago", epochNotInDST))));
}
} And got the output:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe what this PR does / why we need it
PR intends to fix the test EagleEyeCoreUtilsTest.testFormatTime.
Does this pull request fix one issue?
Fixes #3458
Describe how you did it
Used
TimeZone.getDefault().getDSTSavings()
Describe how to verify it
Reran the test-case, now passing successfully
Special notes for reviews