Algorithm wanted
Kee Hinckley
nazgul at apollo.uucp
Sat Dec 8 05:37:39 AEST 1984
....
This is taken from a posting of parsedate a while back. If I had more time
I would comment this excerpt with an explanation, but.... I'll leave it as
an exercise to the reader.
.............................................................................
DATE MANIPULATION PACKAGE
Richard B. Wales
UCLA Center for Experimental Computer Science
Copyright (c) 1984 by Richard B. Wales
The author hereby grants permission to use or redistribute this package
freely and without charge, subject to the following restrictions:
(1) The copyright notice must be retained in all copies of the source.
(2) Any changes made to the source must be clearly documented (such as
by #ifdef's or by use of a source-code control system such as RCS or
SCCS), so that the original version of the source as distributed by
the author can be reconstructed if necessary and distinguished from
modifications made by others.
struct parsedate
{ long unixtime; /* UNIX internal representation of time */
char *error; /* NULL = OK; non-NULL = error */
int year; /* year (1600 on) */
int month; /* month (1-12) */
int day; /* day of month (1-31) */
int hour; /* hour (0-23) */
int minute; /* minute (0-59) */
int second; /* second (0-59) */
int zone; /* time zone offset in minutes -- "+" or "-" */
int dst; /* daylight savings time (0 = no, 1 = yes) */
int weekday; /* real day of week (0-6; 0 = Sunday) */
int c_weekday; /* claimed day of week (0-6; 0 = Sunday) */
};
struct parsedate *pd;
/* Compute the day of the week. The next several lines constitute a
* perpetual-calendar formula. Note, of course, that the "claimed"
* day of the week (pd->c_weekday) is ignored here.
*/
if (pd->year > 0 && pd->month > 0 && pd->day > 0)
{ if (pd->month >= 3) n = pd->year / 100,
l = pd->year % 100;
else n = (pd->year-1) / 100,
l = (pd->year-1) % 100;
a = (26 * ((pd->month+9)%12 + 1) - 2) / 10;
weekday = (a+(l>>2)+(n>>2)+l-(n+n)+pd->day);
while (weekday < 0) weekday += 7;
pd->weekday = weekday % 7;
}
.........................................................................
Kee Hinckley
...decvax!wivax!apollo!nazgul
More information about the Comp.sources.unix
mailing list