-3

朋友.. 在 Perl 中,我们如何检查它是否是本月的第一个星期六,如果是真的,请调用另一个函数..

我在互联网上搜索,但我能找到的所有选项都是用于 ksh 脚本..

--PERL
use warning;
use strict;

IF first_saturday = TRUE
  THEN
     msg "It's First Saturday"
     call delete()
  ELSE
     msg "Not First Saturday
END IF

谢谢...

4

5 回答 5

5

也许是这样的(使用@mob 的本地时间建议):

sub is_first_saturday {
    my @time_elements = localtime(time);
    my $day_of_week = $time_elements[6];
    my $day_of_month = $time_elements[3];
    return $day_of_week == 6 && $day_of_month < 8;
}

您可能希望将日期传递给函数,而不是假设今天。

于 2013-07-30T15:54:39.523 回答
0

你可以试试Date::Manip模块。

use strict;
use Date::Manip;

$main::TZ= 'GMT';
print UnixDate(ParseDate("first Saturday in July 2013"),"First Saturday of the month is %B %E, %Y.");
于 2013-07-30T16:01:51.033 回答
0

我喜欢从 Perl 5.10 开始作为标准模块包含的Time::Piece 。不幸的是,它不包括每月一周的方法。但是,Date::Handler可以。

这是一个干净整洁的界面,让您的搜索内容一目了然。太糟糕了,它不是标准的 Perl 模块,所以你必须从 CPAN 安装它。

use Date::Handler;

....

my $date = Date::Handler->new($time);  #Time is std Unix # of seconds since 01/01/1970

# Is it the sixth day of the week (Mon = 1) and the first week of the month?
if ( $date->WeekDay == 6 and $date->WeekOfMonth == 1 ) {
    print "It's the first Saturday of the month!\n";
}

或者...

my $date = Date::Handler->new($time);

# Is it the first Saturday of the month?
if ( $date->WeekDayName eq "Saturday" and $date->WeekOfMonth == 1 ) {
    print "It's the first Saturday of the month!\n";
}

你不能比这更容易看到你的代码在做什么。

于 2013-07-30T16:12:33.537 回答
0
use strict;
use DateTime;

# day: 1 = Monday, ..., 7 = Sunday
# nth: 1 = 1st time a day appears, ..., 5 = last time a day appears in a month
sub is_nth_day_of_month {
    my ( $nth, $day ) = @_;

    my $now = DateTime->now;

    return $now->day_of_week == $day && $now->weekday_of_month == $nth;
}

print is_nth_day_of_month(1, 6) ? "Yes\n" :  "No\n";
于 2013-07-30T17:22:31.480 回答
0
#!usr/bin/perl -w
use strict;
my $day  = substr (`date`,0,3);
my $date = substr(`date`,8,2) ;
if($day =~/Wed/ and $date <= 7){
   print "Hey today is first Saturday\n";
}
于 2013-07-30T20:47:58.057 回答