Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Date::Manip and daylight savings

by ChrisDennis (Sexton)
on Nov 07, 2011 at 14:09 UTC ( [id://936499]=perlquestion: print w/replies, xml ) Need Help??

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

I've found lots of discussion about this in the net, but nothing that solves my problem.

Here's the bare-bones code, taken from a program that steps backwards in time a period at a time, where 'period' can be an hour, day, week, month, or year.

#!/usr/bin/perl use 5.10.1; use strict; use warnings; use Date::Manip; print "Date::Manip version " . Date::Manip->VERSION . "\n"; print "\nHours: \n"; # Note that daylight saving ended here in the early # hours of 30 October 2011. my @dates = ('2011103003:00:00', '2011103002:00:00', '2011103001:00:00', '2011103000:00:00'); foreach my $date1 (@dates) { my $date2 = DateCalc($date1, '-0:0:-0:0:1:0:0', 1); print "date: $date1 date-1h: $date2\n"; $date2 = DateCalc($date1, '-0:0:-0:0:2:0:0', 1); print "date: $date1 date-2h: $date2\n"; } print "\nDays: \n"; my @dates2 = ('2011110100:00:00', '2011103100:00:00', '2011103000:00:00'); print "mode=0\n"; my $err = 0; foreach my $date1 (@dates2) { my $date2 = DateCalc($date1, '-0:0:-0:1:0:0:0', \$err, 0); print "date: $date1 date-1d: $date2 err=$err\n"; } print "mode=1\n"; foreach my $date1 (@dates2) { my $date2 = DateCalc($date1, '-0:0:-0:1:0:0:0', \$err, 1); print "date: $date1 date-1d: $date2 err=$err\n"; }
which produces the following output:
Date::Manip version 6.11 Hours: date: 2011103003:00:00 date-1h: 2011103002:00:00 date: 2011103003:00:00 date-2h: 2011103001:00:00 date: 2011103002:00:00 date-1h: 2011103001:00:00 date: 2011103002:00:00 date-2h: 2011103001:00:00 date: 2011103001:00:00 date-1h: 2011103001:00:00 date: 2011103001:00:00 date-2h: 2011103000:00:00 date: 2011103000:00:00 date-1h: 2011102923:00:00 date: 2011103000:00:00 date-2h: 2011102922:00:00 Days: mode=0 date: 2011110100:00:00 date-1d: 2011103100:00:00 err=0 date: 2011103100:00:00 date-1d: 2011103001:00:00 err=0 date: 2011103000:00:00 date-1d: 2011102900:00:00 err=0 mode=1 date: 2011110100:00:00 date-1d: 2011103100:00:00 err=0 date: 2011103100:00:00 date-1d: 2011103001:00:00 err=0 date: 2011103000:00:00 date-1d: 2011102900:00:00 err=0

There are two issues here.

When going back an hour at time from 1am on 30 October, it gets back to...1am on 30 October, making the program loop. I can see why this happens, and I can work around it quite easily, but it's a trap for young users of Date::Manip.

Secondly, when jumping back a day past the end of daylight savings, it only goes back 24 hours, despite the day being 25 hours long, whether or not I use 'approximate' mode.

Am I doing something wrong? Is there a way round this issue?

Or should I switch to using something like Date::Calc instead of Date::Manip?

cheers

Chris

Replies are listed 'Best First'.
Re: Date::Manip and daylight savings
by ChrisDennis (Sexton) on Nov 10, 2011 at 15:10 UTC
    Well, I'm glad that my question has triggered a lively debate.

    It seems to me that, whichever module I use, I need to think about what I want the answer to be for the awkward cases that involve leap years or DST changes. And the answer in those cases is "whatever makes sense for my particular application".

    Thanks for all the replies.

    cheers

    Chris

Re: Date::Manip and daylight savings
by Anonymous Monk on Nov 07, 2011 at 14:32 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://936499]
Approved by toolic
Front-paged by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-24 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found