Dual-Life / Time-Piece

Object Oriented time objects
Other
15 stars 33 forks source link

strptime can parse wrong date #50

Open worthmine opened 4 years ago

worthmine commented 4 years ago

when I ran the code like below:

use feature qw(say);
use Time::Piece;
use DateTime;
use DateTime::Format::MySQL;

say 'Time::Piece now: ', my $tp = Time::Piece->new();
say 'DateTime now: ',    my $dt = DateTime->now();

say '---';
say 'with ', my $quite_normal = '2020-07-25T11:32:31';
say '---';

say 'strptime with tp: ',       $tp->strptime( $quite_normal, '%Y-%m-%dT%H:%M:%S' );
say 'parse_datetime with dt: ', DateTime::Format::MySQL->parse_datetime($quite_normal);

say '---';
say 'with ', my $wrong = '2020-09-31T11:32:31';
say '---';

my $result;
$result = eval { $tp->strptime( $wrong, '%Y-%m-%dT%H:%M:%S' ) } || "dies";
say 'strptime with tp: ', $result;
$result = eval { DateTime::Format::MySQL->parse_datetime($wrong) } || "dies";
say 'parse_datetime with dt: ', $result;

say '---';
say 'with ', my $wronger = '2020-02-31T11:32:31';
say '---';

$result = eval { $tp->strptime( $wronger, '%Y-%m-%dT%H:%M:%S' ) } || "dies";
say 'strptime with tp: ', $result;
$result = eval { DateTime::Format::MySQL->parse_datetime($wronger) } || "dies";
say 'parse_datetime with dt: ', $result;

say '---';
say 'with ', my $wrongest = '2020-02-34T11:32:31';
say '---';

$result = eval { $tp->strptime( $wrongest, '%Y-%m-%dT%H:%M:%S' ) } || "dies";
say 'strptime with tp: ', $result;
$result = eval { DateTime::Format::MySQL->parse_datetime($wrongest) } || "dies";
say 'parse_datetime with dt: ', $result;

It returns like this:

Time::Piece now: Tue Oct 13 14:34:48 2020
DateTime now: 2020-10-13T05:34:48
---
with 2020-07-25T11:32:31
---
strptime with tp: Sat Jul 25 11:32:31 2020
parse_datetime with dt: 2020-07-25T11:32:31
---
with 2020-09-31T11:32:31
---
strptime with tp: Thu Oct  1 11:32:31 2020
parse_datetime with dt: dies
---
with 2020-02-31T11:32:31
---
strptime with tp: Mon Mar  2 11:32:31 2020
parse_datetime with dt: dies
---
with 2020-02-34T11:32:31
---
strptime with tp: dies
parse_datetime with dt: dies

Is this a specification?

smith153 commented 4 years ago

Yes. When strptime is used, before the data is returned, it is passed to my_mini_mktime() https://github.com/Dual-Life/Time-Piece/blob/master/Piece.xs#L164

This does do a little bit of math to correct for day errors.

The docs do say that "it's even smart enough to fix my obvious date bug" at https://metacpan.org/pod/Time::Piece#Date-Parsing but perhaps it could be worded better.

worthmine commented 4 years ago

Thank you for fast replying!

I'm sorry that I can't read/write XSs so can't fix this myself.

The parser of this module is more flexible than other Modules. I think it's an advantage.

e.g. DateTime::Format::Mysql can't parse the formats with omission like $t->strptime( "$date 09:00", '%Y-%m-%d %H:%M')), must be DateTime::Format::MySQL->parse_datetime("$date 09:00:00"), it's too redundant.

So, I hope that this issue(not the docs) is fixed. What can I help you? (other than writing XS codes)