philkime has asked for the wisdom of the Perl Monks concerning the following question:

I need to use DateTime::Format::ISO8601 but there is a problem I come across with DateTime in general in that it fills in missing month/days instead of leaving them undef:
use DateTime::Format::ISO8601; my $dt = DateTime::Format::ISO8601->parse_datetime('2016'); say join(',', $dt->year, $dt->month, $dt->day); my $dt = DateTime::Format::ISO8601->parse_datetime('2016-01-01'); say join(',', $dt->year, $dt->month, $dt->day);
Outputs:
2016,1,1 2016,1,1
If I need to know whether there was a month/day in the input, the information is lost. Of course I could parse the input date myself to find this out but this is not lazy enough when there is a module that's supposed to be doing this for me. Any comments on this? I need some of the era functionality of DT but this issue prevents me from being able to use it at the moment.

Replies are listed 'Best First'.
Re: DateTime and ISO8601
by SBECK (Chaplain) on May 19, 2016 at 20:13 UTC

    This is not precisely what you are looking for, but I'll offer it as an alternative.

    I found the same problem when I was adding support for ISO8601 dates to Date::Manip, and I wanted to find out which fields had been set.

    Date::Manip (which will parse all of the ISO8601 dates) has a method (complete) which will tell you what fields were explicitly added, and which are implied. So you end up with:

    use Date::Manip::Date; my $obj = new Date::Manip::Date; $obj->parse("2000-06-01-00:00"); print $obj->value(),"\n"; print $obj->complete('h'),"\n"; print $obj->complete('m'),"\n"; print $obj->complete('s'),"\n";
    prints:
    2000060100000 1 1 0
    So, if you can't get DateTime to do it, you may want to look at Date::Manip.
      Ah, thank you, last time I looked at Date::Manip, I don't think ->complete() was in there. The tricky part is that I need DateTime's era (BC/AD etc) output support.