如何使用 strptime(fromdate.c_str(), "%Y-%m-%d %H %M %S", &tm);在窗户上 [英] How to use strptime(fromdate.c_str(), "%Y-%m-%d %H %M %S", &tm); on windows

查看:30
本文介绍了如何使用 strptime(fromdate.c_str(), "%Y-%m-%d %H %M %S", &tm);在窗户上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 Windows 上运行良好的代码,我必须让它与 Linux 兼容.谁能帮我将以下代码片段配置到 Windows 中?

I have a code that works fine on windows, and I have to make it compatible with Linux. Can anyone help me configure the following code snippet to windows?

    MongoDB* db = MongoDB::getInstance();
    mongocxx::pipeline p{};
    struct tm tm;
    strptime(fromdate.c_str(), "%Y-%m-%d %H %M %S", &tm); //this line gives me some errors 
    std::time_t tt = std::mktime(&tm);

我得到的错误`错误 C3861:'strptime':未找到标识符

The error that I'm getting `error C3861: 'strptime': identifier not found

我尝试使用 http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/time/strptime.c?rev=HEAD但它也给出了一些错误,因为它说缺少一些头文件,例如 #include <sys/cdefs.h>#include "namespace.h"

I tried to use the code in http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/time/strptime.c?rev=HEAD but it also gives some error because it says some header files such as #include <sys/cdefs.h> , #include "namespace.h" are missing

谁能告诉我如何在项目中包含这些头文件或任何其他解决此问题的方法提前致谢

can anyone tell me how to include those header files in the project or any other way of fixing this issue Thank you in advance

推荐答案

如果你的 strptime 的 BSD 版本不能正常工作,你可以试试 Howard Hinnant 的 date.h

If you can't get the BSD version of strptime working, you could try Howard Hinnant's date.h

或者您可以自己为这种特定格式创建一个简单的转换函数.示例:

Or you could create a simple conversion function for this particular format yourself. Example:

#include <cstdio>
#include <ctime>
#include <cerrno>
#include <iostream>
#include <stdexcept>
#include <string>
#include <string_view>

std::tm to_tm(const std::string_view& date) {
    std::tm t{};

    if(std::sscanf(date.data(), "%d-%d-%d %d %d %d",
        &t.tm_year,
        &t.tm_mon,
        &t.tm_mday,
        &t.tm_hour,
        &t.tm_min,
        &t.tm_sec
    ) != 6)
        throw std::runtime_error("Invalid date format: " + std::string(date));

    t.tm_year -= 1900;
    --t.tm_mon;
    t.tm_isdst = -1;  // guess if DST should be in effect when calling mktime
    errno = 0;
    std::mktime(&t);
    
    return t;
}

int main() {
    std::string fromdate = "2021-03-25 08 23 56";

    std::tm x = to_tm(fromdate);

    std::cout << std::asctime(&x) << '\n'; // Thu Mar 25 08:23:56 2021
}


或者,一个非throw直接返回std::time_t的版本:

#include <system_error> // added to be able to set an error code

std::time_t to_time_t(const std::string_view& date) {
    std::tm t{};

    if(std::sscanf(date.data(), "%d-%d-%d %d %d %d",
        &t.tm_year,
        &t.tm_mon,
        &t.tm_mday,
        &t.tm_hour,
        &t.tm_min,
        &t.tm_sec
    ) != 6) {
        // EOVERFLOW (std::errc::value_too_large)
        // This is what Posix versions of mktime() uses to signal that -1 does
        // not mean one second before epoch, but that there was an error.
        errno = static_cast<int>(std::errc::value_too_large);
        return -1;
    }
    t.tm_year -= 1900;
    --t.tm_mon;
    t.tm_isdst = -1;  // guess if DST should be in effect when calling mktime
    
    errno = 0;
    return std::mktime(&t);
}

这篇关于如何使用 strptime(fromdate.c_str(), "%Y-%m-%d %H %M %S", &amp;tm);在窗户上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