c++ - mongocxx : Inserting a Datetime

标签 c++ mongodb c++-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_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;
}

运行我的代码,我在标准输出上得到这个:

2007 107/11/31 0:0

当我在数据库中检查我的日期时,我得到:

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         

来源:timeanddate.com

关于c++ - mongocxx : Inserting a Datetime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54527257/

相关文章:

c++ - MPI : Communication error with rank 1: Connection refused

node.js - Mongoose 竞争条件导致错误

C++ 比较两个具有相同持续时间的 std::chrono::time_points 失败

c++ - 在 32 位和 64 位程序中使用 std::chrono::duration::rep 和 printf

c++ - 有没有办法绕过不同编译器上的 printf 错误?

c++/win32 - LB_GETTEXT 仅返回一个字符

c# - 从 std::fstream 读取 float 返回负零

c++ - 从 cpp 中的函数返回的 char*

mongodb - 错误 : "could not find implicit value for parameter readFileReader" trying to save a file using GridFS with reactivemongo

javascript - 在 NodeJS 中处理多个、顺序、依赖的 mongo 查询结果的最佳方法