Skip to content
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

Time zone and daylight saving issue #255

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/org/jfree/chart/axis/DateAxis.java
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,13 @@ else if (this.tickMarkPosition == DateTickMarkPosition.MIDDLE) {
calendar.set(years, months, days, value, minutes, seconds);
Date d1 = calendar.getTime();
if (d1.getTime() >= date.getTime()) {
Integer previousOffset = calendar.get(Calendar.DST_OFFSET);
calendar.set(Calendar.HOUR_OF_DAY, value - count);
Integer currentOffset = calendar.get(Calendar.DST_OFFSET);
final boolean prevBool = previousOffset.equals(currentOffset);
if(!prevBool && value == 1 && currentOffset == 3_600_000){
calendar.set(Calendar.DST_OFFSET, previousOffset);
}
d1 = calendar.getTime();
}
return d1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.jfree.chart.axis;

import org.jfree.data.time.Hour;
import org.junit.jupiter.api.Test;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
*
* Issue 206 https://github.com/jfree/jfreechart/issues/206
* Details: Correct time is returned regardless of daylight saving time changes
*
*/
public class DaylightSavingsToStandardTest {

/**
* Issue 206 https://github.com/jfree/jfreechart/issues/206
* Details: Correct time is returned regardless of daylight saving time changes
*
* Given: Data that includes a switch from Daylight Saving time to standard
* Expected: Correct timezone abbreviation and hour during the change
* Result: Success, the expected data is returned
*
*/
@Test
public void giveDaylightSavingSwitchDateAt1AM2020ExpectedCorrectTime1HourBefore() {
final Hour dstSwitch2020 = new Hour(1,1, 11, 2020);
final long timeDifference = getOneHourDifferenceInMilliseconds(dstSwitch2020);

assertEquals(3_600_000, timeDifference,"Given DST expected standard one hour before.");
}

/**
* Issue 206 https://github.com/jfree/jfreechart/issues/206
* Details: Correct time is returned regardless of daylight saving time changes
*
* Given: Data that includes a switch from Daylight Saving Time to standard
* Expected: Correct timezone abbreviation and hour during the change
* Result: Success, the expected data is returned
*
*/
@Test
public void giveDaylightSavingSwitchDateAt1AM2021ExpectedCorrectTime1HourBefore() {
final Hour dstSwitch2021 = new Hour(1, 7, 11, 2021);
final long timeDifference = getOneHourDifferenceInMilliseconds(dstSwitch2021);

assertEquals(3_600_000, timeDifference,"Given DST expected standard one hour before.");
}

/**
* Issue 206 https://github.com/jfree/jfreechart/issues/206
* Details: Correct time is returned regardless of daylight saving time changes
*
* Given: Data in standard time with no time change included
* Expected: Correct time and abbreviation and hour during the change
* Result: Success, the expected data is returned
*
*/
@Test
public void giveStandardDateAt1AmExpectedCorrectTime1HourBefore() {
final Hour standardTime = new Hour(1, 7, 12, 2018);
final long timeDifference = getOneHourDifferenceInMilliseconds(standardTime);

assertEquals(3_600_000, timeDifference, "Given standard expected standard one hour before.");
}

/**
* Issue 206 https://github.com/jfree/jfreechart/issues/206
* Details: Correct time is returned regardless of daylight saving time changes
*
* Given: Data in Daylight Saving Time with no time change included
* Expected: Correct time and abbreviation is returns, on hour prior
* Result: Success, the expected data is returned
*
*/
@Test
public void giveDaylightSavingDateAt1AMExpectedCorrectTime1HourBefore() {
final Hour daylightDate = new Hour(1, 18, 6, 2019);
final long timeDifference = getOneHourDifferenceInMilliseconds(daylightDate);
assertEquals(3_600_000, timeDifference, "Given DST expected DST one hour before.");
}

/**
* Given: Various dates return output of previousStandardDate method
*/
public long getOneHourDifferenceInMilliseconds(final Hour hour){
final Date testDate = new Date(hour.getFirstMillisecond());
final Date end = new Date(hour.getLastMillisecond());
final DateAxisTest.MyDateAxis axis = new DateAxisTest.MyDateAxis("Hour");
final DateTickUnit unit = new DateTickUnit(DateTickUnitType.HOUR, 1);
axis.setTickUnit(unit);
axis.setRange(testDate, end);

final Date previous = axis.previousStandardDate(testDate, unit);
return testDate.getTime() - previous.getTime();
}
}