lib/et-orbi/time.rb in et-orbi-1.2.8 vs lib/et-orbi/time.rb in et-orbi-1.2.9

- old
+ new

@@ -86,26 +86,38 @@ "\nTry setting `ENV['TZ'] = 'Continent/City'` in your script " + "(see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)" + (defined?(TZInfo::Data) ? '' : "\nand adding gem 'tzinfo-data'") ) unless @zone + touch + end + + # Nullify the "caches" used by #to_time, #rweek, and others + # + def touch + @time = nil - # cache for #to_time result + @ref = nil end def seconds=(f) - @time = nil @seconds = f + + touch + + f end def zone=(z) - @time = nil - @ref = nil @zone = self.class.get_tzone(zone || :current) + + touch + + @zone end # Returns true if this EoTime instance corresponds to 2 different UTC # times. # It happens when transitioning from DST to winter time. @@ -230,12 +242,12 @@ def >=(o); @seconds >= _to_f(o); end def <(o); @seconds < _to_f(o); end def <=(o); @seconds <= _to_f(o); end def <=>(o); @seconds <=> _to_f(o); end - def add(t); @time = nil; @seconds += t.to_f; self; end - def subtract(t); @time = nil; @seconds -= t.to_f; self; end + def add(t); @seconds += t.to_f; touch; self; end + def subtract(t); @seconds -= t.to_f; touch; self; end def +(t); inc(t, 1); end def -(t); inc(t, -1); end DAY_S = 24 * 3600 @@ -301,23 +313,28 @@ strftime('%H:%M:%S.%6N') end def inc(t, dir=1) - case t - when Numeric - nt = self.dup - nt.seconds += dir * t.to_f - nt - when ::Time, ::EtOrbi::EoTime - fail ArgumentError.new( - "Cannot add #{t.class} to EoTime") if dir > 0 - @seconds + dir * t.to_f - else - fail ArgumentError.new( - "Cannot call add or subtract #{t.class} to EoTime instance") - end + r = + case t + when Numeric + nt = self.dup + nt.seconds += dir * t.to_f + nt + when ::Time, ::EtOrbi::EoTime + fail ArgumentError.new( + "Cannot add #{t.class} to EoTime") if dir > 0 + @seconds + dir * t.to_f + else + fail ArgumentError.new( + "Cannot call add or subtract #{t.class} to EoTime instance") + end + + touch + + r end def localtime(zone=nil) EoTime.new(self.to_f, zone) @@ -334,11 +351,11 @@ # "reference week", used in fugit for cron modulo notation # def rweek - @ref ||= EtOrbi.make_time('2019-01-01 00:00:00', @zone) + @ref ||= EtOrbi.make_time('2019-01-01 12:00:00', @zone) ((self - @ref) / WEEK_S).floor + 1 end # "reference week", used in fugit for cron modulo notation @@ -378,9 +395,14 @@ next if h && t.hour != h break end t + end + + def clone + + EtOrbi::EoTime.new(@seconds, @zone) end protected # Returns a Ruby Time instance.