def self.at_beginning_of_interval(time, interval_type)
#simple substraction does not (seem) to work for long intervals (summer/winter time ?)
#and at_beginning_of_hour is not defined
if [:year, :month, :week, :day].include? interval_type
time.to_date.send("at_beginning_of_#{interval_type}")
else
time - (time.to_i % 1.send(interval_type))
end
end
Refactorings
No refactoring yet !
Matthias Vallentin
May 26, 2009, May 26, 2009 19:05, permalink
Seems like you are trying to truncate a Time object. I toyed with this as well: http://stackoverflow.com/questions/912139/truncating-ruby-time-objects-efficiently
This code was necessary since e.g. time - (time.to_i % 1.month) did not work as expected for long intervals, its a bit clumsy and i would like to replace it through something lighter (and maybe not activesupport dependent), any ideas ?