Write a program or a script that returns the last Sundays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).

Example of an expected output:

./last_sundays 2013
2013-01-27
2013-02-24
2013-03-31
2013-04-28
2013-05-26
2013-06-30
2013-07-28
2013-08-25
2013-09-29
2013-10-27
2013-11-24
2013-12-29


Related tasks
 Day of the week
 Five weekends
 Last Friday of each month


include timedate.e
 
constant SUNDAY=7
 
procedure showlast(integer dow, integer doy, timedate td)
    td = adjust_timedate(td,timedelta(days:=doy-1))
    integer {year,month,day} = td
    while day_of_week(year,month,day)!=dow do day-=1 end while
    printf(1,"%4d-%02d-%02d\n",{year,month,day})
end procedure
 
procedure last_day_of_month(integer year, integer dow)
integer doy
timedate first = {year,1,1,0,0,0,0,0}
    -- start by finding the 1st of the next month, less 1
    for i=1 to 11 do
        doy = day_of_year(year,i+1,1)-1
        showlast(dow,doy,first)
    end for
    -- do December separately, as 1st would be next year
    doy = day_of_year(year,12,31)
    showlast(dow,doy,first)
end procedure
last_day_of_month(2013,SUNDAY)
