CFileTime

此类提供用于管理与文件关联的日期和时间值的方法。

语法

class CFileTime :  public FILETIME

成员

公共构造函数

名称 描述
CFileTime::CFileTime 构造函数。

公共方法

名称 描述
CFileTime::GetCurrentTime 调用此静态函数可检索表示当前系统日期和时间的 CFileTime 对象。
CFileTime::GetTime 调用此方法可从 CFileTime 对象中检索时间。
CFileTime::LocalToUTC 调用此方法可根据协调世界时 (UTC) 将本地文件时间转换为文件时间。
CFileTime::SetTime 调用此方法可设置由 CFileTime 对象存储的日期和时间。
CFileTime::UTCToLocal 调用此方法可根据协调世界时 (UTC) 将时间转换为本地文件时间。

公共运算符

“属性” 描述
CFileTime::operator - 此运算符用于对 CFileTimeCFileTimeSpan 对象执行减法。
CFileTime::operator != 此运算符比较两个 CFileTime 对象是否不相等。
CFileTime::operator + 此运算符用于对 CFileTimeSpan 对象执行加法。
CFileTime::operator += 此运算符用于对 CFileTimeSpan 对象执行加法并对它赋予结果。
CFileTime::operator < 此运算符比较两个 CFileTime 对象以确定较小者。
CFileTime::operator <= 此运算符比较两个 CFileTime 对象以确定是否相等或较小者。
CFileTime::operator = 赋值运算符。
CFileTime::operator -= 此运算符用于对 CFileTimeSpan 对象执行减法并将结果赋予当前对象。
CFileTime::operator == 此运算符比较两个 CFileTime 对象是否相等。
CFileTime::operator > 此运算符比较两个 CFileTime 对象以确定较大者。
CFileTime::operator >= 此运算符比较两个 CFileTime 对象以确定是否相等或较大者。

公共常数

“属性” 描述
CFileTime::Day 一个静态数据成员,存储一天所包含的 100 纳秒间隔数。
CFileTime::Hour 一个静态数据成员,存储一小时所包含的 100 纳秒间隔数。
CFileTime::Millisecond 一个静态数据成员,存储一毫秒所包含的 100 纳秒间隔数。
CFileTime::Minute 一个静态数据成员,存储一分钟所包含的 100 纳秒间隔数。
CFileTime::Second 一个静态数据成员,存储一秒所包含的 100 纳秒间隔数。
CFileTime::Week 一个静态数据成员,存储一周所包含的 100 纳秒间隔数。

注解

此类提供用于管理与创建、访问和修改文件关联的日期和时间值的方法。 此类的方法和数据经常与处理相对时间值的 CFileTimeSpan 对象一起使用。

日期和时间值存储为 64 位值,表示自 1601 年 1 月 1 日以来的 100 纳秒间隔数。 此格式是协调世界时 (UTC) 格式。

提供了以下静态常量成员变量来简化计算:

成员变量 100 纳秒间隔数
Millisecond 10,000
毫秒 * 1,000
分钟 秒 * 60
小时 分钟 * 60
小时 * 24
天 * 7

注意

并非所有文件系统都可以记录创建时间和上次访问时间,并非所有文件系统都以相同的方式记录它们。 例如,在 Windows NT FAT 文件系统上,创建时间的分辨率为 10 毫秒,写入时间的分辨率为 2 秒,访问时间的分辨率为 1 天(访问日期)。 在 NTFS 上,访问时间的分辨率为 1 小时。 此外,FAT 以本地时间将时间记录在磁盘上,但 NTFS 以 UTC 时间将时间记录在磁盘上。 有关详细信息,请参阅文件时间

继承层次结构

FILETIME
 └ CFileTime

要求

头文件:atltime.h

CFileTime::CFileTime

构造函数。

CFileTime() throw();
CFileTime(const FILETIME& ft) throw();
CFileTime(ULONGLONG nTime) throw();

参数

ft
FILETIME 结构。

nTime
表示为 64 位值的日期和时间。

备注

