I'm working on a Spring Boot application which queries a PostgreSQL database.
I have a timestamptz
column. The end users of my application would like to view these timestamps in their own timezone, with the offset included. For example,
Given a row containing 2023-08-10T12:00:00.000+00
When the user queries from time zone 'US/Eastern'
Then the output they should see is 2023-08-10T08:00:00.000-04
Sounds simple enough, but I'm having trouble figuring out how to handle this given that there will be multiple concurrent requests coming from different time zones.
If I execute this in a SQL console, it gives me the exact result I want,
SET LOCAL TIME ZONE 'US/Eastern';
SELECT to_char('2023-08-10T12:00:00.000+00'::TIMESTAMPTZ, 'YYYY-MM-DD"T"HH24:MI:SS.MSOF');
2023-08-10T08:00:00.000000-04
However, I do not know how to use this from within my Spring Boot application.
- According to my benchmarks, if I try to do this in code, it comes with a ~30% performance penalty, which is significant in this case, so I would like to do it entirely in SQL if possible.
- I cannot execute two separate statements separated by
;
in aPreparedStatement
- I do not want to change the default time zone for the database, since there may be multiple concurrent requests.
- For the same reason, I do not want to change the default time zone for the JVM.
So, an answer to either of these questions would be a huge help:
- In jdbc, is there a way to set the time zone for only a single query, without affecting other concurrent queries?
- In PostgreSQL, is there a way I can express the above SQL in a single line (no semi colons)?