summaryrefslogtreecommitdiff
path: root/timev.rb
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-09-05 00:24:23 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2022-12-16 22:52:59 +0900
commit8c272f44816f098c1e057c72a47451efc8cd1739 (patch)
treeb9c180b64a41b37e761c84001275816f283e3dfb /timev.rb
parentee7a338d2b21a84d194015c3680bd2a95c0dd23f (diff)
downloadruby-8c272f44816f098c1e057c72a47451efc8cd1739.tar.gz
[Feature #18033] Make Time.new parse time strings
`Time.new` now parses strings such as the result of `Time#inspect` and restricted ISO-8601 formats.
Diffstat (limited to 'timev.rb')
-rw-r--r--timev.rb15
1 files changed, 13 insertions, 2 deletions
diff --git a/timev.rb b/timev.rb
index b549221315..1370a795a6 100644
--- a/timev.rb
+++ b/timev.rb
@@ -294,6 +294,13 @@ class Time
#
# Time.new # => 2021-04-24 17:27:46.0512465 -0500
#
+ # With one string argument that represents a time, returns a new
+ # \Time object based on the given argument, in the local timezone.
+ #
+ # Time.new('2000-12-31 23:59:59.5') # => 2000-12-31 23:59:59.5 -0600
+ # Time.new('2000-12-31 23:59:59.5 +0900') # => 2000-12-31 23:59:59.5 +0900
+ # Time.new('2000-12-31 23:59:59.5', in: '+0900') # => 2000-12-31 23:59:59.5 +0900
+ #
# With one to six arguments, returns a new \Time object
# based on the given arguments, in the local timezone.
#
@@ -368,7 +375,7 @@ class Time
# Time.new(in: '-12:00')
# # => 2022-08-23 08:49:26.1941467 -1200
#
- def initialize(year = (now = true), mon = nil, mday = nil, hour = nil, min = nil, sec = nil, zone = nil, in: nil)
+ def initialize(year = (now = true), mon = (str = year; nil), mday = nil, hour = nil, min = nil, sec = nil, zone = nil, in: nil)
if zone
if Primitive.arg!(:in)
raise ArgumentError, "timezone argument given as positional and keyword arguments"
@@ -381,6 +388,10 @@ class Time
return Primitive.time_init_now(zone)
end
- Primitive.time_init_args(year, mon, mday, hour, min, sec, zone)
+ if str and Primitive.time_init_parse(str, zone)
+ return self
+ end
+
+ Primitive.time_init_args(year, mon, mday, hour, min, sec, nil, zone)
end
end