mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-14 10:09:20 +02:00
* Patch from Joost van der Sluis to correct negative date-times
git-svn-id: trunk@3635 -
This commit is contained in:
parent
5e5edcb520
commit
7c29068ae3
@ -24,11 +24,6 @@
|
|||||||
{ internal functions }
|
{ internal functions }
|
||||||
{==============================================================================}
|
{==============================================================================}
|
||||||
|
|
||||||
const
|
|
||||||
DayTable: array[Boolean, 1..12] of longint =
|
|
||||||
((0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334),
|
|
||||||
(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335));
|
|
||||||
|
|
||||||
Function DoEncodeDate(Year, Month, Day: Word): longint;
|
Function DoEncodeDate(Year, Month, Day: Word): longint;
|
||||||
|
|
||||||
Var
|
Var
|
||||||
@ -56,8 +51,8 @@ end;
|
|||||||
|
|
||||||
function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
|
function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
|
||||||
begin
|
begin
|
||||||
result.Time := Trunc(Frac(DateTime) * MSecsPerDay);
|
result.Time := Trunc(abs(Frac(DateTime)) * MSecsPerDay);
|
||||||
result.Date := DateDelta + Round(System.Int(DateTime));
|
result.Date := DateDelta + trunc(DateTime);
|
||||||
end ;
|
end ;
|
||||||
|
|
||||||
{ TimeStampToDateTime converts TimeStamp to a TDateTime value }
|
{ TimeStampToDateTime converts TimeStamp to a TDateTime value }
|
||||||
@ -71,7 +66,7 @@ end ;
|
|||||||
|
|
||||||
function MSecsToTimeStamp(MSecs: comp): TTimeStamp;
|
function MSecsToTimeStamp(MSecs: comp): TTimeStamp;
|
||||||
begin
|
begin
|
||||||
result.Date := Round(msecs / msecsperday);
|
result.Date := Trunc(msecs / msecsperday);
|
||||||
msecs:= comp(msecs-result.date*msecsperday);
|
msecs:= comp(msecs-result.date*msecsperday);
|
||||||
result.Time := Round(MSecs);
|
result.Time := Round(MSecs);
|
||||||
end ;
|
end ;
|
||||||
@ -102,7 +97,10 @@ begin
|
|||||||
end;
|
end;
|
||||||
c:= Year DIV 100;
|
c:= Year DIV 100;
|
||||||
ya:= Year - 100*c;
|
ya:= Year - 100*c;
|
||||||
Date := (146097*c) SHR 2 + (1461*ya) SHR 2 + (153*cardinal(Month)+2) DIV 5 + cardinal(Day) - 693900;
|
Date := (146097*c) SHR 2 + (1461*ya) SHR 2 + (153*cardinal(Month)+2) DIV 5 + cardinal(Day);
|
||||||
|
// Note that this line can't be part of the line above, since TDateTime is
|
||||||
|
// signed and c and ya are not
|
||||||
|
Date := Date - 693900;
|
||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -144,32 +142,41 @@ procedure DecodeDate(Date: TDateTime; var Year, Month, Day: word);
|
|||||||
var
|
var
|
||||||
ly,ld,lm,j : cardinal;
|
ly,ld,lm,j : cardinal;
|
||||||
begin
|
begin
|
||||||
j := pred((Trunc(System.Int(Date)) + 693900) SHL 2);
|
if Date <= -datedelta then // If Date is before 1-1-1 then return 0-0-0
|
||||||
ly:= j DIV 146097;
|
begin
|
||||||
j:= j - 146097 * cardinal(ly);
|
Year := 0;
|
||||||
ld := j SHR 2;
|
Month := 0;
|
||||||
j:=(ld SHL 2 + 3) DIV 1461;
|
Day := 0;
|
||||||
ld:= (cardinal(ld) SHL 2 + 7 - 1461*j) SHR 2;
|
end
|
||||||
lm:=(5 * ld-3) DIV 153;
|
|
||||||
ld:= (5 * ld +2 - 153*lm) DIV 5;
|
|
||||||
ly:= 100 * cardinal(ly) + j;
|
|
||||||
if lm < 10 then
|
|
||||||
inc(lm,3)
|
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
dec(lm,9);
|
j := pred((Trunc(System.Int(Date)) + 693900) SHL 2);
|
||||||
inc(ly);
|
ly:= j DIV 146097;
|
||||||
|
j:= j - 146097 * cardinal(ly);
|
||||||
|
ld := j SHR 2;
|
||||||
|
j:=(ld SHL 2 + 3) DIV 1461;
|
||||||
|
ld:= (cardinal(ld) SHL 2 + 7 - 1461*j) SHR 2;
|
||||||
|
lm:=(5 * ld-3) DIV 153;
|
||||||
|
ld:= (5 * ld +2 - 153*lm) DIV 5;
|
||||||
|
ly:= 100 * cardinal(ly) + j;
|
||||||
|
if lm < 10 then
|
||||||
|
inc(lm,3)
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
dec(lm,9);
|
||||||
|
inc(ly);
|
||||||
|
end;
|
||||||
|
year:=ly;
|
||||||
|
month:=lm;
|
||||||
|
day:=ld;
|
||||||
end;
|
end;
|
||||||
year:=ly;
|
|
||||||
month:=lm;
|
|
||||||
day:=ld;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
function DecodeDateFully(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean;
|
function DecodeDateFully(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean;
|
||||||
begin
|
begin
|
||||||
DecodeDate(DateTime,Year,Month,Day);
|
DecodeDate(DateTime,Year,Month,Day);
|
||||||
DOW:=DateTimeToTimeStamp(DateTime).Date mod 7+1;
|
DOW:=DayOfWeek(DateTime);
|
||||||
Result:=IsLeapYear(Year);
|
Result:=IsLeapYear(Year);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -181,7 +188,7 @@ procedure DecodeTime(Time: TDateTime; var Hour, Minute, Second, MilliSecond: wor
|
|||||||
Var
|
Var
|
||||||
l : cardinal;
|
l : cardinal;
|
||||||
begin
|
begin
|
||||||
l := Round(Frac(time) * MSecsPerDay);
|
l := Round(abs(Frac(time)) * MSecsPerDay);
|
||||||
Hour := l div 3600000;
|
Hour := l div 3600000;
|
||||||
l := l mod 3600000;
|
l := l mod 3600000;
|
||||||
Minute := l div 60000;
|
Minute := l div 60000;
|
||||||
@ -241,9 +248,8 @@ var
|
|||||||
SystemTime: TSystemTime;
|
SystemTime: TSystemTime;
|
||||||
begin
|
begin
|
||||||
GetLocalTime(SystemTime);
|
GetLocalTime(SystemTime);
|
||||||
result := DoEncodeDate(SystemTime.Year,SystemTime.Month,SystemTime.Day) +
|
result := systemTimeToDateTime(SystemTime);
|
||||||
DoEncodeTime(SystemTime.Hour,SystemTime.Minute,SystemTime.Second,SystemTime.MilliSecond);
|
end;
|
||||||
end ;
|
|
||||||
|
|
||||||
{ IncMonth increments DateTime with NumberOfMonths months,
|
{ IncMonth increments DateTime with NumberOfMonths months,
|
||||||
NumberOfMonths can be less than zero }
|
NumberOfMonths can be less than zero }
|
||||||
|
Loading…
Reference in New Issue
Block a user