在Windows上的perl中将纪元转换为本地时间

时间:2015-11-08 14:28:03

标签: windows perl epoch

我们正在尝试转换从以下代码

收到的输出

当前输出采用这种形式

testingwindows,1446727960,1446728560,kkulka11,testingwin
testingwindows1,1446727160,141228560,kkulka11,testingwin
testingwindows2,1446727120,1446728560,kkulka11,testingwin
testingwindows3,1446727960,1446728560,kkulka11,testingwin

所需的输出类似于

testingwindows from Fri Oct  3 13:51:05 2015 GMT to Mon Nov  9 13:51:05 2015 GMT by kkulka11 for testingwin.
testingwindows1 from Fri Oct 2 13:51:05 2015 GMT to Mon Nov  9 13:51:05 2015 GMT by kkulka11 for testingwin.
testingwindows2 from Fri Oct 2 13:51:05 2015 GMT to Mon Nov  9 13:51:05 2015 GMT by kkulka11 for testingwin.
testingwindows3 from Fri Oct 12 13:51:05 2015 GMT to Mon Nov  9 13:51:05 2015 GMT by kkulka11 for testingwin.

这是我目前的代码

if ( $COMMAND eq 'queryone' ) {
    my $msend_query = "$MCELL_HOME\\bin\\mquery";
    my @args_query = (
        $msend_query,
        "-q",
        "-c", "$MCELL_HOME\\etc\\mclient.conf",
        "-n", "$CS_BLACKOUT_CELL",
        "-d",
        "-f", "csv",
        "-a", "CS_EMB_GBF_BLACKOUTS" ,
        "-s", "blackout_host,start_timestamp,stop_timestamp,userid,reason",
        "-w", "blackout_host: == '${BLACKOUTHOST}'"
    );
    system(@args_query);

我们尝试使用perl -pe 's/(\d{10})/gmtime($1)/e';但无法转换,但却出现此错误

  

'○〜}go⌂⌂t⌂x⌂w'不被视为内部或外部命令,   可操作程序或批处理文件。

当我们使用代码

if ( $COMMAND eq 'queryone' ) {
    my $msend_query = "$MCELL_HOME\\bin\\mquery";
    my $mqt = "$MCELL_HOME\\mqt.pl";
    my @args_query = (
        $msend_query,
        "-q",
        "-c", "$MCELL_HOME\\etc\\mclient.conf",
        "-n", "$CS_BLACKOUT_CELL",
        "-d",
        "-f", "csv",
        "-a", "CS_EMB_GBF_BLACKOUTS",
        "-s", "blackout_host,start_timestamp,stop_timestamp,userid,reason",
        "-w", "blackout_host: == '${BLACKOUTHOST}'"
    ) | $mqt;
    system(@args_query);

需要专家快速帮助和指导,以人类可读的格式实现输出。

修改

根据Jacob评论更新了代码,但仍未按要求接收输出。请建议

if ( $COMMAND eq 'queryone' ) {
    my $msend_query = "$MCELL_HOME\\bin\\mquery";
    my @args_query = (
        $msend_query,
        "-q",
        "-c", "$MCELL_HOME\\etc\\mclient.conf",
        "-n", "$CS_BLACKOUT_CELL",
        "-d",
        "-f", "csv",
        "-a", "CS_EMB_GBF_BLACKOUTS" ,
        "-s", "blackout_host,start_timestamp,stop_timestamp,userid,reason",
        "-w", "blackout_host: == '${BLACKOUTHOST}'"
    );
    chomp;
    my @parts = split(/,/, system(@args_query));
    $parts[1] = localtime($parts[1]);
    $parts[2] = localtime($parts[2]);
    printf("%s from %s to %s by %s for %s\n", @parts);
}

输出:

M:\AbhayBackup\PerlKK>test.pl -q -h testingwin
testingwin
sub: testingwin
testingwin,1446727960,1446728560,kkulka11,testingwin
0 from Thu Jan  1 05:30:00 1970 to Thu Jan  1 05:30:00 1970 by  for

1 个答案:

答案 0 :(得分:-1)

while (<DATA>) {
    chomp;

    my @parts = split(/,/, $_);
    $parts[1] = localtime($parts[1]);
    $parts[2] = localtime($parts[2]);

    printf("%s from %s to %s by %s for %s\n", @parts);
}

__DATA__
testingwindows,1446727960,1446728560,kkulka11,testingwin
testingwindows1,1446727160,141228560,kkulka11,testingwin
testingwindows2,1446727120,1446728560,kkulka11,testingwin
testingwindows3,1446727960,1446728560,kkulka11,testingwin

输出:

testingwindows from Thu Nov  5 05:52:40 2015 to Thu Nov  5 06:02:40 2015 by kkulka11 for testingwin
testingwindows1 from Thu Nov  5 05:39:20 2015 to Sun Jun 23 07:09:20 1974 by kkulka11 for testingwin
testingwindows2 from Thu Nov  5 05:38:40 2015 to Thu Nov  5 06:02:40 2015 by kkulka11 for testingwin
testingwindows3 from Thu Nov  5 05:52:40 2015 to Thu Nov  5 06:02:40 2015 by kkulka11 for testingwin

编辑:根据您对问题的最新更新,我现在相信您正在尝试捕获命令的输出并对其进行处理。由于您没有提供minimal, complete, and verifiable example,并且因为我不知道mquery是什么,而您也没有提供相关的解释,我向您提出这个猜测:

if ($COMMAND eq 'queryone') {
    my @lines = `$MCELL_HOME\\bin\\mquery -q -c $MCELL_HOME\\etc\\mclient.conf -n $CS_BLACKOUT_CELL -d -f csv -a CS_EMB_GBF_BLACKOUTS -s blackout_host,start_timestamp,stop_timestamp,userid,reason -w blackout_host: == '${BLACKOUTHOST}'`;

    for (@lines) {
        chomp;

        my @parts = split(/,/, $_);
        $parts[1] = localtime($parts[1]);
        $parts[2] = localtime($parts[2]);

        printf("%s from %s to %s by %s for %s\n", @parts);
    }       
}