Day of the week: Difference between revisions

Perl 5: Removed all but the most concise solution. Perl 6: Added.
(Perl 5: Removed all but the most concise solution. Perl 6: Added.)
Line 813:
=={{header|Perl}}==
 
Alternatively inIn one line using grep (read from right to left):
<lang perl>#! /usr/bin/perl -w
 
<lang perl>use DateTime;
use Time::Local;
use strict;
 
print join " ", grep { DateTime->new(year => $_, month => 12, day => 25)->day_of_week == 7 } (2008 .. 2121);</lang>
foreach my $i (2008 .. 2121)
{
my $time = timelocal(0,0,0,25,11,$i);
my ($s,$m,$h,$md,$mon,$y,$wd,$yd,$is) = localtime($time);
if ( $wd == 0 )
{
print "25 Dec $i is Sunday\n";
}
}
 
exit 0;</lang>
 
Output:
<pre>2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118</pre>
 
=={{header|Perl 6}}==
<pre>
25 Dec 2011 is Sunday
25 Dec 2016 is Sunday
25 Dec 2022 is Sunday
25 Dec 2033 is Sunday
Day too big - 25195 > 24855
Sec too small - 25195 < 78352
Sec too big - 25195 > 15247
Cannot handle date (0, 0, 0, 25, 11, 2038) at ./ydate.pl line 8
</pre>
 
{{works with|Rakudo|2010.07}}
Using the DateTime module from CPAN:
<lang perl>#! /usr/bin/perl -w
 
As Perl 5, except <code>DateTime</code> is built-in, so you don't need to download a module of that name:
use DateTime;
use strict;
 
<lang perl6>say join ' ', grep { Date.new($_, 12, 25).day-of-week == 7 }, 2008 .. 2121;</lang>
foreach my $i (2008 .. 2121)
{
my $dt = DateTime->new( year => $i,
month => 12,
day => 25
);
if ( $dt->day_of_week == 7 )
{
print "25 Dec $i is Sunday\n";
}
}
 
exit 0;</lang>
or shorter:
<lang perl>#! /usr/bin/perl -w
 
use DateTime;
use strict;
for (2008 .. 2121) {
print "25 Dec $_ is Sunday\n"
if DateTime->new(year => $_, month => 12, day => 25)->day_of_week == 7;
}
 
exit 0;</lang>
Output:
 
<pre>
25 Dec 2011 is Sunday
25 Dec 2016 is Sunday
25 Dec 2022 is Sunday
25 Dec 2033 is Sunday
25 Dec 2039 is Sunday
25 Dec 2044 is Sunday
25 Dec 2050 is Sunday
25 Dec 2061 is Sunday
25 Dec 2067 is Sunday
25 Dec 2072 is Sunday
25 Dec 2078 is Sunday
25 Dec 2089 is Sunday
25 Dec 2095 is Sunday
25 Dec 2101 is Sunday
25 Dec 2107 is Sunday
25 Dec 2112 is Sunday
25 Dec 2118 is Sunday
</pre>
 
Alternatively in one line using grep (read from right to left):
<lang perl>#! /usr/bin/perl -w
 
use DateTime;
use strict;
 
print join " ", grep { DateTime->new(year => $_, month => 12, day => 25)->day_of_week == 7 } (2008 .. 2121);
 
0;</lang>
Output:
<pre>2011 2016 2022 2033 2039 2044 2050 2061 2067 2072 2078 2089 2095 2101 2107 2112 2118</pre>
 
=={{header|PHP}}==
845

edits