End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
Time.at
# File tmp/rubies/ruby-3.1.3/timev.rb, line 270
def self.at(time, subsec = false, unit = :microsecond, in: nil)
if Primitive.mandatory_only?
Primitive.time_s_at1(time)
else
Primitive.time_s_at(time, subsec, unit, Primitive.arg!(:in))
end
end
Time
This form accepts a Time object time and optional keyword argument in:
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:
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:
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_iis the integer number of seconds in the range0..60. -
msecis the number of milliseconds (Integer,Float, orRational) in the range0..1000. -
usecis the number of microseconds (Integer,Float, orRational) in the range0..1000000. -
nsecis the number of nanoseconds (Integer,Float, orRational) in the range0..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
-
3.1
-
Time.newnow accepts optionalin:keyword argument for the timezone, as well asTime.atandTime.now, so that is now you can omit minor arguments toTime.new. [Feature #17485]ExampleTime.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.
ExampleTime.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, not2021-12-25 07:30:00nor2021-12-25 00:00:00 +07:30.
-