ruby - Create an array of users based on each user's weekday and time attributes -
i have following code creates array of users depending on "global" weekdays , times.
def current_time # server_time = time.now server_time = time.local(2013,8,19,8,30) # time simulation local_offset = (2 * 60 * 60) local_time = server_time + local_offset end def hours_mon_to_thurs?(date) ["monday", "tuesday", "wednesday", "thursday"].include?(date.strftime("%a")) && ("08:30"..."17:00").include?(date.strftime("%h:%m")) end def hours_fri?(date) ["friday"].include?(date.strftime("%a")) && ("08:30"..."15:00").include?(date.strftime("%h:%m")) end def hours_lunch?(date) !("12:00"..."13:00").include?(date.strftime("%h:%m")) end if hours_mon_to_thurs?(current_time) && hours_lunch?(current_time) p "[user1, user2]" elsif hours_fri?(current_time) && hours_lunch?(current_time) p "user2" else p "no users" end
this code works, change how array created, each user has workdays , times attributes , if available should added array. let me provide example.
user 1 - office hours
monday 08:30 - 12:00, 13:00 - 17:00
tuesday 08:30 - 12:00, 13:00 - 17:00
wednesday 08:30 - 12:00, 13:00 - 17:00
thursday 08:30 - 12:00, 13:00 - 17:00
user 2 - office hours
tuesday 08:30 - 12:00, 13:00 - 17:00
thursday 08:30 - 12:00, 13:00 - 17:00
friday 08:30 - 12:00, 13:00 - 15:00
with each user's office hours defined want have similar method in example above. this:
- for each user
- if current time within user's office hours
- add user array
- else if current time not within users' office hours
- array empty
i'm not using database @ moment , don't know best way structure each user's office hours in such way match if current time within user's time range , add them array.