Skip to content

Commit

Permalink
Refactor Time::Location#lookup to use unix_seconds instead of epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Oct 2, 2018
1 parent cd1002b commit e7338cb
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/time/location.cr
Original file line number Diff line number Diff line change
Expand Up @@ -385,28 +385,28 @@ class Time::Location
lookup(time.to_unix)
end

# Returns the time zone offset observed at *epoch*.
# Returns the time zone offset observed at *unix_seconds*.
#
# *epoch* expresses the number of seconds since UNIX epoch
# *unix_seconds* expresses the number of seconds since UNIX epoch
# (`1970-01-01 00:00:00 UTC`).
def lookup(epoch : Int) : Zone
unless @cached_range[0] <= epoch < @cached_range[1]
@cached_zone, @cached_range = lookup_with_boundaries(epoch)
def lookup(unix_seconds : Int) : Zone
unless @cached_range[0] <= unix_seconds < @cached_range[1]
@cached_zone, @cached_range = lookup_with_boundaries(unix_seconds)
end

@cached_zone
end

# :nodoc:
def lookup_with_boundaries(epoch : Int) : {Zone, {Int64, Int64}}
def lookup_with_boundaries(unix_seconds : Int) : {Zone, {Int64, Int64}}
case
when zones.empty?
return Zone::UTC, {Int64::MIN, Int64::MAX}
when transitions.empty? || epoch < transitions.first.when
when transitions.empty? || unix_seconds < transitions.first.when
return lookup_first_zone, {Int64::MIN, transitions[0]?.try(&.when) || Int64::MAX}
else
tx_index = transitions.bsearch_index do |transition|
transition.when > epoch
transition.when > unix_seconds
end || transitions.size

tx_index -= 1 unless tx_index == 0
Expand Down

0 comments on commit e7338cb

Please sign in to comment.