home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / NVDC87.ZIP / JULIAN.ZIP / CHRONO.C next >
Encoding:
C/C++ Source or Header  |  1987-07-20  |  2.6 KB  |  107 lines

  1. /*
  2. CHRONO.C:       Julian date and time routines for C
  3.  
  4. version:        5-20-87
  5. compiler:       Turbo C version 1.0
  6. uses:           stdio.h, stdlib.h, dos.h
  7. module type:    object
  8.  
  9. This file contains functions to work with calendar dates.  A
  10. calendar date is a real number in the format YYMMDD.  For
  11. example, 4/23/87 would be 870423.  Julian dates are "magic"
  12. hashed versions of calendar dates that allow arithmetic,
  13. determining the day of the week, etc.  without consulting an
  14. actual calendar.
  15.  
  16. Functions in this file:
  17.  
  18.   year(D)         Returns the year part of a calendar date
  19.   day(D)          Returns the day part of a calendar date
  20.   month(D)        Returns the month part of a calendar date
  21.   julian(D)       Converts a calendar date to a julian date
  22.   calendar(J)     Converts a julian date to a calendar date
  23.   dayofweek(J)    Returns day of week for a julian date
  24.   today()         Returns today's date (from DOS) as calendar
  25.                   date
  26. */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <dos.h>
  31.  
  32. double year(), day(), month();
  33. double julian(), calendar(), dayofweek();
  34. double today();
  35.  
  36. double year(double cal)
  37. {
  38.         double floor();
  39.  
  40.         return floor(cal / 10000.0);
  41. }
  42.  
  43. double day(double cal)
  44. {
  45.         double floor();
  46.  
  47.         return floor(cal - (floor(cal / 100.0) * 100.0));
  48. }
  49.  
  50. double month(double cal)
  51. {
  52.     double floor();
  53.  
  54.     return floor((cal - (year(cal) * 10000.0) - day(cal)) / 100.0);
  55. }
  56.  
  57. double julian(double cal)
  58. {
  59.     double m, y, floor();
  60.  
  61.     if (month(cal) > 2) {
  62.             m = month(cal) + 1.0;
  63.             y = year(cal);
  64.     }
  65.     else {
  66.             m = month(cal) + 13.0;
  67.             y = year(cal) - 1;
  68.     }
  69.     return floor(365.25*(1900.0+y))+floor(30.6001*m)+day(cal)+1720982;
  70. }
  71.  
  72. double calendar(double jul)
  73. {
  74.         double m, d, y, dayno, floor();
  75.  
  76.         dayno = jul - 1720982;
  77.         y = floor((dayno - 122.1) / 365.25);
  78.         m = floor((dayno - floor(365.25 * y)) / 30.6001);
  79.         d = dayno-floor(365.25 * y) - floor(30.6001 * m);
  80.         m = (m < 14) ? m - 1 : m - 13;
  81.         y = (m < 3) ? y + 1 : y;
  82.         return (y - 1900) * 10000.0 + m * 100.0 + d;
  83. }
  84.  
  85. double dayofweek(double jul)
  86. {
  87.         double dayno, x, fracx, floor();
  88.  
  89.         dayno = jul - 1720982;
  90.         x = (dayno + 5.0) /7.0;
  91.         fracx = x - floor(x);
  92.         return floor(7 * fracx + 0.5);
  93. }
  94.  
  95. /*
  96. The today function is already available from Turbo C.
  97. Here's a shell using getdate().
  98. */
  99.  
  100. double today()
  101. {
  102.     struct date d;
  103.  
  104.     getdate(&d);
  105.     return (d.da_year - 1900) * 10000.0 + d.da_mon * 100.0 + d.da_day;
  106. }
  107.