Skip to content

Instantly share code, notes, and snippets.

@epall
Created November 22, 2011 22:36
Show Gist options
  • Save epall/1387274 to your computer and use it in GitHub Desktop.
Save epall/1387274 to your computer and use it in GitHub Desktop.
overwrite_accessor :date= do |date|
# We want to end up with a Time object with one-day resolution.
# This is the cleanest, most-readable, faster-than-using-Date
# way to do it.
case date
when String then
@date = if /\A(\d{4})-(\d{2})-(\d{2})\Z/ =~ date then
Time.local($1.to_i, $2.to_i, $3.to_i)
else
require 'time'
Time.parse date
end
when Time then
@date = Time.local(date.year, date.month, date.day)
when Date then
@date = Time.local(date.year, date.month, date.day)
else
@date = TODAY
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment