home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / calentool / part03 / datelib.c next >
Encoding:
C/C++ Source or Header  |  1989-05-27  |  43.3 KB  |  1,784 lines

  1. /*
  2.  * $Header: datelib.c,v 2.1 89/05/09 14:18:52 billr Exp $
  3.  *
  4.  * datelib.c - Calendar (date) computation library
  5.  *
  6.  * R. P. C. Rodgers, UCSF, November 1988
  7.  * (with thanks to Greg Couch, Conrad Huang, and Dave Yee for their helpful
  8.  *  suggestions)
  9.  *
  10.  *        Copyright 1988, 1989 R. P. C. Rodgers,  All Rights Reserved
  11.  * Permission is granted by the author for use of this code under the following
  12.  * conditions: 1) improvements and corrections to this code are shared with the
  13.  * author; 2) the code is made freely available to anyone requesting it; 3) the
  14.  * code is not used for any profit-making venture without the express
  15.  * permission of the author; and, 3) all copies of the code will preserve the
  16.  * above authorship and copyright information and this notice.
  17.  *
  18.  * (requires math library)
  19.  * 
  20.  * Source of formulae:
  21.  * 
  22.  * 1) Schocken, Wolfgang Alexander
  23.  *    The calculated confusion of calendars; puzzles in Christian, Jewish and
  24.  *       Moslem calendars.
  25.  *    1st Ed.
  26.  *    Vantage Press
  27.  *    New York
  28.  *    1976
  29.  * 
  30.  * 2) Meeus, Jean
  31.  *    Astronomical Formulae for Calculators
  32.  *    Monografieen over Astronomie en Astrofysica
  33.  *    Volkssterrenwacht Urania V.Z.W.
  34.  *    Mattheessensstraat 62, B 2540 Hove, Belgium
  35.  *    Vereniging voor Sterrenkunde V.Z.W.
  36.  *    Ringlaan 3, B 1180 Brussel, Belgium
  37.  *    Vol. 4
  38.  *    Derde Druk
  39.  *    October 1980
  40.  *    (3rd edition of 1985 available from Willmann-Bell, address below)
  41.  *    (formulae for Easter, Julian and Gregorian date formation, and
  42.  *     solstices/equinoxes)
  43.  * 
  44.  * 3) Assorted contributions to the Hewlett-Packard HP-41C Software Library
  45.  * 
  46.  * In his 1987 Sun program "moontool," John Walker (Autodesk, Sausalito, CA)
  47.  * mentions several other potentially useful references:
  48.  * 
  49.  * 4) Pierre Bretagnon and Jean-Louis Simon
  50.  *    Planetary Programs and Tables from -4000 to +2800
  51.  *    Willmann-Bell
  52.  *    1986
  53.  *    (for utmost accuracy in planetary computations)
  54.  * 
  55.  * 5) Eric Burgess
  56.  *    Celestial BASIC
  57.  *    Revised Edition
  58.  *    Sybex
  59.  *    1985
  60.  *    (cookbook oriented, many algorithms hard to dig out of turgid BASIC)
  61.  * 
  62.  * Many of these references can be obtained from Willmann-Bell, P.O. Box
  63.  * 35025, Richmond, VA 23235, USA.  Phone: (804) 320-7016.  In addition
  64.  * to their own publications, they stock most of the standard references
  65.  * for mathematical and positional astronomy.
  66.  * 
  67.  * NOTES:
  68.  * check ranges on days (0-365)
  69.  * islamic new year and jewish dates not thoroughly tested
  70.  * 
  71.  */
  72.  
  73. #include "ct.h"        /* for the NO_HOLIDAYS #define */
  74.  
  75. #include    <stdio.h>    /* for NULL */
  76. #include    <math.h>
  77.  
  78. double    julian_day();
  79.  
  80. #ifndef NO_HOLIDAYS
  81. extern    char    *malloc();
  82. double  easter_offset(), passover_offset();
  83.  
  84. static char    *monthname[] = { 
  85.         "January", "February", "March", "April", "May",
  86.         "June", "July", "August", "September",
  87.         "October", "November", "December"    };
  88. static char    *dayname[] = { 
  89.         "Sunday", "Monday", "Tuesday", "Wednesday",
  90.         "Thursday", "Friday", "Saturday"    };
  91. static char    timebuf[16];
  92.  
  93. /*
  94.  * date_string:
  95.  * Return date string of form: "Monday 12 December 1988"
  96.  * given day of week (0-6), day (1-31), month (1-12), year
  97.  */
  98. char *
  99. date_string(day_of_week, day, month, year)
  100.     double    day;
  101.     int    day_of_week, month, year;
  102. {
  103.     char *date;
  104.  
  105.     date = (char *) malloc(81 * sizeof(char));
  106.     if (date == NULL)
  107.         err_rpt("out of memory", FATAL);
  108.     (void) sprintf(date, "%s %d %s %d",
  109.         dayname[day_of_week], (int) day, monthname[--month], year);
  110.     return date;
  111. }
  112.  
  113. /*
  114.  * date_string_2:
  115.  * Return date string of form: "Monday 12 December 1988 (NYEAR)"
  116.  * given day of week (0-6), day (1-31), month (1-12), year, and alternate
  117.  * (that is, non-Gregorian) year, NYEAR
  118.  */
  119. char *
  120. date_string_2(day_of_week, day, month, year, nyear)
  121.     double    day;
  122.     int    day_of_week, month, nyear, year;
  123. {
  124.     char *date;
  125.  
  126.     date = (char *) malloc(81 * sizeof(char));
  127.     if (date == NULL)
  128.         err_rpt("out of memory", FATAL);
  129.     (void) sprintf(date, "%s %d %s %d (%d)",
  130.         dayname[day_of_week], (int) day, monthname[--month],
  131.             year, nyear);
  132.     return date;
  133. }
  134.  
  135. /*
  136.  * date_time_string:
  137.  * Return date/time string of form: "10:42 Monday 12 December 1988"
  138.  * given hour (0-24), minute (0-59), day of week (0-6), day (1-31),
  139.  * month (1-12), year
  140.  */
  141. char *
  142. date_time_string(hour, min, day_of_week, day, month, year)
  143.     double    day;
  144.     int    day_of_week, hour, min, month, year;
  145. {
  146.     char *date;
  147.  
  148.     date = (char *) malloc(81 * sizeof(char));
  149.     if (date == NULL)
  150.         err_rpt("out of memory", FATAL);
  151.     (void) sprintf(date, "%02d:%02d %s %d %s %d",
  152.         hour, min, dayname[day_of_week], (int) day,
  153.         monthname[--month], year);
  154.     return date;
  155. }
  156.  
  157. /*
  158.  * election_day:
  159.  * Compute date of Election Day given year (1584-9999)
  160.  * Election day is defined as the first Tuesday following the first
  161.  * Monday in November.
  162.  */
  163. double
  164. election_day(year)
  165.     int    year;
  166. {
  167.     double day, nth_mday_of_month();
  168.  
  169.     /* find first Tuesday in Nov. */
  170.     day = nth_mday_of_month(1, 2, 11, year);
  171.     /* if it's the first day of the month then Election day is next week */
  172.     if (day == 1.)
  173.         day = 8.;
  174.     return julian_day(day, 11, year);
  175. }
  176.  
  177. /*
  178.  * dday2:
  179.  * Compute dday2 for various holiday routines given x value, year (>1583)
  180.  */
  181. dday2(x, year)
  182.     int    x, year;
  183. {
  184.     int    atmp, btmp;
  185.  
  186.     atmp = 1.25 * year;
  187.     btmp = year / 100;
  188.     btmp = 0.75 * (1 + btmp);
  189.     return (x + atmp - btmp) % 7;
  190. }
  191. #endif    /* NO_HOLIDAYS */
  192.  
  193. /*
  194.  * days_remaining_in_year:
  195.  * Compute remaining days of year (0-366)
  196.  * given day (1-31), month (1-12), year (1901-2009)
  197.  */
  198. days_remaining_in_year(day, month, year)
  199.     double    day;
  200.     int    month, year;
  201. {
  202.     int    length_of_year(), day_of_year();
  203.  
  204.     return length_of_year(year) - day_of_year(day, month, year);
  205. }
  206.  
  207. /*
  208.  * length_of_year:
  209.  * Compute days in year (0-366)
  210.  * given year (1901-2009)
  211.  */
  212. length_of_year(year)
  213.     int    year;
  214. {
  215.     int    ylength;
  216.  
  217.     if ((year % 400) == 0)
  218.         ylength = 366;
  219.     else if ((year % 100) == 0)
  220.         ylength = 365;
  221.     else if ((year % 4) == 0)
  222.         ylength = 366;
  223.     else
  224.         ylength = 365;
  225.     return ylength;
  226. }
  227.  
  228. /*
  229.  * get_day_of_week:
  230.  * Compute day of week (0-6 for Sunday-Saturday)
  231.  * given day (1-31), month (1-12), year (1901-2009)
  232.  */
  233. get_day_of_week(day, month, year)
  234.     double    day;
  235.     int    month, year;
  236. {
  237.     int    atmp;
  238.  
  239.     atmp = julian_day(day, month, year) + 1.5;
  240.     return atmp % 7;
  241. }
  242.  
  243. /*
  244.  * day_ of_year:
  245.  * Compute day of year (0-366)
  246.  * given day (1-31), month (1-12), year (1901-2009)
  247.  */
  248. day_of_year(day, month, year)
  249.     double    day;
  250.     int    month, year;
  251. {
  252.     return (int) julian_day(day, month, year)
  253.         - (int) julian_day(0.0, 1, year);
  254. }
  255.  
  256. /*
  257.  * julian_day:
  258.  * Compute Julian day (>=1)
  259.  * given day (1-31), month (1-12), year (1901-2009)
  260.  *
  261.  * Notes: The Gregorian reform is taken into account (that is, the day
  262.  * following 4 October 1582 is 15 October 1582; dates on this latter day or
  263.  * later are said to be in the Gregorian calendar).  B.C. years are counted
  264.  * astronomically (the year prior to year 1 is year 0).  The Julian day
  265.  * begins at GMT noon (the day is expressed in floating point).
  266.  * Example: to obtain Julian day for 4 Jul 1979: julian_day(4.0, 7, 1979)
  267.  */
  268. double
  269. julian_day(day, month, year)
  270.     double    day;
  271.     int    month, year;
  272. {
  273.     int    atmp, btmp, monthp, yearp;
  274.     double    ctmp;
  275.  
  276.     if (month > 2) {
  277.         monthp = month + 1;
  278.         yearp = year;
  279.     }
  280.     else {
  281.         monthp = month + 13;
  282.         yearp = year - 1;
  283.     }
  284.     if ((year > 1582) || (year == 1582 && month >= 10)
  285.         || (year == 1582 && month ==10 && day >= 15)) {
  286.         atmp = year / 100;
  287.         btmp = 2 - atmp + (atmp / 4);
  288.     }
  289.     else
  290.         btmp = 0;
  291.     atmp = 365.25 * yearp;
  292.     ctmp = atmp;
  293.     atmp = 30.6001 * monthp;
  294.     ctmp =  ctmp + atmp;
  295.     return ctmp + day + 1720994.5 + btmp;
  296. }
  297.  
  298. #ifndef NO_HOLIDAYS
  299. /*
  300.  * corrected_julian_day:
  301.  * Correct Julian day (>=1) for conversion from JULIAN CALENDAR
  302.  * to GREGORIAN CALENDAR.
  303.  */
  304. double
  305. corrected_julian_day(jday)
  306.     double    jday;
  307. {
  308.     double    day;
  309.     int    atmp, btmp, month, year;
  310.  
  311.     gregorian_date(&day, &month, &year, jday);
  312.     atmp = year / 100;
  313.     btmp = ((3 * atmp) - 5) / 4;
  314.     return julian_day(day, month, year) + btmp;
  315. }
  316.  
  317. /*
  318.  * gregorian_date:
  319.  * Return pointers to day (1-31), month (1-12), year (1901-2009),
  320.  * given Julian day (>=0)
  321.  *
  322.  * Notes: The Gregorian reform is taken into account (that is, the day
  323.  * following 4 October 1582 is 15 October 1582; dates on this latter day or
  324.  * later are said to be in the Gregorian calendar).  B.C. years are counted
  325.  * astronomically (the year prior to year 1 is year 0).  The Julian day
  326.  * begins at GMT noon.  The Julian day can be expressed in
  327.  * floating point below to get a day result with decimal places.  This method
  328.  * is valid only for positive Julian day numbers.
  329.  */
  330. gregorian_date(p_day, p_month, p_year, jday)
  331.     int    *p_month, *p_year;
  332.     double    *p_day, jday;
  333. {
  334.     int    atmp, btmp, ctmp, dtmp, etmp, gtmp, ztmp;
  335.     double    ftmp;
  336.  
  337.     ztmp = jday + 0.5;
  338.     ftmp = (jday + 0.5) - ztmp;
  339.     if (ztmp >= 2299161) {
  340.         gtmp = (ztmp - 1867216.25) / 36524.25;
  341.         ctmp = gtmp / 4;
  342.         atmp = ztmp + 1 + gtmp - ctmp;
  343.     }
  344.     else
  345.         atmp = ztmp;
  346.     btmp = atmp + 1524;
  347.     ctmp = (btmp - 122.1) / 365.25;
  348.     dtmp = 365.25 * ctmp;
  349.     etmp = ((btmp - dtmp) / 30.6001);
  350.     ztmp = 30.6001 * etmp;
  351.     *p_day = btmp - dtmp - ztmp + ftmp;
  352.     if (etmp > 13.5)
  353.         *p_month = etmp - 13;
  354.     else
  355.         *p_month = etmp - 1;
  356.     if (*p_month > 2.5)
  357.         *p_year = ctmp - 4716;
  358.     else
  359.         *p_year = ctmp - 4715;
  360. }
  361.  
  362. /*
  363.  * mdays_between_dates:
  364.  * Compute number of mdays between two dates (exclusive: don't count the
  365.  * days themselves) given day of week (0-6, O for Sunday),
  366.  * two sets of day (1-31), month (1-12), year (1583-9999)
  367.  */
  368. mdays_between_dates(day_of_week, day1, month1, year1, day2, month2, year2)
  369.     int    day_of_week, month1, year1, month2, year2;
  370.     double    day1, day2;
  371. {
  372.     return wday(day_of_week, day2, month2, year2)
  373.         - wday(day_of_week, day1, month1, year1);
  374. }
  375.  
  376. /*
  377.  * nth_mday_of_month:
  378.  * Compute nth m-day of the month (1-31)
  379.  * given n (1-5), day of week (0-6, 0 for Sunday), month (1-12),
  380.  * year (1583-9999)
  381.  */
  382. double
  383. nth_mday_of_month(n, day_of_week, month, year)
  384.     int    day_of_week, month, n, year;
  385. {
  386.     int    atmp, btmp, ctmp, dtmp, etmp, ftmp, tmonth, tyear;
  387.  
  388.     if (month > 2) {
  389.         tmonth = month + 1;
  390.         tyear = year;
  391.     }
  392.     else {
  393.         tmonth = month + 13;
  394.         tyear = year - 1;
  395.     }
  396.     atmp = 2.6 * tmonth;
  397.     btmp = 1.25 * tyear;
  398.     ctmp = (tyear / 100) - 7;
  399.     dtmp = 0.75 * ctmp;
  400.     etmp = (day_of_week - atmp - btmp + dtmp) % 7;
  401.     if (etmp == 0)
  402.         ftmp = 7;
  403.     else
  404.         ftmp = etmp;
  405.     return (double) (ftmp + (n * 7));
  406. }
  407.  
  408. /*
  409.  * years_date_is_mday:
  410.  * Compute year(s) for which a given date is an m-day
  411.  * given starting year, ending year,
  412.  * day of week (0-6, 0 for Sunday), day, and month
  413.  * algorithm said to be valid for 1 Mar 1900 to 28 Feb 2100.
  414.  */
  415. years_date_is_mday(day_of_week, day, month, start_year, end_year, yearlist,
  416.     number_of_years)
  417.     int    day_of_week, end_year, month, *number_of_years, yearlist[],
  418.         start_year;
  419.     double    day;
  420. {
  421.     int        diff, index, year, tdow;
  422.     static int    augment[] = {6, 11, 6, 5}; 
  423.  
  424.     *number_of_years = 0;
  425.     if (start_year > end_year) return -1;
  426.     for (year = start_year; year <= end_year; year++ ) {
  427.         tdow = get_day_of_week(day, month, year);
  428.         if (tdow == day_of_week) {
  429.             yearlist[(*number_of_years)++] = year;
  430.         }
  431.         if (*number_of_years == 2 || *number_of_years == 3) {
  432.             diff = yearlist[*number_of_years]
  433.                 - yearlist[*number_of_years - 1];
  434.             if (diff == 5) {
  435.                 year++;
  436.                 index = 0;
  437.                 break;
  438.             }
  439.             else if (diff == 11) {
  440.                 year++;
  441.                 index = 2;
  442.                 break;
  443.             }
  444.         }
  445.     }
  446.     for ( ; year <= end_year; year++ ) {
  447.         yearlist[(*number_of_years + 1)] =
  448.             yearlist[*number_of_years] + augment[index++ % 4];
  449.         (*number_of_years)++;
  450.     }
  451.     return 0;
  452. }
  453.  
  454. /*
  455.  * weekdays_between_dates:
  456.  * Compute weekdays between any two dates
  457.  * given two sets of day (1-31), month (1-12), year (1901-2009)
  458.  */
  459. weekdays_between_dates(day1, month1, year1, day2, month2, year2)
  460.     int    month1, month2, year1, year2;
  461.     double    day1, day2;
  462. {
  463.     return wday2(day2, month2, year2) - wday2(day1, month1, year1);
  464. }
  465.  
  466. /*
  467.  * wday:
  468.  * Compute wday for mdays_between_dates routine
  469.  * given day of week, day, month, year
  470.  */
  471. wday(day_of_week, day, month, year)
  472.     int    day_of_week, month, year;
  473.     double    day;
  474. {
  475.     int    atmp, btmp, ctmp, dtmp;
  476.  
  477.     atmp = dday(day, month, year) - day_of_week;
  478.     btmp = atmp / 7;
  479.     ctmp = atmp % 7;
  480.     dtmp = 0.11 * ctmp + 0.9;
  481.     return btmp + (0.5 * dtmp);
  482. }
  483.  
  484. /*
  485.  * wday2:
  486.  * Compute wday2 for weekdays_between_dates routine
  487.  * given day, month, year
  488.  */
  489. wday2(day, month, year)
  490.     int    month, year;
  491.     double    day;
  492. {
  493.     int    atmp, btmp, ctmp, dtmp;
  494.  
  495.     atmp = dday(day, month, year);
  496.     btmp = atmp / 7;
  497.     ctmp = atmp % 7;
  498.     dtmp = 1.801 * ctmp;
  499.     return (5 * btmp) + (0.5 * dtmp);
  500. }
  501.  
  502. /*
  503.  * dday:
  504.  * Compute dday for other routines
  505.  * given day (1-31), month (1-12), year (1901-2009)
  506.  */
  507. dday(day, month, year)
  508.     int    month, year;
  509.     double    day;
  510. {
  511.     int    atmp, btmp, ctmp, dtmp, tmonth, tyear;
  512.  
  513.     if (month > 2) {
  514.         tmonth = month + 1;
  515.         tyear = year;
  516.     }
  517.     else {
  518.         tmonth = month + 13;
  519.         tyear = year - 1;
  520.     }
  521.     atmp = tyear / 100;
  522.     btmp = 0.75 * (atmp - 7);
  523.     ctmp = 365.25 * tyear;
  524.     dtmp = 30.6001 * tmonth;
  525.     return (int) day - btmp + ctmp + dtmp;
  526. }
  527.  
  528. /*
  529.  * vernal_equinox:
  530.  * Compute Julian day for Vernal (March) Equinox given year (1901-2009)
  531.  */
  532. double
  533. vernal_equinox(year)
  534.     int    year;
  535. {
  536.     double    solstice_equinox_exact();
  537.  
  538.     return solstice_equinox_exact(0, year);
  539. }
  540.  
  541. /*
  542.  * summer_solstice:
  543.  * Compute Julian day for Summer Solstice (June) given year (1901-2009)
  544.  */
  545. double
  546. summer_solstice(year)
  547.     int    year;
  548. {
  549.     double    solstice_equinox_exact();
  550.  
  551.     return solstice_equinox_exact(1, year);
  552. }
  553.  
  554. /*
  555.  * autumn_equinox:
  556.  * Compute Julian day for Autumnal (September) Equinox given year (1901-2009)
  557.  */
  558. double
  559. autumn_equinox(year)
  560.     int    year;
  561. {
  562.     double    solstice_equinox_exact();
  563.  
  564.     return solstice_equinox_exact(2, year);
  565. }
  566.  
  567. /*
  568.  * winter_solstice:
  569.  * Compute Julian day for Winter (December) Solstice given year (1901-2009)
  570.  */
  571. double
  572. winter_solstice(year)
  573.     int    year;
  574. {
  575.     double    day, jday;
  576.     int    month, nyear;
  577.     double    solstice_equinox_exact();
  578.  
  579.     jday = solstice_equinox_exact(3, year);
  580.     gregorian_date(&day, &month, &nyear, jday);
  581.     if (nyear == year)
  582.         return jday;
  583.     else
  584.         return solstice_equinox_exact(3, (year + 1));
  585. }
  586.  
  587. /*
  588.  * solstice_equinox__exact:
  589.  * Compute more precise date for Solstice/Equinox
  590.  * given code (1-4, 1 for Vernal Equinox), year (1901-2009)
  591.  */
  592. double
  593. solstice_equinox_exact(code, year)
  594.     int    code, year;
  595. {
  596.     int    count;
  597.  
  598.     double    corr, jday_app;
  599.     double    solstice_equinox_approx(), solstice_equinox_correction();
  600.  
  601.     /* compute first approximation to Julian day */
  602.     jday_app = solstice_equinox_approx(code, year);
  603.     /* iteratively recompute corrected Julian day */
  604.     for(count = 0; count < 15; count++) {
  605.         corr = solstice_equinox_correction(jday_app, code);
  606.         jday_app += corr;
  607.         if (fabs(corr) < 0.00001)
  608.             break;
  609.     }
  610.     return jday_app;
  611. }
  612.  
  613. /*
  614.  * solstice_equinox_correction:
  615.  * Compute correction for Solstice/Equinox Julian day
  616.  * approximate Julian day, code (1-4, 1 for Vernal Equinox)
  617.  */
  618. double
  619. solstice_equinox_correction(jday, code)
  620.     int    code;
  621.     double    jday;
  622. {
  623.     double    apparent_longitude(), sin_degrees();
  624.  
  625.     return(58.0 * sin_degrees((code * 90.0) - apparent_longitude(jday)));
  626. }
  627.  
  628. /*
  629.  * apparent_longitude:
  630.  * Compute apparent longitude (true equinox) of Sun for
  631.  * Solstice/Equinox given approximate Julian day
  632.  */
  633. double
  634. apparent_longitude(jday)
  635.     double    jday;
  636. {
  637.     double    btmp, ctmp, dtmp, etmp, ftmp;
  638.     double    sin_degrees();
  639.  
  640.     btmp = (jday - 2415020.0) / 36525.0;    /* time in Julian centuries: */
  641.                         /* epoch 0.5 January 1900 */
  642.     ctmp = 279.69668 + (36000.76892 * btmp) /* geometric mean longitude */
  643.         + (0.0003025 * btmp * btmp);    /* (mean equinox of the date) */
  644.     dtmp = 358.47583 + (35999.04975 * btmp)    /* mean anomaly of Sun */
  645.         + (0.00015 * btmp * btmp)
  646.         + (0.0000033 * btmp * btmp * btmp);
  647.     etmp = (1.919460 - (0.004789 * btmp)    /* Sun's eqn of the center */
  648.         - (0.000014 * btmp)) * sin_degrees(dtmp)
  649.         + (0.020094 - (0.0001 * btmp)) * sin_degrees(2 * dtmp)
  650.         + 0.000293 * sin_degrees(3 * dtmp);
  651.     ftmp = ctmp + etmp;            /* Sun's true longitude */
  652.     return ftmp - 0.00569            /* app. long. of Sun */
  653.         - 0.00479 * sin_degrees(259.18    /* (true equinox of the date) */
  654.         - (1934.142 * btmp));
  655. }
  656.  
  657. /*
  658.  * sin_degrees:
  659.  * Compute sin for argument in degrees
  660.  */
  661. double
  662. sin_degrees(degrees)
  663. #define    PI_CORR    0.01745329252                /* pi / 180 */
  664.     double    degrees;
  665. {
  666.     return sin(degrees * PI_CORR);
  667. }
  668.  
  669. /*
  670.  * solstice_equinox_approx:
  671.  * Compute approximate date for Solstice/Equinox
  672.  * given code (1-4, 1 for Vernal Equinox), year (1901-2009)
  673.  */
  674. double
  675. solstice_equinox_approx(code, year)
  676.     int    code, year;
  677. {
  678.     return (365.2422 * (year + (code / 4))) + 1721141.3;
  679. }
  680.  
  681. /*
  682.  * easter:
  683.  * Return Julian date for Easter, given year (1901-2009)
  684.  *
  685.  * Offered in the book of Meeus, which cites:
  686.  *
  687.  * 1) Spencer Jones
  688.  *    General Astronomy
  689.  *    1922
  690.  *    pg. 73-74
  691.  *
  692.  * 2) Journal of the British Astronomical Association
  693.  *    Vol. 8
  694.  *    Pg. 91
  695.  *    Dec. 1977
  696.  *    (which attributes method to Butcher's Ecclesiastical Calendar of 1876)
  697.  *
  698.  * Method valid for all dates in the Gregorian calendar
  699.  * (from 15 October 1583 on)
  700.  */
  701. double
  702. easter(year)
  703.     int    year;
  704. {
  705.     double    day;
  706.     int    atmp, btmp, ctmp, dtmp, etmp, ftmp,
  707.         gtmp, htmp, itmp, ktmp, ltmp, mtmp;
  708.     int    month;
  709.  
  710.     atmp = year % 19;
  711.     btmp = year / 100;
  712.     ctmp = year % 100;
  713.     dtmp = btmp / 4;
  714.     etmp = btmp % 4;
  715.     ftmp = (btmp + 8) / 25;
  716.     gtmp = (btmp - ftmp + 1) / 3;
  717.     htmp = ((19 * atmp) + btmp - dtmp - gtmp + 15) % 30;
  718.     itmp = ctmp / 4;
  719.     ktmp = ctmp % 4;
  720.     ltmp = (32 + (2 * etmp) + (2 * itmp) - htmp - ktmp) % 7;
  721.     mtmp = (atmp + (11 * htmp) + (22 * ltmp)) / 451;
  722.     month = (htmp + ltmp - (7 * mtmp) + 114) / 31;
  723.     day = ((htmp + ltmp - (7 * mtmp) + 114) % 31) + 1;
  724.     return julian_day(day, month, year);
  725. }
  726.  
  727. /*
  728.  * first_sunday_advent:
  729.  * Christian holidays: compute Julian day for First Sunday in Advent
  730.  * (closest Sunday to St. Andrew's day, the last day in November)
  731.  * given year (>1583)
  732.  */
  733. double
  734. first_sunday_advent(year)
  735.     int    year;
  736. {
  737.     int    atmp;
  738.  
  739.     atmp = get_day_of_week(30.0, 11, year);
  740.     if (atmp <= 3) {
  741.         return julian_day((30.0 - (double) atmp), 11, year);
  742.     }
  743.     else {
  744.         return julian_day(30.0, 11, year) + (7 - atmp);
  745.     }
  746. }
  747.  
  748. /*
  749.  * easter_offset:
  750.  * Christian holidays: compute Julian day as offset from Easter
  751.  * given year (>1583)
  752.  */
  753. double
  754. easter_offset(offset, year)
  755.     double    offset;
  756.     int    year;
  757. {
  758.     double    easter();
  759.  
  760.     return easter(year) + offset;
  761. }
  762.  
  763. /*
  764.  * septuagesima:
  765.  * Christian holidays: compute Julian day for Septuagesima Sunday
  766.  * (Third Sunday before Lent)
  767.  * given year (>1583)
  768.  */
  769. double
  770. septuagesima(year)
  771.     int    year;
  772. {
  773.     return easter_offset(-63.0, year);
  774. }
  775.  
  776. /*
  777.  * sexagesima:
  778.  * Christian holidays: compute Julian day for Sexagesima Sunday
  779.  * (Second Sunday before Lent)
  780.  * given year (>1583)
  781.  */
  782. double
  783. sexagesima(year)
  784.     int    year;
  785. {
  786.     return easter_offset(-56.0, year);
  787. }
  788.  
  789. /*
  790.  * quinquagesima:
  791.  * Christian holidays: compute Julian day for Quinquagesima (Shrove) Sunday
  792.  * (Sunday before Lent begins on Ash Wednesday)
  793.  * given year (>1583)
  794.  */
  795. double
  796. quinquagesima(year)
  797.     int    year;
  798. {
  799.     return easter_offset(-49.0, year);
  800. }
  801.  
  802. /*
  803.  * shrove_monday:
  804.  * Christian holidays: compute Julian day for Shrove Monday
  805.  * (Two days before Lent begins on Ash Wednesday)
  806.  * given year (>1583)
  807.  */
  808. double
  809. shrove_monday(year)
  810.     int    year;
  811. {
  812.     return easter_offset(-48.0, year);
  813. }
  814.  
  815. /*
  816.  * shrove_tuesday:
  817.  * Christian holidays: compute Julian day for Shrove Tuesday
  818.  * (Day before Lent begins on Ash Wednesday; Mardi Gras)
  819.  * given year (>1583)
  820.  */
  821. double
  822. shrove_tuesday(year)
  823.     int    year;
  824. {
  825.     return easter_offset(-47.0, year);
  826. }
  827.  
  828. /*
  829.  * ash_wednesday:
  830.  * Christian holidays: compute Julian day for Ash Wednesday
  831.  * given year (>1583)
  832.  */
  833. double
  834. ash_wednesday(year)
  835.     int    year;
  836. {
  837.     return easter_offset(-46.0, year);
  838. }
  839.  
  840. /*
  841.  * first_sunday_lent:
  842.  * Christian holidays: compute Julian day for First Sunday in Lent
  843.  * given year (>1583)
  844.  */
  845. double
  846. first_sunday_lent(year)
  847.     int    year;
  848. {
  849.     return easter_offset(-42.0, year);
  850. }
  851.  
  852. /*
  853.  * second_sunday_lent:
  854.  * Christian holidays: compute Julian day for Second Sunday in Lent
  855.  * given year (>1583)
  856.  */
  857. double
  858. second_sunday_lent(year)
  859.     int    year;
  860. {
  861.     return easter_offset(-35.0, year);
  862. }
  863.  
  864. /*
  865.  * third_sunday_lent:
  866.  * Christian holidays: compute Julian day for Third Sunday in Lent
  867.  * given year (>1583)
  868.  */
  869. double
  870. third_sunday_lent(year)
  871.     int    year;
  872. {
  873.     return easter_offset(-28.0, year);
  874. }
  875.  
  876. /*
  877.  * fourth_sunday_lent:
  878.  * Christian holidays: compute Julian day for Fourth Sunday in Lent
  879.  * given year (>1583)
  880.  */
  881. double
  882. fourth_sunday_lent(year)
  883.     int    year;
  884. {
  885.     return easter_offset(-21.0, year);
  886. }
  887.  
  888. /*
  889.  * passion_sunday:
  890.  * Christian holidays: compute Julian day for Passion Sunday
  891.  * (Second Sunday before Easter)
  892.  * given year (>1583)
  893.  */
  894. double
  895. passion_sunday(year)
  896.     int    year;
  897. {
  898.     return easter_offset(-14.0, year);
  899. }
  900.  
  901. /*
  902.  * palm_sunday:
  903.  * Christian holidays: compute Julian day for Palm Sunday
  904.  * (Sunday before Easter)
  905.  * given year (>1583)
  906.  */
  907. double
  908. palm_sunday(year)
  909.     int    year;
  910. {
  911.     return easter_offset(-7.0, year);
  912. }
  913.  
  914. /*
  915.  * maundy_thursday:
  916.  * Christian holidays: compute Julian day for Maundy Thursday
  917.  * (Three days prior to Easter)
  918.  * given year (>1583)
  919.  */
  920. double
  921. maundy_thursday(year)
  922.     int    year;
  923. {
  924.     return easter_offset(-3.0, year);
  925. }
  926.  
  927. /*
  928.  * good_friday:
  929.  * Christian holidays: compute Julian day for Good Friday
  930.  * (Two days prior to Easter)
  931.  * given year (>1583)
  932.  */
  933. double
  934. good_friday(year)
  935.     int    year;
  936. {
  937.     return easter_offset(-2.0, year);
  938. }
  939.  
  940. /*
  941.  * easter_monday:
  942.  * Christian holidays: compute Julian day for Easter Monday (Canada)
  943.  * given year (>1583)
  944.  */
  945. double
  946. easter_monday(year)
  947.     int    year;
  948. {
  949.     return easter_offset(1.0, year);
  950. }
  951.  
  952. /*
  953.  * rogation_sunday:
  954.  * Christian holidays: compute Julian day for Rogation Sunday
  955.  * (Rogation is the period of three days prior to Ascension Day; strictly
  956.  * speaking, this is the period Mon-Wed)
  957.  * given year (>1583)
  958.  */
  959. double
  960. rogation_sunday(year)
  961.     int    year;
  962. {
  963.     return easter_offset(35.0, year);
  964. }
  965.  
  966. /*
  967.  * ascension_day:
  968.  * Christian holidays: compute Julian day for Ascension Day
  969.  * (Holy Thursday; fortieth day after Easter, inclusive)
  970.  * given year (>1583)
  971.  */
  972. double
  973. ascension_day(year)
  974.     int    year;
  975. {
  976.     return easter_offset(39.0, year);
  977. }
  978.  
  979. /*
  980.  * whitsunday:
  981.  * Christian holidays: compute Julian day for Whitsunday (Pentecost)
  982.  * (Seventh Sunday after Easter)
  983.  * given year (>1583)
  984.  */
  985. double
  986. whitsunday(year)
  987.     int    year;
  988. {
  989.     return easter_offset(49.0, year);
  990. }
  991.  
  992. /*
  993.  * trinity_sunday:
  994.  * Christian holidays: compute Julian day for Trinity Sunday
  995.  * (Eighth Sunday after Easter)
  996.  * given year (>1583)
  997.  */
  998. double
  999. trinity_sunday(year)
  1000.     int    year;
  1001. {
  1002.     return easter_offset(56.0, year);
  1003. }
  1004.  
  1005. /*
  1006.  * corpus_christi:
  1007.  * Christian holidays: compute Julian day for Corpus Christi
  1008.  * (First Thursday after Trinity Sunday)
  1009.  * given year (>1583)
  1010.  */
  1011. double
  1012. corpus_christi(year)
  1013.     int    year;
  1014. {
  1015.     return easter_offset(60.0, year);
  1016. }
  1017.  
  1018. /*
  1019.  * passover:
  1020.  * Jewish holidays: compute Julian day of Pesach (first day of Passover)
  1021.  * and establish the Gregorian day of month and month, and Jewish year,
  1022.  * given Julian year (>1583)
  1023.  * This formula, due to Karl Friedrich Gauss (1777-1855) is described in
  1024.  * excruciating detail in the book by Schocken P. 51-61).  Note that it
  1025.  * computes the Julian date, which has to be corrected to a Gregorian date,
  1026.  * and that it exhibits the eccentricity of always computing the day
  1027.  * relative to March, so that April 2 appears as March 33, which is also
  1028.  * corrected below.
  1029.  * This formula is probably only accurate for years up to and including
  1030.  * 2000 (tested); I know that it fails for 2011.
  1031.  */
  1032. double
  1033. passover(year, jyear)
  1034.     int    *jyear, year;
  1035. {
  1036.     double    etmp, p_day;
  1037.     int    atmp, btmp, ctmp, day_of_week, dtmp, ftmp, gtmp;
  1038.     int    p_month;
  1039.  
  1040.     atmp = year + 3760;
  1041.     *jyear = atmp;
  1042.     btmp = (12 * atmp + 17) % 19;
  1043.     ctmp = atmp % 4;
  1044.     etmp = (1.55424180 * btmp) - (0.003177794 * atmp)
  1045.         + (ctmp / 4) + 32.04409316;
  1046.     dtmp = etmp;
  1047.     etmp = etmp - dtmp;
  1048.         /* day_of_week is not to be confused with the
  1049.          value returned by the day_of_week routine; here, Sunday = 1 */
  1050.     day_of_week = ((3 * atmp) + (5 * ctmp) + dtmp + 5) % 7;
  1051.     if (day_of_week == 0 && btmp > 11 && etmp >= 0.89772377)
  1052.         p_day = dtmp + 1.0;
  1053.     else if (day_of_week == 1 && btmp > 6 && etmp >= 0.63287037)
  1054.         p_day = dtmp + 2.0;
  1055.     else if (day_of_week == 2 || day_of_week == 4 || day_of_week ==6)
  1056.         p_day = dtmp + 1.0;
  1057.     else {
  1058.         p_day = dtmp;
  1059.     }
  1060.     ftmp = year / 100;        /* Correct to Gregorian date */
  1061.     gtmp = ((3 * ftmp) - 5) / 4;
  1062.     p_day += gtmp;
  1063.     if (p_day > 31) {        /* Correct for March days > 31 */
  1064.         p_day -= 31;
  1065.         p_month = 4;
  1066.         }
  1067.     else
  1068.         p_month = 3;
  1069.     return julian_day(p_day, p_month, year);
  1070. }
  1071.  
  1072. /*
  1073.  * passover_offset:
  1074.  * Jewish holidays: compute Julian day as offset from Passover
  1075.  * given year (>1583)
  1076.  */
  1077. double
  1078. passover_offset(offset, year, jyear)
  1079.     double    offset;
  1080.     int    *jyear, year;
  1081. {
  1082.     double    passover();
  1083.  
  1084.     return passover(year, jyear) + offset;
  1085. }
  1086.  
  1087. /*
  1088.  * purim:
  1089.  * Jewish holidays: compute Julian day and Jewish year for Purim (Feast of Lots)
  1090.  * given year (>1583)
  1091.  */
  1092. double
  1093. purim(year, jyear)
  1094.     int    *jyear, year;
  1095. {
  1096.     return passover_offset(-30.0, year, jyear);
  1097. }
  1098.  
  1099. /*
  1100.  * shavuot:
  1101.  * Jewish holidays: compute Julian day and Jewish year for First day of Shavuot
  1102.  * given year (>1583)
  1103.  */
  1104. double
  1105. shavuot(year, jyear)
  1106.     int    *jyear, year;
  1107. {
  1108.     return passover_offset(50.0, year, jyear);
  1109. }
  1110.  
  1111. /*
  1112.  * rosh_hashanah:
  1113.  * Jewish holidays: compute Julian day and Jewish year for first day of
  1114.  * Rosh Hashanah (New Year) given year (>1583)
  1115.  */
  1116. double
  1117. rosh_hashanah(year, jyear)
  1118.     int    *jyear, year;
  1119. {
  1120.     double    atmp;
  1121.     atmp = passover_offset(163.0, year, jyear);
  1122.     (*jyear)++;
  1123.     return atmp;
  1124. }
  1125.  
  1126. /*
  1127.  * yom_kippur:
  1128.  * Jewish holidays: compute Julian day and Jewish year for Yom Kippur
  1129.  * given year (>1583)
  1130.  */
  1131. double
  1132. yom_kippur(year, jyear)
  1133.     int    *jyear, year;
  1134. {
  1135.     double    atmp;
  1136.     atmp = passover_offset(172.0, year, jyear);
  1137.     (*jyear)++;
  1138.     return atmp;
  1139. }
  1140.  
  1141. /*
  1142.  * sukkot:
  1143.  * Jewish holidays: compute Julian day and Jewish year for
  1144.  * First day of Sukkot (9 days) given year (>1583)
  1145.  */
  1146. double
  1147. sukkot(year, jyear)
  1148.     int    *jyear, year;
  1149. {
  1150.     double    atmp;
  1151.     atmp = passover_offset(177.0, year, jyear);
  1152.     (*jyear)++;
  1153.     return atmp;
  1154. }
  1155.  
  1156. /*
  1157.  * simchat_torah:
  1158.  * Jewish holidays: compute Julian day and Jewish year for Simchat Torah
  1159.  * (in Diapsora) given year (>1583)
  1160.  */
  1161. double
  1162. simchat_torah(year, jyear)
  1163.     int    *jyear, year;
  1164. {
  1165.     double    atmp;
  1166.     atmp = passover_offset(185.0, year, jyear);
  1167.     (*jyear)++;
  1168.     return atmp;
  1169. }
  1170.  
  1171. /*
  1172.  * chanukah:
  1173.  * Jewish holidays: compute Julian day and Jewish year for
  1174.  * first day of Chanukah (8 days) given year (>1583)
  1175.  */
  1176. double
  1177. chanukah(year, jyear)
  1178.     int    *jyear, year;
  1179. {
  1180.     double    atmp;
  1181.     int    btmp, dummy;
  1182.  
  1183.     atmp = passover(year, jyear);
  1184.     btmp = passover((year + 1), &dummy) - atmp;
  1185.     (*jyear)++;
  1186.     if (btmp == 355 || btmp == 385)
  1187.         return atmp + 247.0;
  1188.     else
  1189.         return atmp + 246.0;
  1190. }
  1191.  
  1192. /*
  1193.  * islamic_date:
  1194.  * Islamic holidays: compute Gregorian date(s) and Islamic year for any
  1195.  * Islamic day, given ISLAMIC day (1-30), ISLAMIC month(1-12),
  1196.  * and GREGORIAN year (>1583)
  1197.  * This is complicated by the fact that a given Islamic date can appear as
  1198.  * often as twice within the same Gregorian year.
  1199.  */
  1200. islamic_date(
  1201.     mday, mmonth, number_of_days, day1, month1, day2, month2, year, myear1,
  1202.     myear2)
  1203.     double    *day1, *day2, mday;
  1204.     int    mmonth, *month1, *month2, *myear1, *myear2,
  1205.         *number_of_days, year;
  1206. {
  1207.     double    day;
  1208.     int    count, month, nyear;
  1209.     double    islamic_to_julian();
  1210.  
  1211.     *myear1 = year - 621;        /* approx. >= Muslim year */
  1212.     nyear = year - 1;
  1213.     for (count = 0; nyear != year && count <= 100; count++) {
  1214.         gregorian_date(&day, &month, &nyear,
  1215.             islamic_to_julian(mday, mmonth, *myear1));
  1216.         *myear1 = *myear1 + (year - nyear);
  1217.     }
  1218.     if (nyear == year) {
  1219.         *day1 = day;
  1220.         *month1 = month;
  1221.         /*
  1222.          * See if there is a second occurrence in same Gregorian year
  1223.          */
  1224.         gregorian_date(&day, &month, &nyear,
  1225.             islamic_to_julian(mday, mmonth, (*myear1 + 1)));
  1226.         if (nyear == year) {
  1227.             *day2 = day;
  1228.             *month2 = month;
  1229.             *number_of_days = 2;
  1230.             *myear2 = *myear1 + 1;
  1231.         }
  1232.         else {
  1233.             *number_of_days = 1;
  1234.             gregorian_date(&day, &month, &nyear,
  1235.                 islamic_to_julian(mday, mmonth, (*myear1 - 1)));
  1236.             if (nyear == year) {
  1237.                 *day2 = day;
  1238.                 *month2 = month;
  1239.                 *number_of_days = 2;
  1240.                 *myear2 = *myear1 + 1;
  1241.             }
  1242.         }
  1243.     }
  1244. /*    else return -1; */
  1245.     return 0;
  1246. }
  1247.  
  1248. /*
  1249.  * islamic_to_julian:
  1250.  * Islamic holidays: compute Julian day for any Islamic day,
  1251.  * given ISLAMIC day (1-30), ISLAMIC month(1-12), and ISLAMIC year (>962)
  1252.  * Formula from Schocken (p. 66)
  1253.  */
  1254. double
  1255. islamic_to_julian(mday, mmonth, myear)
  1256.     double    mday;
  1257.     int    mmonth, myear;
  1258. {
  1259.     double    etmp, ftmp, jday;
  1260.     int    atmp, btmp, ctmp, dtmp, nyear;
  1261.     double    corrected_julian_day();
  1262.  
  1263.     nyear = myear + 621;        /* approx. Julian year */
  1264.     atmp = ((19 * myear) - 4) % 30;
  1265.     btmp = nyear % 4;
  1266.     ctmp = (mmonth - 1) / 2;
  1267.     dtmp = (mmonth - 1) % 2;
  1268.     etmp = mday + (59.0 * ctmp) + (30.0 * dtmp) + (atmp / 30.0)
  1269.          + (btmp / 4.0) - (10.8833333 * myear) + 146.8833333;
  1270.     if (etmp < 0.0) {
  1271.         ftmp = (4.0 * fabs(etmp)) / 1461.0;
  1272.         atmp = ftmp;
  1273.         dtmp = (4.0 * fabs(etmp));
  1274.         dtmp = dtmp % 1461;
  1275.         nyear -= (atmp + 1);
  1276.         ctmp = nyear % 4;
  1277.         jday = julian_day(1.0, 3, nyear)
  1278.             + (((1461.0 - dtmp) + ctmp - btmp) / 4.0)
  1279.             - 1.0;
  1280.         return corrected_julian_day(jday);
  1281.     }
  1282.     jday = julian_day(1.0, 3, nyear) + etmp - 1.0;
  1283.     return corrected_julian_day(jday);
  1284. }
  1285.  
  1286. /*
  1287.  * islamic_new_year:
  1288.  * Islamic holidays: compute Julian date(s) and Islamic year(s)
  1289.  * for Islamic New Year given JULIAN year (>1583)
  1290.  * (note: due to the length of the Islamic year (355 d), there can be portions
  1291.  * of as many as two Islamic New Years within a given Gregorian year; for
  1292.  * example, according to the algorithm below, the Islamic New Year occured
  1293.  * twice in 1975: Wednesday 1 January, Sunday 21 December)
  1294.  *
  1295.  * Algorithm: Schocken, page 66, which agrees with dates I have obtained for
  1296.  * the (Gregorian) years 1962-2000.  Schocken outlines a Muslim calendar
  1297.  * consisting of 12 months which are alternately 30 and 29 days in length
  1298.  * (the first month, Muharram, is 30 days in length).  In a 30-year cycle,
  1299.  * there are 11 leap years in which a 30th day is added to the final month
  1300.  * of the year.  See full details below.
  1301.  *
  1302.  * According to Dr. Omar Afzal of Cornell University ((607)255-5118,
  1303.  * 277-6707; Chairman of the Committee for Crescent Observation;
  1304.  * irfan@mvax.ee.cornell.edu), this is an antiquated system which has been
  1305.  * in use for some 400 years, but today the Muslim calendar follows a more
  1306.  * strictly lunar basis.  I wish to acknowledge the Pakistani Consular
  1307.  * Office of San Francisco (415)788-0677, who referred me to Dr. Muzammil
  1308.  * Siddiqui of the Orange County Islamic Center, (714)531-1722, who in turn
  1309.  * referred me to to Dr. Afzal.  I thank Dr. Afzal for his patient and lucid
  1310.  * explanations of the Islamic calendar, and for providing printed matter and
  1311.  * tables of dates to assist me.  Among the sources he provided:
  1312.  *
  1313.  * %A U. V. Tsybulsky
  1314.  * %B Calendar of Middle Eastern Countries
  1315.  * %I Nauka Publishing House
  1316.  * %C Moscow
  1317.  * %L English
  1318.  * %D 1979
  1319.  *
  1320.  * which provides a scholarly discussion of calendrical conventions in various
  1321.  * Muslim countries, as well as simple formulas for conversion between
  1322.  * Gregorian and Islamic dates.  It also includes translations of tables which
  1323.  * originally appeared in:
  1324.  *
  1325.  * %A F. R. Unat
  1326.  * %B Hicri Tarihleri
  1327.  * %I Turktarih Kurumu Basimevi
  1328.  * %C Ankara
  1329.  * %L Turkish
  1330.  * %D 1959
  1331.  *
  1332.  * Additional tabular material is available in:
  1333.  *
  1334.  * %A G. S. P. Freeman-Grenville
  1335.  * %B The Muslim and Christian Calendars
  1336.  * %I Oxford University Press
  1337.  * %C London
  1338.  * %D 1963
  1339.  *
  1340.  * The Islamic calendar is also known as the Hijri calendar, and dates often
  1341.  * have "A.H." or "a.h." appended to them to indicate "Anno Hijri."  A
  1342.  * listing of the months and their lengths (in the old scheme):
  1343.  *
  1344.  *    Month Name     Days         Days to add to 1 Muharram to get 1st of month
  1345.  * 1  Muharram        30          0
  1346.  * 2  Safar           29         30
  1347.  * 3  Rabi' al-Awwal  30         59
  1348.  * 4  Rabi' ath-Thani 29         89
  1349.  * 5  Jumada al-Ula   30        118
  1350.  * 6  Jumada al-Akhir 29        148
  1351.  * 7  Rajab           30        177
  1352.  * 8  Sha'ban         29        207
  1353.  * 9  Ramadan         30        236
  1354.  * 10 Shawwal         29        266
  1355.  * 11 Dhul-Qa'da      30        295
  1356.  * 12 Dhul-Hijja      29 (30)   325
  1357.  *
  1358.  * In the old scheme, used here for simplicity, adherence to this set of rules
  1359.  * led to a gradual lag of the month behind the actual crescent moon.  Thus,
  1360.  * a leap day was appended to the final month in each of 11 years out of every
  1361.  * 30 year cycle: (2, 5, 7, 10, 13, 16, 18, 21, 24, 26, 29).  If the Hijra year
  1362.  * is divided by 30, and the remainder is equal to one of these numbers, it is
  1363.  * a leap year.
  1364.  *
  1365.  * The Islamic day begins after sunset, and the beginning of the month is tied
  1366.  * to the first VISIBLE appearance of the crescent moon, which often lags behind
  1367.  * the astronomical new moon.  Also, the crescent was sometimes computed
  1368.  * according to Mecca time.  There are numerous methods for defining the date
  1369.  * of the crescent moon (and hence calendar dates) among the various Muslim
  1370.  * regions of the world.  Given these uncertainties, the dates computed here are
  1371.  * likely to be accurate to only +/- 2 days.
  1372.  *
  1373.  * Any errors in this code are my own fault, and
  1374.  * not intended to offend any members of the Islamic faith.
  1375.  */
  1376. islamic_new_year(year, number_of_dates, date1, date2, myear1, myear2)
  1377.     double    *date1, *date2;
  1378.     int    *number_of_dates, *myear1, *myear2, year;
  1379. {
  1380.     double    day1, day2;
  1381.     int    month1, month2;
  1382.  
  1383.     if (islamic_date(1.0, 1, number_of_dates, &day1, &month1, &day2,
  1384.         &month2, year, myear1, myear2) < 0) return -1;
  1385.     *date1 = julian_day(day1, month1, year);
  1386.     if (*number_of_dates == 2) *date2 = julian_day(day2, month2, year);
  1387.     return 0;
  1388. }
  1389.  
  1390. /*
  1391.  * islamic_offset:
  1392.  * Islamic holidays: compute Julian day(s) for an Islamic date as an offset
  1393.  * from the Islamic New Year, given (Gregorian) year
  1394.  */
  1395. islamic_offset(offset, year, number_of_dates, date1, date2, myear1, myear2)
  1396.     double    *date1, *date2, offset;
  1397.     int    *number_of_dates, *myear1, *myear2, year;
  1398. {
  1399.     double    day, tdate1, tdate2;
  1400.     int    month, tyear1, tyear2;
  1401.  
  1402.     (void) islamic_new_year(year, number_of_dates, date1, date2,
  1403.         myear1, myear2);
  1404.     if (*number_of_dates == 2) {
  1405.         tdate2 = *date2 + offset;
  1406.         gregorian_date(&day, &month, &tyear2, tdate2);
  1407.         if (tyear2 > year) {
  1408.             tdate2 = *date1 + offset;
  1409.             (void) islamic_new_year((year - 1), number_of_dates,
  1410.                 date1, date2, myear1, myear2);
  1411.             tdate1 = *date1 + offset;
  1412.             gregorian_date(&day, &month, &tyear1, tdate1);
  1413.             if (tyear1 == year) {
  1414.                 *date1 = tdate1;
  1415.                 *date2 = tdate2;
  1416.                 *number_of_dates = 2;
  1417.                 *myear2 = *myear1 + 1;
  1418.             }
  1419.             else {
  1420.                 *date1 = tdate2;
  1421.                 *number_of_dates = 1;
  1422.                 *myear1 = *myear1 + 1;
  1423.             }
  1424.         }
  1425.         else {
  1426.             *date1 += offset;
  1427.             *date2 = tdate2;
  1428.         }
  1429.     }
  1430.     else {
  1431.         tdate2 = *date1 + offset;
  1432.         gregorian_date(&day, &month, &tyear2, tdate2);
  1433.         if (tyear2 > year) {
  1434.             (void) islamic_new_year((year - 1), number_of_dates,
  1435.                 date1, date2, myear1, myear2);
  1436.             if (*number_of_dates == 2) {
  1437.                 *date1 = *date2 + offset;
  1438.                 *number_of_dates = 1;
  1439.                 *myear1 = *myear2;
  1440.             }
  1441.             else {
  1442.                 *date1 = *date1 + offset;
  1443.             }
  1444.         }
  1445.         else {
  1446.             (void) islamic_new_year((year - 1), number_of_dates,
  1447.                 date1, date2, myear1, myear2);
  1448.             if (*number_of_dates == 2) {
  1449.                 tdate1 = *date2 + offset;
  1450.                 *myear1 = *myear2;
  1451.             }
  1452.             else {
  1453.                 tdate1 = *date1 + offset;
  1454.             }
  1455.             gregorian_date(&day, &month, &tyear1, tdate1);
  1456.             if (tyear1 == year) {
  1457.                 *date1 = tdate1;
  1458.                 *date2 = tdate2;
  1459.                 *number_of_dates = 2;
  1460.                 *myear2 = *myear1 + 1;
  1461.             }
  1462.             else {
  1463.                 *date1 = tdate2;
  1464.                 *myear1 = *myear1 + 1;
  1465.                 *number_of_dates = 1;
  1466.             }
  1467.         }
  1468.     }
  1469. }
  1470.  
  1471. /*
  1472.  * muharram_9:
  1473.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1474.  * Muharram 9 (Day of fasting) given year (>1583)
  1475.  */
  1476. muharram_9(year, number_of_dates, date1, date2, myear1, myear2)
  1477.     double    *date1, *date2;
  1478.     int    *number_of_dates, *myear1, *myear2, year;
  1479. {
  1480.     islamic_offset(
  1481.         8.0, year, number_of_dates, date1, date2, myear1, myear2);
  1482. }
  1483.  
  1484. /*
  1485.  * muharram_10:
  1486.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1487.  * Muharram 10 (Day of deliverance of Moses from the Pharoah; for Shia Islam,
  1488.  * martyrdom of Husain) given year (>1583)
  1489.  */
  1490. muharram_10(year, number_of_dates, date1, date2, myear1, myear2)
  1491.     double    *date1, *date2;
  1492.     int    *number_of_dates, *myear1, *myear2, year;
  1493. {
  1494.     islamic_offset(
  1495.         9.0, year, number_of_dates, date1, date2, myear1, myear2);
  1496. }
  1497.  
  1498. /*
  1499.  * muharram_16:
  1500.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1501.  * Muharram 16 (Imamat Day; Ismaili Khoja) given year (>1583)
  1502.  */
  1503. muharram_16(year, number_of_dates, date1, date2, myear1, myear2)
  1504.     double    *date1, *date2;
  1505.     int    *number_of_dates, *myear1, *myear2, year;
  1506. {
  1507.     islamic_offset(
  1508.         15.0, year, number_of_dates, date1, date2, myear1, myear2);
  1509. }
  1510.  
  1511. /*
  1512.  * eid_i_milad_un_nabi:
  1513.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1514.  * Rabi I 12 (Eid-i-Milad-un-Nabi: The Prophet's Birthday) given year (>1583)
  1515.  */
  1516. eid_i_milad_un_nabi(year, number_of_dates, date1, date2, myear1, myear2)
  1517.     double    *date1, *date2;
  1518.     int    *number_of_dates, *myear1, *myear2, year;
  1519. {
  1520.     islamic_offset(
  1521.         70.0, year, number_of_dates, date1, date2, myear1, myear2);
  1522. }
  1523.  
  1524. /*
  1525.  * jumada_al_akhir_23:
  1526.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1527.  * Jumada al-Akhir 23 (Birth of Agha Khan IV, Ismaili) given year (>1583)
  1528.  */
  1529. jumada_al_akhir_23(year, number_of_dates, date1, date2, myear1, myear2)
  1530.     double    *date1, *date2;
  1531.     int    *number_of_dates, *myear1, *myear2, year;
  1532. {
  1533.     islamic_offset(
  1534.         170.0, year, number_of_dates, date1, date2, myear1, myear2);
  1535. }
  1536.  
  1537. /*
  1538.  * shab_e_miraj:
  1539.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1540.  * Rajab 27 (Shab-e-Mi'raj: The Prophet's Ascension) given year (>1583)
  1541.  */
  1542. shab_e_miraj(year, number_of_dates, date1, date2, myear1, myear2)
  1543.     double    *date1, *date2;
  1544.     int    *number_of_dates, *myear1, *myear2, year;
  1545. {
  1546.     islamic_offset(
  1547.         203.0, year, number_of_dates, date1, date2, myear1, myear2);
  1548. }
  1549.  
  1550. /*
  1551.  * shab_e_barat:
  1552.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1553.  * Shaban 15 (Shab-e-Bara't: Night, followed by day of fasting) given year (>1583)
  1554.  */
  1555. shab_e_barat(year, number_of_dates, date1, date2, myear1, myear2)
  1556.     double    *date1, *date2;
  1557.     int    *number_of_dates, *myear1, *myear2, year;
  1558. {
  1559.     islamic_offset(
  1560.         221.0, year, number_of_dates, date1, date2, myear1, myear2);
  1561. }
  1562.  
  1563. /*
  1564.  * ramadan:
  1565.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1566.  * Ramadan 1 (Fasting month begins) given year (>1583)
  1567.  */
  1568. ramadan(year, number_of_dates, date1, date2, myear1, myear2)
  1569.     double    *date1, *date2;
  1570.     int    *number_of_dates, *myear1, *myear2, year;
  1571. {
  1572.     islamic_offset(
  1573.         236.0, year, number_of_dates, date1, date2, myear1, myear2);
  1574. }
  1575.  
  1576. /*
  1577.  * shab_e_qadr:
  1578.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1579.  * Ramadan 27 (Shab-e Qadr: Night vigil) given year (>1583)
  1580.  */
  1581. shab_e_qadr(year, number_of_dates, date1, date2, myear1, myear2)
  1582.     double    *date1, *date2;
  1583.     int    *number_of_dates, *myear1, *myear2, year;
  1584. {
  1585.     islamic_offset(
  1586.         262.0, year, number_of_dates, date1, date2, myear1, myear2);
  1587. }
  1588.  
  1589. /*
  1590.  * eid_al_fitr:
  1591.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1592.  * Shawwal 1 (Eid-al-Fitr: Day of Feast) given year (>1583)
  1593.  */
  1594. eid_al_fitr(year, number_of_dates, date1, date2, myear1, myear2)
  1595.     double    *date1, *date2;
  1596.     int    *number_of_dates, *myear1, *myear2, year;
  1597. {
  1598.     islamic_offset(
  1599.         266.0, year, number_of_dates, date1, date2, myear1, myear2);
  1600. }
  1601.  
  1602. /*
  1603.  * dhul_hijja_9:
  1604.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1605.  * Dhul-Hijj 9 (Day of Pilgrimage at Arafat, Mecca) given year (>1583)
  1606.  */
  1607. dhul_hijja_9(year, number_of_dates, date1, date2, myear1, myear2)
  1608.     double    *date1, *date2;
  1609.     int    *number_of_dates, *myear1, *myear2, year;
  1610. {
  1611.     islamic_offset(
  1612.         333.0, year, number_of_dates, date1, date2, myear1, myear2);
  1613. }
  1614.  
  1615. /*
  1616.  * eid_al_adha:
  1617.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1618.  * Dhul-Hijj 10 (Eid-al-Adha: Day of Abraham's Sacrifice) given year (>1583)
  1619.  */
  1620. eid_al_adha(year, number_of_dates, date1, date2, myear1, myear2)
  1621.     double    *date1, *date2;
  1622.     int    *number_of_dates, *myear1, *myear2, year;
  1623. {
  1624.     islamic_offset(
  1625.         334.0, year, number_of_dates, date1, date2, myear1, myear2);
  1626. }
  1627.  
  1628. /*
  1629.  * ghadir:
  1630.  * Islamic holidays: compute Julian day(s) and Islamic year(s) for
  1631.  * Dhul-Hijj 18 (Ghadir: Ali's Nomination) given year (>1583)
  1632.  */
  1633. ghadir(year, number_of_dates, date1, date2, myear1, myear2)
  1634.     double    *date1, *date2;
  1635.     int    *number_of_dates, *myear1, *myear2, year;
  1636. {
  1637.     islamic_offset(
  1638.         342.0, year, number_of_dates, date1, date2, myear1, myear2);
  1639. }
  1640.  
  1641. /*
  1642.  * print_islamic_string:
  1643.  * Print formatted date string(s) of form:
  1644.  *    Monday 1 August 1988 (Islamic year 1409)
  1645.  * given string labelling date, (Gregorian) year of event, and function which
  1646.  * takes (Gregorian) year as an argument and produces: the (integer) number of
  1647.  * Gregorian dates (1 or 2), the first and second (Julian) days, and the first
  1648.  * and second Islamic years.
  1649.  */
  1650. print_islamic_string(string, year, func)
  1651.     char    *string;
  1652.     int    year;
  1653.     int    (*func) ();
  1654. {
  1655.  
  1656.     char    *date;
  1657.     double    date1, date2, day, holiday;
  1658.     int    month, myear1, myear2, number_of_dates;
  1659.     char    *date_string();
  1660.  
  1661.     holiday = (*func) (
  1662.         year, &number_of_dates, &date1, &date2, &myear1, &myear2);
  1663.     if (holiday < 0)
  1664.         (void) printf("Can not determine requested date\n");
  1665.     else {
  1666.         (void) printf("%s (%d) Julian day: %f\n", string, year, date1);
  1667.         gregorian_date(&day, &month, &year, date1);
  1668.         date = date_string(get_day_of_week(day, month, year),
  1669.             day, month, year);
  1670.         (void) printf("%s (%d): %s", string, year, date);
  1671.         (void) printf(" (Islamic year %d)\n", myear1);
  1672.         if (number_of_dates == 2) {
  1673.             (void) printf(
  1674.                 "%s (%d) Julian day: %f\n", string, year,
  1675.                 date2);
  1676.             gregorian_date(&day, &month, &year, date2);
  1677.             date = date_string(get_day_of_week(day, month, year),
  1678.                 day, month, year);
  1679.             (void) printf("%s (%d): %s", string, year, date);
  1680.             (void) printf(" (Islamic year %d)\n",
  1681.                 myear2);
  1682.         }
  1683.     }
  1684. }
  1685.  
  1686. /*
  1687.  * print_date_and_time_string:
  1688.  * Print formatted date/time string of form: 10:42 Thursday 8 December 1988
  1689.  * given string labelling date, year of event, and function which takes year
  1690.  * as an argument and produces Julian day, 
  1691.  */
  1692. print_date_and_time_string(string, year, func)
  1693.     char    *string;
  1694.     int    year;
  1695.     double    (*func)();
  1696. {
  1697.     char    *date;
  1698.     double    btmp, ctmp, day, holiday;
  1699.     int    atmp, hour, min, month;
  1700.     char    *date_time_string();
  1701.  
  1702.     holiday = (*func) (year);
  1703.     (void) printf("%s (%d) Julian day: %f\n", string, year, holiday);
  1704.     gregorian_date(&day, &month, &year, holiday);
  1705.     atmp = day;
  1706.     btmp = day - atmp;
  1707.     hour = btmp * 24.0;
  1708.     ctmp = (btmp - (hour / 24.0)) * 1440.0;
  1709.     min = ctmp;
  1710.     date = date_time_string(hour, min, get_day_of_week(day, month, year),
  1711.         day, month, year);
  1712.     (void) printf("%s (%d): %s\n", string, year, date);
  1713. }
  1714.  
  1715. /*
  1716.  * print_date_string:
  1717.  * Print formatted date string of form: Thursday 8 December 1988
  1718.  * given string labelling date, year of event, and function which takes year
  1719.  * as an argument and produces Julian day. 
  1720.  */
  1721. print_date_string(string, year, func)
  1722.     char    *string;
  1723.     int    year;
  1724.     double    (*func) ();
  1725. {
  1726.     char    *date;
  1727.     double    day, holiday;
  1728.     int    month;
  1729.     char    *date_string();
  1730.  
  1731.     holiday = (*func) (year);
  1732.     (void) printf("%s (%d) Julian day: %f\n", string, year, holiday);
  1733.     gregorian_date(&day, &month, &year, holiday);
  1734.     date = date_string(get_day_of_week(day, month, year), day, month, year);
  1735.     (void) printf("%s (%d): %s\n", string, year, date);
  1736. }
  1737.  
  1738. /*
  1739.  * print_jewish_string:
  1740.  * Print formatted date string of form: Thursday 8 December 1988 (NYEAR)
  1741.  * given string labelling date, year of event, and function which takes year
  1742.  * as an argument, sets the value of a non-Gregorian year (NYEAR), and produces
  1743.  * the Julian day.
  1744.  */
  1745. print_jewish_string(string, year, func)
  1746.     char    *string;
  1747.     int    year;
  1748.     double    (*func) ();
  1749. {
  1750.     char    *date;
  1751.     double    day, holiday;
  1752.     int    month, nyear;
  1753.     char    *date_string();
  1754.  
  1755.     holiday = (*func) (year, &nyear);
  1756.     (void) printf("%s (%d) Julian day: %f\n", string, year, holiday);
  1757.     gregorian_date(&day, &month, &year, holiday);
  1758.     date = date_string_2(get_day_of_week(day, month, year), day, month,
  1759.         year, nyear);
  1760.     (void) printf("%s (%d): %s\n", string, year, date);
  1761. }
  1762.  
  1763. /*
  1764.  * extract time of day from Julian day
  1765.  */
  1766. char *
  1767. julian_time(jday)
  1768. double jday;
  1769. {
  1770.     double    h1, floor();
  1771.     int    hours, minutes;
  1772.  
  1773.     h1 = (jday - floor(jday)) * 24.; /* number of hours & frac of hours */
  1774.     hours = (int) floor(h1);
  1775.     minutes = (int) ((h1 - (double) hours) * 60.);
  1776.     sprintf(timebuf, " at %d:%02d", hours, minutes);
  1777.     return(timebuf);
  1778. }
  1779.  
  1780. /*
  1781.  * End of code
  1782.  */
  1783. #endif    /* NO_HOLIDAYS */
  1784.