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 Knappe <[email protected]> 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. > > 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 %> > -- > Posted viahttp://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

