Skip to content

Commit

Permalink
Group @time and @ref reset in #touch method
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Mar 12, 2024
1 parent 4f24532 commit f74d738
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions lib/et-orbi/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,34 @@ def initialize(s, zone)
(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
Expand Down Expand Up @@ -232,8 +244,8 @@ 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
Expand Down Expand Up @@ -303,21 +315,24 @@ def to_time_s

def inc(t, dir=1)

@time = nil

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)
Expand Down

0 comments on commit f74d738

Please sign in to comment.