Guest User

Untitled

a guest
Feb 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. # Returns a new Time representing the "start" of this week (Monday, 0:00)
  2. def beginning_of_week
  3. days_to_monday = self.wday!=0 ? self.wday-1 : 6
  4. (self - days_to_monday.days).midnight
  5. end
  6. alias :monday :beginning_of_week
  7. alias :at_beginning_of_week :beginning_of_week
  8.  
  9. # Returns a new Time representing the start of the given day in next week (default is Monday).
  10. def next_week(day = :monday)
  11. days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
  12. # Adjust in case of switches to or from daylight savings time
  13. week_from_today = self.since(1.week) + (self.since(1.week) <=> self).hour
  14. week_from_today.beginning_of_week.since(days_into_week[day].day).change(:hour => 0)
  15. end
Add Comment
Please, Sign In to add comment