如何使用当地时间获取昨天的日期?

发布于 2024-09-15 11:55:13 字数 417 浏览 7 评论 0原文

如何调整它以使用当地时间获取昨天的日期?

use strict;

sub spGetCurrentDateTime;
print spGetCurrentDateTime;

sub spGetCurrentDateTime {
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $currentDateTime = sprintf "%s %02d %4d", $abbr[$mon], $mday, $year+1900; #Returns => 'Aug 17 2010' 
return $currentDateTime;
}

How do I tweak this to get yesterday's date using localtime?

use strict;

sub spGetCurrentDateTime;
print spGetCurrentDateTime;

sub spGetCurrentDateTime {
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $currentDateTime = sprintf "%s %02d %4d", $abbr[$mon], $mday, $year+1900; #Returns => 'Aug 17 2010' 
return $currentDateTime;
}

~

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

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

发布评论

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

评论(8

朕就是辣么酷 2024-09-22 11:55:13
use DateTime qw();
DateTime->now->subtract(days => 1); 

第二行的表达式返回一个 DateTime 对象。

use DateTime qw();
DateTime->now->subtract(days => 1); 

The expression on the second line returns a DateTime object.

成熟稳重的好男人 2024-09-22 11:55:13

尽管从当前时间减去一天的秒数很诱人,但有时这会产生错误的答案(闰秒、夏令时,以及可能的其他)。我发现让 strftime (在 Perl 5 核心模块中可用 POSIX)为我处理所有这些事情。

#!/usr/bin/perl

use strict;
use warnings;

use Time::Local;
use POSIX qw/strftime/;

#2010-03-15 02:00:00
my ($s, $min, $h, $d, $m, $y) = (0, 0, 0, 15, 2, 110);

my $time      = timelocal $s, $min, $h, $d, $m, $y;    
my $today     = strftime "%Y-%m-%d %T", localtime $time;
my $yesterday = strftime "%Y-%m-%d %T", $s, $min, $h, $d - 1, $m, $y;
my $oops      = strftime "%Y-%m-%d %T", localtime $time - 24*60*60;
print "$today -> $yesterday -> $oops\n";

As tempting as it is to just subtract a day's worth of seconds from the current time, there are times when this will yield the wrong answer (leap seconds, DST, and possibly others). I find it easier to just let strftime (available in the Perl 5 core module POSIX) take care of all of that for me.

#!/usr/bin/perl

use strict;
use warnings;

use Time::Local;
use POSIX qw/strftime/;

#2010-03-15 02:00:00
my ($s, $min, $h, $d, $m, $y) = (0, 0, 0, 15, 2, 110);

my $time      = timelocal $s, $min, $h, $d, $m, $y;    
my $today     = strftime "%Y-%m-%d %T", localtime $time;
my $yesterday = strftime "%Y-%m-%d %T", $s, $min, $h, $d - 1, $m, $y;
my $oops      = strftime "%Y-%m-%d %T", localtime $time - 24*60*60;
print "$today -> $yesterday -> $oops\n";
微暖i 2024-09-22 11:55:13

DST 问题可以通过从今天中午开始计算 3600 秒而不是当前时间来解决:

#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;

sub spGetYesterdaysDate;
print spGetYesterdaysDate;

sub spGetYesterdaysDate {
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
my $yesterday_midday=timelocal(0,0,12,$mday,$mon,$year) - 24*60*60;
($sec, $min, $hour, $mday, $mon, $year) = localtime($yesterday_midday);
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $YesterdaysDate = sprintf "%s %02d %4d", $abbr[$mon], $mday, $year+1900;
return $YesterdaysDate;
}

鉴于 Chas 建议的 strftime 解决方案的“未指定”记录行为,如果您无法测试,这种方法可能会更好跨多个平台的预期但不保证的结果。

The DST problem can be worked around by taking 3600s from midday today instead of the current time:

#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;

sub spGetYesterdaysDate;
print spGetYesterdaysDate;

sub spGetYesterdaysDate {
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
my $yesterday_midday=timelocal(0,0,12,$mday,$mon,$year) - 24*60*60;
($sec, $min, $hour, $mday, $mon, $year) = localtime($yesterday_midday);
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $YesterdaysDate = sprintf "%s %02d %4d", $abbr[$mon], $mday, $year+1900;
return $YesterdaysDate;
}

In light of the "unspecified" documented behaviour of the strftime solution suggested by Chas, this approach might be better if you're not able to test for expected-but-not-guaranteed results across multiple platforms.

夏尔 2024-09-22 11:55:13

使用 Time::Piece

use strict;
use warnings;
use 5.010;

# These are core modules in Perl 5.10 and newer
use Time::Piece;
use Time::Seconds;

my $yesterday = localtime() - ONE_DAY;
say $yesterday->strftime('%b %d %Y');

请注意,在某些临界情况下,例如夏令时开始,这可能会出错。
在这种情况下,以下版本的行为确实正确:

use strict;
use warnings;
use 5.010;

# These are core modules in Perl 5.10 and newer
use Time::Piece;
use Time::Seconds;

my $now = localtime();
my $yesterday = $now - ONE_HOUR*($now->hour + 12);
say $yesterday->strftime('%b %d %Y');

或者,您可以使用 DateTime 模块,如另一个答案中所述。但这不是核心模块。

use Time::Piece.

use strict;
use warnings;
use 5.010;

# These are core modules in Perl 5.10 and newer
use Time::Piece;
use Time::Seconds;

my $yesterday = localtime() - ONE_DAY;
say $yesterday->strftime('%b %d %Y');

Note that this can go wrong in certain borderline cases, such as the start of daylight saving time.
The following version does behave correct in such cases:

use strict;
use warnings;
use 5.010;

# These are core modules in Perl 5.10 and newer
use Time::Piece;
use Time::Seconds;

my $now = localtime();
my $yesterday = $now - ONE_HOUR*($now->hour + 12);
say $yesterday->strftime('%b %d %Y');

Alternatively, you can use the DateTime module as described in a different answer. That is not a core module, though.

轮廓§ 2024-09-22 11:55:13

大多数用户建议的解决方案是错误的!

localtime(time() - 24*60*60)

您能做的最糟糕的事情就是假设1天= 86400秒。

示例:时区是 America/New_York,日期是 Mon Apr 3 00:30:00 2006

timelocal 为我们提供 1144038600

当地时间(1144038600 - 86400) = 2006 年美国东部时间 4 月 1 日星期六 23:30:00

哎呀!

正确且唯一的解决方案是让系统函数规范化值

$prev_day = timelocal(0, 0, 0, $mday-1, $mon, $year);

,或者让日期时间框架(DateTime、Class::Date 等) 执行相同的操作。

就是这样。

Solution suggested by most users is wrong!

localtime(time() - 24*60*60)

The worst thing you can do is to assume that 1 day = 86400 seconds.

Example: Timezone is America/New_York, date is Mon Apr 3 00:30:00 2006

timelocal gives us 1144038600

localtime(1144038600 - 86400) = Sat Apr 1 23:30:00 EST 2006

oops!

The right and the only solution is to let system function normalize values

$prev_day = timelocal(0, 0, 0, $mday-1, $mon, $year);

Or let datetime frameworks (DateTime, Class::Date, etc) do the same.

That's it.

怪我鬧 2024-09-22 11:55:13
localtime(time() - 24*60*60)
localtime(time() - 24*60*60)
冰雪之触 2024-09-22 11:55:13

我的 $yesterday = time();
$yesterday = $yesterday - (24*60*60);

24 表示 24 小时,60 表示小时 60 分钟,60 表示分钟 60 秒
time() 将返回实际时间戳,246060 将删除一天的秒数
之后只需执行以下操作:
当地时间($昨天);

my $yesterday = time();
$yesterday = $yesterday - (24*60*60);

24 as 24 hours, 60 as 60 minutes in hour and 60 as 60 seconds in minute
time() will return actual timestamp, and 246060 will remove seconds for exactly one day
After this simply do:
localtime($yesterday);

半窗疏影 2024-09-22 11:55:13
This is how I do it.

#!/usr/bin/perl

use POSIX qw(strftime);

$epoc = time();
$epoc = $epoc - 24 * 60 * 60;

$datestring = strftime "%F", localtime($epoc);

print "Yesterday's date is $datestring \n";
This is how I do it.

#!/usr/bin/perl

use POSIX qw(strftime);

$epoc = time();
$epoc = $epoc - 24 * 60 * 60;

$datestring = strftime "%F", localtime($epoc);

print "Yesterday's date is $datestring \n";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文