Back to index

php5  5.3.10
Defines | Functions | Variables
jewish.c File Reference
#include "sdncal.h"

Go to the source code of this file.

Defines

#define HALAKIM_PER_HOUR   1080
#define HALAKIM_PER_DAY   25920
#define HALAKIM_PER_LUNAR_CYCLE   ((29 * HALAKIM_PER_DAY) + 13753)
#define HALAKIM_PER_METONIC_CYCLE   (HALAKIM_PER_LUNAR_CYCLE * (12 * 19 + 7))
#define JEWISH_SDN_OFFSET   347997
#define NEW_MOON_OF_CREATION   31524
#define SUNDAY   0
#define MONDAY   1
#define TUESDAY   2
#define WEDNESDAY   3
#define THURSDAY   4
#define FRIDAY   5
#define SATURDAY   6
#define NOON   (18 * HALAKIM_PER_HOUR)
#define AM3_11_20   ((9 * HALAKIM_PER_HOUR) + 204)
#define AM9_32_43   ((15 * HALAKIM_PER_HOUR) + 589)

Functions

static long int Tishri1 (int metonicYear, long int moladDay, long int moladHalakim)
static void MoladOfMetonicCycle (int metonicCycle, long int *pMoladDay, long int *pMoladHalakim)
static void FindTishriMolad (long int inputDay, int *pMetonicCycle, int *pMetonicYear, long int *pMoladDay, long int *pMoladHalakim)
static void FindStartOfYear (int year, int *pMetonicCycle, int *pMetonicYear, long int *pMoladDay, long int *pMoladHalakim, int *pTishri1)
void SdnToJewish (long int sdn, int *pYear, int *pMonth, int *pDay)
long int JewishToSdn (int year, int month, int day)

Variables

static int monthsPerYear [19]
static int yearOffset [19]
char * JewishMonthName [14]
char * JewishMonthHebName [14]

Define Documentation

#define AM3_11_20   ((9 * HALAKIM_PER_HOUR) + 204)

Definition at line 286 of file jewish.c.

#define AM9_32_43   ((15 * HALAKIM_PER_HOUR) + 589)

Definition at line 287 of file jewish.c.

#define FRIDAY   5

Definition at line 282 of file jewish.c.

#define HALAKIM_PER_DAY   25920

Definition at line 270 of file jewish.c.

#define HALAKIM_PER_HOUR   1080

Definition at line 269 of file jewish.c.

#define HALAKIM_PER_LUNAR_CYCLE   ((29 * HALAKIM_PER_DAY) + 13753)

Definition at line 271 of file jewish.c.

#define HALAKIM_PER_METONIC_CYCLE   (HALAKIM_PER_LUNAR_CYCLE * (12 * 19 + 7))

Definition at line 272 of file jewish.c.

#define JEWISH_SDN_OFFSET   347997

Definition at line 274 of file jewish.c.

#define MONDAY   1

Definition at line 278 of file jewish.c.

#define NEW_MOON_OF_CREATION   31524

Definition at line 275 of file jewish.c.

#define NOON   (18 * HALAKIM_PER_HOUR)

Definition at line 285 of file jewish.c.

#define SATURDAY   6

Definition at line 283 of file jewish.c.

#define SUNDAY   0

Definition at line 277 of file jewish.c.

#define THURSDAY   4

Definition at line 281 of file jewish.c.

#define TUESDAY   2

Definition at line 279 of file jewish.c.

#define WEDNESDAY   3

Definition at line 280 of file jewish.c.


Function Documentation

static void FindStartOfYear ( int  year,
int pMetonicCycle,
int pMetonicYear,
long int pMoladDay,
long int pMoladHalakim,
int pTishri1 
) [static]

Definition at line 481 of file jewish.c.

