Find Weekdays range between two

16 views
Skip to first unread message

Naveed Alam

unread,
Mar 13, 2016, 5:54:05 AM3/13/16
to rubyonra...@googlegroups.com
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.

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Mar 13, 2016, 6:28:30 AM3/13/16
to Ruby on Rails: Talk
On 13 March 2016 at 09:53, Naveed Alam <li...@ruby-forum.com> wrote:
> 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

Naveed Alam

unread,
Mar 13, 2016, 6:49:31 AM3/13/16
to rubyonra...@googlegroups.com
> 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.

Colin Law

unread,
Mar 13, 2016, 8:08:52 AM3/13/16
to Ruby on Rails: Talk
What are you storing in the database to indicate the working days?
Tell us the field names and what the fields contain.

Colin

Colin Law

unread,
Mar 13, 2016, 8:20:29 AM3/13/16
to Ruby on Rails: Talk
Also how do you record which RosterDuty records relate to a particular person?

Colin

>
> Colin

Naveed Alam

unread,
Mar 13, 2016, 10:06:52 AM3/13/16
to rubyonra...@googlegroups.com
> 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

Colin Law

unread,
Mar 13, 2016, 10:42:04 AM3/13/16
to Ruby on Rails: Talk


On 13 Mar 2016 14:06, "Naveed Alam" <li...@ruby-forum.com> wrote:
>
> > 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)?

>
> --
> Posted via http://www.ruby-forum.com/.
>

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/cb6dfa79c02e68303e0d0a89e9d6afcb%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Naveed Alam

unread,
Mar 13, 2016, 10:46:48 AM3/13/16
to rubyonra...@googlegroups.com
Colin Law wrote in post #1182172:
> On 13 Mar 2016 14:06, "Naveed Alam" <li...@ruby-forum.com> wrote:
>> 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

Colin Law

unread,
Mar 13, 2016, 11:37:47 AM3/13/16
to Ruby on Rails: Talk
On 13 March 2016 at 14:46, Naveed Alam <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1182172:
>> On 13 Mar 2016 14:06, "Naveed Alam" <li...@ruby-forum.com> wrote:
>>> 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



>
> -...@employee.roster_duties
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ae5a7e8234df5c411076ac4d4818d1c6%40ruby-forum.com.

Naveed Alam

unread,
Mar 13, 2016, 1:20:13 PM3/13/16
to rubyonra...@googlegroups.com
> "(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?

Naveed Alam

unread,
Mar 14, 2016, 7:56:52 AM3/14/16
to rubyonra...@googlegroups.com
>> 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 Law

unread,
Mar 14, 2016, 8:28:11 AM3/14/16
to Ruby on Rails: Talk
On 14 March 2016 at 11:56, Naveed Alam <li...@ruby-forum.com> wrote:
>>> 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

> 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.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/20cb5baf7c9bffdc67fd76fa81da8982%40ruby-forum.com.

Naveed Alam

unread,
Mar 16, 2016, 4:06:15 AM3/16/16
to rubyonra...@googlegroups.com
>
> 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.

Colin Law

unread,
Mar 16, 2016, 4:33:45 AM3/16/16
to Ruby on Rails: Talk
On 16 March 2016 at 08:05, Naveed Alam <li...@ruby-forum.com> wrote:
>>
>> 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')

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
Reply all
Reply to author
Forward
0 new messages