C++ 獲取Unix時間戳

什麼是Unix時間戳?

Unix時間戳(Unix timestamp),或稱Unix時間(Unix time)、POSIX時間(POSIX time),是一種時間表示方式,定義爲從格林威治時間1970年01月01日00時00分00秒起至如今的總秒數。Unix時間戳不只被使用在Unix系統、類Unix系統中,也在許多其餘操做系統中被普遍採用。ios

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

在2038年1月19日,因爲32位整形溢出,Unix時間戳會中止工做。在這個大災難前,數百萬計的應用程序採起新的約定時間的方式,要麼升級到64位版本。操作系統

代碼示例

示例一.net

//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;
}

運行結果:unix

1554986565 seconds since 01-Jan-1970

示例二code

//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";
}

運行結果:blog

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

更多參考get

相關文章
相關標籤/搜索