I'm trying to display a local time using postgres-rs, with time-rs 0.3 instead of chrono-rs 0.4. But with the timezone set to Europe/Amsterdam, all I get is UTC timestamps with no timezone information to correct it.
From psql show timezone; select now();
reads Europe/Amsterdam
and 2024-10-01 10:38:36.63829+02
.
But when I try the same thing in Rust:
// mut conn: PooledConnection<PostgresConnectionManager<MakeTlsConnector>>
let mut tz: Option<String> = None;
if let Ok(r) = conn.query_one("show timezone", &[]) { tz = r.get(0); }
println!("timezone: {:?}", tz);
let mut now: Option<OffsetDateTime> = None;
if let Ok(r) = conn.query_one("select now()", &[]) { now = r.get(0); }
println!("now: {:?}", now);
I get:
timezone: Some("Europe/Amsterdam")
now: Some(2024-10-01 8:41:49.767418 +00:00:00)
Without an appriopriate value from OffsetDateTime.offset(), how can I display the local time correctly?
postgres
crate discourages such reliance by always returningTIMESTAMP WITH TIME ZONE
values in UTC, as you are observing. You will (almost) always know what timezone you want values in irrespective of the server's setting, so you need merely perform conversions after obtaining each value from the database. – Grisbyselect extract( timezone from now() )
toUtcOffset::parse
for onward use. – Grisbytimezone
in the connection's session, so that PostgreSQL would handle the conversions from UTC, just as withpsql
. But for reasons unknown to me I seem to have to repeat the process on the application server. I did tryextract(timezone ..)
before, but that got me into serious trouble with DST changes. I failed to take into account that timestamps are calculated incorrectly when the DST offset was different from the current time zone. – Hoashis