将COleDateTime转换为刻度或整数表示

时间:2012-10-27 00:49:52

标签: c++ mfc atl

如何将COleDateTime转换为某种可以轻松转换回来的整数表示形式。

1 个答案:

答案 0 :(得分:4)

我认为最简单的方法是COleDateTimeSpan如下:

// Create an epoch
static COleDateTime epoch( 2000, 1, 1, 0, 0, 0 );

// Convert to integer
COleDateTime someTime;    // initialize it from somewhere
__int64 nOleDateTimeAsInt = static_cast<__int64>( (someTime - epoch).totalSeconds() );

// Create from integer
COleDateTimeSpan span( nOleDateTimeAsInt / SecondsInDay,
    (nOleDateTime % SecondsInDay) / SecondsInHour,
    (nOleDateTime % SecondsInHour) / SecondsInMinute,
    (nOleDateTime % SecondsInMinute) );
COleDateTime someTime( epoch + span );