以太平洋时区显示结果,以 UTC 显示



你好,我对 Ruby on Rails 很陌生,所以请对我放轻松。我有一个 rails 应用程序,它由一个显示记录的表格组成。我有一个搜索栏,可以按日期过滤表格的结果。我的搜索栏选择标签看起来像这样

select_tag :date, options_for_select(@previous_summary_history_dates.uniq), prompt: "Search by date", id: "summary-history-date-filter", class: "form-control"

这是我在控制器中的@previous_summary_history_dates,选择标签从中获取值

@previous_summary_history_dates = SummarySheet.where(is_history: nil).pluck(:record_inserted_at).map{|a| a.strftime("%m/%d/%Y - %l:%M %p")}.uniq

我已将application.rb文件设置为此

config.time_zone = 'Pacific Time (US & Canada)'
config.active_record.default_timezone = :local

我有一个搜索栏查询,它显示表中按从选择标签中选择的日期过滤的记录,如下所示

@summary_history = SummarySheet.where(record_inserted_at: DateTime.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_beginning_of_minute..DateTime.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_end_of_minute)

此查询有效,但它为我提供了 UTC 格式的输出,该输出与我的 pct 选择标签格式不匹配,我得到的结果为 null。如果我将我的选择标签时区更改为 UTC,我会得到所需的输出。

我尝试在过滤器查询中.in_time_zone("Pacific Time (US & Canada)")以转换PCT中的日期,但这不起作用

有人可以建议我如何在PCT中保留选定的tage并触发查询并仍然获得日期过滤输出吗?提前致谢

我通过将查询更改为

@summary_history = SummarySheet.where(record_inserted_at: Time.zone.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_beginning_of_minute..Time.zone.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_end_of_minute)

最新更新