Perl Script date correction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Script date correction
# 1  
Old 05-17-2013
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


Code:
#!/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])
}


Code:
[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


Last edited by ajayram_arya; 05-17-2013 at 02:18 PM..
# 2  
Old 05-17-2013
That might be the date for DST/BST/IWT/??? Use gmtime or love it. http://en.wikipedia.org/wiki/IANA_time_zone_database
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 05-17-2013
Thank you DGPickett.....:-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script database date convertion

Need assistance Below script get the output correctly I want to convert the date format .Below is the output . Any idea ? #!/usr/bin/perl -w use DBI; # Get a database handle by connecting to the database my $db = DBI->connect(... (3 Replies)
Discussion started by: ajayram_arya
3 Replies

2. Shell Programming and Scripting

Perl script to change the date in some scenario

Hi , I have file FSN.log which contains number 100. i have other Perl script when i run it , it ll increment this FSN.log value. now my requirement is when the count in FSN.log becomes 999, it should make the value to 100 again and Perl script to change the date or it should make the date... (2 Replies)
Discussion started by: santhoshks
2 Replies

3. Shell Programming and Scripting

current date - 8 months in perl script

I have a requirement as follows. when i pass a date to the perl script, it has to calculate the current date - 8 months and output the date back to the shell script in date format (YYYY-MM-DD). Current date - 8 months is not constant.. because leap year, and the months jan, mar, may,.... has... (4 Replies)
Discussion started by: kmanivan82
4 Replies

4. Shell Programming and Scripting

Perl script: extracting numbers from a date formatt

Hi I want to basically sort some data files that I have based on the date. Here is my issue, the date is in the form of "2010-05-24T09:48:55-04" All I really need is the date so perhaps I could sort the number 20100524 in this case. My question is how do extract a number that I can later... (1 Reply)
Discussion started by: vas28r13
1 Replies

5. Shell Programming and Scripting

Perl Script to execute todays date.

Hi Folks, I have created a script last month to retrive files thru FTP and cronjob was running fine till yesterday. But the naming convention of the daily file is Filename_<date>.xml where date is YYYYMMDD. But today i have received file name as Filename_20110232.xml :( Part of my Perl... (4 Replies)
Discussion started by: Sendhil.Kumaran
4 Replies

6. Shell Programming and Scripting

small script correction

input cz1 87942437 87952030 M_001144992 0 + 87942537 87949664 0 3 710,114,2506, 0,2725,7087, script awk '{ n11 = split($11, t11, ",") n12 = split($12, t12, ",") for (i = 0; ++i < n11;) { s12 = $2 + t12 print $4"_xon"i, "\t",$4"_xon"i,"\t", $1,... (1 Reply)
Discussion started by: quincyjones
1 Replies

7. Shell Programming and Scripting

Perl Script to check file date and size

Hi guys, i am new to perl. I started reading the perl documents and try to come up with some logic. I am trying to create a script that would go into a location, search for todays files, then searches for all .txt files from today. If todays not found, its an error If file size is less... (26 Replies)
Discussion started by: DallasT
26 Replies

8. Programming

Date time problem while executing perl script.

Hi All, This Monday 15th March 2010, i have faced a weired issue with my Perl script execution, this script is scheduled to run at 1 minute past midnight on daily basis ( 00:01 EST ) generally for fetching previous business date , say if it is Monday it should give last Friday date, for Tuesday... (0 Replies)
Discussion started by: ravimishra
0 Replies

9. Shell Programming and Scripting

Increment a date variable in perl script

Hi, I have a perl script which prints epoch value of date in milliseconds as the output. My reuirement is that once the output is printed,the day variable shld increment by 1 and when i execute the script for the second time the output shld be for the new day value. My script looks as... (11 Replies)
Discussion started by: jyothi_wipro
11 Replies

10. Shell Programming and Scripting

Perl script to extract last date field (yyyy/mm/dd)

Hi Friends, I've a special requirement, even though I know how to implement this using shell scripting, current requirement is PERL, in which I'm not much familiar !!!. I've a record, which has around 200 fields, out of which I need to extract only one date value from the 97th field (this... (1 Reply)
Discussion started by: ganapati
1 Replies
Login or Register to Ask a Question