unit DateTime;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
MDateTime = class(TComponent)
private
{ Private declarations }
FDay:word;
FMon:word;
FYear:word;
FHour:word;
FMin:word;
FSec:word;
FMSec:word;
procedure SetDay(aDay:word);
procedure SetMonth(aMon:word);
procedure SetYear(aYear:word);
procedure SetHour(aHour:word);
procedure SetMin(aMin:word);
procedure SetSec(aSec:word);
procedure SetMSec(aMSec:word);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure GetSysDate;
procedure GetSysTime;
procedure GetSysDateTime;
function SetSysDate:boolean;
function SetSysTime:boolean;
function SetSysDateTime:boolean;
procedure SetDate(pDate:TDateTime);
procedure SetTime(pTime:TDateTime);
procedure SetDateTime(pDateTime:TDateTime);
published
{ Published declarations }
Property Day:word read FDay write SetDay;
Property Month:word read FMon write SetMonth;
Property Year:word read FYear write SetYear;
Property Hour:word read FHour write SetHour;
Property Minute:word read FMin write SetMin;
Property Sec:word read FSec write SetSec;
Property MSec:word read FMSec write SetMsec;
end;
procedure Register;
implementation
constructor MDateTime.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
GetSysDateTime;
end;
procedure MDateTime.SetDay;
begin
if ((fMon in [1, 3, 5, 7, 8, 10, 12]) and (aDay in [1..31]))
then fDay:=aDay;
if ((fMon in [4, 6, 9, 11]) and (aDay in [1..30]))
then fDay:=aDay;
if fMon = 2 then begin
if aDay in [1..28] then fDay:=aDay else begin
if (((fYear mod 400 = 0) or ((fYear mod 4 = 0) and (fYear mod 100 > 0))) and (aDay=29))
then fDay:=aDay;
end;
end;
end;
procedure MDateTime.SetMonth;
begin
if ((aMon=2) and (fDay>28)) then fDay:=28;
if aMon in [1..12] then fMon:=aMon;
end;
procedure MDateTime.SetYear;
begin
if ((aYear>1) and (aYear<9999)) then fYear:=aYear;
end;
procedure MDateTime.SetHour;
begin
if (aHour in [0..23])
then fHour:=aHour;
end;
procedure MDateTime.SetMin;
begin
if aMin in [0..59] then fMin:=aMin;
end;
procedure MDateTime.SetSec;
begin
if aSec in [0..59] then fSec:=aSec;
end;
procedure MDateTime.SetMSec;
begin
if (aMSec <= 999) then fMSec:=aMSec;
end;
procedure MDateTime.GetSysDate;
begin
DecodeDate(Date, FYear, FMon, FDay);
end;
procedure MDateTime.GetSysTime;
begin
DecodeTime(Time, FHour, FMin, FSec, FMSec);
end;
procedure MDateTime.GetSysDateTime;
begin
GetSysDate;
GetSysTime;
end;
function MDateTime.SetSysDate:boolean;
var
tSetDati,tDati: TDateTime;
vDatiBias: Variant;
tTZI: TTimeZoneInformation;
tST: TSystemTime;
begin
GetTimeZoneInformation(tTZI);
vDatiBias := tTZI.Bias / 1440;
tDati:=EncodeDate(FYear, FMon, FDay) + Time;
tSetDati := tDati + vDatiBias;
with tST do
begin
wYear := StrToInt(FormatDateTime('yyyy', tSetDati));
wMonth := StrToInt(FormatDateTime('mm', tSetDati));
wDay := StrToInt(FormatDateTime('dd', tSetDati));
wHour := StrToInt(FormatDateTime('hh', tSetDati));
wMinute := StrToInt(FormatDateTime('nn', tSetDati));
wSecond := StrToInt(FormatDateTime('ss', tSetDati));
wMilliseconds := 0;
end;
SetSysDate := SetSystemTime(tST);
end;
function MDateTime.SetSysTime:boolean;
var
tSetDati,tDati: TDateTime;
vDatiBias: Variant;
tTZI: TTimeZoneInformation;
tST: TSystemTime;
begin
GetTimeZoneInformation(tTZI);
vDatiBias := tTZI.Bias / 1440;
tDati:=Date + EncodeTime(FHour, FMin, FSec, FMsec);
tSetDati := tDati + vDatiBias;
with tST do
begin
wYear := StrToInt(FormatDateTime('yyyy', tSetDati));
wMonth := StrToInt(FormatDateTime('mm', tSetDati));
wDay := StrToInt(FormatDateTime('dd', tSetDati));
wHour := StrToInt(FormatDateTime('hh', tSetDati));
wMinute := StrToInt(FormatDateTime('nn', tSetDati));
wSecond := StrToInt(FormatDateTime('ss', tSetDati));
wMilliseconds := 0;
end;
SetSysTime := SetSystemTime(tST);
end;
function MDateTime.SetSysDateTime:boolean;
begin
SetSysDateTime:= SetSysDate and SetSysTime;
end;
procedure MDateTime.SetDate(pDate:TDateTime);
begin
DecodeDate(pDate, FYear, FMon, FDay);
end;
procedure MDateTime.SetTime(pTime:TDateTime);
begin
DecodeTime(pTime, FHour, FMin, FSec, FMSec);
end;
procedure MDateTime.SetDateTime(pDateTime:TDateTime);
begin
SetDate(pDateTime);
SetTime(pDateTime);
end;
procedure Register;
begin
RegisterComponents('Milka', [MDateTime]);
end;
end.