0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C++のFILETIME構造体をUnix時間に変換し、ファイル作成からの経過時間を導き出す。

Posted at

概要

GetFileTimeで作成時刻を取得し、PCローカル時刻と比較することで導く。

方法

1.ファイル作成時間の取得
GetFileTimeで作成時刻を取得する。2番目の引数が作成時時刻をFILETIME構造体で返す。

GetFileTime( hFile, &creationTime, null, null);

このFILETIME構造体は1601/1/1:00:00:00からの時間を100nsec単位で返す。
dwLowDateTimeとdwLowDateTimeはそれぞれファイル時刻の下位32bitと上位32bitである

typedef struct _FILETIME {
	DWORD dwLowDateTime;
	DWORD dwHighDateTime;
} FILETIME; 

2.PC loacl 時刻の取得
time_tとlocaltimeを使い、1970/1/1:00:00:00からのUNIX時間で現在時刻を取得する

time_t now = time(NULL);
struct tm *ltime = localtime(&now);

3.FILETIMEのUNIX時間への変換
1601/1/1:00:00:00は1970/1/1:00:00:00の11644473600sec前であるので、変換は下記関数となる

# define WINDOWS_TICK 10000000
# define SEC_TO_UNIX_EPOCH 11644473600LL

unsigned WindowsTickToUnixSeconds(long long windowsTicks)
{
     return (unsigned)(windowsTicks / WINDOWS_TICK - SEC_TO_UNIX_EPOCH);
}

windowsTicksにはdwLowDateTimeとdwHighDateTimeをnsecに変換して、代入する

windowsTicks = (((ULONGLONG)ftFileTime.dwHighDateTime) << 32) + ftFileTime.dwLowDateTime;

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?