什么是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