Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

Time.at

at(time, subsec = false, unit = :microsecond, in: nil)

Time

This form accepts a Time object time and optional keyword argument in:

Example
Time.at(Time.new)               # => 2021-04-26 08:52:31.6023486 -0500
Time.at(Time.new, in: '+09:00') # => 2021-04-26 22:52:31.6023486 +0900

Seconds

This form accepts a numeric number of seconds sec and optional keyword argument in:

Example
Time.at(946702800)               # => 1999-12-31 23:00:00 -0600
Time.at(946702800, in: '+09:00') # => 2000-01-01 14:00:00 +0900

Seconds with Subseconds and Units

This form accepts an integer number of seconds sec_i, a numeric number of milliseconds msec, a symbol argument for the subsecond unit type (defaulting to :usec), and an optional keyword argument in:

Example
Time.at(946702800, 500, :millisecond)               # => 1999-12-31 23:00:00.5 -0600
Time.at(946702800, 500, :millisecond, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900
Time.at(946702800, 500000)                             # => 1999-12-31 23:00:00.5 -0600
Time.at(946702800, 500000, :usec)                      # => 1999-12-31 23:00:00.5 -0600
Time.at(946702800, 500000, :microsecond)               # => 1999-12-31 23:00:00.5 -0600
Time.at(946702800, 500000, in: '+09:00')               # => 2000-01-01 14:00:00.5 +0900
Time.at(946702800, 500000, :usec, in: '+09:00')        # => 2000-01-01 14:00:00.5 +0900
Time.at(946702800, 500000, :microsecond, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900
Time.at(946702800, 500000000, :nsec)                     # => 1999-12-31 23:00:00.5 -0600
Time.at(946702800, 500000000, :nanosecond)               # => 1999-12-31 23:00:00.5 -0600
Time.at(946702800, 500000000, :nsec, in: '+09:00')       # => 2000-01-01 14:00:00.5 +0900
Time.at(946702800, 500000000, :nanosecond, in: '+09:00') # => 2000-01-01 14:00:00.5 +0900

Parameters:

  • isec_i is the integer number of seconds in the range 0..60.

  • msec is the number of milliseconds (Integer, Float, or Rational) in the range 0..1000.

  • usec is the number of microseconds (Integer, Float, or Rational) in the range 0..1000000.

  • nsec is the number of nanoseconds (Integer, Float, or Rational) in the range 0..1000000000.

  • in: zone: a timezone zone, which may be:

    • A string offset from UTC.

    • A single letter offset from UTC, in the range 'A'..'Z', 'J' (the so-called military timezone) excluded.

    • An integer number of seconds.

    • A timezone object; see Timezone Argument for details.

Ruby version changes

  1. 3.1
    • Time.new now accepts optional in: keyword argument for the timezone, as well as Time.at and Time.now, so that is now you can omit minor arguments to Time.new. [Feature #17485]

      Example
      Time.new(2021, 12, 25, in: "+07:00")
      #=> 2021-12-25 00:00:00 +0700
      

      At the same time, time component strings are converted to integers more strictly now.

      Example
      Time.new(2021, 12, 25, "+07:30")
      #=> invalid value for Integer(): "+07:30" (ArgumentError)
      

      Ruby 3.0 or earlier returned probably unexpected result 2021-12-25 07:00:00, not 2021-12-25 07:30:00 nor 2021-12-25 00:00:00 +07:30.

See also .httpdate .iso8601 also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0