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

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

    <tfoot id='KXgHX'></tfoot>
    1. C++ 将时间字符串从纪元转换为秒

      时间:2023-06-30

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

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

              <legend id='OBYdc'><style id='OBYdc'><dir id='OBYdc'><q id='OBYdc'></q></dir></style></legend>
                <tbody id='OBYdc'></tbody>

              • 本文介绍了C++ 将时间字符串从纪元转换为秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个格式如下的字符串:

                I have a string with the following format:

                2010-11-04T23:23:01Z

                2010-11-04T23:23:01Z

                Z 表示时间是 UTC.
                我宁愿将其存储为纪元时间,以便于比较.

                The Z indicates that the time is UTC.
                I would rather store this as a epoch time to make comparison easy.

                推荐的方法是什么?

                目前(经过快速搜索)最简单的算法是:

                Currently (after a quck search) the simplist algorithm is:

                1: <Convert string to struct_tm: by manually parsing string>
                2: Use mktime() to convert struct_tm to epoch time.
                
                // Problem here is that mktime uses local time not UTC time.
                

                推荐答案

                使用 C++11 功能,我们现在可以使用流来解析时间:

                Using C++11 functionality we can now use streams to parse times:

                iomanip std::get_time将根据一组格式参数转换一个字符串,并将它们转换为 struct tz 对象.

                The iomanip std::get_time will convert a string based on a set of format parameters and convert them into a struct tz object.

                然后您可以使用 std::mktime() 将其转换为纪元值.

                You can then use std::mktime() to convert this into an epoch value.

                #include <iostream>
                #include <sstream>
                #include <locale>
                #include <iomanip>
                
                int main()
                {
                    std::tm t = {};
                    std::istringstream ss("2010-11-04T23:23:01Z");
                
                    if (ss >> std::get_time(&t, "%Y-%m-%dT%H:%M:%S"))
                    {
                        std::cout << std::put_time(&t, "%c") << "
                "
                                  << std::mktime(&t) << "
                ";
                    }
                    else
                    {
                        std::cout << "Parse failed
                ";
                    }
                    return 0;
                }
                

                这篇关于C++ 将时间字符串从纪元转换为秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在C++中实现函数的超时 下一篇:C++中的时差

                相关文章

                • <bdo id='fjpHI'></bdo><ul id='fjpHI'></ul>

                  <legend id='fjpHI'><style id='fjpHI'><dir id='fjpHI'><q id='fjpHI'></q></dir></style></legend>
                    <tfoot id='fjpHI'></tfoot>

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

                  1. <small id='fjpHI'></small><noframes id='fjpHI'>