From f03f328bf0e09414d53855af6c789f88498ce796 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Fri, 2 Aug 2019 12:57:39 +0200 Subject: [PATCH] DateTimeType: new methods toZone and toLocaleZone (#945) Signed-off-by: Laurent Garnier --- .../core/library/types/DateTimeType.java | 45 ++- .../core/library/types/DateTimeTypeTest.java | 288 ++++++++++++------ 2 files changed, 242 insertions(+), 91 deletions(-) diff --git a/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/types/DateTimeType.java b/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/types/DateTimeType.java index 48655d3d07f..e4c8fb51e2c 100644 --- a/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/types/DateTimeType.java +++ b/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/types/DateTimeType.java @@ -12,11 +12,13 @@ */ package org.eclipse.smarthome.core.library.types; +import java.time.DateTimeException; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; +import java.time.zone.ZoneRulesException; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Locale; @@ -34,6 +36,7 @@ * @author Erdoan Hadzhiyusein - Refactored to use ZonedDateTime * @author Jan N. Klug - add ability to use time or date only * @author Wouter Born - increase parsing and formatting precision + * @author Laurent Garnier - added methods toLocaleZone and toZone */ @NonNullByDefault public class DateTimeType implements PrimitiveType, State, Command { @@ -108,7 +111,7 @@ public DateTimeType(String zonedValue) { throw new IllegalArgumentException(zonedValue + " is not in a valid format.", invalidFormatException); } - zonedDateTime = date; + zonedDateTime = date.withFixedOffsetZone(); } /** @@ -140,6 +143,46 @@ public String format(Locale locale, String pattern) { return String.format(locale, pattern, zonedDateTime); } + /** + * Create a {@link DateTimeType} being the translation of the current object to the locale time zone + * + * @return a {@link DateTimeType} translated to the locale time zone + * + * @throws DateTimeException if the converted zone ID has an invalid format or the result exceeds the supported date + * range + * @throws ZoneRulesException if the converted zone region ID cannot be found + */ + public DateTimeType toLocaleZone() throws DateTimeException, ZoneRulesException { + return toZone(ZoneId.systemDefault()); + } + + /** + * Create a {@link DateTimeType} being the translation of the current object to a given zone + * + * @param zone the target zone as a string + * + * @return a {@link DateTimeType} translated to the given zone + * + * @throws DateTimeException if the zone has an invalid format or the result exceeds the supported date range + * @throws ZoneRulesException if the zone is a region ID that cannot be found + */ + public DateTimeType toZone(String zone) throws DateTimeException, ZoneRulesException { + return toZone(ZoneId.of(zone)); + } + + /** + * Create a {@link DateTimeType} being the translation of the current object to a given zone + * + * @param zoneId the target {@link ZoneId} + * + * @return a {@link DateTimeType} translated to the given zone + * + * @throws DateTimeException if the result exceeds the supported date range + */ + public DateTimeType toZone(ZoneId zoneId) throws DateTimeException { + return new DateTimeType(zonedDateTime.withZoneSameInstant(zoneId)); + } + @Override public String toString() { return toFullString(); diff --git a/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/library/types/DateTimeTypeTest.java b/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/library/types/DateTimeTypeTest.java index 96414dea0b4..1e041bfdead 100644 --- a/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/library/types/DateTimeTypeTest.java +++ b/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/library/types/DateTimeTypeTest.java @@ -12,12 +12,12 @@ */ package org.eclipse.smarthome.core.library.types; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.*; import java.text.SimpleDateFormat; +import java.time.DateTimeException; import java.time.LocalDateTime; -import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Arrays; @@ -29,6 +29,9 @@ import java.util.TimeZone; import java.util.concurrent.TimeUnit; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -38,7 +41,9 @@ * @author Thomas.Eichstaedt-Engelen - Initial contribution * @author Gaƫl L'hopital - Added Timezone and Milliseconds * @author Erdoan Hadzhiyusein - Added ZonedDateTime tests + * @author Laurent Garnier - Enhanced tests */ +@NonNullByDefault @RunWith(Parameterized.class) public class DateTimeTypeTest { @@ -57,12 +62,14 @@ public static class ParameterSet { * input time. * used to call the {@link Calendar#set(int, int, int, int, int, int)} method to set the time. */ + @Nullable public final Map inputTimeMap; /** * input time zone. * used to call the {@link Calendar#setTimeZone(TimeZone)} to set the time zone. * the time zone offset has direct impact on the result. */ + @Nullable public final TimeZone inputTimeZone; /** * direct input of a time string (with or without time zone). @@ -70,15 +77,43 @@ public static class ParameterSet { * @see {@link DateTimeType#valueOf(String)} * if this is set, the {@link ParameterSet#inputTimeMap} and {@link ParameterSet#inputTimeZone} are ignored */ + @Nullable public final String inputTimeString; /** - * the expected result of the test. + * the expected result of the test without any additional translation to the time zone specified in the + * {@link ParameterSet#defaultTimeZone}. * golden rule: * should always return the input time minus or plus the offset of the given time zone. * if no time zone is specified or the time zone in the {@link ParameterSet#inputTimeString} is wrong, then the * {@link ParameterSet#defaultTimeZone} is used. */ public final String expectedResult; + /** + * the expected result of the test with an additional translation to the time zone specified in the + * {@link ParameterSet#defaultTimeZone}. + * golden rule: + * should always return the input time minus or plus the offset of the time zone specified in the + * {@link ParameterSet#defaultTimeZone}. + * if no time zone is specified or the time zone in the {@link ParameterSet#inputTimeString} is wrong, then the + * {@link ParameterSet#defaultTimeZone} is used to first build the date. + */ + public final String expectedResultLocalTZ; + /** + * the locale parameter to be used to test the method {@link DateTimeType#format(Locale, String)}. + */ + @Nullable + public final Locale locale; + /** + * the pattern parameter to be used to test the method {@link DateTimeType#format(Locale, String)} + * or {@link DateTimeType#format(String)}. + */ + @Nullable + public final String pattern; + /** + * the expected result when testing the method {@link DateTimeType#format(Locale, String)} + * or {@link DateTimeType#format(String)}. + */ + public final String expectedFormattedResult; /** * create a parameter set with {@link ParameterSet#inputTimeMap} and {@link ParameterSet#inputTimeZone} @@ -88,14 +123,12 @@ public static class ParameterSet { * @param inputTimeMap * @param inputTimeZone * @param expectedResult + * @param expectedResultLocalTZ */ public ParameterSet(TimeZone defaultTimeZone, Map inputTimeMap, TimeZone inputTimeZone, - String expectedResult) { - this.defaultTimeZone = defaultTimeZone; - this.inputTimeMap = inputTimeMap; - this.inputTimeZone = inputTimeZone; - this.inputTimeString = null; - this.expectedResult = expectedResult; + String expectedResult, String expectedResultLocalTZ) { + this(defaultTimeZone, inputTimeMap, inputTimeZone, null, expectedResult, expectedResultLocalTZ, null, null, + expectedResult.substring(0, 19)); } /** @@ -104,13 +137,42 @@ public ParameterSet(TimeZone defaultTimeZone, Map inputTimeMap, * @param defaultTimeZone * @param inputTimeString * @param expectedResult + * @param expectedResultLocalTZ + */ + public ParameterSet(TimeZone defaultTimeZone, String inputTimeString, String expectedResult, + String expectedResultLocalTZ) { + this(defaultTimeZone, null, null, inputTimeString, expectedResult, expectedResultLocalTZ, null, null, + expectedResult.substring(0, 19)); + } + + /** + * create a parameter set with either {@link ParameterSet#inputTimeMap} and {@link ParameterSet#inputTimeZone} + * parameters or {@link ParameterSet#inputTimeString} parameter. Non default values can be set for testing + * the format method. + * + * @param defaultTimeZone + * @param inputTimeMap + * @param inputTimeZone + * @param inputTimeString + * @param expectedResult + * @param expectedResultLocalTZ + * @param locale + * @param pattern + * @param expectedFormattedResult */ - public ParameterSet(TimeZone defaultTimeZone, String inputTimeString, String expectedResult) { + public ParameterSet(TimeZone defaultTimeZone, @Nullable Map inputTimeMap, + @Nullable TimeZone inputTimeZone, @Nullable String inputTimeString, String expectedResult, + String expectedResultLocalTZ, @Nullable Locale locale, @Nullable String pattern, + String expectedFormattedResult) { this.defaultTimeZone = defaultTimeZone; - this.inputTimeMap = null; - this.inputTimeZone = null; + this.inputTimeMap = inputTimeMap; + this.inputTimeZone = inputTimeZone; this.inputTimeString = inputTimeString; this.expectedResult = expectedResult; + this.expectedResultLocalTZ = expectedResultLocalTZ; + this.locale = locale; + this.pattern = pattern; + this.expectedFormattedResult = expectedFormattedResult; } } @@ -124,34 +186,45 @@ public static Collection parameters() { // for simplicity we use always the same input time. return Arrays.asList(new Object[][] { { new ParameterSet(TimeZone.getTimeZone("UTC"), initTimeMap(), TimeZone.getTimeZone("UTC"), - "2014-03-30T10:58:47.033+0000") }, + "2014-03-30T10:58:47.033+0000", "2014-03-30T10:58:47.033+0000") }, { new ParameterSet(TimeZone.getTimeZone("UTC"), initTimeMap(), TimeZone.getTimeZone("CET"), - "2014-03-30T08:58:47.033+0000") }, + "2014-03-30T10:58:47.033+0200", "2014-03-30T08:58:47.033+0000") }, { new ParameterSet(TimeZone.getTimeZone("UTC"), "2014-03-30T10:58:47UTC", - "2014-03-30T10:58:47.000+0000") }, + "2014-03-30T10:58:47.000+0000", "2014-03-30T10:58:47.000+0000") }, { new ParameterSet(TimeZone.getTimeZone("CET"), initTimeMap(), TimeZone.getTimeZone("UTC"), - "2014-03-30T12:58:47.033+0200") }, + "2014-03-30T10:58:47.033+0000", "2014-03-30T12:58:47.033+0200") }, { new ParameterSet(TimeZone.getTimeZone("CET"), initTimeMap(), TimeZone.getTimeZone("CET"), - "2014-03-30T10:58:47.033+0200") }, + "2014-03-30T10:58:47.033+0200", "2014-03-30T10:58:47.033+0200") }, { new ParameterSet(TimeZone.getTimeZone("CET"), "2014-03-30T10:58:47CET", - "2014-03-30T10:58:47.000+0200") }, + "2014-03-30T10:58:47.000+0200", "2014-03-30T10:58:47.000+0200") }, { new ParameterSet(TimeZone.getTimeZone("GMT+5"), "2014-03-30T10:58:47.000Z", - "2014-03-30T15:58:47.000+0500") }, + "2014-03-30T10:58:47.000+0000", "2014-03-30T15:58:47.000+0500") }, + { new ParameterSet(TimeZone.getTimeZone("GMT+2"), null, null, "2014-03-30T10:58:47", + "2014-03-30T10:58:47.000+0200", "2014-03-30T10:58:47.000+0200", null, + "%1$td.%1$tm.%1$tY %1$tH:%1$tM", "30.03.2014 10:58") }, { new ParameterSet(TimeZone.getTimeZone("GMT"), initTimeMap(), TimeZone.getTimeZone("GMT"), - "2014-03-30T10:58:47.033+0000") }, + "2014-03-30T10:58:47.033+0000", "2014-03-30T10:58:47.033+0000") }, + // Parameter set with an invalid time zone id as input, leading to GMT being considered { new ParameterSet(TimeZone.getTimeZone("CET"), initTimeMap(), TimeZone.getTimeZone("+02:00"), - "2014-03-30T12:58:47.033+0200") }, + "2014-03-30T10:58:47.033+0000", "2014-03-30T12:58:47.033+0200") }, + // Parameter set with an invalid time zone id as input, leading to GMT being considered { new ParameterSet(TimeZone.getTimeZone("GMT+2"), initTimeMap(), TimeZone.getTimeZone("GML"), - "2014-03-30T12:58:47.033+0200") }, - { new ParameterSet(TimeZone.getTimeZone("GMT-2"), initTimeMap(), TimeZone.getTimeZone("GMT+3"), - "2014-03-30T05:58:47.033-0200") }, + "2014-03-30T10:58:47.033+0000", "2014-03-30T12:58:47.033+0200") }, + { new ParameterSet(TimeZone.getTimeZone("GMT-2"), initTimeMap(), TimeZone.getTimeZone("GMT+3"), null, + "2014-03-30T10:58:47.033+0300", "2014-03-30T05:58:47.033-0200", Locale.GERMAN, + "%1$tA %1$td.%1$tm.%1$tY %1$tH:%1$tM", "Sonntag 30.03.2014 10:58") }, { new ParameterSet(TimeZone.getTimeZone("GMT-2"), initTimeMap(), TimeZone.getTimeZone("GMT-4"), - "2014-03-30T12:58:47.033-0200") }, - { new ParameterSet(TimeZone.getTimeZone("UTC"), "10:58:47", "1970-01-01T10:58:47.000+0000") }, - { new ParameterSet(TimeZone.getTimeZone("UTC"), "10:58", "1970-01-01T10:58:00.000+0000") }, - { new ParameterSet(TimeZone.getTimeZone("CET"), "10:58:47CET", "1970-01-01T10:58:47.000+0100") }, - { new ParameterSet(TimeZone.getTimeZone("CET"), "10:58CET", "1970-01-01T10:58:00.000+0100") }, - { new ParameterSet(TimeZone.getTimeZone("UTC"), "2014-03-30", "2014-03-30T00:00:00.000+0000") }, }); + "2014-03-30T10:58:47.033-0400", "2014-03-30T12:58:47.033-0200") }, + { new ParameterSet(TimeZone.getTimeZone("UTC"), "10:58:47", "1970-01-01T10:58:47.000+0000", + "1970-01-01T10:58:47.000+0000") }, + { new ParameterSet(TimeZone.getTimeZone("UTC"), "10:58", "1970-01-01T10:58:00.000+0000", + "1970-01-01T10:58:00.000+0000") }, + { new ParameterSet(TimeZone.getTimeZone("CET"), "10:58:47CET", "1970-01-01T10:58:47.000+0100", + "1970-01-01T10:58:47.000+0100") }, + { new ParameterSet(TimeZone.getTimeZone("CET"), "10:58CET", "1970-01-01T10:58:00.000+0100", + "1970-01-01T10:58:00.000+0100") }, + { new ParameterSet(TimeZone.getTimeZone("UTC"), "2014-03-30", "2014-03-30T00:00:00.000+0000", + "2014-03-30T00:00:00.000+0000") }, }); } private static Map initTimeMap() { @@ -178,6 +251,12 @@ public DateTimeTypeTest(ParameterSet parameterSet) { this.parameterSet = parameterSet; } + @Before + public void setUp() { + // set default time zone + TimeZone.setDefault(parameterSet.defaultTimeZone); + } + @Test public void serializationTest() { DateTimeType dt = new DateTimeType(Calendar.getInstance()); @@ -198,9 +277,7 @@ public void equalityTest() { DateTimeType dt2 = DateTimeType.valueOf(dt1.toFullString()); assertTrue(dt1.toString().equals(dt2.toString())); - assertTrue(dt1.equals(dt2)); assertTrue(dt1.getCalendar().equals(dt2.getCalendar())); - assertTrue(dt1.equals(dt2)); } @@ -232,79 +309,110 @@ public void parsingTest() { } @Test - public void formattingTest() { - DateTimeType dt1 = new DateTimeType("2019-05-25T17:53:10+00:00"); - assertThat(dt1.toFullString(), is(dt1.toString())); - assertThat(dt1.toString(), is("2019-05-25T17:53:10.000+0000")); - - assertThat(dt1.format(null), is("2019-05-25T17:53:10")); - assertThat(dt1.format("%1$td.%1$tm.%1$tY %1$tH:%1$tM"), is("25.05.2019 17:53")); - - assertThat(dt1.format(Locale.GERMAN, "%1$td.%1$tm.%1$tY %1$tH:%1$tM"), is("25.05.2019 17:53")); - - DateTimeType dt2 = new DateTimeType(dt1.getZonedDateTime().withZoneSameInstant(ZoneId.of("Europe/Berlin"))); - assertThat(dt2, not(is(dt1))); - assertThat(dt2.toString(), is("2019-05-25T19:53:10.000+0200")); - - assertThat(dt2.format(null), is("2019-05-25T19:53:10")); - assertThat(dt2.format("%1$td.%1$tm.%1$tY %1$tH:%1$tM"), is("25.05.2019 19:53")); + public void createDate() { + Map inputTimeMap = parameterSet.inputTimeMap; + TimeZone inputTimeZone = parameterSet.inputTimeZone; + if (inputTimeMap == null || inputTimeZone == null) { + return; + } - assertThat(dt2.format(Locale.GERMAN, "%1$td.%1$tm.%1$tY %1$tH:%1$tM"), is("25.05.2019 19:53")); + // get DateTimeType from the current parameter + final Calendar calendar = Calendar.getInstance(inputTimeZone); + calendar.set(inputTimeMap.get("year"), inputTimeMap.get("month"), inputTimeMap.get("date"), + inputTimeMap.get("hourOfDay"), inputTimeMap.get("minute"), inputTimeMap.get("second")); + calendar.set(Calendar.MILLISECOND, inputTimeMap.get("milliseconds")); + DateTimeType dt1 = new DateTimeType(calendar); + DateTimeType dt2 = new DateTimeType( + new SimpleDateFormat(DateTimeType.DATE_PATTERN_WITH_TZ_AND_MS).format(calendar.getTime())); + // Test + assertEquals(dt1.toFullString(), dt1.toString()); + assertEquals(parameterSet.expectedResultLocalTZ, dt1.toString()); + assertEquals(parameterSet.expectedResultLocalTZ, dt2.toString()); + assertEquals(dt1, dt2); } @Test - public void createDate() { - String inputTimeString; - - // set default time zone - TimeZone.setDefault(parameterSet.defaultTimeZone); + public void createZonedDate() { + // get DateTimeType from the current parameter + DateTimeType dt1; + DateTimeType dt2; + DateTimeType dt3; + Map inputTimeMap = parameterSet.inputTimeMap; + TimeZone inputTimeZone = parameterSet.inputTimeZone; + String inputTimeString = parameterSet.inputTimeString; + if (inputTimeMap != null && inputTimeZone != null) { + int durationInNano = (int) TimeUnit.NANOSECONDS.convert(inputTimeMap.get("milliseconds"), + TimeUnit.MILLISECONDS); - // get formatted time string - if (parameterSet.inputTimeString == null) { - final Calendar calendar = Calendar.getInstance(parameterSet.inputTimeZone); - calendar.set(parameterSet.inputTimeMap.get("year"), parameterSet.inputTimeMap.get("month"), - parameterSet.inputTimeMap.get("date"), parameterSet.inputTimeMap.get("hourOfDay"), - parameterSet.inputTimeMap.get("minute"), parameterSet.inputTimeMap.get("second")); - calendar.set(Calendar.MILLISECOND, parameterSet.inputTimeMap.get("milliseconds")); - inputTimeString = new SimpleDateFormat(DateTimeType.DATE_PATTERN_WITH_TZ_AND_MS).format(calendar.getTime()); + LocalDateTime dateTime = LocalDateTime.of(inputTimeMap.get("year"), inputTimeMap.get("month") + 1, + inputTimeMap.get("date"), inputTimeMap.get("hourOfDay"), inputTimeMap.get("minute"), + inputTimeMap.get("second"), durationInNano); + ZonedDateTime zonedDate = ZonedDateTime.of(dateTime, inputTimeZone.toZoneId()); + dt1 = new DateTimeType(zonedDate); + dt3 = new DateTimeType( + zonedDate.format((DateTimeFormatter.ofPattern(DateTimeType.DATE_PATTERN_WITH_TZ_AND_MS)))); + zonedDate = ZonedDateTime.of(dateTime, inputTimeZone.toZoneId()).toInstant() + .atZone(parameterSet.defaultTimeZone.toZoneId()); + dt2 = new DateTimeType(zonedDate); + } else if (inputTimeString != null) { + dt1 = new DateTimeType(inputTimeString); + dt2 = new DateTimeType(dt1.getZonedDateTime().withZoneSameInstant(TimeZone.getDefault().toZoneId())); + dt3 = new DateTimeType(dt1.getZonedDateTime()); } else { - inputTimeString = parameterSet.inputTimeString; - } - DateTimeType dt = DateTimeType.valueOf(inputTimeString); - if (parameterSet.inputTimeZone == null) { - dt = new DateTimeType(dt.getZonedDateTime().withZoneSameInstant(TimeZone.getDefault().toZoneId())); + throw new DateTimeException("Invalid inputs in parameter set"); } // Test - assertEquals(parameterSet.expectedResult, dt.toString()); + assertEquals(dt1.toFullString(), dt1.toString()); + assertEquals(dt2.toFullString(), dt2.toString()); + assertEquals(parameterSet.expectedResult, dt1.toString()); + assertEquals(parameterSet.expectedResultLocalTZ, dt2.toString()); + assertEquals(parameterSet.expectedResult, dt3.toString()); + assertEquals(dt1, dt3); } @Test - public void createZonedDate() { - String inputTimeString; + public void formattingTest() { + DateTimeType dt = createDateTimeType(); + Locale locale = parameterSet.locale; + String pattern = parameterSet.pattern; + if (locale != null && pattern != null) { + assertEquals(parameterSet.expectedFormattedResult, dt.format(locale, pattern)); + } else { + assertEquals(parameterSet.expectedFormattedResult, dt.format(pattern)); + } + } - // set default time zone - TimeZone.setDefault(parameterSet.defaultTimeZone); + @Test + public void changingZoneTest() { + DateTimeType dt = createDateTimeType(); + DateTimeType dt2 = dt.toLocaleZone(); + assertEquals(parameterSet.expectedResultLocalTZ, dt2.toFullString()); + dt2 = dt.toZone(parameterSet.defaultTimeZone.toZoneId()); + assertEquals(parameterSet.expectedResultLocalTZ, dt2.toFullString()); + } + + @Test(expected = DateTimeException.class) + public void changingZoneThrowsExceptionTest() { + DateTimeType dt = createDateTimeType(); + dt.toZone("XXX"); + } - // get formatted time string - if (parameterSet.inputTimeString == null) { - int durationInNano = (int) TimeUnit.NANOSECONDS.convert(parameterSet.inputTimeMap.get("milliseconds"), + private DateTimeType createDateTimeType() throws DateTimeException { + Map inputTimeMap = parameterSet.inputTimeMap; + TimeZone inputTimeZone = parameterSet.inputTimeZone; + String inputTimeString = parameterSet.inputTimeString; + if (inputTimeMap != null && inputTimeZone != null) { + int durationInNano = (int) TimeUnit.NANOSECONDS.convert(inputTimeMap.get("milliseconds"), TimeUnit.MILLISECONDS); - LocalDateTime dateTime = LocalDateTime.of(parameterSet.inputTimeMap.get("year"), - parameterSet.inputTimeMap.get("month") + 1, parameterSet.inputTimeMap.get("date"), - parameterSet.inputTimeMap.get("hourOfDay"), parameterSet.inputTimeMap.get("minute"), - parameterSet.inputTimeMap.get("second"), durationInNano); - ZonedDateTime zonedDate = ZonedDateTime.of(dateTime, parameterSet.inputTimeZone.toZoneId()).toInstant() - .atZone(parameterSet.defaultTimeZone.toZoneId()); - inputTimeString = zonedDate.format((DateTimeFormatter.ofPattern(DateTimeType.DATE_PATTERN_WITH_TZ_AND_MS))); - } else { - inputTimeString = parameterSet.inputTimeString; - } - DateTimeType dt = new DateTimeType(inputTimeString); - if (parameterSet.inputTimeZone == null) { - dt = new DateTimeType(dt.getZonedDateTime().withZoneSameInstant(TimeZone.getDefault().toZoneId())); + LocalDateTime dateTime = LocalDateTime.of(inputTimeMap.get("year"), inputTimeMap.get("month") + 1, + inputTimeMap.get("date"), inputTimeMap.get("hourOfDay"), inputTimeMap.get("minute"), + inputTimeMap.get("second"), durationInNano); + ZonedDateTime zonedDate = ZonedDateTime.of(dateTime, inputTimeZone.toZoneId()); + return new DateTimeType(zonedDate); + } else if (inputTimeString != null) { + return new DateTimeType(inputTimeString); } - // Test - assertEquals(parameterSet.expectedResult, dt.toString()); + throw new DateTimeException("Invalid inputs in parameter set"); } }