Last Friday of each month: Difference between revisions
Content added Content deleted
Line 3,140: | Line 3,140: | ||
2012-12-28 |
2012-12-28 |
||
</pre> |
</pre> |
||
=={{header|Picat}}== |
|||
<lang Picat>% for command line argument |
|||
main(ARGV) => |
|||
if ARGV.length > 0 then |
|||
Year = ARGV[1].to_integer(), |
|||
show_year(Year), |
|||
nl |
|||
end. |
|||
% Without command line argument |
|||
main => go. |
|||
go => |
|||
show_year(2022), |
|||
nl. |
|||
% Show the months |
|||
show_year(Year) => |
|||
foreach(Date in get_months(Year)) |
|||
println(format_date(Date)) |
|||
end, |
|||
nl. |
|||
% Format date to YYYY-DD-MM |
|||
format_date(Date) = to_fstring("%4d-%02d-%02d",Date[1],Date[2],Date[3]). |
|||
% Return the last Fridays of each month for year Year |
|||
get_months(Year) = |
|||
[ [ [Year,Month,Day] : Day in 1..max_days_in_month(Year,Month), |
|||
dow(Year, Month, Day) == 5].last() : Month in 1..12]. |
|||
% Day of week, Sakamoto's method |
|||
dow(Y, M, D) = R => |
|||
T = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4], |
|||
if M < 3 then |
|||
Y := Y - 1 |
|||
end, |
|||
R = (Y + Y // 4 - Y // 100 + Y // 400 + T[M] + D) mod 7. |
|||
% Maximum days in month |
|||
max_days_in_month(Year,Month) = Days => |
|||
if member(Month, [1,3,5,7,8,10,12]) then |
|||
Days = 31 |
|||
elseif member(Month,[4,6,9,11]) then |
|||
Days = 30 |
|||
else |
|||
if leap_year(Year) then |
|||
Days = 29 |
|||
else |
|||
Days = 28 |
|||
end |
|||
end. |
|||
% Is Year a leap year? |
|||
leap_year(Year) => |
|||
(Year mod 4 == 0, Year mod 100 != 0) |
|||
; |
|||
Year mod 400 == 0. </lang> |
|||
There are several ways to run this program; let's call it "last_friday_of_each_month.pi": |
|||
From Picat's shell: |
|||
<pre> |
|||
$ picat |
|||
Picat> cl(last_friday_of_each_month) |
|||
Picat> show_year(2022) |
|||
2022-01-28 |
|||
2022-02-25 |
|||
2022-03-25 |
|||
2022-04-29 |
|||
2022-05-27 |
|||
2022-06-24 |
|||
2022-07-29 |
|||
2022-08-26 |
|||
2022-09-30 |
|||
2022-10-28 |
|||
2022-11-25 |
|||
2022-12-30 |
|||
</pre> |
|||
From the command line: |
|||
* With the year as a parameter, via main(ARGV): |
|||
<pre>$ picat last_friday_of_each_month.pi 2022</pre> |
|||
* As a goal: |
|||
<pre>$ picat -g "show_year(2022)" last_friday_of_each_month.pi</pre> |
|||
* Run the default goal (go/0): |
|||
<pre>$ picat last_friday_of_each_month.pi</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |