SBECK-github / Date-Manip

Other
10 stars 11 forks source link

Handling months #34

Closed nigelhorne closed 3 years ago

nigelhorne commented 3 years ago

It would be really nice if this code printed 1786, instead of 1986.

#!/usr/bin/env perl

use Date::Manip;

print get_year_from_date('Feb 1786'), "\n";

sub get_year_from_date()
{
        my $date = shift;
        my $year = UnixDate(ParseDate($date), "%Y");

        return $year if($year);
        die;
}
SBECK-github commented 3 years ago

The problem is that Date::Manip works with full dates (i.e. Y-M-D). If you say 'February in the year 1786', that's not a date since no information about the day of month is present.

Date::Manip DOES work with a lot of less common date formats, and so it's treating this as: 'mmm DDYY' (admittedly a less common date format) so 'Feb 1786' could be thought of as: 'Feb 17 86' and 86 defaults to 1986.

So, Date::Manip is working as expected. What you are really asking for (if you really want Date::Manip to understand that 1786 in your string is the year) is for Date::Manip to handle incomplete dates (i.e. a date with some of the information not present). It actually does do that in a very few cases (for example, 'Feb 17' would default to the current year), but most of the time, it doesn't.

So, although I'm not offering to fix this, hopefully that at least explains the situation.