C++ mongocxx:插入日期时间

C++ mongocxx:插入日期时间,c++,mongodb,chrono,mongo-cxx-driver,C++,Mongodb,Chrono,Mongo Cxx Driver,在解析数据文件后,我试图在mongocxx中插入日期和时间,我的实际日期时间是: 2007/12/01 00:00:00 即2007年12月1日午夜。我有以下代码: static bsoncxx::document::value make_doc(sm_struct const sm) { std::tm t{0}; t.tm_sec = 0; t.tm_min=(int)sm.minute; t.tm_hour=(int)sm.hour; t.tm_m

在解析数据文件后,我试图在mongocxx中插入日期和时间,我的实际日期时间是:

2007/12/01 00:00:00
即2007年12月1日午夜。我有以下代码:

static bsoncxx::document::value make_doc(sm_struct const sm) {
    std::tm t{0};
    t.tm_sec = 0;
    t.tm_min=(int)sm.minute;
    t.tm_hour=(int)sm.hour;
    t.tm_mday=(int)sm.day-1;
    t.tm_mon=(int)sm.month;
    t.tm_year=sm.year-1900;
    t.tm_isdst = -1;
    std::time_t tt = mktime(&t);

    std::cout << sm.year << " " << t.tm_year << "/" << t.tm_mon << "/" << t.tm_mday << " " << t.tm_hour << ":" << t.tm_min << std::endl;
    bsoncxx::document::value document = bsoncxx::builder::basic::make_document(
        bsoncxx::builder::basic::kvp("datetime", bsoncxx::types::b_date{
            std::chrono::system_clock::from_time_t(tt)
        }),
    );
    return document;
}
当我在数据库中检查我的日期时,我得到:

ISODate("2007-12-30T13:30:00.000+0000")

为什么这里的小时和分钟设置不正确?

如果您将2007年12月31日插入阿德莱德当地时间午夜,即2007年12月30日13:30(UTC),看起来是正确的。您可以在显示UTC时间时将其转换回本地时间

Adelaide, Australia    Mon, 31 Dec 2007 at 00:00 ACDT    
UTC, Time Zone         Sun, 30 Dec 2007 at 13:30         

资料来源:

您插入阿德莱德当地时间2007年12月31日午夜,即2007年12月30日13:30(UTC),看起来是正确的。您可以在显示UTC时间时将其转换回本地时间

Adelaide, Australia    Mon, 31 Dec 2007 at 00:00 ACDT    
UTC, Time Zone         Sun, 30 Dec 2007 at 13:30         

来源:

谢谢,我用了
gmtime
而不是
mktime
,效果很好。很好,请将我的答案标记为正确。干杯谢谢,我用了
gmtime
而不是
mktime
,效果很好。很好,请将我的答案标记为正确。干杯