home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 March / Chip_1999-03_cd.bin / zkuste / delphi / D234 / AR_COMPS.ZIP / Days.pas < prev    next >
Pascal/Delphi Source File  |  1999-01-24  |  6KB  |  154 lines

  1. {##############################################################################
  2.  # Name:           TDays - Quick # days in any month!                         #
  3.  # Author:         Adrian Roberto Barbosa                                     #
  4.  # Created:        11/11/1998                                                 #
  5.  # UpDated:        12/02/1998                                                 #
  6.  # Version:        2.1                                                        #
  7.  # Description:    This is very simple component to help you to obtain the    #
  8.  #                 months' names and their numbers of the days. It's checks   #
  9.  #                 if the year is the Leap Year and returns the name of month #
  10.  #                 in short or long forms.                                    #
  11.  # Revisions:      1.0 beta                                 Date: 11/11/1998  #
  12.  #                 1.1                                      Date: 11/12/1998  #
  13.  #                 1.2                                      Date: 12/02/1998  #
  14.  # Delphi:         Delphi 2 (tested) or later (developed in Delphi 4)         #
  15.  # Copyright:      None!!! It's free, so comments are welcome...              #
  16.  # Contact:        adrianrb@netsite.com.br OR adrianrb@base.com.br            #
  17.  ##############################################################################}
  18.  
  19. unit Days;
  20.  
  21. interface
  22.  
  23. uses
  24.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
  25.  
  26. type
  27.   TDays = class(TComponent)
  28.   private
  29.     { Private declarations }
  30.     fNumDays        : Word;             // Temporary fields statement
  31.     fMonth          : Word;             //             "
  32.     fYear           : Word;             //             "
  33.     fLeapYear       : Boolean;          //             "
  34.     fMonthName      : String;           //             "
  35.     fShortName : Boolean;               //             "
  36.     procedure SetMonth(Value : Word);          // procedures to manipulate
  37.     procedure SetYear(Value : Word);          // properties and their fields
  38.     procedure SetShortName(Value : Boolean);
  39.   protected
  40.     { Protected declarations }
  41.   public
  42.     { Public declarations }
  43.     constructor Create(AOwner : TComponent);override;
  44.     function GetLeapYear(Year : Word) : Boolean;       // public functions
  45.     function GetNumDays(Month, Year : Word) : Word;
  46.     function GetMonthName(Month: Word; ShortName: Boolean): String;
  47.   published
  48.     { Published declarations } // user interface properties
  49.     property NumDays   : Word read fNumDays write fNumDays;
  50.     property Month     : Word read fMonth write SetMonth;
  51.     property Year      : Word read fYear write SetYear;
  52.     property LeapYear  : Boolean read fLeapYear write fLeapYear;
  53.     property ShortName : Boolean read fShortName write SetShortName;
  54.     property MonthName : String read fMonthName write fMonthName;
  55.   end;
  56.  
  57. procedure Register;
  58.  
  59. implementation
  60.  
  61. procedure Register;
  62. begin
  63.   RegisterComponents('Samples', [TDays]);
  64. end;
  65.  
  66. constructor TDays.Create(AOwner : TComponent);
  67. var d,m,y : Word;
  68. begin
  69.   inherited Create(AOwner);
  70.   DecodeDate(Now,y,m,d);   // Get actual date to initialize component and
  71.   fMonth := m;             // set their properties
  72.   fYear  := y;
  73.   fNumDays   := GetNumDays(fMonth, fYear);
  74.   fMonthName := GetMonthName(fMonth, fShortName);
  75. end;
  76.  
  77. procedure TDays.SetMonth(Value : Word);
  78. begin
  79.   if fMonth <> Value then
  80.     begin                                            // Update month property
  81.       fMonth     := Value;                           // and runs functions that
  82.       fNumDays   := GetNumDays(fMonth, fYear);       // returns the no. of days
  83.       fMonthName := GetMonthName(fMonth,fShortName); // in month and their mame
  84.     end;
  85. end;
  86.  
  87. procedure TDays.SetYear(Value : Word);
  88. begin
  89.   if fYear <> Value then                   // Update year propperty and runs
  90.     begin                                  // the function that returns the no.
  91.       fYear := Value;                      // of days of month
  92.       fNumDays := GetNumDays(fMonth, fYear);
  93.     end;
  94. end;
  95.  
  96. procedure TDays.SetShortName(Value : Boolean);
  97. begin
  98.   if fShortName <> Value then            // Update the ShortName property and
  99.     begin                                // runs the function that returns the
  100.       fShortName := Value;               // name of month
  101.       fMonthName := GetMonthName(fMonth, Value);
  102.     end;
  103. end;
  104.  
  105. function TDays.GetLeapYear(Year : Word) : Boolean;
  106. begin
  107.   if (Year mod 4) <> 0 then
  108.     Result := False                  // Checks if Year is the Leap Year and
  109.   else                               // TRUE or FALSE
  110.     if (Year mod 100) <> 0 then
  111.       Result := True
  112.     else
  113.       if (Year mod 400) <> 0 then
  114.         Result := False
  115.       else
  116.         Result := True;
  117.   fLeapYear := Result;
  118. end;
  119.  
  120. function TDays.GetNumDays(Month, Year : Word) : Word;
  121. begin
  122.   Result := 0;
  123.   case Month of
  124.     1,3,5,7,8,10,12 : Result := 31;              // Returns the no. of days to
  125.     4,6,9,11        : Result := 30;              // month indicated, checking if
  126.     2             : begin                      // the year is the Leap Year
  127.                   if GetLeapYear(Year) then
  128.                   Result := 29
  129.                   else
  130.                   Result := 28;
  131.                   end;
  132.   end;
  133. end;
  134.  
  135. function TDays.GetMonthName(Month: Word; ShortName: Boolean): String;
  136. begin
  137.   case Month of  // Returns the month name, in short or long format
  138.      1 : if ShortName then Result := 'Jan' else Result := 'January';
  139.      2 : if ShortName then Result := 'Feb' else Result := 'February';
  140.      3 : if ShortName then Result := 'Mar' else Result := 'March';
  141.      4 : if ShortName then Result := 'Apr' else Result := 'April';
  142.      5 : if ShortName then Result := 'May' else Result := 'May';
  143.      6 : if ShortName then Result := 'Jun' else Result := 'June';
  144.      7 : if ShortName then Result := 'Jul' else Result := 'July';
  145.      8 : if ShortName then Result := 'Aug' else Result := 'August';
  146.      9 : if ShortName then Result := 'Sep' else Result := 'September';
  147.     10 : if ShortName then Result := 'Oct' else Result := 'October';
  148.     11 : if ShortName then Result := 'Nov' else Result := 'November';
  149.     12 : if ShortName then Result := 'Dec' else Result := 'December';
  150.   end;
  151. end;
  152.  
  153. end.
  154.