Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: DateTime::Format::Strptime Parsing Seems to have a Problem?

by parv (Vicar)
on Mar 27, 2019 at 11:55 UTC ( #1231741=note: print w/replies, xml ) Need Help??


in reply to Re: DateTime::Format::Strptime Parsing Seems to have a Problem?
in thread DateTime::Format::Strptime Parsing Seems to have a Problem?

You have not made your code easy enough to test each pair of date-time string & format-to-parse (by using a list of either array references or hash references).

Do you have issues with both *::Strptime & *::Recurrence? If so, could you please address them in separate threads?

About *::Strptime output as listed, I do not see anything wrong as far as values of $lodt, $hidt, $dt1, & $dt2 are concerned. What am I missing?

About *:Recurrence, as I understood the documentation, each event happens at the start of the interval. If it is "daily", then the even will start at 00:00:00. It can be changed to a different time via options of "hours" & "minutes".

# vim: ts=2 sts=2 sw=2 sr ai nu et: # use strict; use warnings; use DateTime; use DateTime::Format::Strptime; use DateTime::Event::Recurrence; # Current time. my $dtnow = DateTime->now( time_zone => DateTime::TimeZone->new( name +=> 'local' ) ); showtime( $dtnow, q[now dt: ] ); # Various date-time strings & formats to parse. my $dt_format = '%d/%m/%Y %I:%M:%S %p' ; my $start = "18/03/2019 10:12:53 am" ; my @end = ( [ "24/03/2019 3:15:00 pm" , $dt_format ] , [ "24/03/2019 03:15:00 pm" , $dt_format ] , [ "24/03/2019 15:15:00 pm" , '%d/%m/%Y %H:%M:%S %p' ] , [ "24/03/2019 15:15:00" , '%d/%m/%Y %H:%M:%S' ] ); my $start_dt = make_datetime( $start , $dt_format ); showtime( $start_dt , q[start dt: ] ); print "\n"; for my $dt_pair ( @end ) { my $string = $dt_pair->[0]; my $dt = make_datetime( @{ $dt_pair } ); showtime( $dt , sprintf q[end dt (%s) : ] , $string ); } print "\n"; # Recurring event. my $end = $end[ int rand @end ]; my $end_dt = make_datetime( @{ $end } ); my ( $hour , $min ) = ( 11 , 23 ); my $day_ev = DateTime::Event::Recurrence->daily( 'hours' => $hour , 'm +inutes' => $min ); my @set = $day_ev->as_list( 'start' => $start_dt , 'end' => $end_dt ); printf "Recurring event at %d:%d from %s to %s ...\n" , $hour , $min , $start_dt->iso8601() , $end_dt->iso8601() ; showtime( $_ , q[ !! ] ) for @set ; print "\n"; exit; sub make_datetime { my ( $dt , $format ) = @_; return DateTime::Format::Strptime ->new( on_error => 'croak' , time_zone => 'local' , pattern => $format ) ->parse_datetime( $dt ); } sub showtime { my ( $dt , $label ) = @_; $label ||= ''; print $label , $dt->iso8601() , "\n"; return; } __END__
now dt: 2019-03-27T02:46:41 start dt: 2019-03-18T10:12:53 end dt (24/03/2019 3:15:00 pm) : 2019-03-24T15:15:00 end dt (24/03/2019 03:15:00 pm) : 2019-03-24T15:15:00 end dt (24/03/2019 15:15:00 pm) : 2019-03-24T15:15:00 end dt (24/03/2019 15:15:00) : 2019-03-24T15:15:00 Recurring event at 11:23 from 2019-03-18T10:12:53 to 2019-03-24T15:15: +00 ... !! 2019-03-18T11:23:00 !! 2019-03-19T11:23:00 !! 2019-03-20T11:23:00 !! 2019-03-21T11:23:00 !! 2019-03-22T11:23:00 !! 2019-03-23T11:23:00 !! 2019-03-24T11:23:00

Replies are listed 'Best First'.
Re^3: DateTime::Format::Strptime Parsing Seems to have a Problem?
by ozboomer (Friar) on Mar 30, 2019 at 13:23 UTC

    Thanks for your thoughts, parv ...and granted, my code was a bit 'off' and could have made the comparisons clearer.

    Still, after looking at what we've covered, I'd say DateTime::Format::Strptime is doing what's expected -- it was more about how I (mis-)understood how the routine works (but see the bottom of this post).

    ...but the problem with 'Recurrence' is still not explained (well enough), to my thinking. To quote the docs:-

    "If no parameters are given, then the set members each occur at the be +ginning of the specified recurrence. For example, by default, the monthly() method returns a set containin +g the first day of each month. Without parameters, the weekly() method returns a set containing Mond +ays...."

    Unfortunately, we have no example in the docs for the 'daily' method we're looking at. Therefore, we can only assume that the 'daily' method gives us a set where each element (each repeat) begins at '00:00:00', as parv suggested... so we'll test that out. Note that as we're testing the 'beginning of the specified recurrence' note in the docs, we should follow what the docs declare and ensure "no parameters are given" -- hence, we'll modify parv's line of code from:-

    my $day_ev = DateTime::Event::Recurrence->daily( 'hours' => $hour , 'm +inutes' => $min );

    ...to be instead:-

    my $day_ev = DateTime::Event::Recurrence->daily;

    Doing so and running parv's code with no other modifications gives the following output:-

    now dt: 2019-03-30T23:41:09 start dt: 2019-03-18T10:12:53 end dt (24/03/2019 3:15:00 pm) : 2019-03-24T15:15:00 end dt (24/03/2019 03:15:00 pm) : 2019-03-24T15:15:00 end dt (24/03/2019 15:15:00 pm) : 2019-03-24T15:15:00 end dt (24/03/2019 15:15:00) : 2019-03-24T15:15:00 Recurring event at 11:23 from 2019-03-18T10:12:53 to 2019-03-24T15:15: +00 ... !! 2019-03-19T00:00:00 !! 2019-03-20T00:00:00 !! 2019-03-21T00:00:00 !! 2019-03-22T00:00:00 !! 2019-03-23T00:00:00 !! 2019-03-24T00:00:00

    ...showing the same error - the start of the period is '00:00:00' as we expected (hoped)... but the date is still wrong.

    Therefore, there appears to be something wrong with either our assumption of the 'beginning' of a 'daily' recurrence being '00:00:00' ...OR... there is a fault in the Recurrence code when 'no parameters are given'.

    I lean more to there being a fault in the Recurrence code, as if you change the 'hours' and 'minutes' parameters to '0' in the 'as_list()' method in parv's original code, the same error in date occurs.

    The other thing of interest is that if the DateTime::Format::Strptime usage includes a pattern that is only "%d/%m/%Y" and the string dates are specified to match, we still get the '00:00:00' beginning times on each element of the Recurrence set... but the date is correct -- this is what I did in the first modifications I made to the code in my original posting.

    Edit 31-Mar-2019 00:42 UTC: Something else that may be relevant here: The section "Floating DateTimes" in the DateTime module docs includes the comment:

    If you are planning to use any objects with a real time zone, it is strongly recommended that you do not mix these with floating datetimes.

    ...which would indicate we need to include a 'time zone' parameter at every call to a DateTime method... maybe?

    Any other thoughts?

      no longer applicable: Your font may be lying to you for you had referenced some account named p-a-r-y in your reply instead of p-a-r-v with lowercase-V (if you were referring to my reply).

        Very sorry about that... Dodgy glasses and eyes...

        Fixed the references now.

      Case of using default of 0 hours & 0 minutes as the time of the recurring event: If the time has already passed (Mar 18 0:0a) before the start of recurring event period (Mar 18 10:12a to Mar 24 3:15p), then including Mar 18 as one of the dates of the recurring event will be erroneous.

        Curious... but an interesting point.

        Again, the docs state:

        If no parameters are given, then the set members each occur at the beginning of the specified recurrence.

        I would read that to mean that as I'm specifying a 'daily' recurrence, the 'beginning' of the recurrence is 00 hours of the specified day when a set is created (the item's beginning is the next smaller measure - a daily recurrence starts from a zero hour). The curious thing is that the docs say:

        For example, by default, the monthly() method returns a set containing the first day of each month.

        ...which should, if the definition was consistent, refer to the first week of the month... but it refers to the first day.

        I admit I'm sometime as thick as a brick... but if the docs aren't clear (and/or can be misinterpreted), they aren't clear... tha's all.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1231741]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2020-10-26 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (252 votes). Check out past polls.

    Notices?