- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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 ?
接下来,我需要转换这个日期,它包含在
中。电话 结构,到
日期(双)类型。
最佳答案
这可以通过 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。
关于c++ - 如何将 "struct tm"(VTYPE_TM) 转换为 DATE(double) - Native API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65972571/
我有一个数组 items[] items[] 中的每一项都是一个结构体。 item 有键 id、date、value(即 item.id、item.date、item.value) 我想使用 Stru
我想存储 100 名员工。 RollNo,姓名,工资,时间(各种数据,我无法在这里解释,但你可以看下面的代码片段来理解 main() { struct day { int hour
这个问题在这里已经有了答案: storage size of ‘names’ isn’t known (3 个答案) 关闭 5 年前。 我正在尝试蓝牙编程,遇到了这个我不明白的问题。基本上,当我使用
这是一个奇怪的事情: 我有一个结构,它包含指向相同类型结构的指针和指向其他类型结构的指针,以及一些其他值。 struct animal { struct animal * father;
我有一个结构定义如下(名称不同) struct str1 { int field1; struct str2; } 我在一个函数中有一个*str1。我想要一个指向 str2 的指针。 所以
DISK_DETECTION_INFO is defined as有什么原因吗? typedef struct _DISK_DETECTION_INFO { DWORD Size
我正在尝试打包一个字符串和一个字符串的长度。 fmt = '
我在创建结构时遇到问题。 我的结构: public struct Device: Codable { let data: DeviceData let meta: Meta? } pu
struct Item { var name:String? var type:String? var value:Int? var tag:Int? } ... ..
// NewReaderSize returns a new Reader whose buffer has at least the specified 43 // size. If the ar
这个问题在这里已经有了答案: Sorting a vector of custom objects (14 个答案) 关闭 3 年前。 在下面的 C++ 片段中, 如何基于 TwoInts 结构中的
#include struct Header { unsigned long long int alignment; }; int main(void) { struct Heade
我有一个目前看起来像这样的结构(缩写为仅显示基本部分): typedef struct { uint32_t baudrate; ... some other internally u
对此没有太多解释,这就是我所拥有的: public struct PACKET_HEADER { public string computerIp; publi
我有以下代码: struct MyStruct{ data: &'a str, } fn get(S: &'a MyStruct) -> &'a str{ S.data } fn se
struct S1 { char c; int i; }; struct S3 { char c1; struct S1 s; double c2; }; 我正
我有一个名为 Parameter 的协议(protocol): protocol Parameter { var name: String { get } var unit: Unit
有 2 个 struct 定义 A 和 A。我知道 struct A 可以包含指向 struct A 的 POINTER 但我不明白为什么 struct A 不能包含struct A(不是指针) 最佳
我有以下代码: struct MyStruct{ data: &'a str, } fn get(S: &'a MyStruct) -> &'a str{ S.data } fn se
为了说明这一点,这里有一个小的不可变结构和一个更新它的函数: (struct timeseries (variable observations) #:transparent) (define (ad
我是一名优秀的程序员,十分优秀!