diff options
author | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-08-17 07:41:58 +0000 |
---|---|---|
committer | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-08-17 07:41:58 +0000 |
commit | a61756e094462e6548113a7407652230f1f85161 (patch) | |
tree | 94706a5d2b7c58431c5b7750173c005671b3c356 | |
parent | 7766666aed80c7e06dfd8288302a663638b66fe5 (diff) |
merge revision(s) 49788,49790: [Backport #10904]
* lib/time.rb (strptime): Support %s.%N.
[ruby-core:68301] [Bug #10904] Patch by Sadayuki Furuhashi.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@51599 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | lib/time.rb | 10 | ||||
-rw-r--r-- | test/test_time.rb | 11 | ||||
-rw-r--r-- | version.h | 2 |
4 files changed, 25 insertions, 3 deletions
@@ -1,3 +1,8 @@ +Mon Aug 17 16:24:10 2015 Tanaka Akira <akr@fsij.org> + + * lib/time.rb (strptime): Support %s.%N. + [ruby-core:68301] [Bug #10904] Patch by Sadayuki Furuhashi. + Mon Aug 17 16:22:28 2015 Nobuyoshi Nakada <nobu@ruby-lang.org> * transcode.c (load_transcoder_entry): fix transcoder loading race diff --git a/lib/time.rb b/lib/time.rb index dd7a513dba..73a8f81177 100644 --- a/lib/time.rb +++ b/lib/time.rb @@ -393,10 +393,16 @@ class Time d = Date._strptime(date, format) raise ArgumentError, "invalid strptime format - `#{format}'" unless d if seconds = d[:seconds] + if sec_fraction = d[:sec_fraction] + usec = sec_fraction * 1000000 + usec *= -1 if seconds < 0 + else + usec = 0 + end if offset = d[:offset] - Time.at(seconds).localtime(offset) + Time.at(seconds, usec).localtime(offset) else - Time.at(seconds) + Time.at(seconds, usec) end else year = d[:year] diff --git a/test/test_time.rb b/test/test_time.rb index 582e60b8ae..12c8589209 100644 --- a/test/test_time.rb +++ b/test/test_time.rb @@ -404,6 +404,17 @@ class TestTimeExtension < Test::Unit::TestCase # :nodoc: assert_equal(3600, Time.strptime('0 +0100', '%s %z').utc_offset) end + def test_strptime_s_N + assert_equal(Time.at(1, 500000), Time.strptime("1.5", "%s.%N")) + assert_equal(Time.at(-2, 500000), Time.strptime("-1.5", "%s.%N")) + t = Time.strptime("1.000000000001", "%s.%N") + assert_equal(1, t.to_i) + assert_equal(Rational("0.000000000001"), t.subsec) + t = Time.strptime("-1.000000000001", "%s.%N") + assert_equal(-2, t.to_i) + assert_equal(1-Rational("0.000000000001"), t.subsec) + end + def test_nsec assert_equal(123456789, Time.xmlschema("2000-01-01T00:00:00.123456789+00:00").tv_nsec) assert_equal(123456789, Time.parse("2000-01-01T00:00:00.123456789+00:00").tv_nsec) @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.1.7" #define RUBY_RELEASE_DATE "2015-08-17" -#define RUBY_PATCHLEVEL 382 +#define RUBY_PATCHLEVEL 383 #define RUBY_RELEASE_YEAR 2015 #define RUBY_RELEASE_MONTH 8 |