Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

timelocal in a ksh script 1

Status
Not open for further replies.

olded

Programmer
Oct 27, 1998
1,065
US

Hi:

I'm trying to get the number of seconds from the epoch into a Ksh script using Perl's timelocal function. (I'm a Perl newbie using Perl version 5.005_3 under Solaris 7).
The string is space delimited: year month day hour minute second.

I'm getting the error:

Month '-1' out of range 0..11 at -e line 11

Apparently, I'm not identifying the arguments correctly. Any idea what I'm don wrong?

Thanks!

Ed

# 0 1 2 3 4 5
#dt="2003 04 06 15 40 21"

dt="2003 04 06 15 40 21"

echo "$dt"|perl -e '
use Time::Local;

###my $epochseconds = timelocal($seconds, $minutes, $hours, $day, $month - 1, $year);
my $epochseconds = timelocal($ARGV[5], $ARGV[4], $ARGV[3], $ARGV[2], $ARGV[1] - 1, $ARGV[0]);
print "$epochseconds\n";
'
 
Piped-in data isn't passed into a script via @ARGV, you have to take it from <STDIN> (or <>). (Probably so it's possible to pipe in data AND pass command line arguments).

Two alternatives are:
Code:
echo &quot;$dt&quot; | perl -MTime::Local -e '
@ARGV = split /\s+/, <STDIN>;
my $epochseconds = timelocal($ARGV[5], $ARGV[4], $ARGV[3], $ARGV[2], $ARGV[1] -
1, $ARGV[0]);
print &quot;$epochseconds\n&quot;;
'
Or (slightly more Perlish, using the autosplit switch, which splits into @F by default)
Code:
echo &quot;$dt&quot; | perl -MTime::Local -ane '
my $epochseconds = timelocal($F[5], $F[4], $F[3], $F[2], $F[1] - 1, $F[0]);
print &quot;$epochseconds\n&quot;;
 '
jaa
 
Justice:

Thanks! Both of your examples work, but I'll use the auto split method.

Regards,

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top