Last Friday of each month: Difference between revisions

jq
(jq)
Line 676:
2012 Dec 28</pre>
 
 
=={{header|jq}}==
{{ works with|jq|1.4}}
'''Foundations'''
<lang jq># In case your jq does not have "until" defined:
 
def until(cond; next):
def _until:
if cond then . else (next|_until) end;
_until;
 
# Zeller's Congruence is from [[Day_of_the_week#jq]]
 
# Use Zeller's Congruence to determine the day of the week, given
# year, month and day as integers in the conventional way.
# If iso == "iso" or "ISO", then emit an integer in 1 -- 7 where
# 1 represents Monday, 2 Tuesday, etc;
# otherwise emit 0 for Saturday, 1 for Sunday, etc.
#
def day_of_week(year; month; day; iso):
if month == 1 or month == 2 then
[year - 1, month + 12, day]
else
[year, month, day]
end
| .[2] + (13*(.[1] + 1)/5|floor)
+ (.[0]%100) + ((.[0]%100)/4|floor)
+ (.[0]/400|floor) - 2*(.[0]/100|floor)
| if iso == "iso" or iso == "ISO" then 1 + ((. + 5) % 7)
else . % 7
end ;</lang>
'''findLastFridays'''
<lang jq># year and month are numbered conventionally
def findLastFriday(year; month):
def isLeapYear:
year%4 == 0 and ( year%100!=0 or year%400==0 ) ;
def days:
if month == 2 then (if isLeapYear then 29 else 28 end)
else [31, 28, 31,30,31,30,31,31,30,31,30,31][month-1]
end;
year as $year
| month as $month
| days
| until( day_of_week($year; $month; .; null) == 6 ; .-1);
 
# input: year
def findLastFridays:
def months:
["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
. as $year
| "YEAR: \(.)",
(range(0;12) | "\(months[.]) \(findLastFriday($year; .+1))") ;
 
$year|tonumber|findLastFridays</lang>
{{out}}
<lang sh>$ jq --arg year 2012 -n -r -f findLastFridays.jq
YEAR: 2012
January 27
February 24
March 30
April 27
May 25
June 29
July 27
August 31
September 28
October 26
November 30
December 28</lang>
 
=={{header|Lasso}}==
2,490

edits