Last Friday of each month: Difference between revisions
Content added Content deleted
(→{{header|Pike}}: forgot the commandline argument) |
|||
Line 18: | Line 18: | ||
2012-12-28</pre> |
2012-12-28</pre> |
||
=={{header|Icon}} and {{header|Unicon}}== |
|||
This will write the last fridays for every year given as an argument. There is no error checking on the year. |
|||
<lang Icon>procedure main(A) |
|||
every write(lastfridays(!A)) |
|||
end |
|||
procedure lastfridays(year) |
|||
every m := 1 to 12 do { |
|||
d := case m of { |
|||
2 : if IsLeapYear(year) then 29 else 28 |
|||
4|6|9|11 : 30 |
|||
default : 31 |
|||
} # last day of month |
|||
z := 0 |
|||
j := julian(m,d,year) + 1 # first day of next month |
|||
until (j-:=1)%7 = 4 do z -:=1 # backup to last friday=4 |
|||
suspend sprintf("%d-%d-%d",year,m,d+z) |
|||
} |
|||
end |
|||
link datetime, printf</lang> |
|||
{{libheader|Icon Programming Library}} |
|||
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides formatting] |
|||
[http://www.cs.arizona.edu/icon/library/src/procs/datetime.icn datetime.icn provides julian and IsLeapYear] |
|||
Output:<pre>last_fridays.exe 2012 |
|||
2012-1-27 |
|||
2012-2-24 |
|||
2012-3-30 |
|||
2012-4-27 |
|||
2012-5-25 |
|||
2012-6-29 |
|||
2012-7-27 |
|||
2012-8-31 |
|||
2012-9-28 |
|||
2012-10-26 |
|||
2012-11-30 |
|||
2012-12-28</pre> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
Revision as of 12:01, 5 November 2011
Last Friday of each month is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Write a program or a script that returns the last Fridays of each months of a given year provided as argument on the command line.
Example of an expected output:
./last_fridays 2012 2012-01-27 2012-02-24 2012-03-30 2012-04-27 2012-05-25 2012-06-29 2012-07-27 2012-08-31 2012-09-28 2012-10-26 2012-11-30 2012-12-28
Icon and Unicon
This will write the last fridays for every year given as an argument. There is no error checking on the year.
<lang Icon>procedure main(A) every write(lastfridays(!A)) end
procedure lastfridays(year) every m := 1 to 12 do {
d := case m of { 2 : if IsLeapYear(year) then 29 else 28 4|6|9|11 : 30 default : 31 } # last day of month z := 0 j := julian(m,d,year) + 1 # first day of next month until (j-:=1)%7 = 4 do z -:=1 # backup to last friday=4 suspend sprintf("%d-%d-%d",year,m,d+z) }
end
link datetime, printf</lang>
printf.icn provides formatting datetime.icn provides julian and IsLeapYear
Output:
last_fridays.exe 2012 2012-1-27 2012-2-24 2012-3-30 2012-4-27 2012-5-25 2012-6-29 2012-7-27 2012-8-31 2012-9-28 2012-10-26 2012-11-30 2012-12-28
OCaml
<lang ocaml>#load "unix.cma" open Unix
let usage() =
Printf.eprintf "%s <year>\n" Sys.argv.(0); exit 1
let print_date t =
Printf.printf "%d-%02d-%02d\n" (t.tm_year + 1900) (t.tm_mon + 1) t.tm_mday
let is_date_ok tm t =
(tm.tm_year = t.tm_year && tm.tm_mon = t.tm_mon && tm.tm_mday = t.tm_mday)
let () =
let _year = try int_of_string Sys.argv.(1) with _ -> usage() in let year = _year - 1900 in let fridays = Array.make 12 (Unix.gmtime 0.0) in for month = 0 to 11 do for day_of_month = 1 to 31 do let tm = { (Unix.gmtime 0.0) with tm_year = year; tm_mon = month; tm_mday = day_of_month; } in let _, t = Unix.mktime tm in if is_date_ok tm t (* check for months that have less than 31 days *) && t.tm_wday = 5 (* is a friday *) then fridays.(month) <- t done; done; Array.iter print_date fridays</lang>
Output:
$ ocaml last_fridays.ml 2012 2012-01-27 2012-02-24 2012-03-30 2012-04-27 2012-05-25 2012-06-29 2012-07-27 2012-08-31 2012-09-28 2012-10-26 2012-11-30 2012-12-28
Pike
<lang Pike>int(0..1) last_friday(object day) {
return day->week_day() == 5 && day->month_day() > day->month()->number_of_days()-7;
}
int main(int argc, array argv) {
array days = filter(Calendar.Year((int)argv[1])->months()->days()[*], last_friday); write("%{%s\n%}", days->format_ymd()); return 0;
}</lang>