将字符串中的时间戳与当前系统日期进行比较,如果小于 5 天则转为不良状态
Compare timestamp from string with the current system day and turn bad status if it's less than 5 days
我想将字符串中的时间戳与当前系统日期进行比较,如果它小于当前系统日期的 5 天,则变为错误状态,例如,我的意思是这些日期:
20150705
20150911
我想知道如何将它们与当前系统日期进行比较,并在当前系统时间为 2015 年 9 月 11 日时显示这样的输出:
20150705 bad
20150911 good
尝试将日期字符串转换为 unix 时间,例如
cur_date=$(date +"%s");
cat data_file | while read line;
do
lineDate=$(date -d "$line" +"%s");
diff=$(expr $cur_date - $lineDate);
if [ "$diff" -gt 432000 ];
then echo $line " bad";
else
echo $line " good";
fi;
done
在 Perl 中,您可以使用 DateTime
模块对 Dates.You 执行操作 可以轻松地减去日期 如果您将两个日期放入 DateTime
对象。
要将较早的日期转换为 DateTime
,请使用 DateTime::Format::Strptime
解析这些日期,然后从今天的日期中减去它。
所以,总的来说这个脚本可以完成你的工作:
#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
use DateTime::Format::Strptime;
my @dates = ( "20150911", "20150705", "20150710" );
my $strp = DateTime::Format::Strptime->new( pattern => '%Y%m%d' ); # parse
my $today = DateTime->today; # get today's date
foreach (@dates) {
my $mydate = $strp->parse_datetime($_);
my $sub = $today->subtract_datetime($mydate)->{'days'};
if ( $sub > 5 ) {
print "$_ bad\n";
}
else {
print "$_ good\n";
}
}
您可以对 YYYYMMDD 字符串进行字符串比较。 date
使用 -d
:
处理人类可读的日期
#!/bin/bash
old=20150905
new=201510017
good=20150911
before=$(date -d 'today - 5 days' +%Y%m%d)
after=$(date -d 'today + 5 days' +%Y%m%d)
echo $before - $after
for date in $old $new $good ; do
if [[ $before < $date && $date < $after ]] ; then
echo $date in the interval
else
echo $date outside of the interval
fi
done
Time::Piece 自 2007 年 5.10.0 以来已包含在标准 Perl 发行版中。
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
use Time::Seconds;
my $now = localtime;
while (<DATA>) {
chomp;
my $then = Time::Piece->strptime($_, '%Y%m%d');
my $status = 'bad';
if ($now - $then < 5 * ONE_DAY) {
$status = 'good';
}
say "$_ $status";
}
__DATA__
20150705
20150911
您也可以为此使用 Time::Local,它是内置的,可将任意时间和日期值转换为纪元时间以进行比较:
#!/usr/bin/perl --
use warnings;
use strict;
# compare string dates against the current date
# and output each one with a 'bad' or 'good' label
use Time::Local;
while (<DATA>) {
chomp;
my $this_time = timelocal(0,0,12, # time (dst-compliant)
substr($_, -2), # mday
substr($_, 4, 2) - 1, # month
substr($_, 0, 4) - 1900); # year
print "$_ ", (time() - $this_time <= (86400 * 5) ? 'good' : 'bad'), "\n";
}
__DATA__
20150705
20150911
输出:
$ perl script.pl
20150705 bad
20150911 good
$
但是请注意,随着时间的推移,此解决方案不会是 100%。换句话说,它依赖于五个 24 小时时间段,而不是五个实际日历日。
尝试:
#!/bin/bash
TODAY=`date +%s`
FIVE_DAYS=$((5 * 24 * 60 * 60))
for d in '20150705' '20150911'; do
t=`date -d $d +%s`
seconds=$((TODAY - t))
[ $seconds -gt $FIVE_DAYS ] && result=bad || result=good
echo "$d $result"
done
我想将字符串中的时间戳与当前系统日期进行比较,如果它小于当前系统日期的 5 天,则变为错误状态,例如,我的意思是这些日期:
20150705
20150911
我想知道如何将它们与当前系统日期进行比较,并在当前系统时间为 2015 年 9 月 11 日时显示这样的输出:
20150705 bad
20150911 good
尝试将日期字符串转换为 unix 时间,例如
cur_date=$(date +"%s");
cat data_file | while read line;
do
lineDate=$(date -d "$line" +"%s");
diff=$(expr $cur_date - $lineDate);
if [ "$diff" -gt 432000 ];
then echo $line " bad";
else
echo $line " good";
fi;
done
在 Perl 中,您可以使用 DateTime
模块对 Dates.You 执行操作 可以轻松地减去日期 如果您将两个日期放入 DateTime
对象。
要将较早的日期转换为 DateTime
,请使用 DateTime::Format::Strptime
解析这些日期,然后从今天的日期中减去它。
所以,总的来说这个脚本可以完成你的工作:
#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
use DateTime::Format::Strptime;
my @dates = ( "20150911", "20150705", "20150710" );
my $strp = DateTime::Format::Strptime->new( pattern => '%Y%m%d' ); # parse
my $today = DateTime->today; # get today's date
foreach (@dates) {
my $mydate = $strp->parse_datetime($_);
my $sub = $today->subtract_datetime($mydate)->{'days'};
if ( $sub > 5 ) {
print "$_ bad\n";
}
else {
print "$_ good\n";
}
}
您可以对 YYYYMMDD 字符串进行字符串比较。 date
使用 -d
:
#!/bin/bash
old=20150905
new=201510017
good=20150911
before=$(date -d 'today - 5 days' +%Y%m%d)
after=$(date -d 'today + 5 days' +%Y%m%d)
echo $before - $after
for date in $old $new $good ; do
if [[ $before < $date && $date < $after ]] ; then
echo $date in the interval
else
echo $date outside of the interval
fi
done
Time::Piece 自 2007 年 5.10.0 以来已包含在标准 Perl 发行版中。
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
use Time::Seconds;
my $now = localtime;
while (<DATA>) {
chomp;
my $then = Time::Piece->strptime($_, '%Y%m%d');
my $status = 'bad';
if ($now - $then < 5 * ONE_DAY) {
$status = 'good';
}
say "$_ $status";
}
__DATA__
20150705
20150911
您也可以为此使用 Time::Local,它是内置的,可将任意时间和日期值转换为纪元时间以进行比较:
#!/usr/bin/perl --
use warnings;
use strict;
# compare string dates against the current date
# and output each one with a 'bad' or 'good' label
use Time::Local;
while (<DATA>) {
chomp;
my $this_time = timelocal(0,0,12, # time (dst-compliant)
substr($_, -2), # mday
substr($_, 4, 2) - 1, # month
substr($_, 0, 4) - 1900); # year
print "$_ ", (time() - $this_time <= (86400 * 5) ? 'good' : 'bad'), "\n";
}
__DATA__
20150705
20150911
输出:
$ perl script.pl
20150705 bad
20150911 good
$
但是请注意,随着时间的推移,此解决方案不会是 100%。换句话说,它依赖于五个 24 小时时间段,而不是五个实际日历日。
尝试:
#!/bin/bash
TODAY=`date +%s`
FIVE_DAYS=$((5 * 24 * 60 * 60))
for d in '20150705' '20150911'; do
t=`date -d $d +%s`
seconds=$((TODAY - t))
[ $seconds -gt $FIVE_DAYS ] && result=bad || result=good
echo "$d $result"
done