📄 mischelpfunctions.pas
字号:
Result := DaysBetween(StartDate, Enddate);
end;
function GetDaysInTheMonth(StartDate: TDateTime; Month: Integer): Integer;
var
Year: Word;
begin
Year := YearOf(StartDate);
Result := DaysInAMonth(Year, Month);
end;
function SpanOfNowAndThen(const ANow, AThen: TDateTime): TDateTime;
begin
Result := Abs(ANow - AThen);
end;
//Function Added by Jacek
Function GetDaysApart(const Date1, Date2 : TDateTime):Longint;
begin
Result := Trunc(Date2) - Trunc(Date1);
end;
function CanDraw(StartDate, EndDate, GridStart: TDatetime; TimeType: TGanttTimeType): Boolean;
var
ItemStart, ItemEnd, GridDate,TempDate : TDateTime;
begin
Result := False;
GridDate := DateOf(GridStart); // Grid Start Date
ItemStart := DateOf(StartDate); // Item End Date
ItemEnd := DateOf(EndDate); //Item Start Date;
If TimeType = ttday then
begin
if (ItemStart <= GridDate) and (ItemEnd >= GridDate) then Result := True;
exit;
end
else
If TimeType = tthour then
begin
if (ItemStart <= GridDate) and (ItemEnd >= GridDate) then Result := True;
exit;
end
else
begin
Result := True;
end;
end;
function GetTimeFromStart(StartDate, EndDate, GridStart: TDatetime; TimeType: TGanttTimeType): Integer;
var
ItemStart, ItemEnd, GridDate : TDateTime;
begin
GridDate := DateOf(GridStart); // Grid Start Date
ItemStart := DateOf(StartDate); // Item End Date
ItemEnd := DateOf(EndDate); //Item Start Date;
if not CanDraw(StartDate, EndDate, GridStart, TimeType) then
begin
Result := 0;
exit;
end;
If TimeType = ttday then
begin
if (ItemStart = GridDate) then Result := Hourof(StartDate)
else
if (ItemStart < GridDate) and (ItemEnd > GridDate) then Result := 0
else
Result := -1;
exit;
end
else
If TimeType = tthour then
begin
if (ItemStart = GridDate) then Result := MinuteOf(StartDate)
else
if (ItemStart < GridDate) and (ItemEnd > GridDate) then Result := 0
else
Result := -1;
exit;
end
else
begin
if (ItemStart < GridDate) then
Result := -1
else
Result := GetDaysApart(GridDate, ItemStart);
end;
end;
function GetTimeLeft(StartDate, EndDate, GridStart: TDateTime; TimeType: TGanttTimeType): Integer;
var
Start: TDateTime;
TimeBetween : Int64;
Hour,Min,Sec,MSec : Word;
begin
if StartDate > GridStart then Start := StartDate
else
Start := GridStart; // start of chart
if not CanDraw(StartDate, EndDate, GridStart, TimeType) then
begin
Result := 0;
exit;
end;
If TimeType = ttday then
begin
TimeBetween := HoursBetween(Start, Enddate);
DecodeTime(Enddate, Hour, Min, Sec, MSec);
if TimeBetween >= 24 then TimeBetween := 24 // just for drawing purposes
else TimeBetween := Hour;
if (Enddate < Start) then TimeBetween := -1;
Result := TimeBetween;
exit;
end
else
If TimeType = tthour then
begin
TimeBetween := MinutesBetween(Start, Enddate);
DecodeTime(Enddate, Hour, Min, Sec, MSec);
if TimeBetween >= 60 then TimeBetween := 60 // just for drawing purposes
else TimeBetween := Min;
if (Enddate < Start) then TimeBetween := -1;
Result := TimeBetween;
exit;
end
else
begin
if (Enddate < Start) then
Result := -1
else
Result := GetDaysApart(Start, Enddate); //DaysBetween(Start, Enddate);
end;
end;
function GetDaysFromStart(StartDate, GridStart: Tdatetime; StartMonth: Integer): Integer;
var
Start, Enddate: TDateTime;
begin
//
// Start := EncodeDate(YearOf(GridStart), MonthOf(GridStart), 1); // start of chart
Start := GridStart; // start of chart
Enddate := StartDate;//DateOf(StartDate);
if Start>Enddate then
Result := DaysBetween(Start, Enddate)*-1
else
Result := DaysBetween(Start, Enddate);
end;
function GetDaysLeft(StartDate, GridStart: TDateTime; StartMonth, Days: Integer): Integer; overload;
var
Start, Enddate: TDateTime;
begin
// Start := EncodeDate(YearOf(Now), StartMonth, 1); // start of chart
Start := GridStart; // start of chart
Enddate := incDay(StartDate, Days); // end of project item
if (Enddate < Start) then
Result := -1
else
Result := DaysBetween(Start, Enddate);
end;
function GetDaysLeft(StartDate, GridDate: TDateTime; Days: Integer): Integer; overload;
var
Start, Enddate: TDateTime;
begin
// Start := EncodeDate(YearOf(GridDate), MonthOf(GridDate), 1); // start of chart
Start := GridDate; // start of chart
Enddate := incDay(StartDate, Days); // end of project item
if (Enddate < Start) then
Result := -1
else
Result := DaysBetween(Start, Enddate);
end;
function GetFirstDayofMonth (const ADate: TDateTime): TDateTime;
var Day,Month,Year : Word;
begin
DecodeDate(ADate,Year,Month,Day);
Result := EncodeDate(Year,Month,1);
end;
Function GetDateAsTitle(MyDate : TDateTime; FLanguage : TGanttLanguageValue) : String;
Var
StartDate : TDateTime;
DayNo : Double;
Hour,Min,Sec,MSec : Word;
begin
StartDate := 0.0;
DayNo := DaySpan(MyDate,StartDate);
DecodeTime(MyDate, Hour, Min, Sec, MSec);
Result := IntToStr(Trunc(DayNo)) + ' '+FLanguage.DaysString+', ' + Inttostr(Hour) + ' '+FLanguage.HourString+', ' +IntToStr(Min) +' '+ FLanguage.MinuteString;
end;
function ISOWeeksInYear (const Year: Word): Byte;
var
DOW: Integer;
begin
DOW := ISODayOfWeek (GetFirstDayOfYear (Year));
if (DOW = 4) or ((DOW = 3) and IsLeapYear (Year)) then
Result := 53
else
Result :=52;
end;
function ISODayOfWeek (const DT: TDateTime): Integer;
begin
Result := DayOfWeek (DT);
Dec (Result);
if Result = 0 then
Result := 7;
end;
function StartOfISOWeek (const DT: TDateTime): TDateTime;
begin
Result := DT - ISODayOfWeek (DT) + 1;
end;
function EndOfISOWeek (const DT: TDateTime): TDateTime;
begin
Result := DT - ISODayOfWeek (DT) + 7;
end;
function GetFirstDayOfYear (const Year: Word): TDateTime;
begin
Result := EncodeDate (Year, 1, 1);
end;
procedure Date2ISOWeekNo (const DT: TDateTime; var WeekNo: Byte;
var Year: Word);
var
FirstMonday, StartYear: TDateTime;
WeekOfs: Byte;
begin
Year := YearOf (DT);
StartYear := GetFirstDayOfYear (Year) + 3; // Jan 4th
if ISODayOfWeek (StartYear) <= 4 then
begin
FirstMonday := StartOfISOWeek (StartYear);
WeekOfs := 1;
if DT < FirstMonday then
begin
Dec (Year);
WeekNo := ISOWeeksInYear (Year);
Exit;
end;
end
else
begin
FirstMonday := StartOfISOWeek (StartYear) + 7;
WeekOfs := 2;
if DT < FirstMonday then
begin
WeekNo := 1;
Exit;
end;
end;
WeekNo := GetDaysApart (FirstMonday, StartofISOWeek (DT)) div 7 + WeekOfs;
if WeekNo > ISOWeeksInYear (Year) then
begin
WeekNo := 1;
Inc (Year);
end;
end;
function LInt2EStr (const L: LongInt): String;
begin
try
Result := IntToStr (L);
except
Result := '';
end;
end;
function FillStr (const Ch : Char; const N : Integer): string;
begin
SetLength (Result, N);
FillChar (Result [1], N, Ch);
end;
function LeftStr (const S : string; const N : Integer): string;
begin
Result := Copy (S, 1, N);
end;
function PadChLeftStr (const S : string; const Ch : Char;
const Len : Integer): string;
var
N: Integer;
begin
N := Length (S);
if N < Len then
Result := FillStr (Ch, Len - N) + S
else
Result := S;
end;
function LInt2ZStr (const L: LongInt; const Len: Byte): String;
begin
Result := LInt2EStr (L);
Result := PadChLeftStr (LeftStr (Result, Len), '0', Len);
end;
function Date2ISOWeekStr (const DT: TDateTime; weekstring: string): string;
var
WeekNo: Byte;
Year: Word;
begin
Date2ISOWeekNo (DT, WeekNo, Year);
//modif jpm
Result := weekstring + ' ' + LInt2ZStr (WeekNo, 2);
end;
{function MinimizeString(Canvas : TCanvas; aString: string; aMaxLength: integer): string;
const
cDots = '...';
var
i : integer;
begin
Result := '';
if aString = '' then Exit;
try
if Canvas.TextWidth(aString) < aMaxLength then
begin
Result := aString;
exit;
end;
// find a space to add dots
for i := Length(aString) downto 1 do
if aString[i] = ' ' then
if Canvas.TextWidth(Copy(aString, 1, i - 1) + cDots) <= aMaxLength then
begin
Result := Copy(aString, 1, i - 1) + cDots;
break;
end;
// force the dots within string
if Result = '' then
for i := Length(aString) downto 1 do
if Canvas.TextWidth(Copy(aString, 1, i - 1) + cDots) <= aMaxLength then
Result := Copy(aString, 1, i - 1) + cDots;
finally
Canvas.free;
end;
// only show first
if Result = '' then
Result := aString[1] + cDots;
end; }
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -