lib/timerange.rb in timerange-0.0.3 vs lib/timerange.rb in timerange-0.0.4

- old
+ new

@@ -6,36 +6,45 @@ class << self attr_accessor :time_zone end def initialize(b = nil, e = Time.now, exclude_end = false, options = {}) + if !b + raise TypeError, "no implicit conversion of nil into Time" + end + if b.is_a?(Range) b, e, exclude_end = b.begin, b.end, b.exclude_end? end if b.is_a?(Hash) options, b, e, exclude_end = b, nil, nil, false elsif e.is_a?(Hash) options, e, exclude_end = e, Time.now, false end - time_zone = options[:time_zone] || TimeRange.time_zone || Time.zone || "Etc/UTC" + time_zone = options[:time_zone] || (b.respond_to?(:time_zone) && b.time_zone) || TimeRange.time_zone || Time.zone || "Etc/UTC" if time_zone.is_a?(ActiveSupport::TimeZone) or (time_zone = ActiveSupport::TimeZone[time_zone]) # do nothing else raise "Unrecognized time zone" end b = time_zone.parse(b) if b.is_a?(String) e = time_zone.parse(e) if e.is_a?(String) b = b.in_time_zone(time_zone) + e = e.in_time_zone(time_zone) if options[:duration] e = b + options[:duration] exclude_end = true end - e = e.in_time_zone(time_zone) + if options[:within] + e = b + options[:within] + b -= options[:within] + exclude_end = false + end @options = options.merge(time_zone: time_zone) super(b, e, exclude_end) end @@ -108,17 +117,17 @@ end time + day_start.hours end - def self.today - date = Date.today - new(date, date).expand(:day) + def self.today(options = {}) + date = Time.now + new(date, date, options).expand(:day) end - def self.yesterday - date = Date.yesterday - new(date, date).expand(:day) + def self.yesterday(options = {}) + date = Time.now - 1.day + new(date, date, options).expand(:day) end def +(period) self.class.new(self.begin + period, self.end + period, exclude_end?) end