Last Friday of each month: Difference between revisions

Content added Content deleted
(another ocaml solution with a dedicated library)
(→‎{{header|C}}: manual calculate)
Line 23: Line 23:


=={{header|C}}==
=={{header|C}}==
Doesn't work with Julian calendar (then again, you probably don't need to plan your weekends for middle ages).
{{improve|C|With some systems, strptime() never sets <code>tm.tm_wday</code>, so this program displays the last Mondays, Thursdays or Saturdays. See discussion at [[{{TALKPAGENAME}}]].}}


<lang c>#define _XOPEN_SOURCE
<lang c>#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>


int main(int c, char *v[])
int main(int c, char *v[])
Line 33: Line 33:
int days[] = {31,29,31,30,31,30,31,31,30,31,30,31};
int days[] = {31,29,31,30,31,30,31,31,30,31,30,31};
int m, y, w;
int m, y, w;
struct tm tm;
char buf[32];


if (c < 2 || !sscanf(v[1], "%d", &y)) return 1;
if (c < 2 || (y = atoi(v[1])) <= 1700) return 1;
days[1] -= (y % 4) || (!(y % 100) && (y % 400));

days[1] -= y % 4 || (y % 100 && ! (y % 400));
w = y * 365 + (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400 + 6;
sprintf(buf, "%d-1-1", y);
strptime(buf, "%Y-%m-%d", &tm);
w = tm.tm_wday - 1; /* day of week for zeroth of Jan */


for(m = 0; m < 12; m++) {
for(m = 0; m < 12; m++) {
Line 50: Line 45:


return 0;
return 0;
}</lang>output<lang>$ ./a.out 2011
}</lang>
2011-01-28
2011-02-25
2011-03-25
2011-04-29
2011-05-27
2011-06-24
2011-07-29
2011-08-26
2011-09-30
2011-10-28
2011-11-25
2011-12-30</lang>


=={{header|C++}}==
=={{header|C++}}==