English 中文(简体)
从 Perl 时间戳到的天数
原标题:Finding age in days from timestamp in Perl

坦率地说,我根本不认识Perl。我不得不用perl来解决一个问题,原因有某些。我试图寻找快速解决方案,却找不到(我的坏)

Problem: I have got a file that has list of file names and a timestamps (i.e. 2012-05-24T18:19:35.000Z) in it. I need to parse identify which of these are more that 90 days old.

我只需要支票,其他一切我想我已经到位了。当我去谷歌时,有人建议使用一些花哨的约会时间套件,而有些建议是使用 -M。

其实很困惑 感谢大家的帮助 谢谢

最佳回答

该日期格式的优点是,对其中两个字符串进行地名录比较与进行日期-时间比较相同。所以你只需要将过去90天的日期改成这个格式,并进行字符串比较。

use POSIX  strftime ;
$_90_days_ago = strftime("%FT%T.000Z", gmtime( time-90*86400 ));

...
foreach $date (@your_list_of_dates) {
    if ($date lt $_90_days_ago) {
        print "$date was at least 90 days ago.
";
    } else {
        print "$date is less than 90 days ago.
";
    }
}
问题回答

此格式由RFC3339 (别具具体意义)和

类似这样的东西应该有用:

#! perl -w

use strict;
use Time::Local;

# 90 times 24 hours of 60 minutes, 60 seconds
my $ninety_days = 24 * 60 * 60 * 90;
my $now = time;

# parse the time stamps in the file
while (<INPUTFILE>)
{
    chomp();

    if (/(d{4})-(d{2})-(d{2})T(d{2}):(d{2}):(d{2})/)
    {
        my $year = $1;
        my $month = $2;
        my $day = $3;
        my $hour = $4;
        my $minute = $5;
        my $second = $6;

        # Looks like these are in GMT ("Z") so we ll use timegm
        my $time = timegm($second,$minute,$hour,$day,$month - 1,$year - 1900);

        if (($now - $time) > $ninety_days)
        {
            print "$_ is more than 90 days ago!
        }
    }
}

(这只是最基本的 - 它需要关于打开数据文件的细节,等等)





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...