Skip to content

Commit

Permalink
Modifying to work with any timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
davet1985 committed Apr 25, 2024
1 parent 1e29bc1 commit 1b09941
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/main/java/uk/gov/hmcts/darts/utilities/DateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import javax.xml.datatype.XMLGregorianCalendar;

@UtilityClass
Expand All @@ -33,11 +32,10 @@ private OffsetDateTime toOffsetDateTime(ZonedDateTime zonedDateTime) {

public OffsetDateTime toOffsetDateTime(XMLGregorianCalendar date) {
GregorianCalendar gregorianCalendar = date.toGregorianCalendar();
if (date.getTimezone() == 0) {
gregorianCalendar.setTimeZone(TimeZone.getTimeZone(UTC));
} else {
gregorianCalendar.setTimeZone(TimeZone.getTimeZone(LONDON_ZONE_ID));
}

// calculate the timezone from the XML date raw offset minutes
int xmlDateTzOffsetRawMins = date.getTimezone();
gregorianCalendar.setTimeZone(date.getTimeZone(xmlDateTzOffsetRawMins));

ZonedDateTime zonedDateTime = gregorianCalendar.toZonedDateTime();
Instant instant = zonedDateTime.toInstant();
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/uk/gov/hmcts/darts/utilities/DateUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;

import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
Expand All @@ -17,6 +18,8 @@
@SuppressWarnings("PMD.LineLength")
class DateUtilTest {

private static final ZoneId NEW_YORK_ZONE_ID = ZoneId.of("America/New_York");

@Test
void gmt() throws DatatypeConfigurationException {
GregorianCalendar gcal = new GregorianCalendar(2020, Calendar.MARCH, 21, 15, 23, 39);
Expand Down Expand Up @@ -52,4 +55,13 @@ void bstWithTimeZone() throws DatatypeConfigurationException {
assertEquals("2024-04-23T16:00+01:00", offsetDateTime.toString());
}

@Test
void otherTimeZone() throws DatatypeConfigurationException {
GregorianCalendar gcal = new GregorianCalendar(2024, Calendar.APRIL, 23, 15, 0, 0);
gcal.setTimeZone(TimeZone.getTimeZone(NEW_YORK_ZONE_ID));
XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
OffsetDateTime offsetDateTime = DateUtil.toOffsetDateTime(xcal);
assertEquals("2024-04-23T20:00+01:00", offsetDateTime.toString());
}

}

0 comments on commit 1b09941

Please sign in to comment.