Skip to content

Instantly share code, notes, and snippets.

@batasrki
Created February 27, 2009 19:56
Show Gist options
  • Select an option

  • Save batasrki/71663 to your computer and use it in GitHub Desktop.

Select an option

Save batasrki/71663 to your computer and use it in GitHub Desktop.
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module Date #:nodoc:
# Enables the use of time calculations within Time itself
module Calculations
# Returns a new Date/DateTime representing the "start" of this week (i.e, Monday; DateTime objects will have time set to 0:00)
def beginning_of_week
days_to_monday = self.wday!=0 ? self.wday-1 : 6
result = self - days_to_monday
self.acts_like?(:time) ? result.midnight : result
end
alias :monday :beginning_of_week
alias :at_beginning_of_week :beginning_of_week
# Returns a new Date/DateTime representing the end of this week (Sunday, DateTime objects will have time set to 23:59:59)
def end_of_week
days_to_sunday = self.wday!=0 ? 7-self.wday : 0
result = self + days_to_sunday.days
self.acts_like?(:time) ? result.end_of_day : result
end
alias :at_end_of_week :end_of_week
end
end
end
end
#second line of the function
def end_of_week
days_to_sunday = self.wday!=0 ? 7-self.wday : 0
result = self + days_to_sunday #removed .days since it's not really needed; makes the code confusing
self.acts_like?(:time) ? result.end_of_day : result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment