83

如何uint在 C++ 中获取 unix 时间戳?我用谷歌搜索了一下,似乎大多数方法都在寻找更复杂的方式来表示时间。我不能把它当作一个uint吗?

4

7 回答 7

111

C++20 引入了一个time_since_epoch相对于 UNIX 时代的保证, cppreference.com给出了一个我已经提炼到相关代码的示例,并更改为以秒为单位而不是小时:

#include <iostream>
#include <chrono>
 
int main()
{
    const auto p1 = std::chrono::system_clock::now();
 
    std::cout << "seconds since epoch: "
              << std::chrono::duration_cast<std::chrono::seconds>(
                   p1.time_since_epoch()).count() << '\n';
}

使用 C++17 或更早版本time()是最简单的功能 - 自 Epoch 以来的秒数,对于 Linux 和 UNIX,至少是 UNIX 纪元。Linux手册页在这里

上面链接的 cppreference 页面给出了这个例子

#include <ctime>
#include <iostream>
 
int main()
{
    std::time_t result = std::time(nullptr);
    std::cout << std::asctime(std::localtime(&result))
              << result << " seconds since the Epoch\n";
}
于 2011-05-16T02:26:01.880 回答
48
#include<iostream>
#include<ctime>

int main()
{
    std::time_t t = std::time(0);  // t is an integer type
    std::cout << t << " seconds since 01-Jan-1970\n";
    return 0;
}
于 2011-05-16T02:28:56.473 回答
18

The most common advice is wrong, you can't just rely on time(). That's used for relative timing: ISO C++ doesn't specify that 1970-01-01T00:00Z is time_t(0)

What's worse is that you can't easily figure it out, either. Sure, you can find the calendar date of time_t(0) with gmtime, but what are you going to do if that's 2000-01-01T00:00Z ? How many seconds were there between 1970-01-01T00:00Z and 2000-01-01T00:00Z? It's certainly no multiple of 60, due to leap seconds.

于 2011-05-17T08:15:48.850 回答
11

由于这是谷歌上的第一个结果并且还没有 C++20 的答案,这里是如何使用 std::chrono 来做到这一点:

#include <chrono>

//...

using namespace std::chrono;
int64_t timestamp = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();

在 20 之前的 C++ 版本中,system_clock 的纪元是 Unix 纪元是事实上的约定,但它不是标准化的。如果您不在 C++20 上,请自行承担使用风险。

于 2019-07-29T14:54:34.640 回答
7
#include <iostream>
#include <sys/time.h>

using namespace std;

int main ()
{
  unsigned long int sec= time(NULL);
  cout<<sec<<endl;
}
于 2011-05-16T02:29:53.327 回答
6

我创建了一个包含更多信息的全局定义:

#include <iostream>
#include <ctime>
#include <iomanip>

#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)    // only show filename and not it's path (less clutter)
#define INFO std::cout << std::put_time(std::localtime(&time_now), "%y-%m-%d %OH:%OM:%OS") << " [INFO] " << __FILENAME__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") >> "
#define ERROR std::cout << std::put_time(std::localtime(&time_now), "%y-%m-%d %OH:%OM:%OS") << " [ERROR] " << __FILENAME__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") >> "

static std::time_t time_now = std::time(nullptr);

像这样使用它:

INFO << "Hello world" << std::endl;
ERROR << "Goodbye world" << std::endl;

样本输出:

16-06-23 21:33:19 [INFO] main.cpp(main:6) >> Hello world
16-06-23 21:33:19 [ERROR] main.cpp(main:7) >> Goodbye world

将这些行放在头文件中。我发现这对于调试等非常有用。

于 2016-06-24T02:37:51.390 回答
5

Windows 使用不同的纪元和时间单位:请参阅 Convert Windows Filetime to second in Unix/Linux

什么 std::time() 在 Windows 上返回(到目前为止)对我来说是未知的(;-))

于 2013-11-06T17:31:43.907 回答