65.9K
CodeProject 正在变化。 阅读更多。
Home

表示自“0001年1月1日 00:00:00”以来经过秒数的小类。

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.22/5 (7投票s)

2005年6月24日

2分钟阅读

viewsIcon

137392

downloadIcon

304

一个表示自“0001年1月1日 00:00:00”以来经过秒数的小类。

引言

大家有时会遇到 CRT “时间”不符合任务要求的问题。通常是日期范围不足,很多事情都发生在 1970 年之前。我实现了一个简单的类,它将日期/时间存储为自“0001年1月1日 00:00:00”以来经过的秒数。它允许将日期/时间的数字表示转换为秒数,反之亦然。此外,它还允许添加/减去一段时间。

该类可以很容易地修改以满足您可能有的额外要求。

  • CUDateTime - 表示自“0001年1月1日 00:00:00”以来经过秒数的 DateTime 的类。
  • SUDateTime - 表示 DateTime 作为一组无符号短整数的辅助结构体。
  • CUDateSpan - 表示要添加到日期或从日期中减去的时段的辅助类。

CUDateTime 类的接口

  • CUDateTime ( y, m, d, h, n, s )
  • CUDateTime ( const CUDateTime & )
  • CUDateTime ( const SUDateTime & )
  • unsigned short WeekDay ( void ) const - 返回星期几 [0-6] (日-六)。
  • operator SUDateTime ( void ) const - 转换为 SUDateTime
  • operator std::wstring ( void ) const - 转换为 std::wstring
  • friend bool operator < ( const CUDateTime &, const CUDateTime & ) - 小于运算符。
  • friend bool operator == ( const CUDateTime &, const CUDateTime & ) - 等于运算符。
  • friend CUDateTime operator + ( const CUDateTime &, const CUDateSpan & ) - 将跨度添加到日期。
  • friend CUDateTime operator - ( const CUDateTime &, const CUDateSpan & ) - 从日期中减去跨度。
  • friend CUDateTime & operator -= ( CUDateTime &, const CUDateSpan & ) - 将跨度添加到日期。
  • friend CUDateTime & operator += ( CUDateTime &, const CUDateSpan & ) - 从日期中减去跨度。

CUDateSpan 类的接口

  • CUDateSpan ( void )
  • CUDateSpan ( const CUDateSpan & )
  • CUDateSpan ( d, h, n, s )

用法示例

//"22 June, 2005 21:22:15"
CUDateTime dt ( 2005, 6, 22, 21, 22, 15 );
//Add 1 day, 1 hour and 3 seconds
SUDateTime sdt = dt + CUDateSpan ( 1, 1, 0, 3 );
wprintf ( L"%s", ( ( std::wstring ) CUDateTime ( sdt ) ).c_str () );
© . All rights reserved.