寻找以 DDMMYY 格式生成昨天日期的 Perl 5.12 代码 [英] Looking for Perl 5.12 code that generates yesterday's date in DDMMYY format
本文介绍了寻找以 DDMMYY 格式生成昨天日期的 Perl 5.12 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一种简单的方法来获取昨天的日期(当地时间)作为 Perl 脚本中的字符串.最好,我希望在没有模块依赖的情况下执行此操作,因为我们有一个基本的 Perl 5.12 安装.
I would like a simple way to get yesterday's date (local time) as a string in a Perl script. Preferably, I would like to do this with no module dependencies, since we have a bare bones Perl 5.12 installation.
所以,例如:
my $yesterdays_date=...; # Puts for example '301011' into $yesterdays_date,
# if today is October 31st, 2011
推荐答案
Time::一块
是核心:
use Time::Piece;
use Time::Seconds qw(ONE_DAY);
my $yesterday = localtime() - ONE_DAY();
print $yesterday->strftime('%d%m%y'), "\n";
如果您担心夏令时,您可以将当前时间标准化为接近中午:
If you're concerned about daylight savings, you can normalize the current time to near noon:
use Time::Piece;
use Time::Seconds qw(ONE_DAY ONE_HOUR);
my $today = localtime;
my $yesterday = $today + ONE_HOUR * ( 12 - $today->hour ) - ONE_DAY;
print $yesterday->strftime("%d%m%y"), "\n";
如果你可以忍受依赖,那么使用 DateTime
::>
If you can live with dependencies, then use DateTime
:
use DateTime;
print DateTime->now->subtract(days => 1)->strftime('%d%m%y'), "\n";
这篇关于寻找以 DDMMYY 格式生成昨天日期的 Perl 5.12 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文