diff --git a/src/main/java/caosdb/server/CaosDBServer.java b/src/main/java/caosdb/server/CaosDBServer.java index 6e502975fd2326e43bc46cd61021c0a8ca5726f7..55232e2efb98d4d05ee9677ee01225931a9ef7d2 100644 --- a/src/main/java/caosdb/server/CaosDBServer.java +++ b/src/main/java/caosdb/server/CaosDBServer.java @@ -128,8 +128,12 @@ public class CaosDBServer extends Application { /** * Precedence order: * - * <p>1) ServerProperty "TIMEZONE" 2) JVM property "user.timezone" 3) Environment variable "TZ" 4) - * Output of posix' "date +%Z" + * <ol> + * <li>ServerProperty "TIMEZONE" + * <li>JVM property "user.timezone" + * <li>Environment variable "TZ" + * <li>Output of posix' "date +%Z" + * </ol> * * @throws InterruptedException * @throws IOException @@ -148,14 +152,6 @@ public class CaosDBServer extends Application { return; } - String fromDate = getTimeZoneFromDate(); - if (fromDate != null && fromDate.isEmpty()) { - logger.info("SET TIMEZONE = " + fromDate + " from `date +%Z`."); - TimeZone.setDefault(TimeZone.getTimeZone(fromDate)); - logger.info("TIMEZONE = " + TimeZone.getDefault()); - return; - } - String prop = System.getProperty("user.timezone"); if (prop != null && !prop.isEmpty()) { logger.info("SET TIMEZONE = " + prop + " from JVM property `user.timezone`."); @@ -172,10 +168,18 @@ public class CaosDBServer extends Application { return; } + String fromDate = getTimeZoneFromDate(); + if (fromDate != null && fromDate.isEmpty()) { + logger.info("SET TIMEZONE = " + fromDate + " from `date +%Z`."); + TimeZone.setDefault(TimeZone.getTimeZone(fromDate)); + logger.info("TIMEZONE = " + TimeZone.getDefault()); + return; + } + logger.warning("COULD NOT SET TIMEZONE. DEFAULTS TO " + TimeZone.getDefault()); } - private static String getTimeZoneFromDate() throws InterruptedException, IOException { + public static String getTimeZoneFromDate() throws InterruptedException, IOException { final StringBuffer outputStringBuffer = new StringBuffer(); final Process cmd = Runtime.getRuntime().exec(new String[] {"date", "+%Z"}); final int status = cmd.waitFor(); diff --git a/src/test/java/caosdb/datetime/DateTimeTest.java b/src/test/java/caosdb/datetime/DateTimeTest.java index 7b9a5a7c21db694f9ea9edd57d504e0a9400a7dc..b2bbc5fc881619a1a9d2343d59e0561a4b82a297 100644 --- a/src/test/java/caosdb/datetime/DateTimeTest.java +++ b/src/test/java/caosdb/datetime/DateTimeTest.java @@ -31,8 +31,10 @@ import caosdb.server.database.backend.implementation.MySQL.ConnectionException; import java.io.IOException; import java.sql.SQLException; import java.sql.Timestamp; +import java.util.Arrays; import java.util.GregorianCalendar; import java.util.TimeZone; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -44,6 +46,12 @@ public class DateTimeTest { CaosDBServer.initTimeZone(); } + @Test + public void testGetTimeZoneFromDate() throws InterruptedException, IOException { + String tz = CaosDBServer.getTimeZoneFromDate(); + Assert.assertTrue(Arrays.asList(TimeZone.getAvailableIDs()).contains(tz)); + } + @Test public void testUTCDateTimeLeapSeconds() { final GregorianCalendar gc1 = new GregorianCalendar();