Ensure DSpace defaults to UTC time zone in all code / tests. This is necessary so that Spring / Hibernate don't auto-switch timezones when reading from database.

This commit is contained in:
Tim Donohue
2025-02-24 09:16:19 -06:00
parent 77f07a735b
commit 2b2d0b19b6
5 changed files with 25 additions and 6 deletions

View File

@@ -18,6 +18,7 @@ import java.io.InputStream;
import java.sql.SQLException; import java.sql.SQLException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.ZoneOffset;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
@@ -154,7 +155,7 @@ public class BulkAccessControl extends DSpaceRunnable<BulkAccessControlScriptCon
} }
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.setTimeZone(TimeZone.getTimeZone("UTC")); mapper.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC));
BulkAccessControlInput accessControl; BulkAccessControlInput accessControl;
context = new Context(Context.Mode.BATCH_EDIT); context = new Context(Context.Mode.BATCH_EDIT);
setEPerson(context); setEPerson(context);

View File

@@ -13,6 +13,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder; import java.time.format.DateTimeFormatterBuilder;
@@ -107,7 +108,7 @@ public class DateMathParser {
private static final Logger LOG = LogManager.getLogger(); private static final Logger LOG = LogManager.getLogger();
public static final TimeZone UTC = TimeZone.getTimeZone("UTC"); public static final TimeZone UTC = TimeZone.getTimeZone(ZoneOffset.UTC);
/** /**
* Default TimeZone for DateMath rounding (UTC) * Default TimeZone for DateMath rounding (UTC)

View File

@@ -12,6 +12,7 @@ import static org.junit.Assert.fail;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.ZoneOffset;
import java.util.Properties; import java.util.Properties;
import java.util.TimeZone; import java.util.TimeZone;
@@ -73,8 +74,10 @@ public class AbstractDSpaceIntegrationTest {
//Stops System.exit(0) throws exception instead of exitting //Stops System.exit(0) throws exception instead of exitting
System.setSecurityManager(new NoExitSecurityManager()); System.setSecurityManager(new NoExitSecurityManager());
//set a standard time zone for the tests // All tests should assume UTC timezone by default (unless overridden in the test itself)
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Dublin")); // This ensures that Spring doesn't attempt to change the timezone of dates that are read from the
// database (via Hibernate). We store all dates in the database as UTC.
TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.UTC));
//load the properties of the tests //load the properties of the tests
testProps = new Properties(); testProps = new Properties();

View File

@@ -12,6 +12,7 @@ import static org.junit.Assert.fail;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.ZoneOffset;
import java.util.Properties; import java.util.Properties;
import java.util.TimeZone; import java.util.TimeZone;
@@ -82,8 +83,10 @@ public class AbstractDSpaceTest {
@BeforeClass @BeforeClass
public static void initKernel() { public static void initKernel() {
try { try {
//set a standard time zone for the tests // All tests should assume UTC timezone by default (unless overridden in the test itself)
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Dublin")); // This ensures that Spring doesn't attempt to change the timezone of dates that are read from the
// database (via Hibernate). We store all dates in the database as UTC.
TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.UTC));
//load the properties of the tests //load the properties of the tests
testProps = new Properties(); testProps = new Properties();

View File

@@ -9,7 +9,10 @@ package org.dspace.app.rest;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.ZoneOffset;
import java.util.List; import java.util.List;
import java.util.TimeZone;
import javax.annotation.PostConstruct;
import javax.servlet.Filter; import javax.servlet.Filter;
import org.dspace.app.rest.filter.DSpaceRequestContextFilter; import org.dspace.app.rest.filter.DSpaceRequestContextFilter;
@@ -262,4 +265,12 @@ public class Application extends SpringBootServletInitializer {
} }
}; };
} }
@PostConstruct
public void setDefaultTimezone() {
// Set the default timezone in Spring Boot to UTC.
// This ensures that Spring Boot doesn't attempt to change the timezone of dates that are read from the
// database (via Hibernate). We store all dates in the database as UTC.
TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.UTC));
}
} }