Listing 1 Calendar conversion routines

/*
; #defines
*/
#define BOOL int
#define TRUE 1
#define FALSE 0

/*
; Function prototypes
*/
long ToJul( int, int, int );
void FromJul( long, int *, int *, int * );
BOOL IsLeapYear( int );
int DayOfWeek( long );

/*
;
; USAGE: long ToJul( int, int, int );
;   int month;    Gregorian calendar date month
;   int day;      Gregorian calendar date day
;   int year;     Gregorian calendar date year
;
; DESCRIPTION:  Converts Gregorian calendar date to a
;               julian day number.
;
; ALGORITHM:  adaptation of the FORTRAN code used to
;             implement the algorithm presented by
;             H. Fliegl and T. Van Flanders,
;             Communications of the ACM, Vol. 11,
;             No. 10, October, 1968, page 657.
;
;RETURNS:  long int representing the julian day number
*/
long ToJul( int month, int day, int year )
{
  long  jul_day,               // returned julian day
       lmonth = (long)month,  // cast them once
       lday   = (long)day,    // instead of
       lyear  = (long)year;   // each usage
  
  jul_day = lday - 32075L + 1461L *
          (lyear + 4800 + (lmonth - 14L) / 12L) /
          4L + 367L * (lmonth - 2L - (lmonth - 14L) /
          12L * 12L) / 12L - 3L * ((lyear + 4900L +
          (lmonth - 14L) / 12L) / 100L) / 4L;

  return jul_day;
}

/*
;
; USAGE: void FromJul( long, int *, int *, int * );
;   long  jul_day; Julian Day number to convert
;   int   *month;  Gregorian calendar month (return)
;   int   *day;    Gregorian calendar day (return)
;   int   *year;   Gregorian calendar year (return)
;
; DESCRIPTION:  Converts Julian Day number to its
;               corresponding Gregorian calendar date
;               components.
;
; ALGORITHM:  adaptation of the FORTRAN code used to
;             implement the algorithm presented by
;             H. Fliegl and T. Van Flanders,
;             Communications of the ACM, Vol. 11,
;             No. 10, October, 1968, page 657.
;
; RETURNS:  Nothing, updates variables pointed to by
;           year, month and day.
*/
void FromJul( long jul_date,
            int *month,
            int *day,
            int *year )
{
  long t1,      // temporary work variables
      t2,
      yr,
      mo;

  t1 = jul_date + 68569L;
  t2 = 4L * t1 / 146097L;
  t1 = t1 - (146097L * t2 + 3L) / 4L;
  yr = 4000L * (t1 + 1) / 1461001L;
  t1 = t1 - 1461L * yr / 4L + 31;
  mo = 80L * t1 / 2447L;
  *day = (int)(t1 - 2447L * mo / 80L);
  t1 = mo / 11L;
  *month = (int)(mo + 2L - 12L * t1);
  *year = (int){100L * (t2 - 49L) + yr + t1);
}

/*
; USAGE: int IsLeapYear( int );
;   int year; year to check
;
; DESCRIPTION: Determines whether a given year is a
;              leap year.
;
; RETURNS:
;   TRUE (non-zero)   year is a leap year
;   FALSE (zero)       year is not a leap year
*/
BOOL IsLeapYear( int year )
{
  return (year % 4 == 0 &&
        (year % 100 != 0 || year % 400 == 0))
}

/*
; USAGE: int DayOfWeek( long );
;   long jul_day;     julian day number
;
; DESCRIPTION: Determine the day of the week a given
;              julian day number falls on.
;
; RETURNS: 0 ... 7, where 0 is a Monday, 7 is a Sunday.
*/
int DayOfWeek( long jul_day )
{
  return (int)(jul_day % 7L);
}

/* End of File */