How to compare only the time with Rails (from what time to what time, something like)

Background of the problem

How do you write something like that, which is open late at night and increases by 15% from 23:00 to 5:00 the next day? The story. I want to make a good judgment on the condition of "23:00 to 5:00 the next day".

Solution

If you try to compare by Time type, it is troublesome to enter the date information, so you can compare after digitizing only the time part with strftime and to_i.

def late_time?(time)
  num = time.strftime('%H%M').to_i
  [*0..459, *2300..2359].include?(num)
end

target_time = Time.zone.parse('2020/9/1 23:00')
p late_time?(target_time) #=> true

target_time = Time.zone.parse('2020/9/2 0:00')
p late_time?(target_time) #=> true

target_time = Time.zone.parse('2020/9/2 4:59')
p late_time?(target_time) #=> true

target_time = Time.zone.parse('2020/9/2 5:00')
p late_time?(target_time) #=> false

target_time = Time.zone.parse('2020/9/2 6:00')
p late_time?(target_time) #=> false

target_time = Time.zone.parse('2020/9/2 22:59')
p late_time?(target_time) #=> false

Recommended Posts

How to compare only the time with Rails (from what time to what time, something like)
How to convert param to hash with Rails controller (updated from time to time)
[Rails] How to read the XML file uploaded from the screen with Hash type
How to set the display time to Japan time in Rails
Uppercase only the specified range with substring. (How to use substring)
[With back tricks] How to introduce React to the simplest Rails
How to get along with Rails
[Rails] How to introduce kaminari with Slim and change the design
[Java] How to compare with equals method
[Rails] How to use the map method
[Java] How to set the Date time to 00:00:00
How to build Rails 6 environment with Docker
How to decorate the radio button of rails6 form_with (helper) with CSS
How to debug the processing in the Ruby on Rails model only on the console
How to display the text entered in text_area in Rails with line breaks
[Rails] How to apply the CSS used in the main app with Administrate
[Rails] How to solve the time lag of created_at after save method
[Ruby on Rails] How to log in with only your name and password using the gem devise
[Rails] How to decide the destination by "rails routes"
[Rails] I tried to raise the Rails version from 5.0 to 5.2
How to link Rails6 Vue (from environment construction)
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
[Rails] How to register multiple records in the intermediate table with many-to-many association
[Swift] How to link the app with Firebase
How to implement a like feature in Rails
I want to play with Firestore from Rails
Implement the Like feature in Ajax with Rails.
[Rails] How to operate the helper method used in the main application with Administrate
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails
I want to get only the time from Time type data ...! [Strftime] * Additional notes
[Rails] How to build an environment with Docker
Unify the Rails app time zone to Japan time
Try to summarize the common layout with rails
How to check Rails commands in the terminal
[Rails] How to convert UC time display to Japanese time display
How to delete the tweet associated with the user when you delete it at the same time
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
How to delete child elements associated with a parent element at the same time
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
How to implement UICollectionView in Swift with code only
How to change the action with multiple submit buttons
How to make batch processing with Rails + Heroku configuration
What to do if the Rails server can't start
How to implement a like feature in Ajax in Rails
How to push an app developed with Rails to Github
How to remove the underline displayed by Rails link_to
How to delete a new_record object built with Rails
[Java] How to extract the file name from the path
How to make an almost static page with rails
[Ruby on Rails] How to change the column name
How to manually generate a JWT with Rails Knock
[Rails] I don't know how to use the model ...
[Rails] How to change the column name of the table
Organized how to interact with the JDK in stages
How to output standard from an array with forEach
[How to insert a video in haml with Rails]
[Rails] How to deal with URL changes after render
How to download images from AWS S3 (rails, carrierwave)
How to query Array in jsonb with Rails + postgres