CFileTime 对象可以使用 FILETIME 结构中的现有日期和时间创建,或者以 64 位值表示(采用本地时间格式或协调世界时 (UTC) 时间格式)。 默认构造函数将时间设置为 0。

CFileTime::Day

一个静态数据成员,存储一天所包含的 100 纳秒间隔数。

static const ULONGLONG Day = Hour* 24;

示例

请参阅 CFileTime::Millisecond 的示例。

CFileTime::GetCurrentTime

调用此静态函数可检索表示当前系统日期和时间的 CFileTime 对象。

static CFileTime GetCurrentTime() throw();

返回值

以协调世界时 (UTC) 格式返回当前系统日期和时间。

示例

// Retrieve the current time
CFileTime myFT;
myFT = CFileTime::GetCurrentTime();

CFileTime::GetTime

调用此方法可从 CFileTime 对象中检索时间。

ULONGLONG GetTime() const throw();

返回值

以 64 位数字的形式返回日期和时间,该数字可能采用本地时间格式或协调世界时 (UTC) 格式。

CFileTime::Hour

一个静态数据成员,存储一小时所包含的 100 纳秒间隔数。

static const ULONGLONG Hour = Minute* 60;

示例

请参阅 CFileTime::Millisecond 的示例。

CFileTime::LocalToUTC

调用此方法可根据协调世界时 (UTC) 将本地文件时间转换为文件时间。

CFileTime LocalToUTC() const throw();

返回值

返回一个 CFileTime 对象,其中包含 UTC 格式的时间。

示例

请参阅 CFileTime::UTCToLocal 的示例。

CFileTime::Millisecond

一个静态数据成员,存储一毫秒所包含的 100 纳秒间隔数。

static const ULONGLONG Millisecond = 10000;

示例

// Calculate the difference between two times
CFileTime myFT1, myFT2;
CFileTimeSpan myFTS;

// Get the first time
myFT1 = CFileTime::GetCurrentTime();

// Pause for a moment
UINT randVal;
rand_s(&randVal);
Sleep(randVal % 10000);

// Get the second time
myFT2 = CFileTime::GetCurrentTime();

// Calculate the time difference
myFTS = myFT2 - myFT1;

// Measure the difference
if (myFTS.GetTimeSpan() < CFileTime::Minute)
   printf_s("Less than a minute passed\n");
else
   printf_s("A minute or more passed\n");

if (myFTS.GetTimeSpan() < CFileTime::Second)
   printf_s("Less than a second passed\n");
else
   printf_s("A second or more passed\n");

if (myFTS.GetTimeSpan() < CFileTime::Millisecond)
   printf_s("Less than a millisecond passed\n");
else
   printf_s("A millisecond or more passed\n");

CFileTime::Minute

一个静态数据成员,存储一分钟所包含的 100 纳秒间隔数。

static const ULONGLONG Minute = Second* 60;

示例

请参阅 CFileTime::Millisecond 的示例。

CFileTime::operator -

此运算符用于对 CFileTimeCFileTimeSpan 对象执行减法。

CFileTime operator-(CFileTimeSpan span) const throw();
CFileTimeSpan operator-(CFileTime ft) const throw();

参数

span
CFileTimeSpan 对象。

ft
CFileTime 对象。

返回值

返回一个 CFileTime 对象或一个 CFileTimeSpan 对象,表示两个对象之间的时间差的结果。

CFileTime::operator !=

此运算符比较两个 CFileTime 对象是否不相等。

bool operator!=(CFileTime ft) const throw();

参数

ft
要比较的 CFileTime 对象。

返回值

如果所比较的项不等于 CFileTime 对象,则返回 TRUE;否则返回 FALSE

CFileTime::operator +

此运算符用于对 CFileTimeSpan 对象执行加法。

CFileTime operator+(CFileTimeSpan span) const throw();

参数

span
CFileTimeSpan 对象。

返回值

返回一个 CFileTime 对象,表示原始时间加上相对时间的结果。

CFileTime::operator +=

此运算符用于对 CFileTimeSpan 对象执行加法并对它赋予结果。

CFileTime& operator+=(CFileTimeSpan span) throw();

