home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Pascal / Applications / PCalendar 1.0 / Calendar.p next >
Encoding:
Text File  |  1994-07-23  |  4.4 KB  |  161 lines  |  [TEXT/PJMM]

  1. unit Calendar;
  2.  
  3. {Except for IsValidDate, the only connection this unit has to Calendar.c is the}
  4. {calling syntax. It uses the toolbox calls Date2Secs and Secs2Date.}
  5. {By Ingemar Ragnemalm 1994}
  6.  
  7. interface
  8.  
  9.     function IsValidDate (day: Integer; month: Integer; year: Integer): Boolean;
  10.     function DaysInMonth (theMonth: Integer; theYear: Integer): Integer;
  11.     function DaysInYear (theYear: Integer): Integer;
  12.     function FirstDayOfMonth (theMonth: Integer; theYear: Integer): Integer;
  13.     function DayOfMonth (theDay, theMonth, theYear: Integer): Integer;
  14.  
  15. { constants for month of year values (non-macish format, unfortunately)}
  16.     const
  17.         JANUARY = 1;
  18.         FEBRUARY = 2;
  19.         MARCH = 3;
  20.         APRIL = 4;
  21.         MAY = 5;
  22.         JUNE = 6;
  23.         JULY = 7;
  24.         AUGUST = 8;
  25.         SEPTEMBER = 9;
  26.         OCTOBER = 10;
  27.         NOVEMBER = 11;
  28.         DECEMBER = 12;
  29. { constants for days of week values }
  30.         SUNDAY = 1;
  31.         MONDAY = 2;
  32.         TUESDAY = 3;
  33.         WEDNESDAY = 4;
  34.         THURSDAY = 5;
  35.         FRIDAY = 6;
  36.         SATURDAY = 7;
  37.  
  38. implementation
  39.  
  40. {• IsValidDate()                                                         •//}
  41.  
  42. {• Given a day, month, year input, IsValidDate() will return TRUE }
  43. {• if the date is valid and FALSE if not.                                       •//}
  44. {• --------------------------------------------------------------- •//}
  45.  
  46.     function IsValidDate (day: Integer; month: Integer; year: Integer): Boolean;
  47.     begin
  48.         IsValidDate := FALSE;
  49.         if (year > 0) then
  50.             if ((month > 0) and (month <= 12)) then
  51.                 if (day > 0) and (day <= DaysInMonth(month, year)) then
  52.                     IsValidDate := TRUE;
  53.     end; {• IsValidDate()•//}
  54.  
  55. {• --------------------------------------------------------------- •//}
  56.  
  57. {• DaysInMonth()                                                         •//}
  58.  
  59. {• Given a month (range JANUARY..DECEMBER <1..12>) and a year,           •//}
  60. {• DaysInMonth() will return the number of days in the input month. Works•//}
  61. {• for leap years.     •//}
  62.  
  63. {• --------------------------------------------------------------- •//}
  64.  
  65.     function DaysInMonth (theMonth: Integer; theYear: Integer): Integer;
  66.         var
  67.             time: DateTimeRec;
  68.             secs: longint;
  69.     begin
  70.         time.year := theYear;
  71.         time.month := theMonth;
  72.         time.day := 32;
  73.         time.hour := 0;
  74.         time.minute := 0;
  75.         time.second := 0;
  76.         Date2Secs(time, secs);
  77.         Secs2Date(secs, time);
  78.  
  79.         DaysInMonth := 32 - time.day;
  80.     end; {• DaysInMonth()•//}
  81.  
  82. {• --------------------------------------------------------------- •//}
  83.  
  84. {• DaysInYear()                                                          •//}
  85.  
  86. {• Given an input year, DaysInYear() will compute the number of days in  •//}
  87. {• the year.              •//}
  88.  
  89. {• --------------------------------------------------------------- •//}
  90.  
  91.     function DaysInYear (theYear: Integer): Integer;
  92.         var
  93.             time: DateTimeRec;
  94.             secs: longint;
  95.     begin
  96.         time.year := theYear;
  97.         time.month := 1;
  98.         time.day := 367;
  99.         time.hour := 0;
  100.         time.minute := 0;
  101.         time.second := 0;
  102.         Date2Secs(time, secs);
  103.         Secs2Date(secs, time);
  104.  
  105.         DaysInYear := 367 - time.day;
  106.     end; {• DaysInYear()•//}
  107.  
  108. {• --------------------------------------------------------------- •//}
  109.  
  110. {• FirstDayOfMonth()                                                     •//}
  111.  
  112. {• Given a valid month in theMonth (JANUARY..DECEMBER <1..12>),          •//}
  113. {• FirstDayOfMonth() will return the actual day (SUNDAY..SATURDAY <1..7>)•//}
  114. {• that theMonth starts on.     •//}
  115.  
  116. {• --------------------------------------------------------------- •//}
  117.  
  118.     function FirstDayOfMonth (theMonth: Integer; theYear: Integer): Integer;
  119.         var
  120.             time: DateTimeRec;
  121.             secs: longint;
  122.     begin
  123.         time.year := theYear;
  124.         time.month := theMonth;
  125.         time.day := 1;
  126.         time.hour := 0;
  127.         time.minute := 0;
  128.         time.second := 0;
  129.         Date2Secs(time, secs);
  130.         Secs2Date(secs, time);
  131.  
  132.         FirstDayOfMonth := time.dayOfWeek;
  133.     end; {• FirstDayOfMonth()•//}
  134.  
  135. {• --------------------------------------------------------------- •//}
  136.  
  137. {• DayOfMonth()                                                          •//}
  138.  
  139. {• Given a valid input date, DayOfMonth() will return the actual day     •//}
  140. {• (SUNDAY..SATURDAY <1..7>).           •//}
  141.  
  142. {• --------------------------------------------------------------- •//}
  143.  
  144.     function DayOfMonth (theDay, theMonth, theYear: Integer): Integer;
  145.         var
  146.             time: DateTimeRec;
  147.             secs: longint;
  148.     begin
  149.         time.year := theYear;
  150.         time.month := theMonth;
  151.         time.day := theDay;
  152.         time.hour := 0;
  153.         time.minute := 0;
  154.         time.second := 0;
  155.         Date2Secs(time, secs);
  156.         Secs2Date(secs, time);
  157.  
  158.         DayOfMonth := time.dayOfWeek;
  159.     end; {• DayOfMonth()•//}
  160.  
  161. end.