Find Weekdays range between two

Dear Friends,

I have values in table for duties assigned on certain days ranges, e.g
Monday to Tuesday, Wednesday to Friday.

Now I want to show the duties assigned to a person on main screen if the
day of current date is in the range of weekdays assigned to him/her.

I tried the below but no idea.

def show_all_rosters
@dw = Time.now.strftime('%w')
day = Proc.new { |d| Date::DAYNAMES[d] }
@dw1 = day.call(6)
week_days = ["sunday", "monday", "tuesday", "wednesday", "thursday",
"friday", "saturday"]
@ww = week_days[1]
@dd1 = Date.today
@dd2 = Date.tomorrow
@dd3 = Date.today + 1
@theday =Time.now.strftime('%w').downcase
@rosterduties = RosterDuty.find(:all,:conditions=>["lower(duty_day1) =
:theday or lower(duty_day2)=:theday",{:theday => @theday}])
end

Thanks in advance.

Dear Friends,

I have values in table for duties assigned on certain days ranges, e.g
Monday to Tuesday, Wednesday to Friday.

Now I want to show the duties assigned to a person on main screen if the
day of current date is in the range of weekdays assigned to him/her.

I tried the below but no idea.

In order to write s/w you need to be able to debug your code to see
why it is not working. An easy way in rails is just to insert code to
log results to log/development.log, so you could insert into your code
something like:

def show_all_rosters
@dw = Time.now.strftime('%w')

logger.info "@dw: #{@dw.inspect}"

day = Proc.new { |d| Date::DAYNAMES[d] }

logger.info "day: #{day}"

and so on to see what is not working and fix it. If you don't do that
then you will never know why the code is not doing what you want.

After you have done that and either got it working or hit a brick wall
then come back and ask if there is a better way.

Colin

and so on to see what is not working and fix it. If you don't do that
then you will never know why the code is not doing what you want.

After you have done that and either got it working or hit a brick wall
then come back and ask if there is a better way.

Colin

Sorry for not making you understand the problem due to my English skill,
dear all these lines works, but I dont know how to do the following:

e.g A person is assigned to work from Monday to Wednesday of every month

and its Tuesday suppose or Monday or Wed, Computer should show that He
has to do the work today.

hope u will get it now.

What are you storing in the database to indicate the working days?
Tell us the field names and what the fields contain.

Colin

and so on to see what is not working and fix it. If you don't do that
then you will never know why the code is not doing what you want.

After you have done that and either got it working or hit a brick wall
then come back and ask if there is a better way.

Colin

Sorry for not making you understand the problem due to my English skill,
dear all these lines works, but I dont know how to do the following:

e.g A person is assigned to work from Monday to Wednesday of every month

and its Tuesday suppose or Monday or Wed, Computer should show that He
has to do the work today.

hope u will get it now.

What are you storing in the database to indicate the working days?
Tell us the field names and what the fields contain.

Also how do you record which RosterDuty records relate to a particular person?

Colin

Also how do you record which RosterDuty records relate to a particular
person?

Colin

My field names are as below:

id
Employee_id
duty_name
duty_day1 //start day
duty_day2 //end day
duty_time

Also how do you record which RosterDuty records relate to a particular
person?

Colin

My field names are as below:

id
Employee_id
duty_name
duty_day1 //start day
duty_day2 //end day
duty_time

What is saved in duty_dayn? Day as string or as number (0…7)?

Colin Law wrote in post #1182172:

Colin Law wrote in post #1182172:

Employee_id
duty_name
duty_day1 //start day
duty_day2 //end day
duty_time

What is saved in duty_dayn? Day as string or as number (0..7)?

Its, Monday, Wednesday etc. as string

I suggest not doing that, store it as a number and convert when you
want to display or enter it. That will make querying the database
much simpler. Then you can get all the duties for an employee for
today by something like

today = Time.now.wday
@employee.roster_duties.where( "duty_day1 >= ? and duty_day2<=?", today, today)

In fact it may not be quite that simple. It will not cope with a duty
running from Saturday to Sunday for example, so you may have to extend
it for that, possibly a where clause something like
"(duty_day1 <= duty_day2 and duty_day1 <= ? and duty_day2 >= ?) or
(duty_day1 > duty_day2 and (duty_day1 <= ? or duty_day2 >= ?))",
today, today, today, today
I have not tested that so convince yourself it is right before trying
it. Don't forget to provide automated tests to check all the edge
conditions.

Is it something like that you are looking for?

Colin

"(duty_day1 <= duty_day2 and duty_day1 <= ? and duty_day2 >= ?) or
(duty_day1 > duty_day2 and (duty_day1 <= ? or duty_day2 >= ?))",
today, today, today, today
I have not tested that so convince yourself it is right before trying
it. Don't forget to provide automated tests to check all the edge
conditions.

Is it something like that you are looking for?

Colin

let me try this then will tell u what happened?

Colin

Dear I changed the Days from names to numbers, in my Add_rosters its
like below now:

<%= a.select :duty_day1, [[ "Monday","1"],
["Tuesday","2"],["Wednesday","3"],["Thursday","4"],["Friday","5"],["Saturday","6"],["Sunday","0"]],:selected
=> ["Monday","1"] %>

Change the controller's method to this:

def show_all_rosters
@theday =Time.now.strftime('%A').downcase
@rosterduties = RosterDuty.find(:all,:conditions=>["duty_day1 >= :theday
and duty_day2 <=:theday",{:theday => @theday}])
end

and show_all_rosters view is this:

<div id="page-yield">
<% if @rosterduties.present? %>
  <% @rosterduties.each do |r| %>
  <%= r.duty_name %> <br />
  <%= r.duty_place %> <br />
  <%= r.duty_time %> <br />
  <%= r.duty_day1 %> <br />
  <%= r.duty_day2 %> <p>
  <%end%>
<%end%>
</div>

But the result is blank.

Colin

Dear I changed the Days from names to numbers, in my Add_rosters its
like below now:

<%= a.select :duty_day1, [[ "Monday","1"],
["Tuesday","2"],["Wednesday","3"],["Thursday","4"],["Friday","5"],["Saturday","6"],["Sunday","0"]],:selected
=> ["Monday","1"] %>

Change the controller's method to this:

def show_all_rosters
@theday =Time.now.strftime('%A').downcase

Have you tried inserting logger.info lines as I suggested previously
to see what is happening. What is the value of @theday

@rosterduties = RosterDuty.find(:all,:conditions=>["duty_day1 >= :theday
and duty_day2 <=:theday",{:theday => @theday}])

Why are you not using Roster.Duty.where(...)?

Colin

Why are you not using Roster.Duty.where(...)?

Colin

Thanks dear it worked, I just changed

@theday =Time.now.strftime('%A').downcase

to this
@theday =Time.now.strftime('%w')

and the condition line as you suggested to this

@rosterduties = RosterDuty.find(:all, :conditions => ["(duty_day1 <=
duty_day2 and duty_day1 <= ? and duty_day2 >= ?) or (duty_day1 >
duty_day2 and (duty_day1 <= ? or duty_day2 >= ?))",@theday, @theday,
@theday, @theday])

thanks again.

Does Time.now.wday not work?

One thing to watch out for is time zones. If the user and the rails
server are both set to the same timezone then you should be ok, but if
they are using different timezones then time.now (which runs on the
server) will not show the users time, so the time (for the user) at
which the current day changes from, for example, sunday to monday,
will not be midnight.

Colin