home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18423 < prev    next >
Encoding:
Text File  |  1992-12-15  |  2.6 KB  |  82 lines

  1. Newsgroups: comp.lang.c
  2. From: fred@genesis.demon.co.uk (Lawrence Kirby)
  3. Path: sparky!uunet!pipex!demon!genesis.demon.co.uk!fred
  4. Subject: Finding the number of days between two dates 
  5. Distribution: world
  6. References: <1fsk16INNhn0@cs.tut.fi>
  7. Organization: BT
  8. Lines: 69
  9. Date: Tue, 15 Dec 1992 18:31:43 +0000
  10. Message-ID: <724444303snx@genesis.demon.co.uk>
  11. Sender: usenet@demon.co.uk
  12.  
  13.  
  14. In article <1fsk16INNhn0@cs.tut.fi> pjh@cs.tut.fi writes:
  15.  
  16. >
  17. >
  18. >I need to write a standard conforming implementation of a function
  19. >that computes the number of days between two given dates. For example,
  20. >given Dec. 6, 1992 and Dec. 24, 1992, the function should return 18.
  21. >
  22. >The "obvious" way to do this is to form tm-structures of the two
  23. >dates, convert them to time_t using mktime(), and then find the
  24. >difference in seconds using difftime(). However, K&R2 is not explicit
  25. >about this and I would like to ask a few details:
  26. >
  27.  
  28. If the standard time facilities are inadequate for your purposes try the
  29. following functions which are fairly small and quick!
  30.  
  31. fromdate() converts a generalised Gregorian date to a relative day count.
  32. diffdate() takes the difference between 2 relative day counts. For portability
  33. you're limited to roughly year +/- 32766 although changing all year
  34. declarations to long should extend that to over +/- 500,000 years with 32 bit
  35. longs! (Perhaps useful for Fred Flintstone's diary! :-) )
  36.  
  37. fromdate() returns values relative to some date in year 0 (i.e. 1BC) but the
  38. precise date doesn't matter since we're only concerned with the difference.
  39.  
  40.  
  41. /********************************  diffdate()  ********************************
  42.  * Calculates the difference in days between 2 dates
  43.  */
  44.  
  45. static long diffdate(int year1, int month1, int dotm1,
  46.                      int year2, int month2, int dotm2 )
  47.  
  48. {
  49.     return(fromdate(year2, month2, dotm2) - fromdate(year1, month1, dotm1));
  50. }
  51.  
  52.  
  53. /********************************  fromdate()  ********************************
  54.  * Converts from Gregorian date to Julian days
  55.  */
  56.  
  57. static long fromdate(int year, int month, int dotm)
  58.  
  59. {
  60.     long epoque;
  61.  
  62.     month -= 2;             /* Make the year start on March 1st (!) */
  63.     if (month < 1) {
  64.         month += 12;
  65.         year--;
  66.     }
  67.  
  68.     epoque = year/400;
  69.     if (epoque*400 > year)  /* Ensure negative years are dealt with correctly */
  70.         epoque--;
  71.     year -= epoque*400;     /* Now 0 <= year < 400 */
  72.  
  73.     return(epoque*146097L + year*365L + year/4 - year/100 +
  74.            (month*367)/12 + dotm);
  75. }
  76.  
  77.  
  78. -----------------------------------------
  79. Lawrence Kirby | fred@genesis.demon.co.uk
  80. Wilts, England | 70734.126@compuserve.com
  81. -----------------------------------------
  82.