Perl Script date correction

Need assistance for the below script.

Little details: based on the hours specified it calculates future or past date,time. This script works absolutely great . But at one point when i wante specify future date with hours at 1682 hours ...1 hours skips .Dont know why the calculation misses 1 hour . Can someone help me on this issue.

I have given the script and examples

#!/usr/bin/perl
#  Script to get date time based on hours specified. Below example
#  calendays5.sh  01 01 2012  9 46 00 120
#  script        day month year hour minute sec  (hours in future)
#
#

my $day = @ARGV[0];
my $mon = @ARGV[1] -1;
my $year = @ARGV[2];

my $hour = @ARGV[3];
my $min = @ARGV[4];
my $sec = @ARGV[5];


use Time::Local;
my $time = timelocal($sec,$min,$hour,$day,$mon,$year);

my $hours = @ARGV[6];
if ($hours >= 0)
{

@T=localtime($time+($hours * 3600));
printf("%02d/%02d/%02d %02d:%02d:%02d\n",$T[4]+1,$T[3],$T[5]+1900,$T[2],$T[1],$T[0])
}
elsif ($hours <= 0)
{
@T=localtime($time+($hours * 3600));
printf("%02d/%02d/%02d %02d:%02d:%02d\n",$T[4]+1,$T[3],$T[5]+1900,$T[2],$T[1],$T[0])
}
[user@hostname]$ ./hourtodate1.sh  01 01 2012 00 00 00 10
01/01/2012 10:00:00
[user@hostname]$ ./hourtodate1.sh  01 01 2012 00 00 00 15
01/01/2012 15:00:00
[user@hostname]$ ./hourtodate1.sh  01 01 2012 00 00 00 1679
03/10/2012 23:00:00
[user@hostname]$ ./hourtodate1.sh  01 01 2012 00 00 00 1680
03/11/2012 00:00:00
[user@hostname]$ ./hourtodate1.sh  01 01 2012 00 00 00 1680
03/11/2012 00:00:00
[user@hostname]$ ./hourtodate1.sh  01 01 2012 00 00 00 1681
03/11/2012 01:00:00
[user@hostname]$ ./hourtodate1.sh  01 01 2012 00 00 00 1682
03/11/2012 03:00:00

That might be the date for DST/BST/IWT/??? Use gmtime or love it. http://en.wikipedia.org/wiki/IANA\_time\_zone_database

1 Like

Thank you DGPickett.....:slight_smile: