gpt4 book ai didi

c++ - 如何将 "struct tm"(VTYPE_TM) 转换为 DATE(double) - Native API?

转载 作者:行者123 更新时间:2023-12-04 08:10:48 25 4
gpt4 key购买 nike

我正在使用 native API 技术在 native C++(Visual Studio 2013 版本)中编写一个小 DLL。
从我的 DLL 中的外部程序中,我将日期(无时间:仅日、月、年)作为 类型的变量获取。 VTYPE_TM (这是 "struct tm" )。

if (paParams[0].vt == VTYPE_TM) 
double dateFrom = (double)paParams[0].tmVal ; // convert error - but how ?
接下来,我需要转换这个日期,它包含在 中。电话 结构,到 日期(双)类型。
更多信息 日期(双)可以在这里找到 - 在在线计算器中 https://planetcalc.com/7027/ .
现在实际问题和我的问题:如何将“struct tm”转换为 DATE(double) ?
据我了解,我们需要编写一个函数,将每个字段值从 tm 结构转换为所需的 double 值。但是没有现成的解决方案吗?也许我需要使用一些中间类型,但我不知道是哪一种。
你能告诉我如何实现这个吗?
给铁杆 C++ 编程大师的一个问题!

最佳答案

这可以通过 C++20 轻松完成 <chrono> .不幸的是,C++20 的这一部分还没有发布,但供应商正忙于实现它。
幸运的是,存在一个 free, open-source, header-only preview of this part of C++20它适用于 C++11/14/17。此外,预览还有 examples page with this very problem already worked举个例子。
这是转换为仅使用类型 double 来回转换的示例,包括来自 documentation page you link to 的所有测试用例.

#include "date/date.h"
#include <ctime>
#include <iostream>

template <class D>
date::sys_time<D>
to_sys_time(double dt)
{
using namespace date;
using namespace std::chrono;
using fdays = duration<double, days::period>;
using ftime = time_point<system_clock, fdays>;
auto ft = ftime{fdays{dt}};
if (ft < ftime{})
{
auto d = time_point_cast<days>(ft);
auto t = d - ft;
ft = d + t;
}
ft -= sys_days{1970_y/January/1} - sys_days{1899_y/December/30};
return round<D>(ft);
}

template <class D>
double
to_double(date::sys_time<D> tp)
{
using namespace date;
using namespace std::chrono;
using fdays = duration<double, days::period>;
using ftime = time_point<system_clock, fdays>;
auto ft = ftime{tp} + (sys_days{1970_y/January/1} - sys_days{1899_y/December/30});
if (ft >= ftime{})
return ft.time_since_epoch().count();
auto d = floor<days>(ft);
auto t = d - ft;
return (d + t).time_since_epoch().count();
}

void
test(double d)
{
using namespace std;
using namespace std::chrono;
using namespace date;

auto t = to_sys_time<seconds>(d);
auto d2 = to_double(t);
cout << d << " is " << t << " which is " << d2 << '\n';
}

int
main()
{
using namespace date;
using namespace std;
using namespace std::chrono;
test(-1.25);
test(-1.3);
test(-0.5);
test(0.5);

year_month_day today = floor<days>(system_clock::now());

std::tm tm{};
tm.tm_year = int{today.year()} - 1900;
tm.tm_mon = unsigned{today.month()} - 1;
tm.tm_mday = unsigned{today.day()};

sys_days sd = year{tm.tm_year + 1900}/(tm.tm_mon + 1)/tm.tm_mday;
cout << "Today " << today << " is " << to_double(sd) << '\n';
}
这当前输出:
-1.25 is 1899-12-29 06:00:00 which is -1.25
-1.3 is 1899-12-29 07:12:00 which is -1.3
-0.5 is 1899-12-30 12:00:00 which is 0.5
0.5 is 1899-12-30 12:00:00 which is 0.5
Today 2021-01-30 is 44226
前四行只是确认转换函数根据您的文档产生正确的值。
最后从 system_clock::now() 获得当前日期 (UTC) ,转换为 std::tm ,然后转换回天精度 chrono::time_point (基于 system_clock )。此 time_point然后被送入 to_double将其转换为 double 值,当前结果为 44226。
考虑到 your documentation 最后一段中描述的这种格式的奇异历史,这些转换函数不必要地复杂化。 .

关于c++ - 如何将 "struct tm"(VTYPE_TM) 转换为 DATE(double) - Native API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65972571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com