next up previous contents index Search
Next: 0.5.5.3 References Up: 0.5.5 Julian Calendar Algorithms Previous: 0.5.5.1 A Brief History

0.5.5.2 Source Code

Below are routines to convert from mon, day, year dates into julian dates and the reverse.


#include <math.h>
#include <stdio.h>

/* Oct. 15, 1582 */
#declare IGREG (15+31L*(10+12L*1582))

long date_to_julian (int mon, int day, int year) {

  long jul;
  int ja;
  int jy = year;
  int jm;

  if (jy == 0) return(0);
  if (jy < 0) jy++;

  if (mon > 2)
    jm = mon + 1;
  else {
    jy--;
    jm = mon + 13;
  }

  jul = (long) (floor(365.25 * jy) + floor(30.6001 * jm) + day + 1720995);

  if (day + 31L * (mon + 12L * year) >= IGREG) {
    ja = (int)(0.01 * jy);
    jul += 2 - ja + (int) (0.25 * ja);
  }

  return(jul);
}


void julian_to_date(long julian, int *mon, int *day, int *year) {

  long ja, jalpha, jb, jc, jd, je;

  if (julian >= IGREG) {
    jalpha = (long) (((float) (julian - 1867216) - 0.25) / 36524.25);
    ja = julian + 1 + jalpha - (long) (0.25 * jalpha);
  } else
    ja = julian;

  jb = ja + 1524;
  jc = (long) (6680.0 + ((float) (jb - 2439870) - 122.1) / 365.25);
  jd = (long) (365 * jc + (0.25 * jc));
  je = (long) ((jb - jd) / 30.6001);
  *day = jb - jd - (long) (30.6001 * je);
  *mon = je - 1;
  if (*mon > 12) *mon -= 12;
  *year = jc - 4715;
  if (*mon > 2) (*year)--;
  if (*year <= 0) (*year)--;
}

Scott Gasch
1999-07-09