|
델파이 VCL의 DecodeDate()함수를 C++로 변환 작업에 대한 문의 입니다.
현재의 고민 사항은 T = DateTimeToTimeStamp(Days).Date; 입니다.
이 부분을 어떻게 C++로 바꾸어야 할지 모르겠네요.
---------------- 델파이 VCL 소스 -----------------------
//MonthDays 는 다음과 같이 선언되어있네요
//MonthDays: array [Boolean] of TDayTable = ((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
// (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));
function DecodeDateFully(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean;
const
D1 = 365;
D4 = D1 * 4 + 1;
D100 = D4 * 25 - 1;
D400 = D100 * 4 + 1;
var
Y, M, D, I: Word;
T: Integer;
DayTable: PDayTable;
begin
T := DateTimeToTimeStamp(DateTime).Date;
if T <= 0 then
begin
Year := 0;
Month := 0;
Day := 0;
DOW := 0;
Result := False;
end else
begin
DOW := T mod 7 + 1;
Dec(T);
Y := 1;
while T >= D400 do
begin
Dec(T, D400);
Inc(Y, 400);
end;
DivMod(T, D100, I, D);
if I = 4 then
begin
Dec(I);
Inc(D, D100);
end;
Inc(Y, I * 100);
DivMod(D, D4, I, D);
Inc(Y, I * 4);
DivMod(D, D1, I, D);
if I = 4 then
begin
Dec(I);
Inc(D, D1);
end;
Inc(Y, I);
Result := IsLeapYear(Y);
DayTable := @MonthDays[Result];
M := 1;
while True do
begin
I := DayTable^[M];
if D < I then Break;
Dec(D, I);
Inc(M);
end;
Year := Y;
Month := M;
Day := D + 1;
end;
end;
------------------- 델파이 소스 : 끝 ---------------------
---------- C++ 변환 소스 ---------------------------------------
int DayTable[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
struct tm * DecodeDate(int Days, int & Year, int & Month, int & Day)
{
int DOW;
int bRlt;
int D1 = 365;
int D4 = D1 * 4 + 1;
int D100 = D4 * 25 - 1;
int D400 = D100 * 4 + 1;
int Y, M, D, I;
int T=0; //T = DateTimeToTimeStamp(Days).Date;
if( T <= 0 )
{
Year = 0;
Month = 0;
Day = 0;
DOW = 0;
bRlt = false;
}
else
{
DOW = (int)(T % 7) + 1;
Dec(T);
Y = 1;
while( T >= D400 )
{
Dec(T, D400);
Inc(Y, 400);
}
DivMod(T, D100, I, D);
if( I = 4 )
{
Dec(I);
Inc(D, D100);
}
Inc(Y, I * 100);
DivMod(D, D4, I, D);
Inc(Y, I * 4);
DivMod(D, D1, I, D);
if( I = 4 )
{
Dec(I);
Inc(D, D1);
}
Inc(Y, I);
bRlt = IsLeapYear(Y);
if(IsLeapYear(Y))
DayTable[1] = 29;
else
DayTable[1] = 28;
M = 0;
while(true)
{
I = DayTable[M];
if( D < I ) break;
Dec(D, I);
Inc(M);
}
Year = Y;
Month = M;
Day = D + 1;
}
time_t curgmtime = time(NULL);
struct tm *oTime = localtime(&curgmtime );
oTime->tm_year = 1990;
oTime->tm_mon = 11;
oTime->tm_mday = 6;
return oTime;
}
int Inc(int iIn, int i=1)
{
return(iIn+i);
}
int Dec(int iIn, int i=1)
{
return(iIn-i);
}
void DivMod(int T, int D100, int & I, int & D)
{
I = (int)(T/D100);
D = (int)(T%D100);
}
bool IsLeapYear(int year)
{
int leap =0 ;
leap = (year % 400 == 0) || (year % 100 != 0) && (year % 4 ==0);
if(leap)
return true;
else
return false;
}
------------------- C++ 변환 소스 : 끝 ---------------------
|