Bug #10904
closedTime.strptime with %s.%N format should not ignore sec_fraction
Description
Time.strptime
with "%s.%N" format ignores milliseconds, microseconds and nanoseconds.
For example, Time.strptime("0.123", "%s.%N").tv_usec
returns 0.
Here is the script to reproduce:
require 'time'
# sec (this is ok)
t1 = Time.strptime("2015-02-24 21:02:57.321", "%Y-%m-%d %H:%m:%S")
p t1.strftime("%Y-%m-%d %H:%m:%S.%N") #=> "2015-02-24 21:02:57.000000000"
t2 = Time.strptime("1424842137.321", "%s")
p t2.strftime("%Y-%m-%d %H:%m:%S.%N") #=> "2015-02-24 21:02:57.000000000"
# msec
t1 = Time.strptime("2015-02-24 21:02:57.321", "%Y-%m-%d %H:%m:%S.%N")
p t1.strftime("%Y-%m-%d %H:%m:%S.%N") #=> "2015-02-24 21:02:57.321000000"
t2 = Time.strptime("1424842137.321", "%s.%N")
p t2.strftime("%Y-%m-%d %H:%m:%S.%N") #=> "2015-02-24 21:02:57.000000000"
# usec
t1 = Time.strptime("2015-02-24 21:02:57.654321", "%Y-%m-%d %H:%m:%S.%N")
p t1.strftime("%Y-%m-%d %H:%m:%S.%N") #=> "2015-02-24 21:02:57.654321000"
t2 = Time.strptime("1424842137.654321", "%s.%N")
p t2.strftime("%Y-%m-%d %H:%m:%S.%N") #=> "2015-02-24 21:02:57.000000000"
# nsec
t1 = Time.strptime("2015-02-24 21:02:57.987654321", "%Y-%m-%d %H:%m:%S.%N")
p t1.strftime("%Y-%m-%d %H:%m:%S.%N") #=> "2015-02-24 21:02:57.987654321"
t2 = Time.strptime("1424842137.987654321", "%s.%N")
p t2.strftime("%Y-%m-%d %H:%m:%S.%N") #=> "2015-02-24 21:02:57.000000000"
p RUBY_VERSION
I attached a patch to fix this issue (strptime-s-n-format-sec-fraction-fix.patch).
Files
Updated by akr (Akira Tanaka) over 9 years ago
It seems your patch doesn't consider negative seconds
% TZ=GMT ./ruby -Ilib -rtime -e 'p Time.strptime("-3.1", "%s.%N").iso8601(10)'
"1969-12-31T23:59:57.1000000000+00:00"
Updated by frsyuki (Sadayuki Furuhashi) over 9 years ago
- File strptime-s-n-format-sec-fraction-fix.2.patch strptime-s-n-format-sec-fraction-fix.2.patch added
I attached an updated patch (strptime-s-n-format-sec-fraction-fix.2.patch) that invert sign of sec_fraction if seconds is negative.
This is the new behavior:
TZ=GMT ruby -rtime -e 'p Time.strptime("-3.1", "%s.%N").iso8601(10)'
"1969-12-31T23:59:56.9000000000+00:00"
I don't have strong thought how to handle Time.strptime("-3.-1", "%s.%N")
.
Updated by akr (Akira Tanaka) over 9 years ago
- % Done changed from 0 to 100
- Status changed from Open to Closed
Applied in changeset r49788.
- lib/time.rb (strptime): Support %s.%N.
[ruby-core:68301] [Bug #10904] Patch by Sadayuki Furuhashi.
Updated by naruse (Yui NARUSE) over 9 years ago
- Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN to 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED
Updated by nagachika (Tomoyuki Chikanaga) over 9 years ago
- Backport changed from 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED to 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: DONE
r49788 and r49790 were backported into ruby_2_2
branch at r51475.
Updated by usa (Usaku NAKAMURA) about 9 years ago
- Backport changed from 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: DONE to 2.0.0: REQUIRED, 2.1: DONE, 2.2: DONE
ruby_2_1 r51599 merged revision(s) 49788,49790.
note: changed a little to get rid of conflicts.