Agus Suparman
Agus Suparman

Reputation: 1

How to make working hours in ruby ​on rails?

I'm creating a chat panel with a bot. Every message sent by the bot will depend on working hours. For example, during business hours the customer sends a message to the bot and the bot will reply to the text message with the sentence: Hello, you sent a message during business hours and for example when outside working hours the customer sends a text message to the bot and the bot will reply to the message with the sentence: Hello, you sent a message outside of working hours. Where working hours apply every Monday-Saturday at 08: 30-17: 00.

I made a configuration using an array like this:

start_on =  [
  "Monday, 08:30:00",
  "Tuesday, 08:30:00",
  "Wednesday, 08:30:00",
  "Thursday, 08:30:00",
  "Friday, 08:30:00",
  "Saturday, 08:30:00"
]
end_on =  [
  "Monday, 17:00:00",
  "Tuesday, 17:00:00",
  "Wednesday, 17:00:00",
  "Thursday, 17:00:00",
  "Friday, 17:00:00",
  "Saturday, 17:00:00"
]

And what I want to ask is how to set the current time current_time = (Time.now.to_time) by configuring the working hours in the array start_on and end_on using ruby ​​on rails?

Upvotes: 0

Views: 971

Answers (3)

Fernand
Fernand

Reputation: 1333

To convert the values in DateTime, you have to strptime the weekday for Date, and the time for the Time. Then combine the resulting date and time to obtain the DateTime.

curr_index=1 #loop index if necessary

#get start datetime
arr=start_on[curr_index].split(",")
d = Date.strptime(arr[0].strip, '%A')
t = Time.strptime(arr[1].strip, ' %T')
startime = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec, t.zone)

#get end datetime
arr=end_on[curr_index].split(",")
d = Date.strptime(arr[0].strip, '%A')
t = Time.strptime(arr[1].strip, ' %T')
endtime = DateTime.new(d.year, d.month, d.day, t.hour, t.min, t.sec, t.zone)

return (startime..endtime).cover? Time.now #check if within the range

Upvotes: 0

Luskmo
Luskmo

Reputation: 341

This function returns true if the day is not sunday and the time is between 08:30 and 17:00

  def is_working_hour?(time)
   !time.sunday? && time.to_s(:time).between?('08:30','17:00')
  end

 is_working_hour?(Time.now)

Upvotes: 4

Steve Smith
Steve Smith

Reputation: 5201

I would personally structure the start and end times like this using wday as the day of the week (0 is sunday so 1 == monday):

times = [
  { day: 1, start: "08:30:00", end: "17:00:00" },
  { day: 2, start: "08:30:00", end: "17:00:00" },
  ...
]

Once you've got a structure like this (you can work it work it out from the above too of course), you can use the following to get today's times.

today = times.detect { |time| time[:day] == Time.now.wday }

Then start and end times are just:

start_time = Time.parse(today[:start])
end_time = Time.parse(today[:end])
# start_time = Time.parse("08:30:00")
# end_time = Time.parse("17:00:00")

Then it's simply a matter of seeing if the current time is inside that range:

 Time.now > start && Time.now < end_time
=> true

Upvotes: 0

Related Questions