在 Perl 中,我如何找到上周四的日期?

发布于 2024-12-17 11:56:28 字数 290 浏览 1 评论 0原文

可能的重复:
在 Perl 中,如何找到给定日期的上周一的日期?

在perl中,如何编写一个函数来获取上周四的日期?今天(11/21),我想要11/17。如何判断给定日期是否是星期日?

Possible Duplicate:
In Perl, how can find the date of the previous Monday for a given date?

In the perl, how to write a function to get last Thursday's date? Today (11/21), I want to get 11/17. How can I figure out if a given date is a Sunday or not?

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

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

发布评论

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

评论(8

灯下孤影 2024-12-24 11:56:29

看起来像是 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;

Seems like a work for DateTime (which is the module usually recommended to manipulate dates in Perl). This small example should answer both your questions:

use strict;
use warnings;

use DateTime;

my $d = DateTime->now;

while ( $d->day_of_week != 4 ) {
    $d->subtract( days => 1 );
}

print $d->ymd;
赴月观长安 2024-12-24 11:56:29

其他帖子也使用 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

Other posts use DateTime too, but they have issues this post addresses.

use DateTime qw( );
my $dt = DateTime->today( time_zone => 'local' );
$dt->subtract( days => ($dt->day_of_week - 4) % 7 );
say $dt->ymd('');  # 20111117

Issues addressed:

  • Uses the local date instead of the date in or near England.
  • Uses today instead of now since you're only dealing with dates.
  • Avoids needless loops.

Note:

  • Returns the current date is today is a Thursday. You accepted an answer that did the same, so I presume that's what you want.

Update: The above can fail on certain days for certain time zones. (Not all days have a midnight!) Solution:

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
苦行僧 2024-12-24 11:56:29

我会使用拉森写的内容,尽管阅读 本地时间参考 不会伤害你。你可以这样做来检查是否是周日:

($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, 
                         $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
if ($dayOfWeek == 0) print("Don't go to work!");

I'd use what larsen wrote, although reading the localtime reference won't hurt you. You could do something like this to check if its Sunday:

($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, 
                         $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
if ($dayOfWeek == 0) print("Don't go to work!");
寻找我们的幸福 2024-12-24 11:56:29

如果您想要测试的只是星期几,则 localtime() 的数组切片可以很好地工作:

print q(Today is Sunday) if ((localtime)[6]==0)

If all you wanted to test was the day-of-the-week an array slice of the localtime() works nicely:

print q(Today is Sunday) if ((localtime)[6]==0)
沩ん囻菔务 2024-12-24 11:56:29

您还可以使用 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";

You can also use Date::Calc:

How do I calculate the last and the next Saturday for any given date?

  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";
她如夕阳 2024-12-24 11:56:29
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)
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)
呢古 2024-12-24 11:56:29

仅使用 localtimePOSIX 基础模块,您可以这样做:

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] 
    );

With just localtime and the POSIX base module, you can do this:

my @ltime = localtime();
my $t 
    = POSIX::mktime(( 0 ) x 3
    , $ltime[3] - ( 7 - 4 + $ltime[6] )
    , @ltime[4,5] 
    );

This will always give you the last Thursday before the last Sunday. So if the day is Saturday, it will give you the previous week's Thursday, but if it's Sunday, it will give you the last past Thursday.

If you prefer the last past Thursday computation, you might do this:

my $t 
    = POSIX::mktime(( 0 ) x 3
    , $ltime[3] - $ltime[6] + ( $ltime[6] > 4 ?  4 : -3 )
    , @ltime[4,5] 
    );
未央 2024-12-24 11:56:29

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");

Date::Simple has a pretty clean interface.

#!/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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文