Forum: Ruby on Rails Calculating the Date of the Start of Week

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tyler K. (Guest)
on 2009-05-21 03:55
I'm creating a calendar of sorts which maps events.

For each event, I know its start and end date/time.  I also know the
current date and time.  What I need to be able to do is calculate the
start of the week, regardless of the day of the week.

So, if it is Wednesday and I want to populate my Calendar from Monday -
Sunday with the events, I need to know the date of Monday to start.  To
compare absolute days I've converted them to Ruby epoch with the idea of
comparing each day (84600 seconds) versus the end date.  If the day is
greater than the end date, I then check the hours of the day to see on
what hour the event occurred.

It sounds convoluted, but I'm not sure of a better method.

Code:

This is wrong in that I'm using Date.today.to_time.to_i, rather than the
date of the Monday of the week.

<% for j in 0..6 do %>
                <%# check date first if we are past day, then check
hour%>
                <% for i in 0..23 do %>
                        <th><%=h i %>:00</th>

                        <% if (j * 86400) + Date.today.to_time.to_i >
(@lab.end_date.to_time.to_i + ((@lab.end_time.strftime("%M").to_i) *
3600)
                                + ((@lab.end_time.strftime("%S").to_i) *
60))  %>
                                <tr>
                                <% if i >
@lab.end_time.strftime("%M").to_i # because db stores the hour in minute
field (as pulled by ruby) use %M %>
                                        <th> Available </th>
                                <% else %>
                                        <th> Checked Out </th>
                                <% end %>
                        <% end %>
                <% else %>
                                <th> Checked Out </th>
                <% end %>
        <% end %>
Tyler K. (Guest)
on 2009-05-21 04:29
Tyler K. wrote:
> I'm creating a calendar of sorts which maps events.
>
> For each event, I know its start and end date/time.  I also know the
> current date and time.  What I need to be able to do is calculate the
> start of the week, regardless of the day of the week.
>
> So, if it is Wednesday and I want to populate my Calendar from Monday -
> Sunday with the events, I need to know the date of Monday to start.  To
> compare absolute days I've converted them to Ruby epoch with the idea of
> comparing each day (84600 seconds) versus the end date.  If the day is
> greater than the end date, I then check the hours of the day to see on
> what hour the event occurred.

The code posted is wrong (pulled from a backup file), but I think I see
a way to address the problem.  If I know the current day of the week, I
can find Monday and subtract off for each day of the week past Monday.
I just needed to walk away from the problem for a bit to figure it out.
:)
Jeff B. (Guest)
on 2009-05-21 04:53
(Received via mailing list)
Not sure if this is what you're looking for, but if you wanted to find
the prev Mon and next Sun for some given date/time for use in querying/
comparing, you could do something like:

$ irb
> t_now = Time.now
=> Wed May 20 17:50:56 -0700 2009

> t_prev_mon_begin = t_now - (t_now.wday-1)*24*60*60 - t_now.hour*60*60 - t_now.min*60 - 
t_now.sec
=> Mon May 18 00:00:00 -0700 2009

> t_next_sun_end = t_prev_mon_begin + 7*24*60*60 - 1
=> Sun May 24 23:59:59 -0700 2009

Jeff

On May 20, 4:55 pm, Tyler K. <removed_email_address@domain.invalid>
Philip H. (Guest)
on 2009-05-21 05:27
(Received via mailing list)
On May 20, 2009, at 4:55 PM, Tyler K. wrote:

> To
> compare absolute days I've converted them to Ruby epoch with the
> idea of
> comparing each day (84600 seconds) versus the end date.  If the day is
> greater than the end date, I then check the hours of the day to see on
> what hour the event occurred.
>

Why won't this work?

 >> t = Time.now
=> Wed May 20 18:23:37 -0700 2009
 >> t.beginning_of_week
=> Mon May 18 00:00:00 -0700 2009

There are all kinds of fun methods in the Time class...

 >> t.beginning_of_day
=> Wed May 20 00:00:00 -0700 2009
 >> t.end_of_day
=> Wed May 20 23:59:59 -0700 2009
 >> t.tomorrow
=> Thu May 21 18:23:37 -0700 2009
 >> t.yesterday
=> Tue May 19 18:23:37 -0700 2009
 >> t.beginning_of_month
=> Fri May 01 00:00:00 -0700 2009
 >> t.last_month
=> Mon Apr 20 18:23:37 -0700 2009

The one thing to double check is that at least at one point the
"month" methods were 30-day based...

-philip
Rob B. (Guest)
on 2009-05-21 08:09
(Received via mailing list)
You can look at the next_week method

http://www.railsbrain.com/api/rails-2.3.2/doc/inde...

and then back off a week. You could also take the code and create your
own method like the beginning_of_week if it doesn't already do exactly
what you want.

-Rob


On May 20, 2009, at 9:26 PM, Philip H. wrote:

>>
>> what hour the event occurred.
>
>>> t.last_month
>> It sounds convoluted, but I'm not sure of a better method.
>>               <% for i in 0..23 do %>
>> @lab.end_time.strftime("%M").to_i # because db stores the hour in
>>       <% end %>
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>>
>
>
> >

Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
+1 513-295-4739
Skype:  rob.biedenharn
Rick D. (Guest)
on 2009-05-21 18:23
(Received via mailing list)
On Thu, May 21, 2009 at 12:08 AM, Rob B.
<removed_email_address@domain.invalid> wrote:
>
> You can look at the next_week method
>
> http://www.railsbrain.com/api/rails-2.3.2/doc/inde...
>
> and then back off a week. You could also take the code and create your
> own method like the beginning_of_week if it doesn't already do exactly
> what you want.

Well since this is the Rails forum and not the Ruby forum,
Time#beginning_of_week is already defined in ActiveSupport.

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
Rob B. (Guest)
on 2009-05-21 20:34
(Received via mailing list)
On May 21, 2009, at 10:22 AM, Rick DeNatale wrote:
>> exactly
> WWR: http://www.workingwithrails.com/person/9021-rick-denatale
> LinkedIn: http://www.linkedin.com/in/rickdenatale


I guess I didn't make my point very well. (Even I can't see it from
what I actually *wrote*.)

The Time#next_week in ActiveSupport lets you specify the day that
starts the week while Time#beginning_of_week always goes back to
Monday. My reference to Time#beginning_of_week was perhaps a bit too
implicit and I suppose I was relying on the clairvoyance of the OP for
the point about choosing when the "week" begins.

-Rob

Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
This topic is locked and can not be replied to.