Project

General

Profile

Bug #11984

DateTime.parse can parse invalid datetime string

Added by lanscene (jarry xu) over 3 years ago. Updated 19 days ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux] and ruby 2.0.0p645 (2015-04-13 revision 50299) [universal.x86_64-darwin15]
[ruby-core:72837]

Description

It seems if you pass a string which begins with 2 alphabets and following is 7 numerics, it can be parsed as DateTime, as follows

irb(main):002:0> require 'time'
irb(main):008:0> dt0 = DateTime.parse('fg4534253dd')
=> #
irb(main):009:0> dt0 = DateTime.parse('fg4534253')
=> #
irb(main):010:0> DateTime.parse('gg1233234')
=> #
irb(main):011:0> DateTime.parse('gg1233234dd')
=> #

History

Updated by long_long_float (kazuki niimi) 10 months ago

Is it a specification? When you pass 'fg4534253dd' to DateTime#parse, parse_ddd is called. It doesn't parse from begin of string.
And document says DateTime#parse is not for validator.

Updated by jeremyevans0 (Jeremy Evans) 19 days ago

  • Status changed from Open to Rejected

This isn't a bug. DateTime.parse assume the string you are passing in has some date related information in it, and tries the best it can to find something. In this case, it guesses that the first four digits are the year and the last three are the day of the year. The non-digits in this case are ignored.

DateTime._parse('fg4534253dd')
# => {:year=>4534, :yday=>253}

DateTime._parse('4534253')
# => {:year=>4534, :yday=>253}

Also available in: Atom PDF