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

Hello! Searching has proven less fruitful than hoped, and the most successful searches ultimately just point to using DateTime. I like this idea, but the senior Perl devs in our company prefer I stick with Date::Manip.

First, a summary with results. I am in mountain time, constructing a date object, changing it to US/Pacific, then making more changes based on user-supplied datetime parameters. In most cases, things work beautifully, but when going more than 15 days back in time, bizarre behavior is occurring.

[me ~]$ perl datefail.pl --startday='-15 days' --endday='-14 days' Start with Today 20151120T22:51-0700 Fix timezone 20151120T21:51-0800 Set the right day 20151105T21:51-0800 Strip the hours 20151105T00:51-0800 Strip the minutes 20151105T00:00-0800 [me ~]$ perl datefail.pl --startday='-19 days' --endday='-18 days' Start with Today 20151120T22:51-0700 Fix timezone 20151120T21:51-0800 Set the right day 20151101T21:51-0800 Strip the hours 20151101T01:51-0700 Strip the minutes 20151101T01:00-0700

Enjoy this simplified code that illustrates the problem.

#!/usr/bin/perl use strict; use warnings; use Date::Manip; use Getopt::Long qw(GetOptions); my $startDay; my $endDay; GetOptions( 'startday=s' => \$startDay ,'endday=s' => \$endDay ); my ( $startDDelta,$zeroStartH,$zeroStartHDelta,$zeroStartM,$zeroStartM +Delta ); print "Start with Today\n"; my $dateObjStart = new Date::Manip::Date('now'); print $dateObjStart->printf('%Y%m%dT%H:%M%z') . "\n"; print "Fix timezone\n"; $dateObjStart->convert('US/Pacific'); print $dateObjStart->printf('%Y%m%dT%H:%M%z') . "\n"; print "Set the right day\n"; $startDDelta = new Date::Manip::Delta($startDay); $dateObjStart = $dateObjStart->calc($startDDelta); print $dateObjStart->printf('%Y%m%dT%H:%M%z') . "\n"; print "Strip the hours\n"; $zeroStartH = '-'.$dateObjStart->printf('%H').' hours'; $zeroStartHDelta = new Date::Manip::Delta($zeroStartH); $dateObjStart = $dateObjStart->calc($zeroStartHDelta); print $dateObjStart->printf('%Y%m%dT%H:%M%z') . "\n"; print "Strip the minutes\n"; $zeroStartM = '-'.$dateObjStart->printf('%M').' minutes'; $zeroStartMDelta = new Date::Manip::Delta($zeroStartM); $dateObjStart = $dateObjStart->calc($zeroStartMDelta); print $dateObjStart->printf('%Y%m%dT%H:%M%z') . "\n";

Thank you kindly for any suggestions you can offer that don't require me to refactor the code to use another package. (Though if that's the only option, I'll just offer the department their choice between that or, say, ColdFusion. Hah.)

Replies are listed 'Best First'.
Re: Date::Manip time zone is resetting in some cases
by poj (Abbot) on Nov 21, 2015 at 08:06 UTC

    With daylight saving at 02.00 1 Nov 2015 there would be 8 hours between 00:00 and 07:00

    poj
      Yes, that's why I chose to use Date::Manip in the first place. My solution needs to be able to handle the short and long days of the year. I also need it to remain in the time zone that I set without arbitrarily changing.
        ..without arbitrarily changing.

        Are you referring to the change from PST to PDT ?

        poj
Re: Date::Manip time zone is resetting in some cases
by tcabeen (Novice) on Nov 21, 2015 at 06:23 UTC

    I should add, as this is my first post, that search has so far served me brilliantly since I first took up Perl in June. So thank you all for that as well.

    I furthermore welcome your feedback on any other aspect of my code. I am quite new to the language, having spent most of my career in SQL. I have a great mentor at work, but he is just one mind, and quite busy with other things.