home *** CD-ROM | disk | FTP | other *** search
- /*
- CHRONO.C: Julian date and time routines for C
-
- version: 5-20-87
- compiler: Turbo C version 1.0
- uses: stdio.h, stdlib.h, dos.h
- module type: object
-
- This file contains functions to work with calendar dates. A
- calendar date is a real number in the format YYMMDD. For
- example, 4/23/87 would be 870423. Julian dates are "magic"
- hashed versions of calendar dates that allow arithmetic,
- determining the day of the week, etc. without consulting an
- actual calendar.
-
- Functions in this file:
-
- year(D) Returns the year part of a calendar date
- day(D) Returns the day part of a calendar date
- month(D) Returns the month part of a calendar date
- julian(D) Converts a calendar date to a julian date
- calendar(J) Converts a julian date to a calendar date
- dayofweek(J) Returns day of week for a julian date
- today() Returns today's date (from DOS) as calendar
- date
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <dos.h>
-
- double year(), day(), month();
- double julian(), calendar(), dayofweek();
- double today();
-
- double year(double cal)
- {
- double floor();
-
- return floor(cal / 10000.0);
- }
-
- double day(double cal)
- {
- double floor();
-
- return floor(cal - (floor(cal / 100.0) * 100.0));
- }
-
- double month(double cal)
- {
- double floor();
-
- return floor((cal - (year(cal) * 10000.0) - day(cal)) / 100.0);
- }
-
- double julian(double cal)
- {
- double m, y, floor();
-
- if (month(cal) > 2) {
- m = month(cal) + 1.0;
- y = year(cal);
- }
- else {
- m = month(cal) + 13.0;
- y = year(cal) - 1;
- }
- return floor(365.25*(1900.0+y))+floor(30.6001*m)+day(cal)+1720982;
- }
-
- double calendar(double jul)
- {
- double m, d, y, dayno, floor();
-
- dayno = jul - 1720982;
- y = floor((dayno - 122.1) / 365.25);
- m = floor((dayno - floor(365.25 * y)) / 30.6001);
- d = dayno-floor(365.25 * y) - floor(30.6001 * m);
- m = (m < 14) ? m - 1 : m - 13;
- y = (m < 3) ? y + 1 : y;
- return (y - 1900) * 10000.0 + m * 100.0 + d;
- }
-
- double dayofweek(double jul)
- {
- double dayno, x, fracx, floor();
-
- dayno = jul - 1720982;
- x = (dayno + 5.0) /7.0;
- fracx = x - floor(x);
- return floor(7 * fracx + 0.5);
- }
-
- /*
- The today function is already available from Turbo C.
- Here's a shell using getdate().
- */
-
- double today()
- {
- struct date d;
-
- getdate(&d);
- return (d.da_year - 1900) * 10000.0 + d.da_mon * 100.0 + d.da_day;
- }