星期一, 3月 23, 2009

檢查日期時間合不合法


procedure CheckDate(var nYear: Integer; var nMonth: Integer;
var nDay: Integer);
begin
//Check day
case nMonth of
1, 3, 5, 7, 8, 10, 12: begin
//31 days
if(nDay >=31) then
begin
//進位
nMonth:=nMonth+1;
nDay:=nDay-31;
if(nMonth = 2) then
begin
if(nDay >= 28) then
//還要進位
CheckDate(nYear, nMonth, nDay);
end
else if(nDay >= 30) then
//還要進位
CheckDate(nYear, nMonth, nDay);
end
else if(nDay < 0) then
begin
//退位
nMonth:=nMonth-1;
//if(nMonth
end;
end;//break
4, 6, 9, 11: begin
//30 days
end;//break
2: begin
if(((nYear-1900) mod 4) = 0) then
begin
//29 days
end
else
begin
//29 days
end;
end;//break
end;
//Check month
if(nMonth > 12) then
begin
Inc(nYear);
Dec(nMonth, 12);
end
else if(nMonth < 0) then
begin
Dec(nYear);
nMonth:=12+nMonth;
end;
end;
//---------------------------------------------------------------------------

procedure CheckDateTime(var nYear: Integer; var nMonth: Integer;
var nDay: Integer; var nHour: Integer; var nMin: Integer;
var nSec: Integer; var nMSec: Integer);
var
a: Integer;

begin
//Check Millisecond.
if(nMSec >= 1000) then
begin
//進位
a:=nMSec div 1000;
nSec:=nSec+a;
nMSec:=nMSec-a*1000;
end
else if(nMSec < 0) then
begin
//退位
a:=nMSec div 1000;
Inc(a);
nSec:=nSec-a;
nMSec:=a*1000+nMSec;
end;
//Check second.
if(nSec >= 60) then
begin
//進位
a:=nSec div 60;
nMin:=nMin+a;
nSec:=nSec-a*60;
end
else if(nSec < 0) then
begin
//退位
a:=nSec div 60;
Inc(a);
nMin:=nMin-a;
nSec:=a*60+nSec;
end;
//Check minute.
if(nMin >= 60) then
begin
//進位
a:=nMin div 60;
nHour:=nHour+a;
nMin:=nMin-a*60;
end
else if(nMin < 0) then
begin
//退位
a:=nMin div 60;
Inc(a);
nHour:=nHour-a;
nMin:=a*60+nMin;
end;
CheckDate(nYear, nMonth, nDay);
end;
//---------------------------------------------------------------------------

沒有留言: