Project

General

Profile

Actions

Bug #12981

closed

Date.parse raises an Argument error under a specific condition

Added by kenta-s (Kenta SHIRAI) over 7 years ago. Updated almost 5 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
[ruby-core:78374]

Description

Date.parse cannot accept incomplete strings whose delimiter is a hyphen like: "2016-12", although it can accept if a delimiter is a slash like: "2016/12"

I'm not really sure if this is a bug or an expected behavior, but a maintainer of rails/rails recommended me to report this to Ruby team when I issued a PR( https://github.com/rails/rails/pull/27181 ) to them. So I'd appreciate if this issue is looked into.

steps to reproduce:

require "date"
=> true

Date.parse("2016/12/01")
=> #<Date: 2016-12-01 ((2457724j,0s,0n),+0s,2299161j)>

Date.parse("2016-12-01")
=> #<Date: 2016-12-01 ((2457724j,0s,0n),+0s,2299161j)>

Date.parse("2016/12")
=> #<Date: 2016-12-01 ((2457724j,0s,0n),+0s,2299161j)>

Date.parse("2016-12")
ArgumentError: invalid date
	from (irb):3:in `parse'
	from (irb):3
	from /usr/local/bin/irb:11:in `<main>'

Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Bug #12285: Date.iso8601 does not properly handle partial date stringsClosedActions

Updated by kenta-s (Kenta SHIRAI) over 7 years ago

  • Description updated (diff)

Updated by alfiemax (Alfred Dominic) over 7 years ago

I see a similar inconsistency in the Date._parse method. Everything works fine as expected as long as the / delimiter is used.
I think it has something to do with _parse being able to parse strings with delimiter and one without it.

Date._parse("2016/12/01")
=> {:year=>2016, :mon=>12, :mday=>1}
Date._parse("2016-12-01")
=> {:year=>2016, :mon=>12, :mday=>1}
Date._parse("2016/12")
=> {:year=>2016, :mon=>12}
Date._parse("2016-12")
=> {:mon=>20, :mday=>16, :zone=>"-12", :offset=>-43200}
Actions #3

Updated by jeremyevans0 (Jeremy Evans) almost 5 years ago

  • Is duplicate of Bug #12285: Date.iso8601 does not properly handle partial date strings added
Actions #4

Updated by nobu (Nobuyoshi Nakada) almost 5 years ago

  • Status changed from Open to Closed

Applied in changeset git|75fb0a9afad1685cedee9c7665a7f30ec95068fc.


Allow mday in Date.iso8601 to be omitted

[Bug #12285]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0