C ++将字符串转换为time_t变量 [英] C++ Converting A String to a time_t variable

查看:78
本文介绍了C ++将字符串转换为time_t变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个C ++函数,该函数应确定是否在两个时间点之间发生了指定的事件.事件名称,开始日期时间和结束日期时间都从Lua作为字符串传递.因此,我需要将日期时间字符串解析为time_t变量.根据我在StackOverflow和其他论坛上看到的内容,此代码应该可以工作:

I'm working on a C++ function that is supposed to figure out if a specified event happened between two time points. The event name, start datetime, and end datetime are all passed in from Lua as strings. Because of that, I need to parse my datetime strings into time_t variables. Based on what I've seen on StackOverflow and other forums, this code should work:

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);

但是,此处tStart似乎被分配了值-1.如果我使用strftime从whenStart重建一个字符串,则该tm结构似乎已经完全正确地制成了.我认为mktime()某种程度上不喜欢这种结构.此代码有什么问题?

However, tStart appears to be assigned the value of -1 here. If I use strftime to reconstruct a string from whenStart, that tm structure appears to have been made completely correctly. Somehow mktime() is not liking the structure, I think. What is wrong with this code?

另外,在回答之前,请知道我已经尝试使用strptime()调用.由于我不清楚的原因,该函数被拒绝,并出现未定义对'strptime'的引用"错误. 各种

Also, before you answer, know that I already tried using a strptime() call. For reasons that are unclear to me, this function gets rejected with an "undefined reference to 'strptime'" error. 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.

感谢您的帮助!

推荐答案

您发布的代码正确.

这使我相信您的输入字符串(startTime)的格式不符合您的期望,因此sscanf无法解析出这些值.

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.

示例:

#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;
}

输出:

1463595000

您是否理智地检查了您的输入?

Have you sanity checked your inputs?

请注意,您可以检查 sscanf 验证其是否按预期工作.

Please note that you can check the return value of sscanf to verify if it worked as you expected.

返回值
成功分配了接收参数的数目;如果在分配第一个接收参数之前发生读取失败,则为EOF.

Return value
Number of receiving arguments successfully assigned, or EOF if read failure occurs before the first receiving argument was assigned.

如果返回值不是6,则输入的字符串不正确.

If the return value is not 6, then the input string is incorrect.

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.

这篇关于C ++将字符串转换为time_t变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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