Can you take an input line instead of an object? If you pass in a DateTime object, then its counter counter is intuitive, because the presentation time of the object is not the actual time you are looking for. This will meet my expectations if you either pass in the correct, absolute DateTime, or in the string itself. The actual meat of the problem is fully handled by ActiveSupport, which I think you are using since you flagged this question with Rails. What does it look like?
def range_for(input, tz=nil) if tz.is_a?(String) tz = ActiveSupport::TimeZone.new(tz) else tz = Time.zone end if input.acts_like?(:date) || input.acts_like?(:time) d = input.in_time_zone(tz) else d = tz.parse(input) end return d.beginning_of_day..d.end_of_day end
Take a look:
ruby-1.9.2-p0 > range_for('2011-01-15', 'Alaska') => Sat, 15 Jan 2011 00:00:00 AKST -09:00..Sat, 15 Jan 2011 23:59:59 AKST -09:00 ruby-1.9.2-p0 > range_for(Time.zone.now) => Mon, 10 Jan 2011 00:00:00 EST -05:00..Mon, 10 Jan 2011 23:59:59 EST -05:00 ruby-1.9.2-p0 > range_for('2011-01-15', 'EST') => Sat, 15 Jan 2011 00:00:00 EST -05:00..Sat, 15 Jan 2011 23:59:59 EST -05:00
source share