Cyber
Cyber

Reputation: 5000

Rails 3 :How to compare Datetime.

In my Rails application I have a User model ,Department model, Group model and a Register model.User model has basic user information,

User Model:

id , name
has_and_belongs_to_many :departments , :groups

Department Model:

id,name
has_and_belongs_to_many :users
has_many :registers

Group Model:

id,name
has_and_belongs_to_many :users
has_many :registers

Register Model:

date ,department_id , group_id , one , two , three
belongs_to :department ,:group

Among the Register Model "one" , "two" , "three" are time slots say: 9-12 , 12-3 , 3-6.Every day each user has to mark attendance so that their attendance has to mark against the time slot in Register table. How to mark attendance based on current time with the time slot.

Thanks in advance.

Upvotes: 0

Views: 166

Answers (3)

Michael Durrant
Michael Durrant

Reputation: 96454

You may need to do

current_time = Time.now
if (9..11).include?(current_time.hour)
# Do A
elsif (12..14).include?(current_time.hour)
# Do B
elsif (15..18).include?(current_time.hour)
# Do C
end

(or)

current_time = Time.now
if (9...12).include?(current_time.hour)
# Do A
elsif (12...15).include?(current_time.hour)
# Do B
elsif (15...18).include?(current_time.hour)
# Do C
end
# I think of the extra "." as 'eating' the last value so you don't have it.

to deal with the overlaps

Upvotes: 1

shweta
shweta

Reputation: 8169

try this

current_time = Time.now
if (9..12).include?(current_time.hour)
# 9-12
elsif (12..15).include?(current_time.hour)
# 12-3
elsif (15..18).include?(current_time.hour)
# 3-6
end

Upvotes: 1

seand
seand

Reputation: 5286

Might consider creating a bitmap. A 32 bit integer is lightweight and gives you 0-24, one for each hour. Write some functions that can compare, set,... hour ranges.

Upvotes: 0

Related Questions