SBECK-github / Date-Manip

Other
10 stars 11 forks source link

Date::Manip::TZ::convert fails with strange error if input date in unexpected format #47

Closed epa closed 1 year ago

epa commented 1 year ago
% perl -MDate::Manip::TZ -E '($err, $ref) = Date::Manip::TZ->new->convert("2020-07-29 13:30:00", "US/Central", "America/Chicago"); say @$ref'
Can't use an undefined value as an ARRAY reference at /usr/share/perl5/vendor_perl/Date/Manip/TZ.pm line 1587.

If you change the space character between date and time to - then it works.

The error message should be better: "date and time string not in recognized format".

Also, since you already accept a handful of different formats, I think the regexps used could be a bit more tolerant and allow examples like the above.

SBECK-github commented 1 year ago

In the documentation for Date::Manip::TZ, it states:

$date A date is always a string containing a date in one of the formats:

          YYYYMMDDHH:MN:SS
          YYYY-MM-DD-HH:MN:SS
          YYYYMMDDHHMNSS

       or a list reference:

          [Y,M,D,H,MN,S]

Since you did not supply the date in one of the allowed formats, an error is expected. Since the Date::Manip::TZ module isn't about parsing dates (that's what Date::Manip::Date is for), it only supports a couple formats (which are used internally for all of the Date::Manip modules).

As for the error message... you didn't look at the return values. The convert function returns a 4 (it actually didn't before... you did find a bug). The documentation says that that means an invalid date. So check the return code ($err in your example) before you try to use $ref.

The bug will be fixed in the next release. Thanks