houseabsolute / DateTime-Format-ISO8601

Parses ISO8601 formats
http://metacpan.org/release/DateTime-Format-ISO8601/
Other
3 stars 3 forks source link

Is the SYNOPSIS wrong? #12

Closed worthmine closed 3 years ago

worthmine commented 3 years ago

Hi, I've met something strange.

At the SYNOPSIS,

use DateTime::Format::ISO8601;

my $str = '2020-07-25T11:32:31';

my $dt = DateTime::Format::ISO8601->parse_datetime($str);
$dt = DateTime::Format::ISO8601->parse_time($str);
...

when I run it within my test script, It says Invalid date format: 2020-07-25T11:32:31 ~ and dies at the end of lines. It is strange.

And, I succeeded to pass this situation like this:

my $str = '2020-07-25T11:32:31';
say 'parse_datetime: ', my $dt = DateTime::Format::ISO8601->parse_datetime($str);
# parse_datetime: 2020-07-25T11:32:31    # correct

( undef, my $time ) = split 'T', $dt;
$time =~ s/://g;
say 'parse_time: ', $dt = DateTime::Format::ISO8601->parse_time($time);
# parse_time: 2020-10-12T11:32:31            # is it correct?

In DateTime, there is no definition for parse_time. And there is no document for parse_time.

What does parse_time mean?

autarch commented 3 years ago

That's definitely a bug in the synopsis. The parse_time method will only accept times, like 12:13:14, not a full datetime.

worthmine commented 3 years ago

thank you for fast replying!

it still strange because:

my $dt = DateTime::Format::ISO8601->parse_datetime($str);
( undef, my $time ) = split 'T', $dt;
#$time =~ s/://g;     # it seems this line is required to pass
say 'parse_time: ', $dt = DateTime::Format::ISO8601->parse_time($time);
# said "Invalid date format: 11:32:31 at ~"
autarch commented 3 years ago

That looks like a separate bug. The parse_time method should be able to parse that format, but for some reason it's not.

worthmine commented 3 years ago

I'm not in a hurry, but when will it be fixed?

autarch commented 3 years ago

I'd certainly welcome a PR to fix these issues. Otherwise it'll be whenever I get around to it. Typically I do FOSS stuff on weekends, so maybe this weekend.

worthmine commented 3 years ago

I'd like to try to fix it indeed. but I'm busy until this Thursday 15. Probably, I'll wait for your fix your own.

best regard.

autarch commented 3 years ago

So I did some more digging and I realized parse_time is working as intended. That method is only used for formats which might be either a date or a time. The parse_time method treats those as a time, whereas parse_datetime will treat them as a date.

autarch commented 3 years ago

Fixed in 0.15.

worthmine commented 3 years ago

Thank you for fast fixing. Lastly, I have a question about README.

At 5.3 Time of Day, there is a mention to the format 'hh:mm:ss' as chapter 5.3.1.1, but it still fails to parse this format with parse_time().

use feature qw(say);
use DateTime::Format::ISO8601;

my $time_str = '23:32:31';
$dt = DateTime::Format::ISO8601->parse_time($time_str); # dies here
say $dt;

Is this a spec?

I read the source, it seems $time_str must be like/^T?(?:\d\d){1,3}$/ think it will be more strictly based on ISO 8601 if parse_time() can accept the format like /T?\d\d(?::?\d\d){0,2}Z?$/