简体   繁体   English

C ++将字符串转换为time_t变量

[英]C++ Converting A String to a time_t variable

I'm working on a C++ function that is supposed to figure out if a specified event happened between two time points. 我正在研究一个C ++函数,该函数应确定是否在两个时间点之间发生了指定事件。 The event name, start datetime, and end datetime are all passed in from Lua as strings. 事件名称,开始日期时间和结束日期时间都是从Lua作为字符串传递的。 Because of that, I need to parse my datetime strings into time_t variables. 因此,我需要将日期时间字符串解析为time_t变量。 Based on what I've seen on StackOverflow and other forums, this code should work: 根据我在StackOverflow和其他论坛上看到的内容,此代码应该可以工作:

time_t tStart;
int yy, month, dd, hh, mm, ss;
struct tm whenStart = {0};
const char *zStart = startTime.c_str();

sscanf(zStart, "%d/%d/%d %d:%d:%d", &yy, &month, &dd, &hh, &mm, &ss);
whenStart.tm_year = yy - 1900;
whenStart.tm_mon = month - 1;
whenStart.tm_mday = dd;
whenStart.tm_hour = hh;
whenStart.tm_min = mm;
whenStart.tm_sec = ss;
whenStart.tm_isdst = -1;

tStart = mktime(&whenStart);

However, tStart appears to be assigned the value of -1 here. 但是,此处tStart似乎被分配了值-1。 If I use strftime to reconstruct a string from whenStart, that tm structure appears to have been made completely correctly. 如果我使用strftime从whenStart重建一个字符串,那么该tm结构似乎已经完全正确地制成了。 Somehow mktime() is not liking the structure, I think. 我认为mktime()有点不喜欢这种结构。 What is wrong with this code? 此代码有什么问题?

Also, before you answer, know that I already tried using a strptime() call. 另外,在回答之前,请知道我已经尝试过使用strptime()调用。 For reasons that are unclear to me, this function gets rejected with an "undefined reference to 'strptime'" error. 由于我不清楚的原因,该函数被拒绝,并出现“未定义对'strptime'的引用”错误。 The various descriptions I've found for how to fix this problem only serve to destroy the rest of the code base I'm working with, so I would rather avoid messing with _XOPEN_SOURCE or similar redefinitions. 找到的有关解决此问题的各种 描述仅能破坏我正在使用的其余代码库,因此,我宁愿避免弄乱_XOPEN_SOURCE或类似的重新定义。

Thanks for your help! 谢谢你的帮助!

The code you posted is correct. 您发布的代码是正确的。

This leads me to believe that your input string ( startTime ) is not in the format you are expecting, and therefore sscanf cannot parse out the values. 这使我相信您的输入字符串( startTime )不是您所期望的格式,因此sscanf无法解析出这些值。

Example: 例:

#include <iostream>

int main()
{
    std::string startTime = "2016/05/18 13:10:00";

    time_t tStart;
    int yy, month, dd, hh, mm, ss;
    struct tm whenStart;
    const char *zStart = startTime.c_str();

    sscanf(zStart, "%d/%d/%d %d:%d:%d", &yy, &month, &dd, &hh, &mm, &ss);
    whenStart.tm_year = yy - 1900;
    whenStart.tm_mon = month - 1;
    whenStart.tm_mday = dd;
    whenStart.tm_hour = hh;
    whenStart.tm_min = mm;
    whenStart.tm_sec = ss;
    whenStart.tm_isdst = -1;

    tStart = mktime(&whenStart);

    std::cout << tStart << std::endl;
}

Output: 输出:

1463595000

Have you sanity checked your inputs? 您是否理智地检查了您的输入?

Please note that you can check the return value of sscanf to verify if it worked as you expected. 请注意,您可以检查sscanf的返回值以验证其是否按预期工作。

Return value 返回值
Number of receiving arguments successfully assigned, or EOF if read failure occurs before the first receiving argument was assigned. 成功分配的接收参数的数量;如果在分配第一个接收参数之前发生读取失败,则为EOF。

If the return value is not 6, then the input string is incorrect. 如果返回值不是6,则输入的字符串不正确。

int num_args = sscanf(zStart, "%d/%d/%d %d:%d:%d", &yy, &month, &dd, &hh, &mm, &ss);
if (num_args != 6)
{
    std::cout << "error in format string " << startTime << '\n';
    return 1;
}

As a rule of thumb you shouldn't ever assume that your inputs will be correct. 根据经验,您永远不应假设您的输入是正确的。 As such, defensive programming is a good habit to get into. 因此, 防御性编程是一种很好的习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM