Skip to content
Snippets Groups Projects
Verified Commit 31f51c6d authored by Timm Fitschen's avatar Timm Fitschen
Browse files

WIP: timezone

parent 8c0614f7
No related branches found
No related tags found
1 merge request!69BUG: setup timezone in deploy
Pipeline #27713 failed
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
# #
variables: variables:
DEPLOY_REF: dev DEPLOY_REF: f-timezone
CI_REGISTRY_IMAGE: $CI_REGISTRY/caosdb/src/caosdb-server/caosdb-server-testenv:latest CI_REGISTRY_IMAGE: $CI_REGISTRY/caosdb/src/caosdb-server/caosdb-server-testenv:latest
GIT_SUBMODULE_STRATEGY: normal GIT_SUBMODULE_STRATEGY: normal
......
...@@ -25,6 +25,14 @@ package org.caosdb.datetime; ...@@ -25,6 +25,14 @@ package org.caosdb.datetime;
import java.util.Date; import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
/**
* Represents the shift, i.e. the time difference between a time zone 'X' and the UTC+0 time zone.
*
* <p>This is used for (de-)serialization of ISO8601-compliant representations of UTC-relative time
* zones (e.g. +0100 for CEST)
*
* @author tf
*/
public class UTCTimeZoneShift extends TimeZone { public class UTCTimeZoneShift extends TimeZone {
private static final long serialVersionUID = 6962096078347188058L; private static final long serialVersionUID = 6962096078347188058L;
public static final int POSITIVE = 1; public static final int POSITIVE = 1;
...@@ -35,10 +43,10 @@ public class UTCTimeZoneShift extends TimeZone { ...@@ -35,10 +43,10 @@ public class UTCTimeZoneShift extends TimeZone {
public UTCTimeZoneShift(final int sign, final int h, final int m) { public UTCTimeZoneShift(final int sign, final int h, final int m) {
if (59 < m || m < 0) { if (59 < m || m < 0) {
throw new IllegalArgumentException("59<m<0 is not allowed."); throw new IllegalArgumentException("59<m<0 is not allowed. m was " + Integer.toString(m));
} }
if (12 < h || h < 0) { if (14 < h || h < 0) {
throw new IllegalArgumentException("12<h<0 is not allowed."); throw new IllegalArgumentException("14<h<0 is not allowed. h was " + Integer.toString(h));
} }
rawOffset = sign * (3600 * h + 60 * m) * 1000; rawOffset = sign * (3600 * h + 60 * m) * 1000;
...@@ -48,6 +56,12 @@ public class UTCTimeZoneShift extends TimeZone { ...@@ -48,6 +56,12 @@ public class UTCTimeZoneShift extends TimeZone {
private static String generateString(final int sign, final int h, final int m) { private static String generateString(final int sign, final int h, final int m) {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
sb.append(sign == NEGATIVE ? '-' : '+'); sb.append(sign == NEGATIVE ? '-' : '+');
if (59 < m || m < 0) {
throw new IllegalArgumentException("59<m<0 is not allowed. m was " + Integer.toString(m));
}
if (14 < h || h < 0) {
throw new IllegalArgumentException("14<h<0 is not allowed. h was " + Integer.toString(h));
}
if (h < 10) { if (h < 10) {
sb.append('0'); sb.append('0');
} }
...@@ -116,12 +130,15 @@ public class UTCTimeZoneShift extends TimeZone { ...@@ -116,12 +130,15 @@ public class UTCTimeZoneShift extends TimeZone {
* Generate an ISO 8601 time zone offset string (e.g. +0200) from the given raw offset in * Generate an ISO 8601 time zone offset string (e.g. +0200) from the given raw offset in
* milliseconds. * milliseconds.
* *
* @param rawOffset - offset in ms. * @param timezone - the time zone in question.
* @return ISO 8601 time zone offset. * @return ISO 8601 time zone offset.
*/ */
public static String getISO8601Offset(int rawOffset) { public static String getISO8601Offset(TimeZone timezone) {
int h = Integer.divideUnsigned(rawOffset, 1000 * 60 * 60); int divisor = 1000 * 60 * 60;
int m = Integer.remainderUnsigned(rawOffset, 1000 * 60 * 60); int offset = timezone.getOffset(System.currentTimeMillis());
return generateString(Integer.signum(h), h, m); int sign = Integer.signum(offset);
int h = Integer.divideUnsigned(sign * offset, divisor);
int m = Integer.remainderUnsigned(sign * offset, divisor);
return generateString(sign, h, m);
} }
} }
...@@ -87,6 +87,8 @@ import org.caosdb.server.scripting.ScriptingPermissions; ...@@ -87,6 +87,8 @@ import org.caosdb.server.scripting.ScriptingPermissions;
import org.caosdb.server.transaction.ChecksumUpdater; import org.caosdb.server.transaction.ChecksumUpdater;
import org.caosdb.server.utils.FileUtils; import org.caosdb.server.utils.FileUtils;
import org.caosdb.server.utils.Initialization; import org.caosdb.server.utils.Initialization;
import org.caosdb.server.utils.Observable;
import org.caosdb.server.utils.Observer;
import org.quartz.JobDetail; import org.quartz.JobDetail;
import org.quartz.Scheduler; import org.quartz.Scheduler;
import org.quartz.SchedulerException; import org.quartz.SchedulerException;
...@@ -118,7 +120,7 @@ import org.slf4j.LoggerFactory; ...@@ -118,7 +120,7 @@ import org.slf4j.LoggerFactory;
public class CaosDBServer extends Application { public class CaosDBServer extends Application {
private static Logger logger = LoggerFactory.getLogger(CaosDBServer.class); private static Logger logger = LoggerFactory.getLogger(CaosDBServer.class);
private static Properties SERVER_PROPERTIES = null; private static ServerProperties SERVER_PROPERTIES = null;
private static Component component = null; private static Component component = null;
private static ArrayList<Runnable> postShutdownHooks = new ArrayList<Runnable>(); private static ArrayList<Runnable> postShutdownHooks = new ArrayList<Runnable>();
private static ArrayList<Runnable> preShutdownHooks = new ArrayList<Runnable>(); private static ArrayList<Runnable> preShutdownHooks = new ArrayList<Runnable>();
...@@ -210,6 +212,27 @@ public class CaosDBServer extends Application { ...@@ -210,6 +212,27 @@ public class CaosDBServer extends Application {
*/ */
public static void initTimeZone() throws InterruptedException, IOException { public static void initTimeZone() throws InterruptedException, IOException {
final String serverProperty = getServerProperty(ServerProperties.KEY_TIMEZONE); final String serverProperty = getServerProperty(ServerProperties.KEY_TIMEZONE);
// Setup an observer which updates the server's time zone when the relevant ServerProperty
// changes (this can only happen when in debug mode).
if (isDebugMode()) {
SERVER_PROPERTIES.acceptObserver(
new Observer() {
@Override
public boolean notifyObserver(String e, Observable sender) {
if (e.equals(ServerProperties.KEY_TIMEZONE)) {
String[] availableIDs = TimeZone.getAvailableIDs();
TimeZone newZoneId =
TimeZone.getTimeZone(getServerProperty(ServerProperties.KEY_TIMEZONE));
TimeZone.setDefault(newZoneId);
}
return true; // true means, keep this observer.
}
});
}
if (serverProperty != null && !serverProperty.isEmpty()) { if (serverProperty != null && !serverProperty.isEmpty()) {
logger.info( logger.info(
"SET TIMEZONE = " "SET TIMEZONE = "
......
...@@ -31,11 +31,15 @@ import java.util.Arrays; ...@@ -31,11 +31,15 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Properties; import java.util.Properties;
import org.caosdb.server.utils.AbstractObservable;
import org.caosdb.server.utils.Observable;
import org.caosdb.server.utils.Observer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class ServerProperties extends Properties { public class ServerProperties extends Properties implements Observable {
private Observable delegateObservable = new AbstractObservable() {};
private static final long serialVersionUID = -6178774548807398071L; private static final long serialVersionUID = -6178774548807398071L;
private static Logger logger = LoggerFactory.getLogger(ServerProperties.class.getName()); private static Logger logger = LoggerFactory.getLogger(ServerProperties.class.getName());
...@@ -150,8 +154,8 @@ public class ServerProperties extends Properties { ...@@ -150,8 +154,8 @@ public class ServerProperties extends Properties {
* *
* @throws IOException * @throws IOException
*/ */
public static Properties initServerProperties() throws IOException { public static ServerProperties initServerProperties() throws IOException {
final Properties serverProperties = new Properties(); final ServerProperties serverProperties = new ServerProperties();
final String basepath = System.getProperty("user.dir"); final String basepath = System.getProperty("user.dir");
final URL url = CaosDBServer.class.getResource("/build.properties"); final URL url = CaosDBServer.class.getResource("/build.properties");
...@@ -207,4 +211,21 @@ public class ServerProperties extends Properties { ...@@ -207,4 +211,21 @@ public class ServerProperties extends Properties {
sp_in.close(); sp_in.close();
} }
} }
@Override
public synchronized Object setProperty(String key, String value) {
Object ret = super.setProperty(key, value);
delegateObservable.notifyObservers(key);
return ret;
}
@Override
public boolean acceptObserver(Observer o) {
return delegateObservable.acceptObserver(o);
}
@Override
public void notifyObservers(String e) {
delegateObservable.notifyObservers(e);
}
} }
...@@ -56,7 +56,7 @@ public class InfoResource extends AbstractCaosDBServerResource { ...@@ -56,7 +56,7 @@ public class InfoResource extends AbstractCaosDBServerResource {
public Element getTimeZone() { public Element getTimeZone() {
TimeZone d = TimeZone.getDefault(); TimeZone d = TimeZone.getDefault();
return new Element("TimeZone") return new Element("TimeZone")
.setAttribute("offset", UTCTimeZoneShift.getISO8601Offset(d.getRawOffset())) .setAttribute("offset", UTCTimeZoneShift.getISO8601Offset(d))
.setAttribute("id", d.getID()) .setAttribute("id", d.getID())
.addContent(d.getDisplayName()); .addContent(d.getDisplayName());
} }
......
package org.caosdb.datetime;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.TimeZone;
import org.junit.jupiter.api.Test;
class UTCTimeZoneShiftTest {
@Test
void test() {
TimeZone timeZone = TimeZone.getTimeZone("CST");
assertEquals("-0500", UTCTimeZoneShift.getISO8601Offset(timeZone));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment