Comparing times only, without dates?

后端 未结 8 1222
不知归路
不知归路 2021-01-31 16:30

I need to write a method that will check if Time.now is in between the open hours and the close hours of a shop.

The open and close hours are saved as a Tim

相关标签:
8条回答
  • 2021-01-31 16:48

    This will work only for time being in 24 hour format and when start hour is less than end hour.

    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;
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-31 16:49

    You can compare the Time without a date part, for example, as follows:

    time1.utc.strftime( "%H%M%S%N" ) <= time2.utc.strftime( "%H%M%S%N" )
    
    0 讨论(0)
  • 2021-01-31 16:51

    There is a nice library https://github.com/bokmann/business_time which will do this and more for you.

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

    It will do much more for you, like rolling a time to next or previous opening time, counting business hours between two timestamps, etc.

    0 讨论(0)
  • 2021-01-31 16:58
    close_or_open_time_object.to_a.first(3).reverse <=> Time.now.to_a.first(3).reverse
    
    0 讨论(0)
  • 2021-01-31 17:06

    You can strip the Time into its hours, minutes and seconds.

    As described in Time Class:

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

    Since you need to just compare whether it's within 2 hours you can check it as below.

    if hour > openingHour and hour < closingHour
    
    0 讨论(0)
  • 2021-01-31 17:08

    Try converting the Time into a number and strip off the days. Since the Time is represented as a number of seconds since the UNIX Epoch with the decimal being a fraction of the second, you can convert this number to a number of days with the fraction being a fraction of a day.

    Day based number = Ruby Time Number / 60 / 60 / 24

    You can then use the modulus operator to strip the day portion so all you have left to compare is the time. So you want something like this:

    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
    
    0 讨论(0)
提交回复
热议问题