2013-07-30 34 views
-3

啦.. 在Perl中,我們如何檢查它是否是這個月的第一個星期六,如果這是真的調用另一個函數..的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

['的perldoc -f localtime'](http://search.cpan.org/perldoc?perlfunc#localtime) – mob

回答

0

我喜歡Time::Piece,自Perl 5.10以來,它就被包含在標準模塊中。不幸的是,它不包括月的方法。但是,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"; 
} 

你不能更容易地看到什麼你的代碼是不是該做的事情。

+0

感謝您的簡單代碼,但不幸的是我不能使用Date:Handler ..我有perl vesion-5.8.2當我嘗試使用Date:Handler時,它給了我錯誤「無法找到對象方法」使用新處理程序? – Khallas301

+0

您可以安裝Date :: Handler,它可以在5.8上運行:http://www.cpantesters.org/distro/D/Date-Handler.html#Date-Handler-1.2 – mirod

+0

不幸的是,我沒有權限安裝和它將需要很長時間才能獲得管理層的批准.. :( – Khallas301

5

也許是這樣的(本地時間的使用@暴民的建議):

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; 
} 

您可能想將日期傳遞給函數,而不是假設今天。

+0

感謝@ Scroog1所以函數將檢查第一Satuday和我可以使用它在if..else條件還是我需要檢查功能的真/假? – Khallas301

+0

@ Khallas301如果是第一個星期六,此函數將返回_true_,否則返回_false_。 –

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