C++ C-style 日期和時間

一 簡介

1. 類型

(1) struct tm 

日曆時間類型;

被解開( broken down )的日曆時間類型。

struct tm
{
    int tm_sec;   // seconds after the minute - [0, 60] including leap second
    int tm_min;   // minutes after the hour - [0, 59]
    int tm_hour;  // hours since midnight - [0, 23]
    int tm_mday;  // day of the month - [1, 31]
    int tm_mon;   // months since January - [0, 11]
    int tm_year;  // years since 1900
    int tm_wday;  // days since Sunday - [0, 6]
    int tm_yday;  // days since January 1 - [0, 365]
    int tm_isdst; // daylight savings time flag
};

(2)time_t 

從紀元( epoch 或叫做起始點)起的時間類型;

數值類型的 typedef, 表現爲 timepoint。

(3)clock_t 

進程運行時間;

數值類型的 typedef, 表現爲elapsed CPU time;

clock()返回。

(4)timespec(C++17)

本次不介紹

2. 時間操作(Time manipulation)

(1)difftime

計算時間( time_t )之間的差 。

double difftime( std::time_t time_end, std::time_t time_beg );

(2)time

返回自紀元起計的系統當前時間 。

std::time_t time( std::time_t* arg );

(3)clock

返回自程序啓動時起的原始處理器時鐘時間 ;

單位是 1/CLOCKS_PER_SEC 秒。

std::clock_t clock();

(4)timespec_get(C++17)

本次不介紹

3. 格式轉換

(1)asctime

轉換 tm 對象爲文本表示;

轉換爲一個標準日曆時間字符串。格式爲 “Www Mmm dd hh:mm:ss yyyy\n”

char* asctime( const std::tm* time_ptr );

(2)ctime

轉換 time_t  對象爲文本表示;

轉換爲一個標準日曆時間字符串,考慮時區。

如同通過調用 std::asctime(std::localtime(time)) 。

char* ctime( const std::time_t* time );

(3)strftime

轉換 tm 對象到自定義的文本表示。

注意各種轉換指定符的含義。

std::size_t strftime( char* str, std::size_t count, const char* format, const std::tm* time );

(4)wcsftime

轉換  tm 對象爲定製的寬字符串文本表示。

(5)gmtime

轉換紀元起時間爲以 UTC 表示的日曆時間;

time_t 轉換爲 tm, 不考慮時區。

std::tm* gmtime( const std::time_t* time );

(6)localtime

轉換紀元起時間爲以本地時間表示的日曆時間。

time_t 轉換爲 tm,考慮時區。

std::tm* localtime( const std::time_t *time );

(7)mktime

轉換日曆時間爲紀元起的時間;

tm 轉換爲一個 time_t。

std::time_t mktime( std::tm* time );

3. 常量

(1)CLOCKS_PER_SEC 

每秒的處理器始時鐘嘀嗒數 (宏常量)。定義了clock()的單位類型。

二 例子

#include <ctime>
#include <thread> // this_thread
#include <chrono> // chrono
#include <iomanip> // put_time
#include <iostream>

int main() 
{
  {
    // std::time
    auto time = std::time(0);
    std::cout << "time: "<< time << std::endl;
  }
  {
    // std::difftime
    auto start = std::time(0);
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    auto end = std::time(0);
    std::cout << "std:difftime: " << std::difftime(end, start) << std::endl;
  }
  {
    // clock
    auto clock = std::clock();
    std::cout << "clock: " << clock << std::endl;
  }
  {
    // asctime
    auto time = std::time(0);
    std::cout << "std::asctime: " << std::asctime(std::localtime(&time)) << std::endl;
  }
  {
    // ctime
    auto time = std::time(0);
    std::cout << "std::ctime: " << std::ctime(&time) << std::endl;
  }
  {
    // strftime
    auto time = std::time(0);
    char str[200];
    std::strftime(str, sizeof(str), "%A %c", std::localtime(&time));
    std::cout << "std::strftime: " << str << std::endl;
  }
  {
    // gmtime
    // localtime
    auto time = std::time(0);
    std::cout << "std:gmtime: " << std::put_time(std::gmtime(&time), "%c %A") << std::endl;
    std::cout << "std:localtime: " << std::put_time(std::localtime(&time), "%c %A") << std::endl;
  }
  {
    // mktime
     auto time = std::time(0);
     auto tm = *std::localtime(&time);
     tm.tm_mon += 2;
     std::mktime(&tm);
     auto time1 = std::mktime(&tm);
     std::cout << "std:mktime: " << std::put_time(std::localtime(&time1), "%c %A") << std::endl;
  }
  {
    // CLOCKS_PER_SEC
    std::cout << "CLOCKS_PER_SEC: " << CLOCKS_PER_SEC << std::endl;
  }

  std::cin.get();
  return 0;
}

過程中的 this_thread, 請參考 C++11 std::this_thread

猜猜結果中的兩個空行怎麼來的。

三 參考

C-style 日期和時間

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章