Is it possible to let Postgresql return the dates in the timezone I need, query by query? #3047
Unanswered
frederikhors
asked this question in
Q&A
Replies: 1 comment
-
Postgres can only return timestamps as UTC. The typename Technically you can return timestamps without a timezone using the SELECT (now()::timestamp at time zone 'UTC')::timestamp This would become a You can also get around this by returning time as ISO-8601.. something like SELECT to_char (now()::timestamp at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') It's a bit verbose but you could create a function |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm using
sqlx
0.7.3 andtime
0.3.34.I have this struct:
I can read with queries like:
and I can insert with queries like:
and everything works (other more difficult queries too).
Now the issue.
The
created_at
anddate_time
fields are saved in the database as Postgresql'stimestamptz
type. Great.But when I retrieve those fields I need to get them in the current user's timezone.
Example:
If the current user querying that games data is currently on timezone
Europe/Berlin
the backend code in Rust should work on that struct datetime fields on that timezone, not onUTC
which apparently is the default usingtime::OffsetDateTime
with sqlx.I know I can do conversion on backend in Rust code (for example converting the
time::OffsetDateTime
totime::PrimitiveDateTime
or using time-tz crate's methods), but I would like to do conversion directly in Postgresql.I read I can use the
AT TIME ZONE 'Europe/Berlin'
PG's construct to do this, but what about all dates in all the query? Even when I use something likeRETURNING *
at the end of a PG's CTE?I read I can use instead
SET TIME ZONE 'Europe/Berlin'
before the queries and I tried it but sqlx's author answered:So I'm lost now.
Is it possible to let Postgresql return the dates in the timezone I need, query by query?
Beta Was this translation helpful? Give feedback.
All reactions