home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / DATE1.ZIP / DATE.LBR / gtoj.c < prev    next >
Text File  |  2011-01-30  |  3KB  |  59 lines

  1. /*                   *** gtoj.c ***                                  */
  2. /*                                                                   */
  3. /* IBM - PC microsoft "C"                                            */
  4. /*                                                                   */
  5. /* integer function that returns the julian date (1 = 1st day AD)    */
  6. /* associated with a gregorian date in the form mm/dd/yy.  Returns a */
  7. /* -1 if an error occured.                                           */
  8. /*                                                                   */
  9. /* Written by L. Cuthbertson, March 1983.                            */
  10. /*                                                                   */
  11. /*********************************************************************/
  12. /*                                                                   */
  13. /* Corrected bug in call to sscanf.  Lew Paper, 6/13/85              */
  14. /*                                                                   */
  15. /*********************************************************************/
  16.                  
  17. #define CENTRY 19        /* current century */
  18.  
  19. long gtoj(indate)
  20. char indate[];
  21. {
  22.         static int monthd[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  23.         int i;
  24.         int leapd,iyr,imo,iday;
  25.         static long cdays = 36524, ydays = 365;
  26.  
  27.         /* convert into expanded format if necessary */
  28.         if ((cvtdate(indate)) != 0) return(-1);
  29.  
  30.         /* parse gregorian date into its pieces */
  31.         sscanf(indate,"%2d%*1c%2d%*1c%2d",&imo,&iday,&iyr); /*Add &'s, L.P.*/
  32.  
  33.         /* adjust month array for leap year/non-leap year */
  34.         if (iyr < 0 || iyr > 99) return(-1);
  35.         if (iyr%4 == 0 && iyr != 0 || CENTRY%4 == 0)
  36.                 monthd[1] = 29;
  37.         else
  38.                 monthd[1] = 28;
  39.  
  40.         /* check for invalid month */
  41.         if (imo < 1 || imo > 12) return(-1);
  42.  
  43.         /* check for invalid day */
  44.         if (iday < 1 || iday > monthd[imo-1]) return(-1);
  45.  
  46.         /* determine the number of "extra" leap years caused by the  */
  47.         /* %400 criteria and add to it the number of leap years that */
  48.         /* has occured up to the prior year of current century.      */
  49.         leapd = CENTRY/4;
  50.         if (iyr != 0) leapd += (iyr-1)/4;
  51.  
  52.         /* determine number of days elapsed in current year */
  53.         for (i=0;i<(imo-1);i++)
  54.                 iday = iday + monthd[i];
  55.  
  56.         /* calculate julian date */
  57.         return (CENTRY*cdays + iyr*ydays + leapd + iday);
  58. }
  59.