CTimeSpan

时间量,在内部存储为时间跨度的秒数。

语法

class CTimeSpan

成员

公共构造函数

名称 描述
CTimeSpan::CTimeSpan 以各种方式构造 CTimeSpan 对象。

公共方法

名称 描述
CTimeSpan::Format CTimeSpan 转换为格式化字符串。
CTimeSpan::GetDays 返回一个值,该值表示此 CTimeSpan 中的完成天数。
CTimeSpan::GetHours 返回一个值,该值表示当前日(-23 到 23)中的小时数。
CTimeSpan::GetMinutes 返回一个值,该值表示当前小时(-59 到 59)中的分钟数。
CTimeSpan::GetSeconds 返回一个值,该值表示当前分钟(-59 到 59)中的秒数。
CTimeSpan::GetTimeSpan 返回 CTimeSpan 对象的值。
CTimeSpan::GetTotalHours 返回一个值,该值表示此 CTimeSpan 中的总完成小时。
CTimeSpan::GetTotalMinutes 返回一个值,该值表示此 CTimeSpan 中的总完成分数。
CTimeSpan::GetTotalSeconds 返回一个值,该值表示此 CTimeSpan 中的总完成秒数。
CTimeSpan::Serialize64 将数据序列化为存档或从存档进行序列化。

运算符

名称 描述
operator + - 添加和减去 CTimeSpan 对象。
operator += -= 向/从 CTimeSpan 添加/减去 CTimeSpan 对象。
operator == < etc. 比较两个相对时间值。

注解

CTimeSpan 没有基类。

CTimeSpan 函数将秒转换为天、小时、分钟和秒的各种组合。

CTimeSpan 对象存储在 8 个字节的 __time64_t 结构中。

配套类 CTime 表示绝对时间。

CTimeCTimeSpan 类不是为派生而设计的。 由于没有虚拟函数,因此 CTimeCTimeSpan 对象的大小正好是 8 个字节。 大多数成员函数都是内联函数。

有关使用 CTimeSpan 的详细信息,请参阅运行时库参考中的“日期和时间”“时间管理”等文。

要求

标头atltime.h

CTimeSpan 比较运算符

比较运算符。

bool operator==(CTimeSpan span) const throw();
bool operator!=(CTimeSpan span) const throw();
bool operator<(CTimeSpan span) const throw();
bool operator>(CTimeSpan span) const throw();
bool operator<=(CTimeSpan span) const throw();
bool operator>=(CTimeSpan span) const throw();

参数

span
要比较的对象。

返回值

这些运算符比较两个相对时间值。 如果条件的结果为 true,则为 TRUE;否则为 FALSE

示例

CTimeSpan ts1(100);
CTimeSpan ts2(110);
ATLASSERT((ts1 != ts2) && (ts1 < ts2) && (ts1 <= ts2));   

CTimeSpan::CTimeSpan

以各种方式构造 CTimeSpan 对象。

CTimeSpan() throw();
CTimeSpan(__time64_t time) throw();

CTimeSpan(
    LONG lDays,
    int nHours,
    int nMins,
    int nSecs) throw();

参数

timeSpanSrc
已存在的 CTimeSpan 对象。

time
__time64_t 时间值,即时间跨度中的秒数。

lDays, nHours, nMins, nSecs
分别为天、小时、分钟和秒。

备注

所有这些构造函数将创建使用指定相对时间初始化的新 CTimeSpan 对象。 每个构造函数如下所述:

  • CTimeSpan( ); 构造未初始化的 CTimeSpan 的对象。

  • CTimeSpan( const CTimeSpan& ); 从另一个 CTimeSpan 值构造 CTimeSpan 对象。

  • CTimeSpan( __time64_t ); 从另一个 __time64_t 类型构造 CTimeSpan 对象。

  • CTimeSpan( LONG, int, int, int ); 从以下范围的每个组件构造 CTimeSpan 对象:

    组件 范围
    lDays 0-25,000(大约)
    nHours 0-23
    nMins 0-59
    nSecs 0-59

请注意,Microsoft 基础类库的调试版本将判断一个或多个时间日组件是否在范围内。 应在调用前验证参数。

示例

CTimeSpan ts1;  // Uninitialized time value
CTimeSpan ts2a(ts1); // Copy constructor
CTimeSpan ts2b = ts1; // Copy constructor again
CTimeSpan ts3(100); // 100 seconds
CTimeSpan ts4(0, 1, 5, 12);    // 1 hour, 5 minutes, and 12 seconds   

CTimeSpan::Format

生成与此 CTimeSpan 对应的格式化字符串。

CString Format(LPCSTR pFormat) const;
CString Format(LPCTSTR pszFormat) const;
CString Format(UINT nID) const;

参数

pFormatpszFormat
类似于 printf 格式字符串的格式字符串。 每个以百分号 (%) 开头的格式设置代码将由相应的 CTimeSpan 组件替换。 格式字符串中的其他字符按原样复制到返回的字符串。 Format 的格式设置代码的值和含义如下:

  • %DCTimeSpan中的总天数

  • %H 当前日期的小时数

  • %M 当前小时的分钟数

  • %S 当前分钟的秒数

  • %% 百分号

nID
标识此格式的字符串的 ID。

返回值

包含格式化时间的 CString 对象。

注解

库的调试版本检查格式设置代码,并断言代码是否不在上面的列表中。

示例

