gpt4 book ai didi

ruby-on-rails - rails + PostgreSQL : How to query timezone info along with time

转载 作者:太空宇宙 更新时间:2023-11-03 16:14:42 24 4
gpt4 key购买 nike

我正在使用 Rails4 和 PostgreSQL v9.6.2,我的问题是我想在输入时区的 Rails 中处理 datetime,它会正确存储在 PostreSQL 中,但是当我使用 Rails 检索时区信息丢失。

我在 PostgreSQL 中使用 timestamp with time zone,在 Rails 中使用 datetime。例如,在 PostgreSQL 上我看到 2012-10-26 19:26:00+02,在 Rails 上我看到 Fri, 26 Oct 2012 17:26:00 UTC +00 :00,这在技术上是相同的日期,但在 UTC 上,或者没有原始时区。我想从 Rails 获取存储在 PostgreSQL 上的时区信息。

最佳答案

来自 Postgres 手册:

For timestamp with time zone, the internally stored value is always inUTC (Universal Coordinated Time, traditionally known as Greenwich MeanTime, GMT). An input value that has an explicit time zone specified isconverted to UTC using the appropriate offset for that time zone. Ifno time zone is stated in the input string, then it is assumed to bein the time zone indicated by the system's TimeZone parameter, and isconverted to UTC using the offset for the timezone zone.

When a timestamp with time zone value is output, it is alwaysconverted from UTC to the current timezone zone, and displayed aslocal time in that zone. To see the time in another time zone, eitherchange timezone or use the AT TIME ZONE construct

您可以使用以下方法在 session 级别覆盖当前时区:

SET TIME ZONE timezone;

timezone 您想要的时区。

关于ruby-on-rails - rails + PostgreSQL : How to query timezone info along with time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48765856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com