From 1679b909f51e9467b29e6ff84ba5f341ae7b961c Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Wed, 19 Feb 2014 12:27:33 +0100 Subject: [PATCH 1/3] Add default arguments to Time::strptime to match Date::strptime --- lib/time.rb | 2 +- test/test_time.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/time.rb b/lib/time.rb index 8c324e6..df9f5ea 100644 --- a/lib/time.rb +++ b/lib/time.rb @@ -389,7 +389,7 @@ def parse(date, now=self.now) # %Z :: Time zone name # %% :: Literal "%" character - def strptime(date, format, now=self.now) + def strptime(date='-4712-01-01', format='%F', now=self.now) d = Date._strptime(date, format) raise ArgumentError, "invalid strptime format - `#{format}'" unless d if seconds = d[:seconds] diff --git a/test/test_time.rb b/test/test_time.rb index 582e60b..c4e208f 100644 --- a/test/test_time.rb +++ b/test/test_time.rb @@ -402,6 +402,7 @@ def test_strptime assert_equal(Time.at(1).localtime, Time.strptime("1", "%s")) assert_equal(false, Time.strptime('0', '%s').utc?) assert_equal(3600, Time.strptime('0 +0100', '%s %z').utc_offset) + assert_equal(Date.strptime.to_time, Time.strptime) end def test_nsec -- 1.8.5.1 From 70087ce27f605c3cc3265ad1c196544dfd0d8b08 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Wed, 19 Feb 2014 13:02:07 +0100 Subject: [PATCH 2/3] Raise ArgumentError if time passed to Time::strptime is invalid --- lib/time.rb | 15 ++++++++------- test/test_time.rb | 3 +++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/time.rb b/lib/time.rb index df9f5ea..4bf4669 100644 --- a/lib/time.rb +++ b/lib/time.rb @@ -389,19 +389,20 @@ def parse(date, now=self.now) # %Z :: Time zone name # %% :: Literal "%" character - def strptime(date='-4712-01-01', format='%F', now=self.now) - d = Date._strptime(date, format) - raise ArgumentError, "invalid strptime format - `#{format}'" unless d - if seconds = d[:seconds] - if offset = d[:offset] + def strptime(time='-4712-01-01', format='%F', now=self.now) + date = Date.strptime(time, format) # will raise an ArgumentError if the time is invalid + hash = Date._strptime(time, format) + raise ArgumentError, "invalid strptime format - `#{format}'" unless hash + if seconds = hash[:seconds] + if offset = hash[:offset] Time.at(seconds).localtime(offset) else Time.at(seconds) end else - year = d[:year] + year = date.year year = yield(year) if year && block_given? - make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now) + make_time(year, date.mon, date.mday, hash[:hour], hash[:min], hash[:sec], hash[:sec_fraction], hash[:zone], now) end end diff --git a/test/test_time.rb b/test/test_time.rb index c4e208f..5d87b26 100644 --- a/test/test_time.rb +++ b/test/test_time.rb @@ -403,6 +403,9 @@ def test_strptime assert_equal(false, Time.strptime('0', '%s').utc?) assert_equal(3600, Time.strptime('0 +0100', '%s %z').utc_offset) assert_equal(Date.strptime.to_time, Time.strptime) + assert_raise(ArgumentError) { + Time.strptime('31/2/2014', '%d/%m/%Y') + } end def test_nsec -- 1.8.5.1 From 306938092e79f5581e466879fdb5abe538b7549f Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Wed, 19 Feb 2014 13:04:36 +0100 Subject: [PATCH 3/3] Rename variables in Time::parse to be consistent with Time::strptime --- lib/time.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/time.rb b/lib/time.rb index 4bf4669..7021b0f 100644 --- a/lib/time.rb +++ b/lib/time.rb @@ -320,15 +320,15 @@ def make_time(year, mon, day, hour, min, sec, sec_fraction, zone, now) # # You must require 'time' to use this method. # - def parse(date, now=self.now) + def parse(time, now=self.now) comp = !block_given? - d = Date._parse(date, comp) - if !d[:year] && !d[:mon] && !d[:mday] && !d[:hour] && !d[:min] && !d[:sec] && !d[:sec_fraction] + hash = Date._parse(time, comp) + if !hash[:year] && !hash[:mon] && !hash[:mday] && !hash[:hour] && !hash[:min] && !hash[:sec] && !hash[:sec_fraction] raise ArgumentError, "no time information in #{date.inspect}" end - year = d[:year] + year = hash[:year] year = yield(year) if year && !comp - make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now) + make_time(year, hash[:mon], hash[:mday], hash[:hour], hash[:min], hash[:sec], hash[:sec_fraction], hash[:zone], now) end # -- 1.8.5.1