SBECK-github / Date-Manip

Other
10 stars 11 forks source link

Issue with Dates less than 24 and of specific pattern #27

Closed ajymtl closed 4 years ago

ajymtl commented 5 years ago

Version: 6.75

`use Date::Manip; my $dateString = "October 23,2018"; my $date = ParseDate($dateString); print $date; No output

use Date::Manip; my $dateString = "October 24,2018"; my $date = ParseDate($dateString); print $date; 2018102400:00:00`

SBECK-github commented 5 years ago

I've identified what's going on.

The problem is that commas serve multiple functions, including fractional times and a common language separator. If you add in a space: "October 23, 2018" (note the space after the comma), it will parse fine. Date::Manip knows that the comma is being used as a language separator.

When you have digit-comma-digit, it is interpreting it (in some cases) as a fractional time. To be honest, I'm not sure how "October 24,2018" is getting parsed in the way you expect.

I am going to have to investigate further. There's clearly a problem since the two strings are getting parsed in different ways, and I want both of them to be handled in an identical fashion. Then I need to decide if there's any way that I can better distinguish how the commas should be interpreted.

Unfortunately, I'm going to be leaving tomorrow for Christmas break, so I won't be able to spend time on this until after the new year, but I wanted to let you know that I'll fix this (one way or another) in a couple weeks.

SBECK-github commented 4 years ago

I have pretty thoroughly studied this situation, and this is a tricky one. The problem is that ISO 8601 (which is given the highest priority when parsing) recognizes the time format:

HH,fffffff

where HH can be an hour (00-23) and ffffff is some fraction. So, the string:

23,5

refers to the hour 23:00 plus 0.5 hour, so the result is 23:30 .

I recognize that this is not a common interpretation... but the standard defines it.

Of course, if you insert a space:

October 23, 2018

then the ISO 8601 format (which does not have spaces) doesn't match. And (as you noted) if you start with a number outside of 00-23, it doesn't match, which is why October 24,2018 works as expected.

I can't really change the regexp in a way which will fully support the standard but would match what you expect, so unfortunately, the short term answer is "do nothing" (other than perhaps some clarification in the documentation).

And of course, your workaround is to insert a space.

A potential longer-term solution would be to disable (either by default, or more likely at the choice of the user) some of the less common forms. Since this would have other advantages (primarily performance related), I'm going to investigate doing that, but that is a bit longer term.

Another solution for you would be to switch to using the OO interface. Then you could use the parse_date method (instead of the parse method which you are using indirectly):

use Date::Manip::Date;
my $date       = new Date::Manip::Date;

my $dateString = "October 23,2018";
$date->parse_date($dateString);
my $str        = $date->value();
print $str,"\n";

$dateString    = "October 24,2018";
$date->parse_date($dateString);
$str           = $date->value();
print $str,"\n";

Sorry I can't be more helpful in this situation.