How to convert era time to normal time in Perl?

I am trying to write a Perl script that parses a log where on each line the second value is a date. The script takes three arguments: input log file, start time, and end time. The start and end times are used to analyze a specific value on each line that is between these two points. But for the correct launch of this, I convert the start and end times into an era. The problem I am facing is to convert the value of the loop i at normal time to compare with the log file. After running localtime($i) I print this value and only see that the link is not printed with the actual value.

Here is the script I still have (this is work in progress):

 #!/usr/bin/perl use strict; use warnings; use Time::Local; use Time::localtime; use File::stat; my $sec = 0; my $min = 0; my $hour = 0; my $mday = 0; my $mon = 0; my $year = 0; my $wday = 0; my $yday = 0; my $isdst = 0; ########################## # Get the engine log date ########################## my $date = `grep -m 1 'Metric' "$ARGV[0]" | awk '{print \$2}'`; ($year,$mon,$mday) = split('-', $date); $mon--; ######################################### # Calculate the start and end epoch time ######################################### ($hour,$min,$sec) = split(':', $ARGV[1]); my $startTime = timelocal($sec,$min,$hour,$mday,$mon,$year); ($hour,$min,$sec) = split(':', $ARGV[2]); my $endTime = timelocal($sec,$min,$hour,$mday,$mon,$year); my $theTime = 0; for (my $i = $startTime; $i <= $endTime + 29; $i++) { #print "$startTime $i \n"; $theTime = localtime($i); #my $DBInstance0 = `grep "$hour:$min:$sec" "$ARGV[0]"`;# | grep 'DBInstance-0' | awk '{print \$9}'`; #print "$DBInstance0\n"; print "$theTime\n"; } print "$startTime $endTime \n"; 

The result is as follows:

 Time::tm=ARRAY(0x8cbbd40) Time::tm=ARRAY(0x8cbc1a0) Time::tm=ARRAY(0x8cbbe80) Time::tm=ARRAY(0x8cbc190) Time::tm=ARRAY(0x8bbb170) Time::tm=ARRAY(0x8cbc180) Time::tm=ARRAY(0x8cbbf30) Time::tm=ARRAY(0x8cbc170) Time::tm=ARRAY(0x8cbc210) Time::tm=ARRAY(0x8cbc160) 1275760356 1275760773 

I only have access to the core Perl modules, and I cannot install any other.

+6
perl epoch
source share
2 answers

You can use ctime , depending on your definition of "Normal Time":

Code example:

 use Time::Local; use Time::localtime; my $time=timelocal(1,2,3,24,6,2010); print "$time\n"; $theTime = ctime($time); print "$theTime\n"; 

Result:

 1279954921 Sat Jul 24 03:02:01 2010 

Also, you don't need to use Time :: Localtime ( so you get Time :: tm instead of the standard array / string from Perl internal localtime ):

 use Time::Local; my $time=timelocal(1,2,3,24,6,2010); print "$time\n"; $theTime = localtime($time); print "$theTime\n"; 1279954921 Sat Jul 24 03:02:01 2010 
+3
source share

Remember to subtract 1900 from the year!

Remember that in the scalar context, localtime and gmtime returns a ctime formatted string, so you can use it as shown below. If this is unsuitable, you can use strftime from the POSIX module.

 #! /usr/bin/perl use warnings; use strict; use Time::Local; my $start = "01:02:03"; my $end = "01:02:05"; my $date = "2010-02-10"; my($year,$mon,$mday) = split /-/, $date; $mon--; $year -= 1900; my($startTime,$endTime) = map { my($hour,$min,$sec) = split /:/; timelocal $sec,$min,$hour,$mday,$mon,$year } $start, $end; for (my $i = $startTime; $i <= $endTime + 29; $i++) { print scalar localtime($i), "\n"; } print "$startTime $endTime \n"; 

Output Tail:

  Wed Feb 10 01:02:26 2010
 Wed Feb 10 01:02:27 2010
 Wed Feb 10 01:02:28 2010
 Wed Feb 10 01:02:29 2010
 Wed Feb 10 01:02:30 2010
 Wed Feb 10 01:02:31 2010
 Wed Feb 10 01:02:32 2010
 Wed Feb 10 01:02:33 2010
 Wed Feb 10 01:02:34 2010
 1265785323 1265785325 
+2
source share

All Articles