home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / calendrs / jdayjdat.c < prev    next >
Encoding:
Text File  |  1989-03-21  |  2.2 KB  |  85 lines

  1.  5-Jul-85 08:21:39-MDT,2308;000000000001
  2. Return-Path: <unix-sources-request@BRL.ARPA>
  3. Received: from BRL-TGR.ARPA by SIMTEL20.ARPA with TCP; Fri 5 Jul 85 08:18:08-MDT
  4. Received: from usenet by TGR.BRL.ARPA id a027771; 5 Jul 85 9:46 EDT
  5. From: nrh@inmet.uucp
  6. Newsgroups: net.sources
  7. Subject: Re: date <-> day-number software sought
  8. Message-ID: <12900006@inmet.UUCP>
  9. Date: 3 Jul 85 14:06:00 GMT
  10. Nf-ID: #R:bocklin:-23200:inmet:12900006:000:1746
  11. Nf-From: inmet!nrh    Jul  3 10:06:00 1985
  12. To:       unix-sources@BRL-TGR.ARPA
  13.  
  14.  
  15. Here are two routines, jday.c and jdate.c that have served
  16. me pretty well.  They are translations from ALGOL in Collected
  17. Algorithms of CACM.
  18. /*
  19. ** Takes a date, and returns a Julian day. A Julian day is the number of
  20. ** days since some base date  (in the very distant past).
  21. ** Handy for getting date of x number of days after a given Julian date
  22. ** (use jdate to get that from the Gregorian date).
  23. ** Author: Robert G. Tantzen, translator: Nat Howard
  24. ** Translated from the algol original in Collected Algorithms of CACM
  25. ** (This and jdate are algorithm 199).
  26. */
  27. long
  28. jday(mon, day, year)
  29. int mon, day, year;
  30. {
  31.     long m = mon, d = day, y = year;
  32.     long c, ya, j;
  33.  
  34.     if(m > 2) m -= 3;
  35.     else {
  36.         m += 9;
  37.         --y;
  38.     }
  39.     c = y/100L;
  40.     ya = y - (100L * c);
  41.     j = (146097L * c) /4L + (1461L * ya) / 4L + (153L * m + 2L)/5L + d + 1721119L;
  42.     return(j);
  43. }
  44. /* Julian date converter. Takes a julian date (the number of days since
  45. ** some distant epoch or other), and returns an int pointer to static space.
  46. ** ip[0] = month;
  47. ** ip[1] = day of month;
  48. ** ip[2] = year (actual year, like 1977, not 77 unless it was  77 a.d.);
  49. ** ip[3] = day of week (0->Sunday to 6->Saturday)
  50. ** These are Gregorian.
  51. ** Copied from Algorithm 199 in Collected algorithms of the CACM
  52. ** Author: Robert G. Tantzen, Translator: Nat Howard
  53. */
  54. int *
  55. jdate(j)
  56. long j;
  57. {
  58.     static int ret[4];
  59.  
  60.     long d, m, y;
  61.  
  62.     ret[3] = (j + 1L)%7L;
  63.     j -= 1721119L;
  64.     y = (4L * j - 1L)/146097L;
  65.     j = 4L * j - 1L - 146097L * y;
  66.     d = j/4L;
  67.     j = (4L * d + 3L)/1461L;
  68.     d = 4L * d + 3L - 1461L * j;
  69.     d = (d + 4L)/4L;
  70.     m = (5L * d - 3L)/153L;
  71.     d = 5L * d - 3 - 153L * m;
  72.     d = (d + 5L) / 5L;
  73.     y = 100L * y + j;
  74.     if(m < 10) 
  75.         m += 3;
  76.     else {
  77.         m -= 9;
  78.         ++y;
  79.     }
  80.     ret[0] =  m;
  81.     ret[1] = d;
  82.     ret[2] = y;
  83.     return(ret);
  84. }
  85.