C++ 獲取Unix時間戳


什么是Unix時間戳?

Unix時間戳(Unix timestamp),或稱Unix時間(Unix time)、POSIX時間(POSIX time),是一種時間表示方式,定義為從格林威治時間1970年01月01日00時00分00秒起至現在的總秒數。Unix時間戳不僅被使用在Unix系統、類Unix系統中,也在許多其他操作系統中被廣泛采用。

2038年1月19日會發生什么?

在2038年1月19日,由於32位整形溢出,Unix時間戳會停止工作。在這個大災難前,數百萬計的應用程序采取新的約定時間的方式,要么升級到64位版本。

代碼示例

示例一

//Code::Blocks編譯通過
#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;
}

運行結果:

1554986565 seconds since 01-Jan-1970

示例二

//Code::Blocks 編譯通過;
#include <ctime>
#include <iostream>

int main()
{
    std::time_t result = std::time(NULL);
    std::cout << std::asctime(std::localtime(&result))
              << result << " seconds since the Epoch\n";
}

運行結果:

Sun Nov 22 11:48:58 2015 
1448164138 seconds since the Epoch

更多參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM