class method
Time.now
(?in: ::String | ::Integer | nil) -> ::Time
now(in: nil)
timev.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/timev.rb, line 270
def self.now(in: nil)
Primitive.time_s_now(Primitive.arg!(:in))
end
Creates a new Time object from the current system time. This is the same as Time.new without arguments.
Example
Time.now # => 2009-06-24 12:39:54 +0900 Time.now(in: '+04:00') # => 2009-06-24 07:39:54 +0400
For forms of argument zone, see Timezone Specifiers.
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.
-