2012-05-12 21 views
4

ファイルの最終更新日時ie. something like thisの間の経過時間を計算する必要がありますが、その違いは日数、月数、年単位であってもかまいません。Delphiの経過時間/時間間隔は?

私はこの試みた:

var 
    TimeDiff : Double; 
begin 
    TimeDiff := Now - FileAgeEx('C:\my-file.txt'); 
    if (TimeDiff >= 1) then 
     Caption := FormatDateTime('dd hh:nn:ss', TimeDiff) 
    else 
     Caption := FormatDateTime('hh:nn:ss', TimeDiff); 
end; 

をしかし、(1)それは動作しませんし、(2)私はよりよい書式設定をしたいと思います。

最終的に私の目標は、このような何かを持っていることです。

  • 時間差分< 1日==>表示本:12時○○分01秒
  • 時間のDiff> = 1日==> 、25日12時00分01秒
  • 時間のDiff> = 1年==>この表示:この表示2年、3ヶ月、10日、12時00分01秒

どうすればいいですか?

ありがとうございます!

答えて

10

主な問題は、ファイルの最終変更時刻を保持しているように見えます。あなたは、ファイルの時間形式の最終更新時刻を取得するためにLastWriteTimeを呼び出し

function LastWriteTime(const FileName: string): TFileTime; 
var 
    AttributeData: TWin32FileAttributeData; 
begin 
    if not GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, @AttributeData) then 
    RaiseLastOSError; 
    Result := AttributeData.ftLastWriteTime; 
end; 

function UTCFileTimeToSystemTime(const FileTime: TFileTime): TSystemTime; 
//returns equivalent time in current locality, taking account of daylight saving 
var 
    LocalFileTime: Windows.TFileTime; 
begin 
    Windows.FileTimeToLocalFileTime(FileTime, LocalFileTime); 
    Windows.FileTimeToSystemTime(LocalFileTime, Result); 
end; 

function UTCFileTimeToDateTime(const FileTime: TFileTime): TDateTime; 
begin 
    Result := SystemTimeToDateTime(UTCFileTimeToSystemTime(FileTime)); 
end; 

:私は、次のコードを使用します。次にUTCFileTimeToDateTimeを呼び出して、マシンの現地時間帯を考慮してTDateTimeに変換します。その値をNowと比較することができます。

フォーマットについては、既にその方法を知っているようです。あなたは基本的なアプローチがうまくいくだけで、詳細を肉付けする必要があります。あなたは2を期待するとき

FormatDateTime('dd hh:nn:ss', 2.9); 

その日の1を示していると言うコメントで


。問題は、この関数が時間間隔ではなく日付をフォーマットすることです。値2.9は、経過時間として扱われるのではなく、デルファイ時代の後の絶対日時として扱われます(2.9)。私はTruncFracを使用して、それぞれ日数と日数を取得し、そこから作業します。

Days := Trunc(TimeDiff); 
Time := Frac(TimeDiff); 

私のコードベースから直接抽出された次のコードは、あなたにいくつかのポインタを与えるかもしれません。入力は秒単位ですが、正しいパスに設定する必要があります。

function CorrectPlural(const s: string; Count: Integer): string; 
begin 
    Result := IntToStr(Count) + ' ' + s; 
    if Count<>1 then begin 
    Result := Result + 's'; 
    end; 
end; 

function HumanReadableTime(Time: Double): string; 
//Time is in seconds 
const 
    SecondsPerMinute = 60; 
    SecondsPerHour = 60*SecondsPerMinute; 
    SecondsPerDay = 24*SecondsPerHour; 
    SecondsPerWeek = 7*SecondsPerDay; 
    SecondsPerYear = 365*SecondsPerDay; 

var 
    Years, Weeks, Days, Hours, Minutes, Seconds: Int64; 

begin 
    Try 
    Years := Trunc(Time/SecondsPerYear); 
    Time := Time - Years*SecondsPerYear; 
    Weeks := Trunc(Time/SecondsPerWeek); 
    Time := Time - Weeks*SecondsPerWeek; 
    Days := Trunc(Time/SecondsPerDay); 
    Time := Time - Days*SecondsPerDay; 
    Hours := Trunc(Time/SecondsPerHour); 
    Time := Time - Hours*SecondsPerHour; 
    Minutes := Trunc(Time/SecondsPerMinute); 
    Time := Time - Minutes*SecondsPerMinute; 
    Seconds := Trunc(Time); 

    if Years>5000 then begin 
     Result := IntToStr(Round(Years/1000))+' millennia'; 
    end else if Years>500 then begin 
     Result := IntToStr(Round(Years/100))+' centuries'; 
    end else if Years>0 then begin 
     Result := CorrectPlural('year', Years) + ' ' + CorrectPlural('week', Weeks); 
    end else if Weeks>0 then begin 
     Result := CorrectPlural('week', Weeks) + ' ' + CorrectPlural('day', Days); 
    end else if Days>0 then begin 
     Result := CorrectPlural('day', Days) + ' ' + CorrectPlural('hour', Hours); 
    end else if Hours>0 then begin 
     Result := CorrectPlural('hour', Hours) + ' ' + CorrectPlural('minute', Minutes); 
    end else if Minutes>0 then begin 
     Result := CorrectPlural('minute', Minutes); 
    end else begin 
     Result := CorrectPlural('second', Seconds); 
    end; 
    Except 
    Result := 'an eternity'; 
    End; 
end; 
+0

+1特に夏時間を考慮する場合。 –

+0

ありがとう!しかし、まだ何かが間違っています... 'FormatDateTime( 'dd hh:nn:ss'、TimeDiff);' TimeDiff = '2.9487917361'(最後のmodifと現在のファイルの間でほぼ3日間)' 01 22:46:15 ' – TheDude

+1

最新の更新が役立つことを願っています。 –