• <small id='4J1WN'></small><noframes id='4J1WN'>

    1. <i id='4J1WN'><tr id='4J1WN'><dt id='4J1WN'><q id='4J1WN'><span id='4J1WN'><b id='4J1WN'><form id='4J1WN'><ins id='4J1WN'></ins><ul id='4J1WN'></ul><sub id='4J1WN'></sub></form><legend id='4J1WN'></legend><bdo id='4J1WN'><pre id='4J1WN'><center id='4J1WN'></center></pre></bdo></b><th id='4J1WN'></th></span></q></dt></tr></i><div id='4J1WN'><tfoot id='4J1WN'></tfoot><dl id='4J1WN'><fieldset id='4J1WN'></fieldset></dl></div>
      <legend id='4J1WN'><style id='4J1WN'><dir id='4J1WN'><q id='4J1WN'></q></dir></style></legend>
        • <bdo id='4J1WN'></bdo><ul id='4J1WN'></ul>
        <tfoot id='4J1WN'></tfoot>

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

        时间:2023-09-26
      1. <i id='9234K'><tr id='9234K'><dt id='9234K'><q id='9234K'><span id='9234K'><b id='9234K'><form id='9234K'><ins id='9234K'></ins><ul id='9234K'></ul><sub id='9234K'></sub></form><legend id='9234K'></legend><bdo id='9234K'><pre id='9234K'><center id='9234K'></center></pre></bdo></b><th id='9234K'></th></span></q></dt></tr></i><div id='9234K'><tfoot id='9234K'></tfoot><dl id='9234K'><fieldset id='9234K'></fieldset></dl></div>

                <tbody id='9234K'></tbody>
              • <bdo id='9234K'></bdo><ul id='9234K'></ul>
                <legend id='9234K'><style id='9234K'><dir id='9234K'><q id='9234K'></q></dir></style></legend><tfoot id='9234K'></tfoot>

                  <small id='9234K'></small><noframes id='9234K'>

                  本文介绍了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’的未定义引用"错误.各种 说明 我 找到 如何解决这个问题只会破坏我正在使用的其余代码库,所以我会而是避免弄乱 _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 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  • <bdo id='vcnOR'></bdo><ul id='vcnOR'></ul>
                      <tbody id='vcnOR'></tbody>

                      <small id='vcnOR'></small><noframes id='vcnOR'>

                      <tfoot id='vcnOR'></tfoot>
                      • <legend id='vcnOR'><style id='vcnOR'><dir id='vcnOR'><q id='vcnOR'></q></dir></style></legend>
                      • <i id='vcnOR'><tr id='vcnOR'><dt id='vcnOR'><q id='vcnOR'><span id='vcnOR'><b id='vcnOR'><form id='vcnOR'><ins id='vcnOR'></ins><ul id='vcnOR'></ul><sub id='vcnOR'></sub></form><legend id='vcnOR'></legend><bdo id='vcnOR'><pre id='vcnOR'><center id='vcnOR'></center></pre></bdo></b><th id='vcnOR'></th></span></q></dt></tr></i><div id='vcnOR'><tfoot id='vcnOR'></tfoot><dl id='vcnOR'><fieldset id='vcnOR'></fieldset></dl></div>