lib/timing/time_in_zone.rb in timing-0.0.3 vs lib/timing/time_in_zone.rb in timing-0.0.4

- old
+ new

@@ -22,15 +22,18 @@ alias_method :utc_offset, :zone_offset alias_method :gmt_offset, :zone_offset alias_method :gmtoff, :zone_offset def +(seconds) + raise ArgumentError, "#{seconds} must be a valid seconds count" unless seconds.is_a? Numeric self.class.new (time + seconds), zone_offset end def -(seconds) - self.class.new (time - seconds), zone_offset + raise ArgumentError, "#{seconds} must be a time or a valid seconds count" unless seconds.respond_to? :to_f + result = self.class.at (time.to_f - seconds.to_f), zone_offset + seconds.is_a?(Numeric) ? result : result.to_f end def utc? zone_offset == 0 end @@ -54,9 +57,21 @@ end alias_method :inspect, :to_s def strftime(format) time_with_offset.strftime format.gsub('%Z', '').gsub('%z', zone_offset.to_s) + end + + def iso8601 + strftime "%FT%T#{zone_offset.iso8601}" + end + + def as_json(*args) + iso8601 + end + + def to_json(*args) + "\"#{as_json(*args)}\"" end %w(hour day week month year).each do |interval| beginning_method_name = "beginning_of_#{interval}" define_method beginning_method_name do \ No newline at end of file