home *** CD-ROM | disk | FTP | other *** search
- /*
- * Calendar routines for DTK clock/calendar board handler.
- * (C) Copyright 1989 Richard B. Wales. All Rights Reserved.
- */
-
- #include "clkdefs.h"
-
-
- /*
- * ADVANCE_TIME
- * Add a given number of seconds to a date/time.
- */
- void advance_time (struct clockval *cv, int seconds)
- {
- /*
- * Perform sanity checking on the parameters.
- */
- if (cv == NULL_CLOCK) return;
- if (seconds > 120 || seconds < 0) return;
-
- /*
- * If the "seconds" increment is zero, there is nothing to do.
- */
- if (seconds == 0) return;
-
- /*
- * Move the time forward by "seconds".
- * Carry over into the rest of the time/date as needed.
- * Assume "seconds" is no more than two minutes
- * (i.e., carry-over is no more than one for hours and up).
- */
- cv->cv_second += seconds;
- if (cv->cv_second < 60) return;
- while (cv->cv_second >= 60)
- cv->cv_minute++, cv->cv_second -= 60;
- if (cv->cv_minute < 60) return;
- cv->cv_hour++; cv->cv_minute -= 60;
- if (cv->cv_hour < 24) return;
- cv->cv_day++; cv->cv_hour = 0;
- switch (cv->cv_month)
- { case 1: case 3: case 5: case 7:
- case 8: case 10: case 12:
- if (cv->cv_day <= 31) return;
- break;
- case 2: if (cv->cv_day <=
- ((cv->cv_year & 0x3) ? 28 : 29))
- return;
- break;
- case 4: case 6: case 9: case 11:
- if (cv->cv_day <= 30) return;
- break;
- default:return; /* garbled date/time */
- }
- cv->cv_day = 1; cv->cv_month++;
- if (cv->cv_month <= 12) return;
- cv->cv_month = 1; cv->cv_year++;
- if (cv->cv_year <= 99) return;
- cv->cv_year = 0; cv->cv_century++;
- }
-
-
- /*
- * DAY_OF_THE_WEEK
- * Determine the day of the week corresponding to a date
- * (0 = Sunday, 1 = Monday, . . ., 6 = Saturday).
- */
- int day_of_the_week (struct clockval *cv)
- { int answer, m, n;
- static mtab[12] = { 1, 4, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6 };
-
- /*
- * Perform sanity checking on the input parameter.
- */
- if (cv == NULL_CLOCK) return -1;
-
- /*
- * Use the algorithm in Martin Gardner's _Mathematical Carnival_
- * (Vintage Books, 1977; ISBN 0-394-72349-X), pp. 83-86.
- */
- n = cv->cv_year; m = n % 12;
- answer = n/12 + m + m/4;
- if (cv->cv_century == 20) answer += 6;
- m = cv->cv_month - 1; answer += mtab[m];
- answer += cv->cv_day; answer += 6;
- if (m < 2 && (n & 0x3) == 0) answer += 6;
- answer %= 7;
- return answer;
- }