在C ++中将日期/时间(以Double形式)转换为struct * tm [英] Convert Date/Time (as Double) to struct* tm in C++
问题描述
我从C#(DateTime.ToOADate(),这是一个OLE日期时间)中获取一个日期/时间作为双精度值。它包含从1899/12/31开始的已过去的日子,其中小数部分是当天的过去部分。乘以86400,我得到了秒数,最后得到了当天的时间。
I get a date/time as double value from C# (DateTime.ToOADate(), which is a OLE Date Time). It contains the passed days from 1899/12/31, with the fraction being the passed part of the day. Multiplied with 86400 I get the seconds and from that finally the day's time.
但是,获取日期比较困难。除了UNIX时间涵盖的日期(1970/01/01至2038/01/19)之外,我仍然没有解决方案。在这段时间内,可以使用mktime()将过去的日期转换为日期时间。
Getting the date however is harder. I still don't have an solution except for the dates that the UNIX time covers (1970/01/01 to 2038/01/19). During this time, mktime() can be used to convert the passed days to a datetime.
double OleDateTimeValue = 28170.654351851852; // 14.02.1977 15:42:16
struct tm * timeinfo;
int passedDays = (int)OLEDateTimeValue;
// between 1.1.1970 and 18.1.2038
if((passedDays >= 25569) && (passedDays <= 50423))
{
timeinfo->tm_year = 70; //1970
timeinfo->tm_mon = 0;
timeinfo->tm_mday = 1 + (passedDays - 25569);
mktime(timeinfo);
}
else // date outside the UNIX date/time
{
}
现在,mktime()格式化tm结构,使其表示请求的日期,如果值在给定日期之外,则返回-1。
Now, mktime() formats the tm struct so that it represents the requested date, or returns -1 if the value is outside the given dates.
有通用的计算方法吗?不幸的是,我不能使用MFC,而必须使用Visual C ++ 6.0。
Is there a generic way to do the calculation? Unfortunately I can't use MFC and have to use Visual C++ 6.0.
谢谢,
Markus
Thanks, Markus
推荐答案
我相信您可以使用 VariantTimeToSystemTime 函数转换为SYSTEMTIME。
I believe you can use the VariantTimeToSystemTime function to convert into a SYSTEMTIME.
这篇关于在C ++中将日期/时间(以Double形式)转换为struct * tm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!