PERL:我从本地时间获取 mday 值,如下所示。我如何从本地时间获取的 mday 中减去 1
PERL : I take mday value from locatime as below , . How can I subtract 1 from mday which I take from localtime
PERL:我从 locatime 中获取 mday 值如下,现在我想要前天的值。我如何从本地时间
中获取的 mday 中减去 1
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
my $part = "P".$mday;
print "Today value is $part \n";
my $part_yes = "P".$mday - $num;
print "$part_yes \n";
my $now = time(); # time as seconds since 1970-01-01
my $day_ago = $now - 24*60*60;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($day_ago);
my $part = "P".$mday;
警告(ikegami 评论):并非所有的日子都有 24 小时。这可以产生提前 0、1 或 2 天的结果
https://perldoc.perl.org/functions/time
https://perldoc.perl.org/functions/localtime
使用日期时间:
my $dt =
DateTime
->now( time_zone => 'local' )
->set_time_zone('floating') # Do this when working with dates.
->truncate( to => 'days' ); # Optional.
$dt->subtract( days => 2 );
my $yesterday = $dt->day;
DateTime 非常繁重,似乎人们问 date-time 问题总是回来说“只有核心模块!”,所以这里有一个只使用核心模块的解决方案。
use Time::Local qw( timegm );
# Create an timestamp with the same date in UTC as the one local one.
my $epoch = timegm(0, 0, 0, ( localtime() )[3,4,5]);
# We can now do date arithmetic without having to worry about DST switches.
$epoch -= 2 * 24*60*60;
my $yesterday = ( gmtime($epoch) )[3] + 1;
PERL:我从 locatime 中获取 mday 值如下,现在我想要前天的值。我如何从本地时间
中获取的 mday 中减去 1my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
my $part = "P".$mday;
print "Today value is $part \n";
my $part_yes = "P".$mday - $num;
print "$part_yes \n";
my $now = time(); # time as seconds since 1970-01-01
my $day_ago = $now - 24*60*60;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($day_ago);
my $part = "P".$mday;
警告(ikegami 评论):并非所有的日子都有 24 小时。这可以产生提前 0、1 或 2 天的结果
https://perldoc.perl.org/functions/time
https://perldoc.perl.org/functions/localtime
使用日期时间:
my $dt =
DateTime
->now( time_zone => 'local' )
->set_time_zone('floating') # Do this when working with dates.
->truncate( to => 'days' ); # Optional.
$dt->subtract( days => 2 );
my $yesterday = $dt->day;
DateTime 非常繁重,似乎人们问 date-time 问题总是回来说“只有核心模块!”,所以这里有一个只使用核心模块的解决方案。
use Time::Local qw( timegm );
# Create an timestamp with the same date in UTC as the one local one.
my $epoch = timegm(0, 0, 0, ( localtime() )[3,4,5]);
# We can now do date arithmetic without having to worry about DST switches.
$epoch -= 2 * 24*60*60;
my $yesterday = ( gmtime($epoch) )[3] + 1;