不使用VariantTimeToSystemTime将OLE自动化日期(OADate)double转换为struct tm

时间:2017-07-17 16:48:51

标签: c++ date double coledatetime

我在大多数std C ++(VS2010)中编写了一个Windows DLL,它不使用MFC / ATL。

父模块确实使用MFC并将COleDateTime.m_dt传递给我的DLL,它以double的形式到达。我相信这是一个OLE自动化日期,也称为OADate。

我想将此转换为任何类型的标准结构(tm ...),它具有天,小时等,而不会将MFC,OLE等拖入我的DLL中。

之前已经问过这个问题(Convert Date/Time (as Double) to struct* tm in C++)但是,答案总是使用VariantTimeToSystemTime(),这个问题忽略了这个问题 - 不使用MFC / OLE等。

VariantTimeToSystemTime的要求是:

标题 - OleAuto.h
图书馆 - OleAut32.lib
DLL - OleAut32.dll

我的DLL目前基本没有依赖关系,所以我不想拉OleAut32.dll进行这一次转换。

到目前为止,我发现的最好的事情是C# mono code,我可以将其转换为C ++。

1 个答案:

答案 0 :(得分:1)

我有2个解决方案,第一个是使用实现<repository> <snapshots> <enabled>false</enabled> </snapshots> </repository> 的函数,因此该解决方案不会使用任何标准函数。第二种解决方案是使用标准函数gmtime_r

<强> 1。第一个解决方案:自己实现gmtime_r(gmtime_r01-Jan-1601):

适用于31-Dec-999901-Jan-1601之间的日期。我已经实现了31-Dec-9999函数,该函数使用此answer on SO中的fromOADate函数,将SecondsSinceEpochToDateTime之前或之后的秒转换为01-Jan-1970结构,但仅适用于tm on。

我通过添加一个01-Jan-1601后缀,将该功能从该答案更改为32位工作。这要求ULL类型为64位宽,如果不是这种情况,则此解决方案将无效。

如果您需要在1601年之前的日期,您可以更改long long,因为它已经过充分记录 为了测试不同的值,这个online conversion非常好,它还支持unix时间戳和OADate类型。

完整的工作代码和example on ideone

SecondsSinceEpochToDateTime

<强> 2。第二个解决方案:使用gmtime_r(#include <iostream> #include <ctime> #include <cstring> struct tm* SecondsSinceEpochToDateTime(struct tm* pTm, uint64_t SecondsSinceEpoch) { uint64_t sec; unsigned int quadricentennials, centennials, quadrennials, annuals/*1-ennial?*/; unsigned int year, leap; unsigned int yday, hour, min; unsigned int month, mday, wday; static const unsigned int daysSinceJan1st[2][13]= { {0,31,59,90,120,151,181,212,243,273,304,334,365}, // 365 days, non-leap {0,31,60,91,121,152,182,213,244,274,305,335,366} // 366 days, leap }; /* 400 years: 1st hundred, starting immediately after a leap year that's a multiple of 400: n n n l \ n n n l } 24 times ... / n n n l / n n n n 2nd hundred: n n n l \ n n n l } 24 times ... / n n n l / n n n n 3rd hundred: n n n l \ n n n l } 24 times ... / n n n l / n n n n 4th hundred: n n n l \ n n n l } 24 times ... / n n n l / n n n L <- 97'th leap year every 400 years */ // Re-bias from 1970 to 1601: // 1970 - 1601 = 369 = 3*100 + 17*4 + 1 years (incl. 89 leap days) = // (3*100*(365+24/100) + 17*4*(365+1/4) + 1*365)*24*3600 seconds sec = SecondsSinceEpoch + 11644473600ULL; wday = (uint)((sec / 86400 + 1) % 7); // day of week // Remove multiples of 400 years (incl. 97 leap days) quadricentennials = (uint)(sec / 12622780800ULL); // 400*365.2425*24*3600 sec %= 12622780800ULL; // Remove multiples of 100 years (incl. 24 leap days), can't be more than 3 // (because multiples of 4*100=400 years (incl. leap days) have been removed) centennials = (uint)(sec / 3155673600ULL); // 100*(365+24/100)*24*3600 if (centennials > 3) { centennials = 3; } sec -= centennials * 3155673600ULL; // Remove multiples of 4 years (incl. 1 leap day), can't be more than 24 // (because multiples of 25*4=100 years (incl. leap days) have been removed) quadrennials = (uint)(sec / 126230400); // 4*(365+1/4)*24*3600 if (quadrennials > 24) { quadrennials = 24; } sec -= quadrennials * 126230400ULL; // Remove multiples of years (incl. 0 leap days), can't be more than 3 // (because multiples of 4 years (incl. leap days) have been removed) annuals = (uint)(sec / 31536000); // 365*24*3600 if (annuals > 3) { annuals = 3; } sec -= annuals * 31536000ULL; // Calculate the year and find out if it's leap year = 1601 + quadricentennials * 400 + centennials * 100 + quadrennials * 4 + annuals; leap = !(year % 4) && (year % 100 || !(year % 400)); // Calculate the day of the year and the time yday = sec / 86400; sec %= 86400; hour = sec / 3600; sec %= 3600; min = sec / 60; sec %= 60; // Calculate the month for (mday = month = 1; month < 13; month++) { if (yday < daysSinceJan1st[leap][month]) { mday += yday - daysSinceJan1st[leap][month - 1]; break; } } // Fill in C's "struct tm" memset(pTm, 0, sizeof(*pTm)); pTm->tm_sec = sec; // [0,59] pTm->tm_min = min; // [0,59] pTm->tm_hour = hour; // [0,23] pTm->tm_mday = mday; // [1,31] (day of month) pTm->tm_mon = month - 1; // [0,11] (month) pTm->tm_year = year - 1900; // 70+ (year since 1900) pTm->tm_wday = wday; // [0,6] (day since Sunday AKA day of week) pTm->tm_yday = yday; // [0,365] (day since January 1st AKA day of year) pTm->tm_isdst = -1; // daylight saving time flag return pTm; } struct tm* fromOADate(struct tm* p_Tm, double p_OADate) { static const int64_t OA_UnixTimestamp = -2209161600; /* 30-Dec-1899 */ if (!( -109205 <= p_OADate /* 01-Jan-1601 */ && p_OADate <= 2958465)) /* 31-Dec-9999 */ { throw std::string("OADate must be between 109205 and 2958465!"); } int64_t OADatePassedDays = p_OADate; double OADateDayTime = p_OADate - OADatePassedDays; int64_t OADateSeconds = OA_UnixTimestamp + OADatePassedDays * 24LL * 3600LL + OADateDayTime * 24.0 * 3600.0; return SecondsSinceEpochToDateTime(p_Tm, OADateSeconds); } int main() { struct tm timeVal; std::cout << asctime(fromOADate(&timeVal, -109205)); /* 01-Jan-1601 00:00:00 */ std::cout << asctime(fromOADate(&timeVal, 0)); /* 30-Dec-1899 00:00:00 */ std::cout << asctime(fromOADate(&timeVal, 25569)); /* 01-Jan-1970 00:00:00 */ std::cout << asctime(fromOADate(&timeVal, 50424.134803241)); /* 19-Jan-2038 03:14:07 */ std::cout << asctime(fromOADate(&timeVal, 2958465)); /* 31-Dec-9999 00:00:00 */ return 0; } 01-Jan-1970 / 19-Jan-2038(32/64位)):

正如已经说过的那样,这个解决方案的范围不是很广,只是使用标准函数(full working example at ideone):

31-Dec-9999