home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / croutes.zip / JTOJ.C < prev    next >
Text File  |  1984-04-05  |  2KB  |  52 lines

  1. /*                   *** jtoj.c ***                                  */
  2. /*                                                                   */
  3. /* IBM - PC microsoft "C"                                            */
  4. /*                                                                   */
  5. /* integer function that returns the julian date (1 = 1st day AD)    */
  6. /* associated with the julian date in the form (yddd).  Returns a -1 */
  7. /* if an error occured.                                              */
  8. /*                                                                   */
  9. /* Written by L. Cuthbertson, March 1983.                            */
  10. /*                                                                   */
  11. /*********************************************************************/
  12.                  
  13. #define CENTRY 19    /* current century */
  14. #define DECADE 80    /* current decade */
  15.  
  16. long jtoj(jin)
  17. char jin[];
  18. {
  19.     static int monthd[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  20.     int i;
  21.     int leapd,iyr,idays;
  22.     static long cdays = 36524, ydays = 365;
  23.  
  24.     /* parse input julian date into its pieces */
  25.     sscanf(jin,"%1d%3d",iyr,idays);
  26.  
  27.     /* calculate year */
  28.     iyr += DECADE;
  29.     if (iyr < 0 || iyr > 99) return(-1);
  30.  
  31.     /* check for invalid number of days */
  32.     if (idays < 1)
  33.         return(-1);
  34.  
  35.     if ((iyr%4 == 0) && ((iyr != 0) || (CENTRY%4 == 0))) {
  36.         if (idays > (ydays+1))
  37.             return(-1);
  38.     } else {
  39.         if (idays > ydays)
  40.             return(-1);
  41.     }
  42.  
  43.     /* determine the number of "extra" leap years caused by the  */
  44.     /* %400 criteria and add to it the number of leap years that */
  45.     /* has occured up to the prior year of current century.      */
  46.     leapd = CENTRY/4;
  47.     if (iyr != 0) leapd += (iyr-1)/4;
  48.  
  49.     /* calculate julian date */
  50.     return (CENTRY*cdays + iyr*ydays + leapd + idays);
  51. }
  52.