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

View File

@@ -13,6 +13,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
@@ -107,7 +108,7 @@ public class DateMathParser {
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)

View File

@@ -12,6 +12,7 @@ import static org.junit.Assert.fail;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.time.ZoneOffset;
import java.util.Properties;
import java.util.TimeZone;
@@ -73,8 +74,10 @@ public class AbstractDSpaceIntegrationTest {
//Stops System.exit(0) throws exception instead of exitting
System.setSecurityManager(new NoExitSecurityManager());
//set a standard time zone for the tests
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Dublin"));
// All tests should assume UTC timezone by default (unless overridden in the test itself)
// 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
testProps = new Properties();

View File

@@ -12,6 +12,7 @@ import static org.junit.Assert.fail;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.time.ZoneOffset;
import java.util.Properties;
import java.util.TimeZone;
@@ -82,8 +83,10 @@ public class AbstractDSpaceTest {
@BeforeClass
public static void initKernel() {
try {
//set a standard time zone for the tests
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Dublin"));
// All tests should assume UTC timezone by default (unless overridden in the test itself)
// 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
testProps = new Properties();

View File

@@ -9,7 +9,10 @@ package org.dspace.app.rest;
import java.io.IOException;
import java.sql.SQLException;
import java.time.ZoneOffset;
import java.util.List;
import java.util.TimeZone;
import javax.annotation.PostConstruct;
import javax.servlet.Filter;
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));
}
}