I have a simple Postgres test table of id, timestamp with timezone. The test and output below should be self explanatory, but to summarize, I insert a row that has a timestamp with a -6 offset. It is properly inserted into the database, then loaded out of the database with the same hour, but the wrong offset, specifically Z instead of -6.
I've tried setting my database to UTC, and when I manually select at the command line, it properly shows the time in UTC. Set database to mountain, it shows the expected time with an offset of -6.
Forcing the database to various timezones via a statement executed in jOOQ seems to do nothing.
context.execute( "set timezone TO 'GMT';" ); // this has no effect
Forcing my system time to UTC effectively works around the problem, but is for many reasons unacceptable.
TimeZone.setDefault( TimeZone.getTimeZone( "UTC" ) ); // this is a band aid that works, but is not sustainable
Here's the unit test:
@Test
public void confirmDateRoundTripFromDb() throws SQLException, DatatypeConfigurationException
{
ZonedDateTime testDate = ZonedDateTime.of( 2019, 05, 30, 12, 54, 32, 203, TimeUtilities.CENTRAL_ZONEID );
final OffsetDateTime testDateAsOffset = testDate.toOffsetDateTime( );
try( PGConnection dbConnection = DatabaseUtility.getPostgresConnection( _unitTestConfig.getSection("Postgres").getProperties(), _testDbName ) )
{
DSLContext context = DSL.using( dbConnection, SQLDialect.POSTGRES );
DateTestsRecord dateTestsRecord = context.newRecord( DATE_TESTS );
dateTestsRecord.setTestTimestamp( testDateAsOffset );
dateTestsRecord.store();
int id = dateTestsRecord.getId();
DateTestsRecord insertedRecord = context.selectFrom( DATE_TESTS ).where( DATE_TESTS.ID.eq( id ) ).fetchAny();
System.out.println( testDateAsOffset );
System.out.println( insertedRecord.getTestTimestamp() );
}
}
And the output:
2019-05-30T12:54:32.000000203-05:00
2019-05-30T11:54:32Z
Interestingly, if I add a date in central, the hour is correctly changed to mountain, but the output after the round trip still just happily reports Z.
I assume this is not expected? Am I doing something wrong? If not, any ideas for a workaround that would apply globally? There are several developers on this, I'm not excited about having to handle the date with some special logic every time we select, seems fragile.
I've been running 3.10, but just upgraded to 3.12 with the same results.