unit ssTimes;
interface
function BeforeTimeString(DT: TDateTime):
String;
function DateTimeDurationString(DT: TDateTime; DifFlag, NullFlag, HourFlag, SecondFlag: Boolean):
String;
function SecondDurationString(Sec: Integer; DifFlag, NullFlag: Boolean):
String;
implementation
uses
SysUtils;
function BeforeTimeString(DT: TDateTime):
String;
var
Hour, Min, Sec, MSec: Word;
N, Dif: TDateTime;
D, H: TDate;
T: Word;
S:
String;
begin
S := '
';
if DT > 0
then
begin
N := Now;
D := TDate(DT);
H := TDate(N);
T := Trunc(H) - Trunc(D);
Dif := N - DT;
if Dif <= StrToTime('
6:00:00')
then
begin
DecodeTime(Dif, Hour, Min, Sec, MSec);
if Hour > 0
then
S := '
vor ' + FormatDateTime('
h:nn', Dif) + '
h'
else
if Min > 0
then
S := '
vor ' + FormatDateTime('
n', Dif) + '
min'
else
if Sec > 1
then
S := '
vor ' + FormatDateTime('
s', Dif) + '
sec'
else
S := '
jetzt';
end
else
begin
case T
of
0: S := '
heute';
1: S := '
gestern';
2: S := '
vorgestern';
else
S := '
vor ' + IntToStr(T) + '
Tagen';
end;
end;
end;
Result := S;
end;
function DateTimeDurationString(DT: TDateTime; DifFlag, NullFlag, HourFlag, SecondFlag: Boolean):
String;
var
Tage, Hour, Min, Sec, MSec: Word;
N, Dif: TDateTime;
S:
String;
begin
S := '
';
if DifFlag
then
begin
N := Now;
if DT > N
then
Dif := DT - N
else
Dif := 0;
end
else
begin
if DT > 0
then
Dif := DT
else
Dif := 0;
end;
if (Dif <> 0)
or (NullFlag)
then
begin
Tage := Trunc(Dif);
DecodeTime(Dif, Hour, Min, Sec, MSec);
if (Tage = 0)
or (HourFlag)
then
begin
if Hour > 0
then
begin
if SecondFlag
then
S := FormatDateTime('
hh:nn:ss', Dif) + '
h'
else
S := FormatDateTime('
hh:nn', Dif) + '
h';
end
else
if Min > 0
then
begin
if SecondFlag
then
S := FormatDateTime('
nn:ss', Dif) + '
min'
else
S := FormatDateTime('
n', Dif) + '
min';
end
else
begin
if SecondFlag
then
S := FormatDateTime('
ss', Dif) + '
sec'
else
S := '
< 1 min';
end;
end;
if Tage > 0
then
begin
if Dif - Tage = 0
then
S := '
'
else
if S <> '
'
then
S := '
, ' + S;
if Tage = 1
then
S := '
1 Tag' + S
else
S := IntToStr(Tage) + '
Tage' + S;
end;
end
else
S := '
';
Result := S;
end;
function SecondDurationString(Sec:Integer;DifFlag,NullFlag:Boolean):
String;
var
DT: TDateTime;
begin
DT := (Sec/60/60/24);
Result := DateTimeDurationString(DT, DifFlag, NullFlag, True, True);
end;
end.