End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
Time.now
# File tmp/rubies/ruby-3.1.3/timev.rb, line 223
def self.now(in: nil)
new(in: Primitive.arg!(:in))
end
Creates a new Time object from the current system time. This is the same as Time.new without arguments.
Time.now # => 2009-06-24 12:39:54 +0900 Time.now(in: '+04:00') # => 2009-06-24 07:39:54 +0400
Parameter:
-
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.
-