Passing date formats in Perl: i.e. Jul/10/2007 -> 20070710 (yyyymmdd) - Perl

Hi ,
This script working for fine if pass script-name.sh Jul/10/2007 ,I want to pass 20070710(yyyymmdd) .Please any help it should be appereciated.


use Time::Local;

my $d = $ARGV[0];
my $t = $ARGV[1];
my $m = "";

@d = split /\//, $d;
@t = split /:/, $t;

if ( $d[0] eq "Jan" ) { $m = 0 }
elsif ( $d[0] eq "Feb" ) { $m = 1 }
elsif ( $d[0] eq "Mar" ) { $m = 2 }
elsif ( $d[0] eq "Apr" ) { $m = 3 }
elsif ( $d[0] eq "May" ) { $m = 4 }
elsif ( $d[0] eq "Jun" ) { $m = 5 }
elsif ( $d[0] eq "Jul" ) { $m = 6 }
elsif ( $d[0] eq "Aug" ) { $m = 7 }
elsif ( $d[0] eq "Sep" ) { $m = 8 }
elsif ( $d[0] eq "Oct" ) { $m = 9 }
elsif ( $d[0] eq "Nov" ) { $m = 10 }
elsif ( $d[0] eq "Dec" ) { $m = 11 };

$time = timelocal($t[2], $t[1], $t[0], $d[1], $m, $d[2]);

print "$time\n";usage : 

Thank,
Akil

Hello,

Per our forum rules, all threads must have a descriptive subject text. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".

The reason for this is that nearly 95% of all visitors to this site come here because they are referred by a search engine. In order for future searches on your post (with answers) to work well, the subject field must be something useful and related to the problem!

In addition, current forum users who are kind enough to answer questions should be able to understand the essence of your query at first glance.

So, as a benefit and courtesy to current and future knowledge seekers, please be careful with your subject text. You might receive a forum infraction if you don't pay attention to this.

Thank you.

The UNIX and Linux Forums

Are you passing one or two arguments to the script? You only mention YYYYMMDD but your code appears to be expecting two arguments:

my $d = $ARGV[0];
my $t = $ARGV[1];

Assuming two arguments, see if this helps:

use Time::Local;

my $d = $ARGV[0];
my $t = $ARGV[1];
my ($y, $m, $d) = unpack("A4A2A2",$d)
my ($h, $min, $s) = split(/:/,$t);
$time = timelocal($s, $min, $h, $d, $m-1, $y);

print "$time\n";usage :

Thanks for your reply ,when executing ,its througing the below error.

perl a6.sh 20060708 05:32:11

syntax error at a6.sh line 5, near ")
my "
Execution of a6.sh aborted due to compilation errors.

Thanks,
Akil

That's because the line does not end properly, thereby forcing the perl interpreter to baulk.

tyler_durden

Hi KevinADC
Thanks for your great help.Its workinf fine.

Thanks,
Akil

oops, sorry, but I guess you have it corrected now. :o

Hi KevinADC,
I have corrected ,thanks a lot

Thanks,
Akil