gpt4 book ai didi

ruby-on-rails - rails 4 : GROUP BY date with time zone

转载 作者:行者123 更新时间:2023-12-03 12:17:02 28 4
gpt4 key购买 nike

我正在尝试获取每天切割的 LaserSheets 数量以在图表中使用。

pages_controller.rb:

start_date = 7.days.ago
recent_cut_stats = LaserSheet.where('cut_at IS NOT NULL')
.where('cut_at > ?', start_date.beginning_of_day)
.group("DATE(cut_at)")
.count

我遇到了问题,因为我站点的其余部分使用 application.rb 中设置的时区,但上述查询返回按 UTC 时间日期分组的结果。 PDT 下午 5:00 (UTC -0700) 之后,我开始看到明天的结果。有没有办法将 group("Date(cut_at)" 转换为应用程序的时区?

最佳答案

正如上面的评论所暗示的,您需要使用 SQL 将日期转换为您的 native 数据库:

start_date = 7.days.ago
recent_cut_stats = LaserSheet.where('cut_at IS NOT NULL')
.where('cut_at > ?', start_date.beginning_of_day)
.group("DATE(CONVERT_TZ(cut_at, 'UTC','<name of time zone>'))")
.count

参见 https://en.wikipedia.org/wiki/List_of_tz_database_time_zones获取数据库时区列表。

对于 .where('cut_at > ?', start_date.beginning_of_day) 语句,请确保您在 Rails 中正确设置了时区:

application.rb中:

config.time_zone  = '<name of time zone>'

关于ruby-on-rails - rails 4 : GROUP BY date with time zone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36587974/

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