TIMTOWTDI! :-)
my $day = 28;
my $weekday = 'Thu';
my $year = '2014';
my @months = ('Aug', 'Sep', 'Oct');
my @mons = qw/ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec /;
my %mons = map {$mons[$_]=>$_+1} 0..$#mons;
@months = map {$mons{$_}} @months; # convert @months to numerical
my @dow = qw/ Sun Mon Tue Wed Thu Fri Sat /;
my %dow = map {$dow[$_]=>$_} 0..$#dow;
$weekday = $dow{$weekday}; # convert $weekday to numerical
use POSIX qw/mktime/; # core
use Time::Piece (); # core since v5.10
use DateTime (); # CPAN
use Date::Calc qw/Day_of_Week/; # CPAN
use Date::Manip qw/Date_DayOfWeek/; # CPAN
for my $mon (@months) {
my $date = sprintf "%04d-%02d-%02d", $year, $mon, $day;
print " mktime: $date\n" # 0 = Sunday
if (localtime mktime(0,0,0,$day,$mon-1,$year-1900))[6]
== $weekday;
print "Time::Piece: $date\n" # 0 = Sunday
if Time::Piece->strptime("$day-$mon-$year","%d-%m-%Y")
->day_of_week == $weekday;
print " DateTime: $date\n" # 1 = Monday, 7 = Sunday
if DateTime->new(year=>$year, month=>$mon, day=>$day)
->dow%7 == $weekday;
print " Date::Calc: $date\n" # 1 = Monday, 7 = Sunday
if Day_of_Week($year,$mon,$day)%7 == $weekday;
print "Date::Manip: $date\n" # 1 = Monday, 7 = Sunday
if Date_DayOfWeek($mon,$day,$year)%7 == $weekday;
}
__END__
# Output:
mktime: 2014-08-28
Time::Piece: 2014-08-28
DateTime: 2014-08-28
Date::Calc: 2014-08-28
Date::Manip: 2014-08-28
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.