Project

General

Profile

Actions

Bug #5399

closed

Time.strptime does not correctly parse seconds since epoch

Bug #5399: Time.strptime does not correctly parse seconds since epoch

Added by ceberz (Christopher Eberz) about 14 years ago. Updated about 14 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0]
Backport:
[ruby-core:39903]

Description

Reproducing the bug inside the IRB:

Time.strptime("1", "%s")
=> 2011-10-03 15:51:02 -0700

as opposed to:

Date.strptime("1", "%s").to_time
=> 1970-01-01 00:00:00 -0800

The docs read that Time.strptime relies on Date._strptime to do the parsing, and then converts it to a Time object. Date._strptime returns the time only as seconds (with "%s" as the format string):

Date.send(:_strptime, "100000", "%s")
=> {:seconds=>100000}

Class Date seems to check for this case (see Date.rewrite_frags) but class Time does not; Time seems to assume that Date._strptime will return a hash with all of the key/values necessary to correctly call Time.make_time:

File lib/time.rb, line 277

d = Date._strptime(date, format)
. . .
make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)

Updated by naruse (Yui NARUSE) about 14 years ago Actions #1

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r33398.
Christopher, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • lib/time.rb (Time.strptime): use Time.at if d[:seconds] is set.
    Reported by Christopher Eberz. [ruby-core:39903] Bug #5399
Actions

Also available in: PDF Atom