4fc8066b5aeba133f665ec8f30f92a9c

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 ?

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 !

D41d8cd98f00b204e9800998ecf8427e

Matthias Vallentin

May 26, 2009, May 26, 2009 19:05, permalink

No rating. Login to rate!

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

Your refactoring





Format Copy from initial code

or Cancel