I have a model which has startDateTime field which is storing DateTime in OffsetDateTime format.
startDateTime: 2023-07-25T04:40:46.143-08:00
However when we store the above object in our PostgreSQL db on GCP, it is getting stored in below format:
2023-07-25 12:40:46.143+00
It looks like it is adjusting the above object to UTC time.
But my requirement is, it sohuld store in the same format which is there in object and should not adjust to UTC time.
I explored the methods given here, but none of the methods is satisfying my requirement.
Can someone please suggest if there is a way to acheive this. Any help would be appreciated.
Code I used and input is startDateTime: 2023-07-25T12:40:46.143Z
and "timeZoneOffset": "UTC-08:00". I am converting input to expected OffsetDateTime based on timezoneOffset value.
val actualDateTime: OffsetDateTime = OffsetDateTime.parse(startDateTime)
val zoneOffset: ZoneOffset = ZoneOffset.of(timeZoneOffset.replace("UTC", ""))
val expectedDateTime: OffsetDateTime = actualDateTime.withOffsetSameInstant(zoneOffset)
return expectedDateTime.toString()
DDL:
CREATE TABLE IF NOT EXISTS TRANSACTION
(
id uuid DEFAULT,
start_date_time TIMESTAMP WITH TIME ZONE NOT NULL,
end_date_time TIMESTAMP WITH TIME ZONE NOT NULL,
currency VARCHAR(5) NOT NULL,
country VARCHAR(50) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
created_by VARCHAR (255) NOT NULL,
startDateTime TIMESTAMP WITH TIME ZONE NOT NULL
)
Your best practice is to think of time zone as a presentation attribute and the timestamp itself as an instant in time. If your business need requires that you preserve the timezone of the input field, then you must store that in another field outside of the timestamp.
The type "timestamp with timezone" is used by postgresql only to interpret the timezone offset from an input string. That timezone data is not preserved by the database. The timestamp is always stored in UTC, and the time zone is immediately lost.