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