简体   繁体   English

C++ 从打包的 DATE 转换为 CTime

[英]C++ Convert from packed DATE to CTime

I have looked but cannot find the API to convert from DATE to CTime and vice versa.我看过但找不到 API 从 DATE 转换为 CTime,反之亦然。 Is there such a thing?有这样的事吗?

There's probably a shorter method, but going by the docs you can store a DATE in a COleDateTime, get that as a SYSTEMTIME and then use that to construct your CTime.可能有一个更短的方法,但是通过文档,您可以将 DATE 存储在 COleDateTime 中,将其作为 SYSTEMTIME 获取,然后使用它来构造您的 CTime。 Assuming it's representable as a CTime.假设它可以表示为 CTime。

SYSTEMTIME st;
DATE d(whatever);
COleDateTime ole(d);
d.GetAsSystemTime(st);
CTime ct(st);

CTime include a number of constructors and converters for various time formats; CTime 包含许多用于各种时间格式的构造函数和转换器; it seems odd that DATE is not included.不包括DATE似乎很奇怪。 However _time64_t is supported, and differs from DATE only in the starting date and scale.但是支持_time64_t ,它与DATE的区别仅在于开始日期和比例。 You can easily convert from one to another if you have the starting date of January 1, 1970 as a DATE value:如果您将 1970 年 1 月 1 日的开始日期作为DATE值,则可以轻松地从一个转换为另一个:

DATE       jan1_1970 = ????;
__time64_t t;
DATE       d;
double     seconds_per_day = 24.*60.*60.;
t = (d - jan1_1970) * seconds_per_day;
d = (t / seconds_per_day) + jan1_1970;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM