|
@@ -24,11 +24,6 @@
|
|
|
{ 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;
|
|
|
|
|
|
Var
|
|
@@ -48,6 +43,13 @@ begin
|
|
|
Result:=0;
|
|
|
end;
|
|
|
|
|
|
+function ComposeDateTime(Date,Time : TDateTime) : TDateTime;
|
|
|
+
|
|
|
+begin
|
|
|
+ if Date < 0 then Result := Date - Time
|
|
|
+ else Result := Date + Time;
|
|
|
+end;
|
|
|
+
|
|
|
{==============================================================================}
|
|
|
{ Public functions }
|
|
|
{==============================================================================}
|
|
@@ -56,22 +58,22 @@ end;
|
|
|
|
|
|
function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
|
|
|
begin
|
|
|
- result.Time := Trunc(Frac(DateTime) * MSecsPerDay);
|
|
|
- result.Date := DateDelta + Trunc(System.Int(DateTime));
|
|
|
+ result.Time := Trunc(abs(Frac(DateTime)) * MSecsPerDay);
|
|
|
+ result.Date := DateDelta + trunc(DateTime);
|
|
|
end ;
|
|
|
|
|
|
{ TimeStampToDateTime converts TimeStamp to a TDateTime value }
|
|
|
|
|
|
function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
|
|
|
begin
|
|
|
- result := (TimeStamp.Date - DateDelta) + (TimeStamp.Time / MSecsPerDay);
|
|
|
-end ;
|
|
|
+ Result := ComposeDateTime(TimeStamp.Date - DateDelta,TimeStamp.Time / MSecsPerDay)
|
|
|
+end;
|
|
|
|
|
|
{ MSecsToTimeStamp }
|
|
|
|
|
|
function MSecsToTimeStamp(MSecs: comp): TTimeStamp;
|
|
|
begin
|
|
|
- result.Date := Round(msecs / msecsperday);
|
|
|
+ result.Date := Trunc(msecs / msecsperday);
|
|
|
msecs:= comp(msecs-result.date*msecsperday);
|
|
|
result.Time := Round(MSecs);
|
|
|
end ;
|
|
@@ -102,7 +104,10 @@ begin
|
|
|
end;
|
|
|
c:= Year DIV 100;
|
|
|
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;
|
|
|
|
|
@@ -144,32 +149,41 @@ procedure DecodeDate(Date: TDateTime; var Year, Month, Day: word);
|
|
|
var
|
|
|
ly,ld,lm,j : cardinal;
|
|
|
begin
|
|
|
- j := pred((Trunc(System.Int(Date)) + 693900) SHL 2);
|
|
|
- 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)
|
|
|
+ if Date <= -datedelta then // If Date is before 1-1-1 then return 0-0-0
|
|
|
+ begin
|
|
|
+ Year := 0;
|
|
|
+ Month := 0;
|
|
|
+ Day := 0;
|
|
|
+ end
|
|
|
else
|
|
|
begin
|
|
|
- dec(lm,9);
|
|
|
- inc(ly);
|
|
|
+ j := pred((Trunc(System.Int(Date)) + 693900) SHL 2);
|
|
|
+ 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;
|
|
|
- year:=ly;
|
|
|
- month:=lm;
|
|
|
- day:=ld;
|
|
|
end;
|
|
|
|
|
|
|
|
|
function DecodeDateFully(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean;
|
|
|
begin
|
|
|
DecodeDate(DateTime,Year,Month,Day);
|
|
|
- DOW:=DateTimeToTimeStamp(DateTime).Date mod 7+1;
|
|
|
+ DOW:=DayOfWeek(DateTime);
|
|
|
Result:=IsLeapYear(Year);
|
|
|
end;
|
|
|
|
|
@@ -181,7 +195,7 @@ procedure DecodeTime(Time: TDateTime; var Hour, Minute, Second, MilliSecond: wor
|
|
|
Var
|
|
|
l : cardinal;
|
|
|
begin
|
|
|
- l := Round(Frac(time) * MSecsPerDay);
|
|
|
+ l := Round(abs(Frac(time)) * MSecsPerDay);
|
|
|
Hour := l div 3600000;
|
|
|
l := l mod 3600000;
|
|
|
Minute := l div 60000;
|
|
@@ -203,8 +217,8 @@ end ;
|
|
|
|
|
|
function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
|
|
|
begin
|
|
|
- result := DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day) +
|
|
|
- DoEncodeTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond);
|
|
|
+ result := ComposeDateTime(DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day),
|
|
|
+ DoEncodeTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond));
|
|
|
end ;
|
|
|
|
|
|
{ DayOfWeek returns the Day of the week (sunday is day 1) }
|
|
@@ -241,9 +255,8 @@ var
|
|
|
SystemTime: TSystemTime;
|
|
|
begin
|
|
|
GetLocalTime(SystemTime);
|
|
|
- result := DoEncodeDate(SystemTime.Year,SystemTime.Month,SystemTime.Day) +
|
|
|
- DoEncodeTime(SystemTime.Hour,SystemTime.Minute,SystemTime.Second,SystemTime.MilliSecond);
|
|
|
-end ;
|
|
|
+ result := systemTimeToDateTime(SystemTime);
|
|
|
+end;
|
|
|
|
|
|
{ IncMonth increments DateTime with NumberOfMonths months,
|
|
|
NumberOfMonths can be less than zero }
|
|
@@ -475,7 +488,7 @@ function StrToDateTime(const s: string): TDateTime;
|
|
|
var i: integer;
|
|
|
begin
|
|
|
i := pos(' ', s);
|
|
|
-if i > 0 then result := StrToDate(Copy(S, 1, i - 1)) + StrToTime(Copy(S, i + 1, length(S)))
|
|
|
+if i > 0 then result := ComposeDateTime(StrToDate(Copy(S, 1, i - 1)), StrToTime(Copy(S, i + 1, length(S))))
|
|
|
else result := StrToDate(S);
|
|
|
end ;
|
|
|
|
|
@@ -567,7 +580,7 @@ var
|
|
|
end ;
|
|
|
token:=#255;
|
|
|
lastformattoken:=' ';
|
|
|
- while FormatCurrent < FormatEnd do
|
|
|
+ while FormatCurrent < FormatEnd do
|
|
|
begin
|
|
|
Token := UpCase(FormatCurrent^);
|
|
|
Count := 1;
|
|
@@ -691,9 +704,8 @@ var
|
|
|
end ;
|
|
|
|
|
|
begin
|
|
|
- DecodeDate(DateTime, Year, Month, Day);
|
|
|
+ DecodeDateFully(DateTime, Year, Month, Day, DayOfWeek);
|
|
|
DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
|
|
|
- DayOfWeek := SysUtils.DayOfWeek(DateTime);
|
|
|
ResultLen := 0;
|
|
|
ResultCurrent := @ResultBuffer;
|
|
|
StoreFormat(FormatStr);
|
|
@@ -715,17 +727,16 @@ Var YY,MM,DD,H,m,s,msec : Word;
|
|
|
|
|
|
begin
|
|
|
Decodedate (DateTime,YY,MM,DD);
|
|
|
+ DecodeTime (DateTime,h,m,s,msec);
|
|
|
{$ifndef unix}
|
|
|
If (YY<1980) or (YY>2099) then
|
|
|
Result:=0
|
|
|
else
|
|
|
begin
|
|
|
- DecodeTime (DateTime,h,m,s,msec);
|
|
|
Result:=(s shr 1) or (m shl 5) or (h shl 11);
|
|
|
Result:=Result or DD shl 16 or (MM shl 21) or ((YY-1980) shl 25);
|
|
|
end;
|
|
|
{$else unix}
|
|
|
- Decodetime(DateTime,h,m,s,msec);
|
|
|
Result:=LocalToEpoch(yy,mm,dd,h,m,s);
|
|
|
{$endif unix}
|
|
|
end;
|
|
@@ -745,15 +756,15 @@ Var Date,Time : Word;
|
|
|
begin
|
|
|
Date:=FileDate shr 16;
|
|
|
Time:=FileDate and $ffff;
|
|
|
- Result:=EncodeDate((Date shr 9) + 1980,(Date shr 5) and 15, Date and 31) +
|
|
|
- EncodeTime(Time shr 11, (Time shr 5) and 63, (Time and 31) shl 1,0);
|
|
|
+ Result:=ComposeDateTime(EncodeDate((Date shr 9) + 1980,(Date shr 5) and 15, Date and 31),
|
|
|
+ EncodeTime(Time shr 11, (Time shr 5) and 63, (Time and 31) shl 1,0));
|
|
|
end;
|
|
|
{$else unix}
|
|
|
var
|
|
|
y, mon, d, h, min, s: word;
|
|
|
begin
|
|
|
EpochToLocal(FileDate,y,mon,d,h,min,s);
|
|
|
- Result:=EncodeDate(y,mon,d) + EncodeTime(h,min,s,0);
|
|
|
+ Result:=ComposeDateTime(EncodeDate(y,mon,d),EncodeTime(h,min,s,0));
|
|
|
end;
|
|
|
{$endif unix}
|
|
|
|
|
@@ -768,8 +779,8 @@ function TryStrToDate(const S: string; out Value: TDateTime): Boolean;
|
|
|
result:=false
|
|
|
end;
|
|
|
end;
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
// function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
|
|
|
|
|
|
|
|
@@ -783,8 +794,8 @@ function TryStrToTime(const S: string; out Value: TDateTime): Boolean;
|
|
|
result:=false
|
|
|
end;
|
|
|
end;
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
// function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
|
|
|
|
|
|
|
|
@@ -798,8 +809,8 @@ function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean;
|
|
|
result:=false
|
|
|
end;
|
|
|
end;
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
// function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
|
|
|
|
|
|
|