参数

span
CFileTimeSpan 对象。

返回值

返回更新的 CFileTime 对象,表示原始时间加上相对时间的结果。

CFileTime::operator <

此运算符比较两个 CFileTime 对象以确定较小者。

bool operator<(CFileTime ft) const throw();

参数

ft
要比较的 CFileTime 对象。

返回值

如果第一个对象小于(在时间上更早)第二个对象,则返回 TRUE,否则返回 FALSE

示例

// Test for one time less than another
// Declare the CFileType objects
CFileTime myFT1, myFT2;

// Obtain the first time value
myFT1 = CFileTime::GetCurrentTime();

// Pause for a moment...
Sleep(1000);

// Obtain the second time value
myFT2 = CFileTime::GetCurrentTime();

// Perform the comparison
if (myFT1 < myFT2)
   _tprintf_s(_T("Time is going in the correct direction.\n"));
else
   _tprintf_s(_T("Oh dear. Time is going backwards.\n"));

CFileTime::operator <>=

此运算符比较两个 CFileTime 对象以确定是否相等或较小者。

bool operator<=(CFileTime ft) const throw();

参数

ft
要比较的 CFileTime 对象。

返回值

如果第一个对象小于(在时间上更早)或等于第二个对象,则返回 TRUE,否则返回 FALSE

CFileTime::operator =

赋值运算符。

CFileTime& operator=(const FILETIME& ft) throw();

参数

ft
包含新时间和日期的 CFileTime 对象。

返回值

返回更新的 CFileTime 对象。

CFileTime::operator -=

此运算符用于对 CFileTimeSpan 对象执行减法并将结果赋予当前对象。

CFileTime& operator-=(CFileTimeSpan span) throw();

参数

span
一个 CFileTimeSpan 对象,包含要减去的相对时间。

返回值

返回更新的 CFileTime 对象。

CFileTime::operator ==

此运算符比较两个 CFileTime 对象是否相等。

bool operator==(CFileTime ft) const throw();

参数

ft
要比较的 CFileTime 对象。

返回值

如果对象相等,则返回 TRUE;否则返回 FALSE

CFileTime::operator >

此运算符比较两个 CFileTime 对象以确定较大者。

bool operator>(CFileTime ft) const throw();

参数

ft
要比较的 CFileTime 对象。

返回值

如果第一个对象大于(在时间上更晚)第二个对象,则返回 TRUE,否则返回 FALSE

CFileTime::operator >=

此运算符比较两个 CFileTime 对象以确定是否相等或较大者。

bool operator>=(CFileTime ft) const throw();

参数

ft
要比较的 CFileTime 对象。

返回值

如果第一个对象大于(在时间上更晚)或等于第二个对象,则返回 TRUE,否则返回 FALSE

CFileTime::Second

一个静态数据成员,存储一天所包含的 100 纳秒间隔数。

static const ULONGLONG Second = Millisecond* 1000;

示例

请参阅 CFileTime::Millisecond 的示例。

CFileTime::SetTime

调用此方法可设置由 CFileTime 对象存储的日期和时间。

void SetTime(ULONGLONG nTime) throw();

参数

nTime
表示日期和时间的 64 位值,采用本地时间格式或协调世界时 (UTC) 格式。

CFileTime::UTCToLocal

调用此方法可根据协调世界时 (UTC) 将时间转换为本地文件时间。

CFileTime UTCToLocal() const throw();

返回值

返回一个 CFileTime 对象,其中包含采用本地文件时间格式的时间。

示例

// Convert a UTC time to local file time format
CFileTime myUTC_FT, myL_FT;
// Get system time (in UTC format)
myUTC_FT = CFileTime::GetCurrentTime();
// Convert to local file time
myL_FT = myUTC_FT.UTCToLocal();         

CFileTime::Week

一个静态数据成员,存储一周所包含的 100 纳秒间隔数。

static const ULONGLONG Week = Day* 7;

示例

请参阅 CFileTime::Millisecond 的示例。

另请参阅

FILETIME
CFileTimeSpan
层次结构图
ATL/MFC 共享类