Commit 6c9ecf9d authored by Marco Descher's avatar Marco Descher
Browse files

[23257] TimeUtil#toLocalDateTime adapt value to local timezone

parent c92605cc
Pipeline #22731 passed with stages
in 4 minutes and 34 seconds
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.pde.ui.JunitLaunchConfig">
<booleanAttribute key="append.args" value="true"/>
<stringAttribute key="application" value="org.eclipse.pde.junit.runtime.coretestapplication"/>
<booleanAttribute key="askclear" value="false"/>
<booleanAttribute key="automaticAdd" value="false"/>
<booleanAttribute key="automaticValidate" value="false"/>
<stringAttribute key="bootstrap" value=""/>
<stringAttribute key="checked" value="[NONE]"/>
<booleanAttribute key="clearConfig" value="true"/>
<booleanAttribute key="clearws" value="true"/>
<booleanAttribute key="clearwslog" value="false"/>
<stringAttribute key="configLocation" value="${workspace_loc}/.metadata/.plugins/org.eclipse.pde.core/pde-junit"/>
<booleanAttribute key="default" value="false"/>
<setAttribute key="deselected_workspace_bundles"/>
<booleanAttribute key="includeOptional" value="false"/>
<stringAttribute key="location" value="${workspace_loc}/../junit-workspace"/>
<setAttribute key="name.schedenig.eclipse.grepconsole.DisabledIds"/>
<setAttribute key="name.schedenig.eclipse.grepconsole.EnabledIds"/>
<setAttribute key="name.schedenig.eclipse.grepconsole.FilterDisabledIds"/>
<setAttribute key="name.schedenig.eclipse.grepconsole.FilterEnabledIds"/>
<setAttribute key="name.schedenig.eclipse.grepconsole.StatisticsDisabledIds"/>
<setAttribute key="name.schedenig.eclipse.grepconsole.StatisticsEnabledIds"/>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/ch.elexis.core.test/src/ch/elexis/core/TimeUtilTest.java"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="1"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/>
<booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/>
<stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/>
<stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
<stringAttribute key="org.eclipse.jdt.launching.JRE_CONTAINER" value="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="ch.elexis.core.TimeUtilTest"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog -console"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="ch.elexis.core.test"/>
<stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.pde.ui.workbenchClasspathProvider"/>
<stringAttribute key="pde.version" value="3.3"/>
<stringAttribute key="product" value="org.eclipse.platform.ide"/>
<booleanAttribute key="run_in_ui_thread" value="false"/>
<setAttribute key="selected_target_bundles">
<setEntry value="ca.uhn.hapi.osgi-base@default:default"/>
<setEntry value="ch.qos.logback.classic@default:default"/>
<setEntry value="ch.qos.logback.core@default:default"/>
<setEntry value="ch.qos.logback.slf4j@default:false"/>
<setEntry value="com.eclipsesource.jaxrs.consumer@default:default"/>
<setEntry value="com.eclipsesource.jaxrs.jersey-all-nojavax@default:default"/>
<setEntry value="com.google.gson@default:default"/>
<setEntry value="com.google.guava@default:default"/>
<setEntry value="com.healthmarketscience.jackcess@default:default"/>
<setEntry value="com.ibm.icu@default:default"/>
<setEntry value="com.mysql.cj@default:default"/>
<setEntry value="com.sun.activation.javax.activation@default:default"/>
<setEntry value="jakarta.persistence@default:default"/>
<setEntry value="jakarta.xml.soap-api@default:default"/>
<setEntry value="jakarta.xml.ws-api@default:default"/>
<setEntry value="javax.el@default:default"/>
<setEntry value="javax.enterprise.cdi-api@default:default"/>
<setEntry value="javax.inject@default:default"/>
<setEntry value="javax.interceptor-api@default:default"/>
<setEntry value="javax.servlet@default:default"/>
<setEntry value="javax.transaction-api@default:default"/>
<setEntry value="javax.xml.stream.stax-api@default:default"/>
<setEntry value="javax.xml@1:default"/>
<setEntry value="jaxb-api@default:default"/>
<setEntry value="org.apache.commons.beanutils@default:default"/>
<setEntry value="org.apache.commons.codec@default:default"/>
<setEntry value="org.apache.commons.collections4@default:default"/>
<setEntry value="org.apache.commons.collections@default:default"/>
<setEntry value="org.apache.commons.dbcp@default:default"/>
<setEntry value="org.apache.commons.io*2.5.0@default:default"/>
<setEntry value="org.apache.commons.io*2.6.0.v20190123-2029@default:default"/>
<setEntry value="org.apache.commons.lang3@default:default"/>
<setEntry value="org.apache.commons.lang@default:default"/>
<setEntry value="org.apache.commons.logging@default:default"/>
<setEntry value="org.apache.commons.pool@default:default"/>
<setEntry value="org.apache.felix.scr@1:true"/>
<setEntry value="org.apache.poi.ooxml.schemas@default:default"/>
<setEntry value="org.apache.poi.ooxml@default:default"/>
<setEntry value="org.apache.poi@default:default"/>
<setEntry value="org.apache.xmlbeans@default:default"/>
<setEntry value="org.beanshell.bsh@default:default"/>
<setEntry value="org.bouncycastle.bcprov@default:default"/>
<setEntry value="org.dom4j@default:default"/>
<setEntry value="org.eclipse.ant.core@default:default"/>
<setEntry value="org.eclipse.core.contenttype@default:default"/>
<setEntry value="org.eclipse.core.expressions@default:default"/>
<setEntry value="org.eclipse.core.filesystem.linux.x86_64@default:false"/>
<setEntry value="org.eclipse.core.filesystem@default:default"/>
<setEntry value="org.eclipse.core.jobs@default:default"/>
<setEntry value="org.eclipse.core.resources@default:default"/>
<setEntry value="org.eclipse.core.runtime@default:true"/>
<setEntry value="org.eclipse.core.variables@default:default"/>
<setEntry value="org.eclipse.emf.common@default:default"/>
<setEntry value="org.eclipse.emf.ecore@default:default"/>
<setEntry value="org.eclipse.equinox.app@default:default"/>
<setEntry value="org.eclipse.equinox.common@2:true"/>
<setEntry value="org.eclipse.equinox.event@default:default"/>
<setEntry value="org.eclipse.equinox.preferences@default:default"/>
<setEntry value="org.eclipse.equinox.registry@default:default"/>
<setEntry value="org.eclipse.gemini.jpa@3:true"/>
<setEntry value="org.eclipse.osgi.compatibility.state@default:false"/>
<setEntry value="org.eclipse.osgi.services@default:default"/>
<setEntry value="org.eclipse.osgi.util@default:default"/>
<setEntry value="org.eclipse.osgi@-1:true"/>
<setEntry value="org.eclipse.persistence.antlr@default:default"/>
<setEntry value="org.eclipse.persistence.asm@default:default"/>
<setEntry value="org.eclipse.persistence.core@default:default"/>
<setEntry value="org.eclipse.persistence.extension@default:false"/>
<setEntry value="org.eclipse.persistence.jpa.jpql@default:default"/>
<setEntry value="org.eclipse.persistence.jpa@default:default"/>
<setEntry value="org.h2@default:default"/>
<setEntry value="org.hamcrest.core@default:default"/>
<setEntry value="org.jdom@default:default"/>
<setEntry value="org.json@default:default"/>
<setEntry value="org.junit@default:default"/>
<setEntry value="org.postgresql.jdbc42@default:default"/>
<setEntry value="org.slf4j.api@default:default"/>
<setEntry value="osgi.enterprise@1:default"/>
</setAttribute>
<setAttribute key="selected_workspace_bundles">
<setEntry value="ch.elexis.core.hl7.v2x.tests@default:false"/>
<setEntry value="ch.elexis.core.hl7.v2x@default:default"/>
<setEntry value="ch.elexis.core.importer.div@default:default"/>
<setEntry value="ch.elexis.core.jcifs.test@default:false"/>
<setEntry value="ch.elexis.core.jcifs@default:default"/>
<setEntry value="ch.elexis.core.jpa.datasource@3:true"/>
<setEntry value="ch.elexis.core.jpa.entities.test@default:false"/>
<setEntry value="ch.elexis.core.jpa.entities@3:true"/>
<setEntry value="ch.elexis.core.jpa.logging.slf4j@default:false"/>
<setEntry value="ch.elexis.core.jpa.test@default:false"/>
<setEntry value="ch.elexis.core.jpa@default:false"/>
<setEntry value="ch.elexis.core.l10n@default:default"/>
<setEntry value="ch.elexis.core.logback.rocketchat@default:false"/>
<setEntry value="ch.elexis.core.logging.default_configuration@default:false"/>
<setEntry value="ch.elexis.core.services.test@default:false"/>
<setEntry value="ch.elexis.core.services@default:default"/>
<setEntry value="ch.elexis.core.tasks.test@default:false"/>
<setEntry value="ch.elexis.core.tasks@default:default"/>
<setEntry value="ch.elexis.core.test.context@default:false"/>
<setEntry value="ch.elexis.core.test@default:default"/>
<setEntry value="ch.elexis.core@default:default"/>
<setEntry value="ch.rgw.utility.tests@default:false"/>
<setEntry value="ch.rgw.utility@default:default"/>
<setEntry value="jaxb-api-impl-import-java9@default:false"/>
</setAttribute>
<booleanAttribute key="show_selected_only" value="false"/>
<booleanAttribute key="tracing" value="false"/>
<booleanAttribute key="useCustomFeatures" value="false"/>
<booleanAttribute key="useDefaultConfig" value="true"/>
<booleanAttribute key="useDefaultConfigArea" value="false"/>
<booleanAttribute key="useProduct" value="false"/>
</launchConfiguration>
package ch.elexis.core;
import static org.junit.Assert.assertEquals;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.junit.BeforeClass;
import org.junit.Test;
import ch.elexis.core.time.TimeUtil;
public class TimeUtilTest {
private static DatatypeFactory dtf;
@BeforeClass
public static void beforeClass() throws DatatypeConfigurationException{
dtf = DatatypeFactory.newInstance();
}
@Test
public void testToLocalDateXMLGregorianCalendar(){
XMLGregorianCalendar calendar = dtf.newXMLGregorianCalendar("2021-10-13T00:00:00.0Z");
LocalDate result = TimeUtil.toLocalDate(calendar);
LocalDate reference = LocalDate.of(2021, Month.OCTOBER, 13);
assertEquals(reference, result);
}
@Test
public void testToLocalDateTimeXMLGregorianCalendar(){
XMLGregorianCalendar calendar = dtf.newXMLGregorianCalendar("2021-10-13T05:49:44.303Z");
LocalDateTime result = TimeUtil.toLocalDateTime(calendar);
LocalDateTime reference = LocalDateTime.of(2021, Month.OCTOBER, 13, 7, 49, 44, 303000000);
assertEquals(reference, result);
}
@Test
public void testToDateXMLGregorianCalendar(){
XMLGregorianCalendar calendar = dtf.newXMLGregorianCalendar("2021-10-13T05:49:44.303Z");
Date result = TimeUtil.toDate(calendar);
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.set(2021, 9, 13, 7, 49, 44);
Date reference = gregorianCalendar.getTime();
assertEquals(reference, result);
}
}
......@@ -74,7 +74,8 @@ public class TimeUtil {
return formatSafe(toLocalDateTime(new Date(lastUpdate)));
}
public static @Nullable LocalDate toLocalDate(@Nullable Date date){
public static @Nullable LocalDate toLocalDate(@Nullable
Date date){
if (date == null) {
return null;
}
......@@ -83,7 +84,8 @@ public class TimeUtil {
return gc.toZonedDateTime().toLocalDate();
}
public static @Nullable LocalDateTime toLocalDateTime(@Nullable Date date){
public static @Nullable LocalDateTime toLocalDateTime(@Nullable
Date date){
if (date == null) {
return null;
}
......@@ -153,8 +155,16 @@ public class TimeUtil {
return xcal.toGregorianCalendar().toZonedDateTime().toLocalDate();
}
/**
* Convert the {@link XMLGregorianCalendar} value to a LocalDateTime in the systems default
* timezone.
*
* @param xcal with a time zone
* @return value adapted to local timezone
*/
public static LocalDateTime toLocalDateTime(XMLGregorianCalendar xcal){
return xcal.toGregorianCalendar().toZonedDateTime().toLocalDateTime();
return xcal.toGregorianCalendar().toZonedDateTime()
.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
}
public static LocalDateTime toLocalDateTime(BigInteger timestamp){
......@@ -168,7 +178,7 @@ public class TimeUtil {
gc.setTimeInMillis(timestamp);
return gc.toZonedDateTime().toLocalDateTime();
}
public static LocalDate toLocalDate(BigInteger timestamp){
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(timestamp.longValue());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment