获取基于日期和时间的数据

Fetching Data based date and time

我想从今天开始寻找结果,但如果时间介于 12:00am-5:00am

之间,我还想包括昨天的计划

现在我有以下

def self.current
    where(
      "plan_date >= :today",
      today: Date.current,
    )
end

有没有一种方法可以让我根据在应用程序控制器中设置为波纹管的用户时区知道一天中的时间,并确保如果它在第二天 6:am 之前我想包括前几天的结果也是如此。

def set_time_zone(&block)
    if current_user
      Time.use_zone(current_user.time_zone_name, &block)
    else
      yield
    end
end

试试这个:

def self.current
    where(
      "plan_date >= :today",
      today: (Time.zone.now.in_time_zone(get_user_time_zone) - 6.hours).beginning_of_day,
    )
end

...其中 get_user_time_zone returns 用户的时区(例如:America/New_York)。我使用 - 6.hours 是因为你希望它是 "before 6am" 当地时间。