Skip to content
Snippets Groups Projects
Unverified Commit 6152f1fb authored by Timm Fitschen's avatar Timm Fitschen
Browse files

BUG: use correct TimeZone implementation

parent 68dc1b61
No related branches found
No related tags found
No related merge requests found
......@@ -64,11 +64,14 @@ import caosdb.server.utils.FileUtils;
import caosdb.server.utils.Initialization;
import caosdb.server.utils.NullPrintStream;
import caosdb.server.utils.Utils;
import com.ibm.icu.util.TimeZone;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
import java.util.TimeZone;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
......@@ -134,8 +137,22 @@ public class CaosDBServer extends Application {
public static void initTimeZone() throws InterruptedException, IOException {
String serverProperty = getServerProperty(ServerProperties.KEY_TIMEZONE);
if (serverProperty != null && !serverProperty.isEmpty()) {
logger.info("SET TIMEZONE = " + serverProperty + " from ServerProperty `" + ServerProperties.KEY_TIMEZONE + "`.");
logger.info(
"SET TIMEZONE = "
+ serverProperty
+ " from ServerProperty `"
+ ServerProperties.KEY_TIMEZONE
+ "`.");
TimeZone.setDefault(TimeZone.getTimeZone(serverProperty));
logger.info("TIMEZONE = " + TimeZone.getDefault());
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;
}
......@@ -143,6 +160,7 @@ public class CaosDBServer extends Application {
if (prop != null && !prop.isEmpty()) {
logger.info("SET TIMEZONE = " + prop + " from JVM property `user.timezone`.");
TimeZone.setDefault(TimeZone.getTimeZone(prop));
logger.info("TIMEZONE = " + TimeZone.getDefault());
return;
}
......@@ -150,13 +168,7 @@ public class CaosDBServer extends Application {
if (envVar != null && !envVar.isEmpty()) {
logger.info("SET TIMEZONE = " + envVar + " from evironment variable `TZ`.");
TimeZone.setDefault(TimeZone.getTimeZone(envVar));
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;
}
......@@ -164,16 +176,29 @@ public class CaosDBServer extends Application {
}
private 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();
if (cmd.waitFor() == 0) {
return Utils.InputStream2String(cmd.getInputStream());
}
if (status != 0) {
logger.warning(
"Could not determine time zone from `date +%Z`. The command returned with exit code "
+ cmd.exitValue());
return null;
}
final Reader r = new InputStreamReader(cmd.getInputStream());
final BufferedReader buf = new BufferedReader(r);
String line;
while ((line = buf.readLine()) != null) {
outputStringBuffer.append(line);
}
if (outputStringBuffer.length() > 0) {
return outputStringBuffer.toString().trim();
} else {
throw new RuntimeException("Output of `date +%Z` command was empty.");
}
}
/**
* This main method starts up a web application that will listen on a port defined in the config
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment