34

我需要编写一个方法来检查是否Time.now在商店的营业时间和关闭时间之间。

营业时间和关闭时间保存为时间对象,但我无法正确比较它,因为商店保存了营业时间,2012/2/2因此营业时间将类似于:

2012-02-02 02:30:00 UTC

并且Time.now将是:

07:23 +0200

我怎样才能比较没有日期部分的时间部分?

4

8 回答 8

38

您可以比较Time没有日期的部分,例如,如下所示:

time1.utc.strftime( "%H%M%S%N" ) <= time2.utc.strftime( "%H%M%S%N" )
于 2014-01-24T13:58:29.530 回答
4

有一个不错的库https://github.com/bokmann/business_time可以为您做这些以及更多。

BusinessTime::Config.with(beginning_of_workday: "8:30 am", end_of_workday: "5:30 pm") do
  Time.now.during_business_hours?
end

它将为您做更多的事情,例如将时间滚动到下一个或上一个开放时间,计算两个时间戳之间的营业时间等。

于 2018-10-05T11:28:29.010 回答
3

您可以只比较没有日期部分的rails中的时间,例如:---这post_review是表格,我们只得到这些记录,这些记录post_reviewcreated_at任何日期的上午10点---下午5点之间

post_review.where("(created_at::time >= :start_time) AND (created_at::time <= :end_time)", 
    start_time: Time.parse("10 am").strftime("%r"),
    end_time:   Time.parse("5 pm").strftime("%r")
 )
于 2015-04-06T09:57:52.187 回答
3

您可以将时间拆分为小时、分钟和秒。

时间类中所述:

t = Time.now
hour = t.hour
minute = t.min
seconds = t.sec

由于您只需要比较它是否在 2 小时内,您可以按如下方式进行检查。

if hour > openingHour and hour < closingHour
于 2013-01-15T00:25:55.193 回答
2

尝试将时间转换为数字并去掉天数。由于时间表示为自 UNIX 纪元以来的秒数,小数点是秒的小数部分,因此您可以将此数字转换为天数,小数部分是一天的小数部分。

基于日期的数字 = Ruby 时间数字 / 60 / 60 / 24

然后,您可以使用取模运算符去除日期部分,这样您就剩下要比较的就是时间了。所以你想要这样的东西:

def is_open?(time)
  open_h=Time.parse('2012-02-02 02:30:00 UTC')
  close_h=Time.parse('2012-02-02 10:00:00 UTC')
  (((time.to_r / 60 / 60 / 24) % 1) >= ((open_h.to_r / 60 / 60 / 24) % 1)) && (((time.to_r / 60 / 60 / 24) % 1) <= ((close_h.to_r / 60 / 60 / 24) % 1))
end

is_open? (Time.parse('2013-01-01 09:58:00 UTC'))
=> true
is_open? (Time.parse('2013-01-01 12:58:00 UTC'))
=> false
于 2015-02-16T16:39:05.093 回答
2

Instead of trying to compare the point of time directly, you can compare their offsets from a common reference (e.g. midnight). You might need to make sure all times are using the same time zone, depending on your use case.

In Rails, this can be done easily with one of the helpers such as #seconds_since_midnight:

#given
opening_hour = DateTime.new(2012,2,2,2,30,0)

#compare
now = DateTime.now.in_time_zone('UTC')
opening_hour_since_midnight = opening_hour.seconds_since_midnight
now_since_midnight = now.seconds_since_midnight
p 'shop opened' if now_since_midnight > opening_hour_since_midnight
于 2018-10-04T22:23:27.010 回答
-1
close_or_open_time_object.to_a.first(3).reverse <=> Time.now.to_a.first(3).reverse
于 2013-01-15T00:27:06.543 回答
-6

这仅适用于 24 小时格式且开始时间小于结束时间的时间。

Time start = DateUtil.convertStringToTime(Object.getStartTime());
Time mid = DateUtil.convertStringToTime(time);
Time end = DateUtil.convertStringToTime(Object.getEndTime());

    if(mid.getHours()>start.getHours() && mid.getHours()< end.getHours())
    {
        flag=true;
    }
    else if(mid.getHours() == start.getHours() && mid.getHours() < end.getHours())
    {
        if(mid.getMinutes() > start.getMinutes())
        {               
            flag=true;              
        }
        else if(mid.getMinutes() == start.getMinutes())
        {               
            if(mid.getSeconds() >= start.getSeconds())
            {
                flag=true;
            }
        }
    }
    else if(mid.getHours() > start.getHours() && mid.getHours() == end.getHours())
    {
        if(mid.getMinutes() < end.getMinutes())
        {
            flag=true;
        }
        else if(mid.getMinutes() == end.getMinutes())
        {
            if(mid.getSeconds() <= end.getSeconds())
            {
                flag=true;
            }
        }
    }
    else if(mid.getHours() == start.getHours() && mid.getHours() == end.getHours())
    {
        if(mid.getMinutes() > start.getMinutes() && mid.getMinutes() < end.getMinutes())
        {
            flag=true;
        }           
        else if(mid.getMinutes() == start.getMinutes() && mid.getMinutes() < end.getMinutes())
        {
            if(mid.getSeconds() > start.getSeconds())
            {
                flag=true;
            }
        }
        else if(mid.getMinutes() > start.getMinutes() && mid.getMinutes() == end.getMinutes())
        {
            if(mid.getSeconds() < end.getSeconds())
            {
                flag=true;
            }
        }
        else if(mid.getMinutes() == start.getMinutes() && mid.getMinutes() == end.getMinutes())
        {
            if(mid.getSeconds() > start.getSeconds() && mid.getSeconds() < end.getSeconds())
            {
                flag=true;
            }
        }
    }
于 2015-11-21T09:30:46.300 回答