【问题标题】:C++: how to get the actual time with time and localtime?C ++:如何使用时间和本地时间获取实际时间?
【发布时间】:2011-12-23 11:36:40
【问题描述】:

我正在寻找一种在 C++ 中以 HH::MM::SS 方式节省时间的方法。我在这里看到它们有很多解决方案,经过一番研究后,我选择了timelocaltime。但是,localtime 函数似乎有点棘手,因为它是says

所有对 localtime 和 gmtime 的调用都使用相同的静态结构,所以 每次调用都会覆盖上一次调用的结果。

这导致的问题显示在下一个sn-p代码中:

#include <ctime>
#include <iostream>
using namespace std;

int main() {
time_t t1 = time(0);   // get time now
struct tm * now = localtime( & t1 );

std::cout << t1 << std::endl;
sleep(2);
time_t t2 = time(0);   // get time now
struct tm * now2 = localtime( & t2 );
std::cout << t2 << std::endl;

cout << (now->tm_year + 1900) << '-'
     << (now->tm_mon + 1) << '-'
     <<  now->tm_mday << ", "
     << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec
     << endl;

cout << (now2->tm_year + 1900) << '-'
     << (now2->tm_mon + 1) << '-'
     <<  now2->tm_mday << ", "
     << now2->tm_hour << ":" << now2->tm_min << ":" << now2->tm_sec
     << endl;
}

一个典型的输出是:

1320655946
1320655948
2011-11-7, 9:52:28
2011-11-7, 9:52:28

如您所见,time_t 时间戳是正确的,但本地时间会搞砸一切。

我的问题是:如何将time_t 类型的时间戳转换为人类可读的时间?

【问题讨论】:

    标签: c++ time localtime


    【解决方案1】:

    如果你担心localtimegmtime的重入,有localtime_rgmtime_r可以处理多个调用。

    要根据自己的喜好格式化时间,请检查函数strftime

    【讨论】:

      【解决方案2】:

      localtime() 调用将结果存储在内部缓冲区中。

      每次调用它都会覆盖缓冲区。
      另一种解决方案是制作缓冲区的副本。

      time_t      t1  = time(0);           // get time now
      struct tm* now  = localtime( & t1 ); // convert to local time
      struct tm  copy = *now;              // make a local copy.
       //     ^^^ notice no star.
      

      但请注意:您应该转换为本地时间的唯一时间是显示值时。在所有其他时间,您应该将时间保持为 UTC(用于存储和操作)。由于您只是将对象转换为显示转换然后立即打印,然后事情就不会出错。

      【讨论】:

      • +1 澄清我只需要在最后进行转换。谢谢!
      【解决方案3】:

      localtime 拥有最好的遗留接口。不可能 例如,在多线程代码中使用。在多线程中 环境,可以在Posix下使用localtime_r或者localtime_s 在 Windows 下。否则,您所要做的就是保存结果:

      tm then = *localtime( &t1 );
      //  ...
      tm now = *localtime( &t2 );
      

      不过,只调用localtime可能会更习惯用法
      在格式化输出之前,例如:

      std::string
      timestampToString( time_t timeAndDate )
      {
          char results[100];
          if ( strftime( results, sizeof( results ), "%Y-%m-%d, %H:%M:%S", 
                      localtime( &timeAndDate) ) == 0 ) {
              assert( 0 );
          }
          return results;
      }
      

      然后写:

      std::cout << formatTime( t1 ) << std::endl;
      

      (你也可以创建一个更通用的格式化函数,它需要 格式作为参数。)

      【讨论】:

        【解决方案4】:

        您可以使用以下代码运行连续时钟。效果很好。

        #include<iostream> 
        #include <Windows.h> 
        #include<ctime> 
        using namespace std;
        
        void main() {
          while(true) {
            system("cls"); //to clear screen
            time_t tim;
            time(&tim); 
            cout << ctime(&tim); 
            Sleep(1);
          }
        }
        

        【讨论】:

        • 我认为这无法回答这个问题。 (如何以人类可读的格式显示 time_t)
        猜你喜欢
        • 2021-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多