{
       *pMetonicCycle = (year - 1) / 19;
       *pMetonicYear = (year - 1) % 19;
       MoladOfMetonicCycle(*pMetonicCycle, pMoladDay, pMoladHalakim);

       *pMoladHalakim += HALAKIM_PER_LUNAR_CYCLE * yearOffset[*pMetonicYear];
       *pMoladDay += *pMoladHalakim / HALAKIM_PER_DAY;
       *pMoladHalakim = *pMoladHalakim % HALAKIM_PER_DAY;

       *pTishri1 = Tishri1(*pMetonicYear, *pMoladDay, *pMoladHalakim);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void FindTishriMolad ( long int  inputDay,
int pMetonicCycle,
int pMetonicYear,
long int pMoladDay,
long int pMoladHalakim 
) [static]

Definition at line 429 of file jewish.c.

{
       long int moladDay;
       long int moladHalakim;
       int metonicCycle;
       int metonicYear;

       /* Estimate the metonic cycle number.  Note that this may be an under
        * estimate because there are 6939.6896 days in a metonic cycle not
        * 6940, but it will never be an over estimate.  The loop below will
        * correct for any error in this estimate. */
       metonicCycle = (inputDay + 310) / 6940;

       /* Calculate the time of the starting molad for this metonic cycle. */
       MoladOfMetonicCycle(metonicCycle, &moladDay, &moladHalakim);

       /* If the above was an under estimate, increment the cycle number until
        * the correct one is found.  For modern dates this loop is about 98.6%
        * likely to not execute, even once, because the above estimate is
        * really quite close. */
       while (moladDay < inputDay - 6940 + 310) {
              metonicCycle++;
              moladHalakim += HALAKIM_PER_METONIC_CYCLE;
              moladDay += moladHalakim / HALAKIM_PER_DAY;
              moladHalakim = moladHalakim % HALAKIM_PER_DAY;
       }

       /* Find the molad of Tishri closest to this date. */
       for (metonicYear = 0; metonicYear < 18; metonicYear++) {
              if (moladDay > inputDay - 74) {
                     break;
              }
              moladHalakim += HALAKIM_PER_LUNAR_CYCLE * monthsPerYear[metonicYear];
              moladDay += moladHalakim / HALAKIM_PER_DAY;
              moladHalakim = moladHalakim % HALAKIM_PER_DAY;
       }

       *pMetonicCycle = metonicCycle;
       *pMetonicYear = metonicYear;
       *pMoladDay = moladDay;
       *pMoladHalakim = moladHalakim;
}

Here is the call graph for this function:

Here is the caller graph for this function:

long int JewishToSdn ( int  year,
int  month,
int  day 
)

Definition at line 646 of file jewish.c.

{
       long int sdn;
       int metonicCycle;
       int metonicYear;
       int tishri1;
       int tishri1After;
       long int moladDay;
       long int moladHalakim;
       int yearLength;
       int lengthOfAdarIAndII;

       if (year <= 0 || day <= 0 || day > 30) {
              return (0);
       }
       switch (month) {
              case 1:
              case 2:
                     /* It is Tishri or Heshvan - don't need the year length. */
                     FindStartOfYear(year, &metonicCycle, &metonicYear,
                                                 &moladDay, &moladHalakim, &tishri1);
                     if (month == 1) {
                            sdn = tishri1 + day - 1;
                     } else {
                            sdn = tishri1 + day + 29;
                     }
                     break;

              case 3:
                     /* It is Kislev - must find the year length. */

                     /* Find the start of the year. */
                     FindStartOfYear(year, &metonicCycle, &metonicYear,
                                                 &moladDay, &moladHalakim, &tishri1);

                     /* Find the end of the year. */
                     moladHalakim += HALAKIM_PER_LUNAR_CYCLE * monthsPerYear[metonicYear];
                     moladDay += moladHalakim / HALAKIM_PER_DAY;
                     moladHalakim = moladHalakim % HALAKIM_PER_DAY;
                     tishri1After = Tishri1((metonicYear + 1) % 19, moladDay, moladHalakim);

                     yearLength = tishri1After - tishri1;

                     if (yearLength == 355 || yearLength == 385) {
                            sdn = tishri1 + day + 59;
                     } else {
                            sdn = tishri1 + day + 58;
                     }
                     break;

              case 4:
              case 5:
              case 6:
                     /* It is Tevet, Shevat or Adar I - don't need the year length. */

                     FindStartOfYear(year + 1, &metonicCycle, &metonicYear,
                                                 &moladDay, &moladHalakim, &tishri1After);

                     if (monthsPerYear[(year - 1) % 19] == 12) {
                            lengthOfAdarIAndII = 29;
                     } else {
                            lengthOfAdarIAndII = 59;
                     }

                     if (month == 4) {
                            sdn = tishri1After + day - lengthOfAdarIAndII - 237;
                     } else if (month == 5) {
                            sdn = tishri1After + day - lengthOfAdarIAndII - 208;
                     } else {
                            sdn = tishri1After + day - lengthOfAdarIAndII - 178;
                     }
                     break;

              default:
                     /* It is Adar II or later - don't need the year length. */
                     FindStartOfYear(year + 1, &metonicCycle, &metonicYear,
                                                 &moladDay, &moladHalakim, &tishri1After);

                     switch (month) {
                            case 7:
                                   sdn = tishri1After + day - 207;
                                   break;
                            case 8:
                                   sdn = tishri1After + day - 178;
                                   break;
                            case 9:
                                   sdn = tishri1After + day - 148;
                                   break;
                            case 10:
                                   sdn = tishri1After + day - 119;
                                   break;
                            case 11:
                                   sdn = tishri1After + day - 89;
                                   break;
                            case 12:
                                   sdn = tishri1After + day - 60;
                                   break;
                            case 13:
                                   sdn = tishri1After + day - 30;
                                   break;
                            default:
                                   return (0);
                     }
       }
       return (sdn + JEWISH_SDN_OFFSET);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void MoladOfMetonicCycle ( int  metonicCycle,
long int pMoladDay,
long int pMoladHalakim 
) [static]

Definition at line 389 of file jewish.c.

{
       register unsigned long int r1, r2, d1, d2;

       /* Start with the time of the first molad after creation. */
       r1 = NEW_MOON_OF_CREATION;

       /* Calculate metonicCycle * HALAKIM_PER_METONIC_CYCLE.  The upper 32
        * bits of the result will be in r2 and the lower 16 bits will be
        * in r1. */
       r1 += metonicCycle * (HALAKIM_PER_METONIC_CYCLE & 0xFFFF);
       r2 = r1 >> 16;
       r2 += metonicCycle * ((HALAKIM_PER_METONIC_CYCLE >> 16) & 0xFFFF);

       /* Calculate r2r1 / HALAKIM_PER_DAY.  The remainder will be in r1, the
        * upper 16 bits of the quotient will be in d2 and the lower 16 bits
        * will be in d1. */
       d2 = r2 / HALAKIM_PER_DAY;
       r2 -= d2 * HALAKIM_PER_DAY;
       r1 = (r2 << 16) | (r1 & 0xFFFF);
       d1 = r1 / HALAKIM_PER_DAY;
       r1 -= d1 * HALAKIM_PER_DAY;

       *pMoladDay = (d2 << 16) | d1;
       *pMoladHalakim = r1;
}

Here is the caller graph for this function:

void SdnToJewish ( long int  sdn,
int pYear,
int pMonth,
int pDay 
)

Definition at line 507 of file jewish.c.

{
       long int inputDay;
       long int day;
       long int halakim;
       int metonicCycle;
       int metonicYear;
       int tishri1;
       int tishri1After;
       int yearLength;

       if (sdn <= JEWISH_SDN_OFFSET) {
              *pYear = 0;
              *pMonth = 0;
              *pDay = 0;
              return;
       }
       inputDay = sdn - JEWISH_SDN_OFFSET;

       FindTishriMolad(inputDay, &metonicCycle, &metonicYear, &day, &halakim);
       tishri1 = Tishri1(metonicYear, day, halakim);

       if (inputDay >= tishri1) {
              /* It found Tishri 1 at the start of the year. */
              *pYear = metonicCycle * 19 + metonicYear + 1;
              if (inputDay < tishri1 + 59) {
                     if (inputDay < tishri1 + 30) {
                            *pMonth = 1;
                            *pDay = inputDay - tishri1 + 1;
                     } else {
                            *pMonth = 2;
                            *pDay = inputDay - tishri1 - 29;
                     }
                     return;
              }
              /* We need the length of the year to figure this out, so find
               * Tishri 1 of the next year. */
              halakim += HALAKIM_PER_LUNAR_CYCLE * monthsPerYear[metonicYear];
              day += halakim / HALAKIM_PER_DAY;
              halakim = halakim % HALAKIM_PER_DAY;
              tishri1After = Tishri1((metonicYear + 1) % 19, day, halakim);
       } else {
              /* It found Tishri 1 at the end of the year. */
              *pYear = metonicCycle * 19 + metonicYear;
              if (inputDay >= tishri1 - 177) {
                     /* It is one of the last 6 months of the year. */
                     if (inputDay > tishri1 - 30) {
                            *pMonth = 13;
                            *pDay = inputDay - tishri1 + 30;
                     } else if (inputDay > tishri1 - 60) {
                            *pMonth = 12;
                            *pDay = inputDay - tishri1 + 60;
                     } else if (inputDay > tishri1 - 89) {
                            *pMonth = 11;
                            *pDay = inputDay - tishri1 + 89;
                     } else if (inputDay > tishri1 - 119) {
                            *pMonth = 10;
                            *pDay = inputDay - tishri1 + 119;
                     } else if (inputDay > tishri1 - 148) {
                            *pMonth = 9;
                            *pDay = inputDay - tishri1 + 148;
                     } else {
                            *pMonth = 8;
                            *pDay = inputDay - tishri1 + 178;
                     }
                     return;
              } else {
                     if (monthsPerYear[(*pYear - 1) % 19] == 13) {
                            *pMonth = 7;
                            *pDay = inputDay - tishri1 + 207;
                            if (*pDay > 0)
                                   return;
                            (*pMonth)--;
                            (*pDay) += 30;
                            if (*pDay > 0)
                                   return;
                            (*pMonth)--;
                            (*pDay) += 30;
                     } else {
                            *pMonth = 6;
                            *pDay = inputDay - tishri1 + 207;
                            if (*pDay > 0)
                                   return;
                            (*pMonth)--;
                            (*pDay) += 30;
                     }
                     if (*pDay > 0)
                            return;
                     (*pMonth)--;
                     (*pDay) += 29;
                     if (*pDay > 0)
                            return;

                     /* We need the length of the year to figure this out, so find
                      * Tishri 1 of this year. */
                     tishri1After = tishri1;
                     FindTishriMolad(day - 365,
                                                 &metonicCycle, &metonicYear, &day, &halakim);
                     tishri1 = Tishri1(metonicYear, day, halakim);
              }
       }

       yearLength = tishri1After - tishri1;
       day = inputDay - tishri1 - 29;
       if (yearLength == 355 || yearLength == 385) {
              /* Heshvan has 30 days */
              if (day <= 30) {
                     *pMonth = 2;
                     *pDay = day;
                     return;
              }
              day -= 30;
       } else {
              /* Heshvan has 29 days */
              if (day <= 29) {
                     *pMonth = 2;
                     *pDay = day;
                     return;
              }
              day -= 29;
       }

       /* It has to be Kislev. */
       *pMonth = 3;
       *pDay = day;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static long int Tishri1 ( int  metonicYear,
long int  moladDay,
long int  moladHalakim 
) [static]

Definition at line 344 of file jewish.c.

{
       long int tishri1;
       int dow;
       int leapYear;
       int lastWasLeapYear;

       tishri1 = moladDay;
       dow = tishri1 % 7;
       leapYear = metonicYear == 2 || metonicYear == 5 || metonicYear == 7
              || metonicYear == 10 || metonicYear == 13 || metonicYear == 16
              || metonicYear == 18;
       lastWasLeapYear = metonicYear == 3 || metonicYear == 6
              || metonicYear == 8 || metonicYear == 11 || metonicYear == 14
              || metonicYear == 17 || metonicYear == 0;

       /* Apply rules 2, 3 and 4. */
       if ((moladHalakim >= NOON) ||
              ((!leapYear) && dow == TUESDAY && moladHalakim >= AM3_11_20) ||
              (lastWasLeapYear && dow == MONDAY && moladHalakim >= AM9_32_43)) {
              tishri1++;
              dow++;
              if (dow == 7) {
                     dow = 0;
              }
       }
       /* Apply rule 1 after the others because it can cause an additional
        * delay of one day. */
       if (dow == WEDNESDAY || dow == FRIDAY || dow == SUNDAY) {
              tishri1++;
       }
       return (tishri1);
}

Here is the caller graph for this function:


Variable Documentation

char* JewishMonthHebName[14]
Initial value:
{
       "",
       "תשרי",
       "חשון",
       "כסלו",
       "טבת",
       "שבט",
       "אדר",
       "'אדר ב",
       "ניסן",
       "אייר",
       "סיון",
       "תמוז",
       "אב",
       "אלול"
}

Definition at line 318 of file jewish.c.

char* JewishMonthName[14]
Initial value:
{
       "",
       "Tishri",
       "Heshvan",
       "Kislev",
       "Tevet",
       "Shevat",
       "AdarI",
       "AdarII",
       "Nisan",
       "Iyyar",
       "Sivan",
       "Tammuz",
       "Av",
       "Elul"
}

Definition at line 300 of file jewish.c.

int monthsPerYear[19] [static]
Initial value:
{
12, 12, 13, 12, 12, 13, 12, 13, 12, 12, 13, 12, 12, 13, 12, 12, 13, 12, 13
}

Definition at line 289 of file jewish.c.

int yearOffset[19] [static]
Initial value:
{
       0, 12, 24, 37, 49, 61, 74, 86, 99, 111, 123,
       136, 148, 160, 173, 185, 197, 210, 222
}

Definition at line 294 of file jewish.c.