CTimeSpan ts(3, 1, 5, 12); // 3 days, 1 hour, 5 min, and 12 sec
CString s = ts.Format(_T("Total days: %D, hours: %H, mins: %M, secs: %S"));
ATLASSERT(s == _T("Total days: 3, hours: 01, mins: 05, secs: 12"));   

CTimeSpan::GetDays

返回一个值,该值表示此 CTimeSpan 中的完成天数。

LONGLONG GetDays() const throw();

返回值

返回时间跨度中完成的 24 小时天数。 如果时间跨度为负值,则此值可能是负值。

注解

请注意,夏令时 (DST) 可能会导致 GetDays 返回可能出人意料的结果。 例如,当 DST 生效时, GetDays 将 4 月 1 日至 5 月 1 之间的天数报告为 29,而不是 30,因为 4 月份的某一天少了一小时,因此不能算作整天。

示例

CTimeSpan ts(3, 1, 5, 12); // 3 days, 1 hour, 5 min, and 12 sec
ATLASSERT(ts.GetDays() == 3);   

CTimeSpan::GetHours

返回一个值,该值表示当前日(-23 到 23)中的小时数。

LONG GetHours() const throw();

返回值

返回当前日期中的小时数。 范围为 -23 到 23。

示例

CTimeSpan ts(3, 1, 5, 12); // 3 days, 1 hour, 5 min, and 12 sec
ATLASSERT(ts.GetHours() == 1);
ATLASSERT(ts.GetMinutes() == 5);
ATLASSERT(ts.GetSeconds() == 12);   

CTimeSpan::GetMinutes

返回一个值,该值表示当前小时(-59 到 59)中的分钟数。

LONG GetMinutes() const throw();

返回值

返回当前小时内的分钟数。 范围为 -59 到 59。

示例

请参阅 GetHours 的示例。

CTimeSpan::GetSeconds

返回一个值,该值表示当前分钟(-59 到 59)中的秒数。

LONG GetSeconds() const throw();

返回值

返回当前分钟中的秒数。 范围为 -59 到 59。

示例

请参阅 GetHours 的示例。

CTimeSpan::GetTimeSpan

返回 CTimeSpan 对象的值。

__ time64_t GetTimeSpan() const throw();

返回值

返回 CTimeSpan 对象的当前值。

CTimeSpan::GetTotalHours

返回一个值,该值表示此 CTimeSpan 中的总完成小时。

LONGLONG GetTotalHours() const throw();

返回值

返回 CTimeSpan 中的完整小时的总数。

示例

CTimeSpan ts(3, 1, 5, 12); // 3 days, 1 hour, 5 min, and 12 sec
ATLASSERT(ts.GetTotalHours() == 73);
ATLASSERT(ts.GetTotalMinutes() == 4385);
ATLASSERT(ts.GetTotalSeconds() == 263112);   

CTimeSpan::GetTotalMinutes

返回一个值,该值表示此 CTimeSpan 中的总完成分数。

LONGLONG GetTotalMinutes() const throw();

返回值

返回 CTimeSpan 中完整分钟的总数。

示例

请参阅 GetTotalHours 的示例。

CTimeSpan::GetTotalSeconds

返回一个值,该值表示此 CTimeSpan 中的总完成秒数。

LONGLONG GetTotalSeconds() const throw();

返回值

返回 CTimeSpan 中完整秒数的总数。

示例

请参阅 GetTotalHours 的示例。

CTimeSpan::operator +CTimeSpan::operator -

添加和减去 CTimeSpan 对象。

CTimeSpan operator+(CTimeSpan span) const throw();
CTimeSpan operator-(CTimeSpan span) const throw();

参数

span
要添加到 CTimeSpan 的对象值。

返回值

表示操作结果的 CTimeSpan 对象。

注解

通过这两个运算符,可以相互添加和减去 CTimeSpan 对象。

示例

CTimeSpan ts1(3, 1, 5, 12); // 3 days, 1 hour, 5 min, and 12 sec
CTimeSpan ts2(100); // 100 seconds
CTimeSpan ts3 = ts1 + ts2;
ATLASSERT(ts3.GetSeconds() == 52); // 6 mins, 52 secs   

CTimeSpan::operator +=CTimeSpan::operator -=

向/从 CTimeSpan 添加/减去 CTimeSpan 对象。

CTimeSpan& operator+=(CTimeSpan span) throw();
CTimeSpan& operator-=(CTimeSpan span) throw();

参数

span
要添加到 CTimeSpan 的对象值。

返回值

已更新的 CTimeSpan 对象。

注解

这些运算符用于将 CTimeSpan 对象与 CTimeSpan 相加和相减。

示例

CTimeSpan ts1(10); // 10 seconds
CTimeSpan ts2(100); // 100 seconds
ts2 -= ts1;
ATLASSERT(ts2.GetTotalSeconds() == 90);   

CTimeSpan::Serialize64

注意

此方法仅在 MFC 项目中可用。

将与成员变量关联的数据序列化为存档,或从存档序列化此类数据。

CArchive& Serialize64(CArchive& ar);

参数

ar
要更新的 CArchive 对象。

返回值

已更新的 CArchive 对象。

另请参阅

asctime_wasctime
_ftime_ftime32_ftime64
gmtime_gmtime32_gmtime64
localtime_localtime32_localtime64
strftimewcsftime_strftime_l_wcsftime_l
time_time32_time64
层次结构图
ATL/MFC 共享类