Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ def make_time(date, year, yday, mon, day, hour, min, sec, sec_fraction, zone, no
# If the extracted time zone abbreviation does not match any of them,
# it is ignored and the given time is regarded as a local time.
#
# A +zone+ argument can be provided to specify the zone for the given
# +date+, if the +date+ does not include a time zone or offset.
#
# ArgumentError is raised if Date._parse cannot extract information from
# +date+ or if the Time class cannot represent specified date.
#
Expand All @@ -382,12 +385,12 @@ def make_time(date, year, yday, mon, day, hour, min, sec, sec_fraction, zone, no
#
# You must require 'time' to use this method.
#
def parse(date, now=self.now)
def parse(date, now=self.now, zone: nil)
comp = !block_given?
d = Date._parse(date, comp)
year = d[:year]
year = yield(year) if year && !comp
make_time(date, year, d[:yday], d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
make_time(date, year, d[:yday], d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone] || zone, now)
end

#
Expand Down
9 changes: 9 additions & 0 deletions test/test_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,15 @@ def test_parse_offset_hour_minute_second
assert_equal(t, Time.parse("1200-02-15 BC 14:13:20-00:00:00"))
end

def test_parse_custom_offset
t = Time.at(-100000000000).utc
assert_equal(t, Time.parse("1200-02-15 BC 14:13:20", zone: "UTC"))
assert_equal(t, Time.parse("1200-02-15 BC 15:13:20", zone: "+01:00").utc)

assert_equal(t, Time.parse("1200-02-15 BC 15:13:20+01:00", zone: "UTC").utc)
assert_equal(t, Time.parse("1200-02-15 BC 15:13:20+01:00", zone: "+02:00").utc)
end

def test_parse_leap_second
t = Time.utc(1998,12,31,23,59,59)
assert_equal(t, Time.parse("Thu Dec 31 23:59:59 UTC 1998"))
Expand Down