问题描述
我正在研究一个 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’的未定义引用"错误.各种 说明 我 找到 如何解决这个问题只会破坏我正在使用的其余代码库,所以我会而是避免弄乱 _XOPEN_SOURCE 或类似的重新定义.
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.
感谢您的帮助!
推荐答案
您发布的代码是正确的.
The code you posted is correct.
这让我相信您的输入字符串 (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 << '
';
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 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!