【问题标题】:Find out if current time is between two times找出当前时间是否在两次之间
【发布时间】:2018-11-16 22:35:47
【问题描述】:

我在 Postgresql 数据库中存储了两个时间列:open_timeclose_time。我试图找出忽略日期的当前时间是否在两个时间之间,忽略日期。

此代码比较日期和时间:

current_time = Time.now
if current_time.between?(store.open_time, store.close_time)
  puts "IN BETWEEN"      
end

它不起作用,例如,current_time # => 2018-06-06 23:59:49 -0600open_time # => 2000-01-01 22:59:00 UTC

如何让它不包括日期,只比较时间?

【问题讨论】:

  • 这没有意义。时间是比日期更精确的概念。如果不知道它是哪个日期,你就不可能有一个特定的时间。
  • @sawa 嗯? Postgres perfectly allows 存储没有日期的时间,而 ruby​​ 没有为它提供方便的包装器这一事实并不意味着“时间更精确”。 DateTime 是。
  • Comparing times only, without dates? 是一个类似的问题,虽然不是 Postgres 特定的。
  • @Stefan,您提供的链接上的评论表明,较早的问题实际上与 Postgres 相关,但将这个问题视为重复问题将是一种耻辱,因为我不相信任何之前的答案是正确的。
  • 我真的很感谢所有的答案!我最终从所有答案中汲取了灵感,所以我不确定要标记哪一个。

标签: ruby-on-rails ruby postgresql date time


【解决方案1】:

也许你想要这样的东西:

current_time = Time.now
open_time = store.open_time
close_time = store.close_time
current_time -= current_time.beginning_of_day
open_time -= open_time.beginning_of_day
close_time -= close_time.beginning_of_day
if current_time.between?(open_time, close_time)
  puts "IN BETWEEN"      
end

current_time = Time.now
open_time = store.open_time
close_time = store.close_time
current_time = [current_time.hour, current_time.min, current_time.sec]
open_time = [open_time.hour, open_time.min, open_time.sec]
close_time = [close_time.hour, close_time.min, close_time.sec]
if open_time <=> current_time == -1 and current_time <=> close_time == -1
  puts "IN BETWEEN"      
end

【讨论】:

  • Array 没有声明between? 你需要使用像open_time..close_time === current_time 这样的smth。
  • @mudasobwa 你是对的。我开始觉得我的回答有些不对劲。谢谢。
  • Array 实现了&lt;=&gt;,但包含Comparable!?
  • 显然 tokland 请求了这个功能,但是Matz rejected it。真可惜。
  • 考虑一下:如果商店是酒吧呢?
【解决方案2】:

你可以使用CAST()你的datetimetime

cast(tbl_store.open_time as time) as SomeVariable

cast(tbl_store.close_time as time) as SomeOtherVariable

这只会给你time,而不是你必须开始的完整datetime值,这就是你想要的。

然后,您可以对 curtime() between 使用相同的逻辑来获取您正在寻找的值。

例子:

SELECT
  CAST(tbl_store.open_time as TIME) as open_time,
  CAST(tbl_store.close_time as TIME) as close_time,
  CURTIME() BETWEEN (cast(tbl_store.open_time as TIME)) AND (cast(tbl_store.close_time as TIME)) as time_between
FROM
  tbl_store

Working SQL Fiddle

您可以更改 fiddle 中的架构构建以测试您想要的 datetime 值。

请注意,如果您有一个包含午夜时间的逻辑,则必须针对该逻辑创建 CASE WHEN 逻辑,否则它将失败并返回 0,而应该返回 1。

【讨论】:

  • 我希望我能投票两次。让数据库做数据库的事情。
【解决方案3】:

您可以利用范围以及数字字符串的比较方式

r = Range.new('09:00', '18:00')

r.include?('08:59') # => false
r.include?('09:01') # => true
r.include?('18:01') # => false

那么我们可以使用

open_hours_range = Range.new(open_time.strftime('%R'), close_time.strftime('%R'))
shop_open? = open_hours_range.include?(Time.now.strftime('%R'))

【讨论】:

    【解决方案4】:
    require 'time'
    
    TIME_FMT = "%H%M%S"
    
    def store_open_now?(open_time, close_time)
      nt = Time.now.strftime(TIME_FMT)
      ot = open_time.strftime(TIME_FMT)
      ct = close_time.strftime(TIME_FMT)
      ot <= ct ? (nt >= ot && nt <= ct) : (nt >= ot || nt <= ct)
    end
    

    在我写这篇文章的时候,现在时间大约是午夜过后 32 分钟。

    Time.now.strftime(TIME_FMT)
      #=> "003252"
    

    假设

    open_time  = DateTime.parse("09:00")
      #=> #<DateTime: 2018-06-07T09:00:00+00:00 ((2458277j,32400s,0n),
      #               +0s,2299161j)>
    close_time = DateTime.parse("17:00")
      #=> #<DateTime: 2018-06-07T17:00:00+00:00 ((2458277j,61200s,0n),
      #               +0s,2299161j)>
    

    然后

    open_time.strftime(TIME_FMT)
      #=> "090000"
    close_time.strftime(TIME_FMT)
      #=> "170000"
    store_open_now?(open_time, close_time)
      #=> false
    

    现在假设开盘时间相同,但收盘时间更晚。

    close_time = DateTime.parse("01:00")
      #=> #<DateTime: 2018-06-07T01:00:00+00:00 ((2458277j,3600s,0n),
      #               +0s,2299161j)>
    

    然后

    close_time.strftime(TIME_FMT)
      #=> "010000"
    store_open_now?(open_time, close_time)
      #=> true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 2014-01-04
      • 2017-04-27
      相关资源
      最近更新 更多