class TZInfo::Timestamp

def for(value, offset = :preserve)

Experimental RBS support (using type sampling data from the type_fusion project).

def for: ((Time | TZInfo::Timestamp) value, ?Symbol offset) -> (TZInfo::TimeWithOffset | TZInfo::Timestamp)

This signature was generated using 97 samples from 4 applications.

Returns:
  • (Object) - if called without a block, the {Timestamp}

Other tags:
    Yieldreturn: - a {Timestamp} to be converted back to the type

Other tags:
    Yieldparam: timestamp - the {Timestamp} representation of

Other tags:
    Yield: - if a block is provided, the {Timestamp}

Parameters:
  • offset (Symbol) -- either `:preserve` to preserve the offset of
  • value (Object) -- a `Time`, `DateTime` or {Timestamp}.
def for(value, offset = :preserve)
  raise ArgumentError, 'value must be specified' unless value
  case offset
    when :ignore
      ignore_offset = true
      target_utc_offset = nil
    when :treat_as_utc
      ignore_offset = true
      target_utc_offset = :utc
    when :preserve
      ignore_offset = false
      target_utc_offset = nil
    else
      raise ArgumentError, 'offset must be :preserve, :ignore or :treat_as_utc'
  end
  time_like = false
  timestamp = case value
    when Time
      for_time(value, ignore_offset, target_utc_offset)
    when DateTime
      for_datetime(value, ignore_offset, target_utc_offset)
    when Timestamp
      for_timestamp(value, ignore_offset, target_utc_offset)
    else
      raise ArgumentError, "#{value.class} values are not supported" unless is_time_like?(value)
      time_like = true
      for_time_like(value, ignore_offset, target_utc_offset)
  end
  if block_given?
    result = yield timestamp
    raise ArgumentError, 'block must return a Timestamp' unless result.kind_of?(Timestamp)
    case value
      when Time
        result.to_time
      when DateTime
        result.to_datetime
      else # A Time-like value or a Timestamp
        time_like ? result.to_time : result
    end
  else
    timestamp
  end
end