如何在 Perl 中将纪元时间转换为正常时间?

发布于 2024-09-07 06:17:30 字数 1733 浏览 9 评论 0原文

我正在尝试编写一个 Perl 脚本来解析日志,其中每行的第二个值是日期。该脚本接受三个参数:输入日志文件、开始时间和结束时间。开始时间和结束时间用于解析出每行上位于这两个时间之间的特定值。但为了正确运行它,我将开始和结束时间转换为纪元时间。我遇到的问题是将循环“i”值转换回正常时间以与日志文件进行比较。运行 localtime($i) 后,我打印该值,但只看到打印的引用,而不是实际值。

这是我到目前为止的脚本(它是一个正在进行的工作):

#!/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";

输出如下:

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

我只能访问核心 Perl 模块,无法安装任何其他模块。

I am attempting to write a Perl script that parses a log where on each line the second value is the date. The script takes in three arguments: the input log file, the start time, and the end time. The start and end time are used to parse out a certain value on each line that that falls between those two times. But to properly run this I am converting the start and end time to epoch time. The problem I am having is that to convert the loops 'i' value back to normal time to compare against the log file. After running localtime($i) I print the value and only see a reference printed not the actual value.

Here is the script I have so far (it is a 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 output looks like:

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 am unable to install any others.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖竹 2024-09-14 06:17:30

您可以使用 ctime,具体取决于您对“正常时间”的定义:

示例代码:

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

结果:

1279954921
Sat Jul 24 03:02:01 2010

此外,您不需要使用 Time::Localtime (这就是为什么你从 Perl 内部 localtime 获取 Time::tm 而不是标准数组/字符串):

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

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

Example code:

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 (which is why you get Time::tm instead of a standard array/string from Perl's 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
感性 2024-09-14 06:17:30

不要忘记从年份中减去 1900!

请记住,在标量上下文中,localtimegmtime 返回一个 ctime 格式的字符串,因此您可以按如下方式使用它。如果这不合适,您可能需要使用 strftime POSIX 模块。

#! /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";

输出的尾部:

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

Don't forget to subtract 1900 from the year!

Remember that in scalar context, localtime and gmtime returns a ctime-formatted string, so you could use it as in the following. If that's unsuitable, you might want to 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";

Tail of the output:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文