如何将 postgres UTC 转换为另一个时区?
How to convert postgres UTC into another timezone?
有没有办法将 postgres UTC 时间转换为用户的时区或至少是东部标准时间(我将不胜感激)?
我认为有一种方法可以 change postgres UTC time,,但我认为没有一种方法不会引起很多问题。根据我的阅读,最好在前端使用代码将其转换为正确的时区?
这对我来说几乎没有意义。
有什么意义?
因此,当用户勾选他完成了一个好习惯时,这个习惯就消失了,本来应该在明天中午 12 点重新显示,但习惯最终会在当天晚些时候重新显示 因为 UTC。
habit.rb
scope :incomplete, -> {where("completed_at is null OR completed_at < ?", Date.today)} # aka those habits not completed today will be shown
def completed=(boolean)
self.completed_at = boolean ? Time.current : nil
end
def completed
completed_at && completed_at >= Time.current.beginning_of_day
end
将此代码片段放入您的 application.rb
config.time_zone = 'Eastern Time (US & Canada)'
和 运行 这样它将在 heroku heroku config:add TZ=America/Los_Angeles
.
上设置
一般也可以用#in_time_zone
。始终使用像 Time.zone.now
.
这样的时间
Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
DateTime.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
Date.new(2000).in_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00
请将范围更改为此,它将专门搜索时区。
scope :incomplete, -> {where("completed_at is null OR completed_at < ?", Time.zone.now.beginning_of_day)}
有没有办法将 postgres UTC 时间转换为用户的时区或至少是东部标准时间(我将不胜感激)?
我认为有一种方法可以 change postgres UTC time,,但我认为没有一种方法不会引起很多问题。根据我的阅读,最好在前端使用代码将其转换为正确的时区?
这对我来说几乎没有意义。
有什么意义?
因此,当用户勾选他完成了一个好习惯时,这个习惯就消失了,本来应该在明天中午 12 点重新显示,但习惯最终会在当天晚些时候重新显示 因为 UTC。
habit.rb
scope :incomplete, -> {where("completed_at is null OR completed_at < ?", Date.today)} # aka those habits not completed today will be shown
def completed=(boolean)
self.completed_at = boolean ? Time.current : nil
end
def completed
completed_at && completed_at >= Time.current.beginning_of_day
end
将此代码片段放入您的 application.rb
config.time_zone = 'Eastern Time (US & Canada)'
和 运行 这样它将在 heroku heroku config:add TZ=America/Los_Angeles
.
一般也可以用#in_time_zone
。始终使用像 Time.zone.now
.
Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
DateTime.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
Date.new(2000).in_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00
请将范围更改为此,它将专门搜索时区。
scope :incomplete, -> {where("completed_at is null OR completed_at < ?", Time.zone.now.beginning_of_day)}