使用C++获取Unix时间戳

问题描述 投票:0回答:9

如何在 C++ 中获取

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

c++ unix timestamp uint
9个回答
149
投票

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()
是最简单的函数 - 自纪元以来的秒数,对于 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";
}

54
投票
#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;
}

19
投票

最常见的建议是错误的,你不能仅仅依赖

time()
。用于相对计时:ISO C++ 未指定
1970-01-01T00:00Z
time_t(0)

更糟糕的是你也无法轻易弄清楚。当然,您可以使用

time_t(0)
找到
gmtime
的日历日期,但如果那是
2000-01-01T00:00Z
您要做什么?
1970-01-01T00:00Z
2000-01-01T00:00Z
之间有多少秒?由于闰秒的原因,它肯定不是 60 的倍数。


14
投票

由于这是 google 上的第一个结果,并且还没有 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,请自行承担使用风险。


12
投票

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

#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

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


10
投票
#include <iostream>
#include <sys/time.h>

using namespace std;

int main ()
{
  unsigned long int sec= time(NULL);
  cout<<sec<<endl;
}

5
投票

Windows 使用不同的纪元和时间单位:请参阅 在 Unix/Linux 中将 Windows 文件时间转换为秒

我(目前)还不知道 std::time() 在 Windows 上返回什么 (;-))


3
投票

这对我有用,只需将纳秒转换为您喜欢的任何内容,然后将 uint64_t 转换为您喜欢的任何内容。

#include <chrono>
uint64_t unix_time()
{
    return std::chrono::duration_cast<std::chrono::nanoseconds>
    (
        std::chrono::system_clock::now().time_since_epoch()
    ).count();
}

0
投票
#include <time.h>    // /usr/include/time.h

time_t myTimestampSecs = time(0);   //__SYSCALL_SLONG_TYPE == [signed] long int
// long int myTimestampSecs;
// signed is necessary, to represent times before 1970, which are negative

https://man7.org/linux/man-pages/man2/time.2.html

© www.soinside.com 2019 - 2024. All rights reserved.