class TZInfo::Timezone

def period_for_local(local_time, dst = Timezone.default_dst)

Raises:
  • (AmbiguousTime) - if `local_time` was ambiguous for the time zone and
  • (PeriodNotFound) - if `local_time` is not valid for the time zone
  • (ArgumentError) - if `local_time` is `nil`.

Returns:
  • (TimezonePeriod) - the {TimezonePeriod} that is valid at

Other tags:
    Yieldreturn: - to resolve the ambiguity: a chosen {TimezonePeriod}

Other tags:
    Yieldparam: periods - an `Array` containing all

Other tags:
    Yield: - if the `dst` parameter did not resolve an ambiguity, an

Parameters:
  • dst (Boolean) -- whether to resolve ambiguous local times by always
  • local_time (Object) -- a `Time`, `DateTime` or {Timestamp}.
def period_for_local(local_time, dst = Timezone.default_dst)
  raise ArgumentError, 'local_time must be specified' unless local_time
  local_time = Timestamp.for(local_time, :ignore)
  results = periods_for_local(local_time)
  if results.empty?
    raise PeriodNotFound, "#{local_time.strftime('%Y-%m-%d %H:%M:%S')} is an invalid local time."
  elsif results.size < 2
    results.first
  else
    # ambiguous result try to resolve
    if !dst.nil?
      matches = results.find_all {|period| period.dst? == dst}
      results = matches if !matches.empty?
    end
    if results.size < 2
      results.first
    else
      # still ambiguous, try the block
      if block_given?
        results = yield results
      end
      if results.is_a?(TimezonePeriod)
        results
      elsif results && results.size == 1
        results.first
      else
        raise AmbiguousTime, "#{local_time.strftime('%Y-%m-%d %H:%M:%S')} is an ambiguous local time."
      end
    end
  end
end