Day of the week: Difference between revisions

→‎{{header|C}}: Change from strptime() to a manual calculation. Last Fridays of year#C had similar change.
(another ocaml solution using a dedicated library)
(→‎{{header|C}}: Change from strptime() to a manual calculation. Last Fridays of year#C had similar change.)
Line 231:
 
=={{header|C}}==
Because of problems with various C libraries (such as ''time_t'' overflowing during 2038, or strptime() or mktime() not filling in ''tm_wday''), this program uses [http://mathforum.org/library/drmath/view/62324.html Zeller's Rule] to calculate day of week.
<lang c>#define _XOPEN_SOURCE
 
#include <stdio.h>
<lang c>#include <timestdio.h>
 
/* Calculate day of week in proleptic Gregorian calendar. Sunday == 0. */
int wday(int year, int month, int day)
{
int adjustment, mm, yy;
 
adjustment = (14 - month) / 12;
mm = month + 12 * adjustment - 2;
yy = year - adjustment;
return (day + (13 * mm - 1) / 5 +
yy + yy / 4 - yy / 100 + yy / 400) % 7;
}
 
int main()
{
int y;
struct tm tm;
char buf[32];
 
for (y = 2008; y <= 2121; y++) {
sprintfif (bufwday(y, 12, 25) == 0) printf("%d04d-12-25\n", y);
strptime(buf, "%Y-%m-%d", &tm);
if (tm.tm_wday == 0) puts(buf);
}
 
Anonymous user