Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

class method

Time.at

(::Time, ?in: ::String | ::Integer | nil) -> ::Time
(::Numeric, ?in: ::String | ::Integer | nil) -> ::Time
(::Integer sec_i, ::Numeric msec, ::Time::subsec_unit msec, ?in: ::String | ::Integer | nil) -> ::Time
at(time, subsec = false, unit = :microsecond, in: nil)

Returns a new Time object based on the given arguments.

Required argument time may be either of:

  • A Time object, whose value is the basis for the returned time; also influenced by optional keyword argument in: (see below).

  • A numeric number of Epoch seconds for the returned time.

Examples:

Example
t = Time.new(2000, 12, 31, 23, 59, 59) # => 2000-12-31 23:59:59 -0600
secs = t.to_i                          # => 978328799
Time.at(secs)                          # => 2000-12-31 23:59:59 -0600
Time.at(secs + 0.5)                    # => 2000-12-31 23:59:59.5 -0600
Time.at(1000000000)                    # => 2001-09-08 20:46:40 -0500
Time.at(0)                             # => 1969-12-31 18:00:00 -0600
Time.at(-1000000000)                   # => 1938-04-24 17:13:20 -0500

Optional numeric argument subsec and optional symbol argument units work together to specify subseconds for the returned time; argument units specifies the units for subsec:

  • :millisecond: subsec in milliseconds:

    Example
    Time.at(secs, 0, :millisecond)     # => 2000-12-31 23:59:59 -0600
    Time.at(secs, 500, :millisecond)   # => 2000-12-31 23:59:59.5 -0600
    Time.at(secs, 1000, :millisecond)  # => 2001-01-01 00:00:00 -0600
    Time.at(secs, -1000, :millisecond) # => 2000-12-31 23:59:58 -0600
    
  • :microsecond or :usec: subsec in microseconds:

    Example
    Time.at(secs, 0, :microsecond)        # => 2000-12-31 23:59:59 -0600
    Time.at(secs, 500000, :microsecond)   # => 2000-12-31 23:59:59.5 -0600
    Time.at(secs, 1000000, :microsecond)  # => 2001-01-01 00:00:00 -0600
    Time.at(secs, -1000000, :microsecond) # => 2000-12-31 23:59:58 -0600
    
  • :nanosecond or :nsec: subsec in nanoseconds:

    Example
    Time.at(secs, 0, :nanosecond)           # => 2000-12-31 23:59:59 -0600
    Time.at(secs, 500000000, :nanosecond)   # => 2000-12-31 23:59:59.5 -0600
    Time.at(secs, 1000000000, :nanosecond)  # => 2001-01-01 00:00:00 -0600
    Time.at(secs, -1000000000, :nanosecond) # => 2000-12-31 23:59:58 -0600
    

Optional keyword argument in: zone specifies the timezone for the returned time:

Example
Time.at(secs, in: '+12:00') # => 2001-01-01 17:59:59 +1200
Time.at(secs, in: '-12:00') # => 2000-12-31 17:59:59 -1200

For the forms of argument zone, see Timezone Specifiers.

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 .gm .httpdate also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0