Back to index

php5  5.3.10
Defines | Functions
julian.c File Reference
#include "sdncal.h"
#include <limits.h>

Go to the source code of this file.

Defines

#define JULIAN_SDN_OFFSET   32083
#define DAYS_PER_5_MONTHS   153
#define DAYS_PER_4_YEARS   1461

Functions

void SdnToJulian (long int sdn, int *pYear, int *pMonth, int *pDay)
long int JulianToSdn (int inputYear, int inputMonth, int inputDay)

Define Documentation

#define DAYS_PER_4_YEARS   1461

Definition at line 153 of file julian.c.

#define DAYS_PER_5_MONTHS   153

Definition at line 152 of file julian.c.

#define JULIAN_SDN_OFFSET   32083

Definition at line 151 of file julian.c.


Function Documentation

long int JulianToSdn ( int  inputYear,
int  inputMonth,
int  inputDay 
)

Definition at line 215 of file julian.c.

{
       int year;
       int month;

       /* check for invalid dates */
       if (inputYear == 0 || inputYear < -4713 ||
              inputMonth <= 0 || inputMonth > 12 ||
              inputDay <= 0 || inputDay > 31) {
              return (0);
       }
       /* check for dates before SDN 1 (Jan 2, 4713 B.C.) */
       if (inputYear == -4713) {
              if (inputMonth == 1 && inputDay == 1) {
                     return (0);
              }
       }
       /* Make year always a positive number. */
       if (inputYear < 0) {
              year = inputYear + 4801;
       } else {
              year = inputYear + 4800;
       }

       /* Adjust the start of the year. */
       if (inputMonth > 2) {
              month = inputMonth - 3;
       } else {
              month = inputMonth + 9;
              year--;
       }

       return ((year * DAYS_PER_4_YEARS) / 4
                     + (month * DAYS_PER_5_MONTHS + 2) / 5
                     + inputDay
                     - JULIAN_SDN_OFFSET);
}

Here is the caller graph for this function:

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

Definition at line 155 of file julian.c.

{
       int year;
       int month;
       int day;
       long int temp;
       int dayOfYear;

       if (sdn <= 0) {
              goto fail;
       }
       /* Check for overflow */
       if (sdn > (LONG_MAX - JULIAN_SDN_OFFSET * 4 + 1) / 4 || sdn < LONG_MIN / 4) {
              goto fail;
       }
       temp = sdn * 4 + (JULIAN_SDN_OFFSET * 4 - 1);

       /* Calculate the year and day of year (1 <= dayOfYear <= 366). */
       {
              long yearl = temp / DAYS_PER_4_YEARS;
              if (yearl > INT_MAX || yearl < INT_MIN) {
                     goto fail;
              }
              year = (int) yearl;
       }
       dayOfYear = (temp % DAYS_PER_4_YEARS) / 4 + 1;

       /* Calculate the month and day of month. */
       temp = dayOfYear * 5 - 3;
       month = temp / DAYS_PER_5_MONTHS;
       day = (temp % DAYS_PER_5_MONTHS) / 5 + 1;

       /* Convert to the normal beginning of the year. */
       if (month < 10) {
              month += 3;
       } else {
              year += 1;
              month -= 9;
       }

       /* Adjust to the B.C./A.D. type numbering. */
       year -= 4800;
       if (year <= 0)
              year--;

       *pYear = year;
       *pMonth = month;
       *pDay = day;
       return;

fail:
       *pYear = 0;
       *pMonth = 0;
       *pDay = 0;
}

Here is the caller graph for this function: