可能重复:
In Perl, how can find the date of the previous Monday for a given date?
在perl中,如何编写一个函数来获取上周四的日期?今天(11月21日),我想要11/17。如何判断给定日期是否为星期日?
答案 0 :(得分:7)
似乎是DateTime的工作(通常建议在Perl中操作日期的模块)。这个小例子应该回答你的两个问题:
use strict;
use warnings;
use DateTime;
my $d = DateTime->now;
while ( $d->day_of_week != 4 ) {
$d->subtract( days => 1 );
}
print $d->ymd;
答案 1 :(得分:6)
其他帖子也使用DateTime,但他们发布了这个帖子地址。
use DateTime qw( );
my $dt = DateTime->today( time_zone => 'local' );
$dt->subtract( days => ($dt->day_of_week - 4) % 7 );
say $dt->ymd(''); # 20111117
解决的问题:
today
代替now
,因为您只处理日期。注意:
更新:上述某些时区的某些日期可能会失败。 (并非所有日子都有午夜!)解决方案:
use DateTime qw( );
my $dt = DateTime->now( time_zone => 'local' );
$dt->set_time_zone('floating');
$dt->truncate( to => 'days' );
$dt->subtract( days => ($dt->day_of_week - 4) % 7 );
say $dt->ymd(''); # 20111117
答案 2 :(得分:1)
我会使用larsen写的内容,虽然阅读localtime reference不会伤害你。你可以做这样的事情来检查它的星期日:
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset,
$dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
if ($dayOfWeek == 0) print("Don't go to work!");
答案 3 :(得分:0)
如果您想要测试的是星期几,那么localtime()的数组切片可以正常工作:
print q(Today is Sunday) if ((localtime)[6]==0)
答案 4 :(得分:0)
您还可以使用Date::Calc:
如何计算任何指定日期的最后一个和下一个星期六?
use Date::Calc qw( Today Day_of_Week Add_Delta_Days
Day_of_Week_to_Text Date_to_Text );
$searching_dow = 6; # 6 = Saturday
@today = Today();
$current_dow = Day_of_Week(@today);
if ($searching_dow == $current_dow)
{
@prev = Add_Delta_Days(@today,-7);
@next = Add_Delta_Days(@today,+7);
}
else
{
if ($searching_dow > $current_dow)
{
@next = Add_Delta_Days(@today,
$searching_dow - $current_dow);
@prev = Add_Delta_Days(@next,-7);
}
else
{
@prev = Add_Delta_Days(@today,
$searching_dow - $current_dow);
@next = Add_Delta_Days(@prev,+7);
}
}
$dow = Day_of_Week_to_Text($searching_dow);
print "Today is: ", ' ' x length($dow),
Date_to_Text(@today), "\n";
print "Last $dow was: ", Date_to_Text(@prev), "\n";
print "Next $dow will be: ", Date_to_Text(@next), "\n";
答案 5 :(得分:0)
use Date::Calc qw(:all);
($week, $year) = Week_of_Year(Today());# Week_of_Year(2011,11,21);
print Date_to_Text(Add_Delta_Days(Monday_of_Week($week - 1,$year),3));#THU - MON = 3
#output:Thu 17-Nov-2011
#isSunday?
Day_of_Week(2011,11,20) == 7 # 7 is Sunday enum code(from 1 : Monday)
答案 6 :(得分:0)
Date :: Simple有一个非常干净的界面。
#!/usr/bin/perl
use strict;
use warnings;
use Date::Simple qw/ today /;
my $d = today;
# If today is already Thurs, subtract 1 before searching for prior Thursday.
# If today is Thurs and you want to capture that (and not Thur a week ago),
# don't subtract the 1 from $d.
$d--;
$d-- while $d->day_of_week != 4;
print $d->strftime("%Y%m%d");
答案 7 :(得分:0)
只使用localtime
和POSIX
基础模块,您可以执行以下操作:
my @ltime = localtime();
my $t
= POSIX::mktime(( 0 ) x 3
, $ltime[3] - ( 7 - 4 + $ltime[6] )
, @ltime[4,5]
);
这将始终为您提供上周日之前的最后一个星期四。因此,如果这一天是星期六,它会给你前一周的星期四,但如果它是星期天,它会给你最后一个星期四。
如果你更喜欢上周四的计算,你可以这样做:
my $t
= POSIX::mktime(( 0 ) x 3
, $ltime[3] - $ltime[6] + ( $ltime[6] > 4 ? 4 : -3 )
, @ltime[4,5]
);