home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************/
- /* *** jtog.c *** */
- /* */
- /* This function converts a julian day number ( 1 = 1st day AD ), into */
- /* either a gregorian date character string, in any one of the four */
- /* formats described below, or an array of three integers representing */
- /* the month, day, and year, respectively, of the equivalent gregorian */
- /* date. Storage for the output is provided by the calling routine. */
- /* */
- /* The input values, in their proper order, are: */
- /* */
- /* 1. the julian date to be converted, ( unsigned long ) */
- /* */
- /* 2. a pointer to the output array, ( char * ) */
- /* */
- /* 3. the format code ( described below ) */
- /* specifying the desired output format. ( unsigned int ) */
- /* */
- /* The format codes are defined as follows: */
- /* */
- /* CODE - EXPLANATION - */
- /* */
- /* 0 - output array will hold three integers defined as: */
- /* */
- /* array[0] = month ( mm ) */
- /* array[1] = day ( dd ) */
- /* array[2] = year ( yyyy ) */
- /* */
- /* If this format option is used, a type cast will have to */
- /* be performed on the output pointer to convert it to the */
- /* appropriate pointer type. ( see example below ) */
- /* */
- /* 1 - output string will hold a character string in the */
- /* format "mmddyy" ( minimum - char string[7] ) */
- /* */
- /* 2 - output string will hold a character string in the */
- /* format "mmddyyyy" ( minimum - char string[9] ) */
- /* */
- /* 3 - output string will hold a character string in the */
- /* format "mm/dd/yy" ( minimum - char string[9] ) */
- /* */
- /* 4 - output string will hold a character string in the */
- /* format "mm/dd/yyyy" ( minimum - char string[11] ) */
- /* */
- /* NOTE - input of any format code not listed above will be */
- /* interpreted as code 0. */
- /* */
- /* The function returns a character pointer to the output array. */
- /* ______________________________________________________________ */
- /* | *** example of integer array output *** | */
- /* | | */
- /* | int month, dte_array[3]; | */
- /* | long julian = 548784; | */
- /* | | */
- /* | month = ( (int *)jtog( julian, (char *)dte_array, 0 ) )[0]; | */
- /* |______________________________________________________________| */
- /* */
- /* WARNING - the output array pointed to must be dimensioned */
- /* accordingly or a memory overwrite will occur. */
- /************************************************************************/
-
- char *jtog( julian, date, fmt )
-
- long julian;
- char *date;
- unsigned int fmt;
-
- {
- int month, day, year, i;
- long numdte, indx = !( fmt & 1 ) * 9900 + 100;
-
- year = ( julian += 146037 ) * 400 / 146097;
- julian -= (long)year*365 + year/4 - year/100 + year/400;
- year += julian / ( i = 366 - !!( ( year + 1 ) % 4 ) );
-
- if ( !( julian %= i ) ) julian = 365 + !( year % 4 ), --year;
-
- year += ( month = ( day = (int)julian * 5 - 3 ) / 153 + 2 ) / 12 - 400;
- month = month % 12 + 1;
- day = day % 153 / 5 + 1;
-
- if ( fmt && fmt < 5 )
- {
- numdte = (long)month * 100 * indx + day * indx + year % indx;
- for ( indx *= 10000, i = 0; indx > 1; ++i )
- date[i] = ( fmt > 2 && ( i == 2 || i == 5 ) ) ? '/'
- : (char)( numdte / ( indx /= 10 ) % 10 + 48 );
- date[i] = '\0';
- }
- else
- {
- ((int *)date)[0] = month;
- ((int *)date)[1] = day;
- ((int *)date)[2] = year;
- }
-
- return( date );
- }