Find out if current time is between two times

时间:2021-07-17 21:32:51

I have two time columns stored in a Postgresql database: open_time and close_time. I'm trying to find out if the current time, ignoring the date, is between the two times, ignoring the dates.

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

This code compares the dates as well as the time:

此代码比较日期和时间:

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

It doesn't work, for example, when current_time # => 2018-06-06 23:59:49 -0600 and open_time # => 2000-01-01 22:59:00 UTC.

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

How do I get it to not include the dates, and just compare the times?

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

4 个解决方案

#1


4  

Perhaps you want something like this:

也许你想要这样的东西:

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

or

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

#2


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

As I write, the time is now about 32 minutes past midnight.

在我写的时候,时间现在是午夜过了约32分钟。

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

Suppose

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)>

Then

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

Now suppose the open time is the same, but the close time is later.

现在假设开放时间相同,但关闭时间稍晚。

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

Then

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

#3


2  

You could CAST() your datetime to time by using,

你可以使用CAST()你的日期时间,

cast(tbl_store.open_time as time) as SomeVariable

cast(tbl_store.close_time as time) as SomeOtherVariable

That would give you the time only instead of the full datetime value that you had to begin with, which is what you wanted.

那只会给你时间而不是你必须开始的完整日期时间值,这就是你想要的。

You can then use the same logic with your curtime() between to the get value that you were looking for.

然后,您可以在curtime()之间使用与您要查找的get值相同的逻辑。

Example:

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

工作SQL小提琴

You can change the schema build in the fiddle to test the datetime values you desire.

您可以更改小提琴中的架构构建以测试所需的日期时间值。

Note that if you ever have a logic that will include midnight time, you will have to make a CASE WHEN logic against that, else it will fail and return 0, whereas it should return 1.

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

#4


0  

You can take advantage of ranges and how numeric strings are compared

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

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

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

Then we could use

然后我们可以使用

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

#1


4  

Perhaps you want something like this:

也许你想要这样的东西:

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

or

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

#2


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

As I write, the time is now about 32 minutes past midnight.

在我写的时候,时间现在是午夜过了约32分钟。

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

Suppose

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)>

Then

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

Now suppose the open time is the same, but the close time is later.

现在假设开放时间相同,但关闭时间稍晚。

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

Then

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

#3


2  

You could CAST() your datetime to time by using,

你可以使用CAST()你的日期时间,

cast(tbl_store.open_time as time) as SomeVariable

cast(tbl_store.close_time as time) as SomeOtherVariable

That would give you the time only instead of the full datetime value that you had to begin with, which is what you wanted.

那只会给你时间而不是你必须开始的完整日期时间值,这就是你想要的。

You can then use the same logic with your curtime() between to the get value that you were looking for.

然后,您可以在curtime()之间使用与您要查找的get值相同的逻辑。

Example:

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

工作SQL小提琴

You can change the schema build in the fiddle to test the datetime values you desire.

您可以更改小提琴中的架构构建以测试所需的日期时间值。

Note that if you ever have a logic that will include midnight time, you will have to make a CASE WHEN logic against that, else it will fail and return 0, whereas it should return 1.

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

#4


0  

You can take advantage of ranges and how numeric strings are compared

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

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

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

Then we could use

然后我们可以使用

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