
- C++ 基礎
- C++ 首頁
- C++ 概述
- C++ 環境設定
- C++ 基本語法
- C++ 註釋
- C++ Hello World
- C++ 省略名稱空間
- C++ 常量/字面量
- C++ 關鍵字
- C++ 識別符號
- C++ 資料型別
- C++ 數值資料型別
- C++ 字元資料型別
- C++ 布林資料型別
- C++ 變數型別
- C++ 變數作用域
- C++ 多個變數
- C++ 基本輸入/輸出
- C++ 修飾符型別
- C++ 儲存類
- C++ 運算子
- C++ 數字
- C++ 列舉
- C++ 引用
- C++ 日期和時間
- C++ 控制語句
- C++ 決策
- C++ if 語句
- C++ if else 語句
- C++ 巢狀 if 語句
- C++ switch 語句
- C++ 巢狀 switch 語句
- C++ 迴圈型別
- C++ while 迴圈
- C++ for 迴圈
- C++ do while 迴圈
- C++ foreach 迴圈
- C++ 巢狀迴圈
- C++ break 語句
- C++ continue 語句
- C++ goto 語句
- C++ 建構函式
- C++ 建構函式和解構函式
- C++ 複製建構函式
C++ 日期和時間
C++ 標準庫沒有提供合適的日期型別。C++ 從 C 繼承了用於日期和時間操作的結構體和函式。要訪問與日期和時間相關的函式和結構體,您需要在 C++ 程式中包含 <ctime> 標頭檔案。
有四種與時間相關的型別:clock_t、time_t、size_t 和 tm。型別 - clock_t、size_t 和 time_t 能夠以某種整數形式表示系統時間和日期。
結構體型別 tm 以 C 結構體 的形式儲存日期和時間,具有以下元素:
struct tm { int tm_sec; // seconds of minutes from 0 to 61 int tm_min; // minutes of hour from 0 to 59 int tm_hour; // hours of day from 0 to 24 int tm_mday; // day of month from 1 to 31 int tm_mon; // month of year from 0 to 11 int tm_year; // year since 1900 int tm_wday; // days since sunday int tm_yday; // days since January 1st int tm_isdst; // hours of daylight savings time }
以下是我們在 C 或 C++ 中處理日期和時間時使用的重要函式。所有這些函式都是標準 C 和 C++ 庫的一部分,您可以使用下面給出的 C++ 標準庫參考檢查它們的詳細資訊。
序號 | 函式和用途 |
---|---|
1 | time_t time(time_t *time); 此函式返回系統當前日曆時間,以自 1970 年 1 月 1 日以來的秒數表示。如果系統沒有時間,則返回 .1。 |
2 | char *ctime(const time_t *time); 此函式返回一個指向字串的指標,該字串的格式為 day month year hours:minutes:seconds year\n\0。 |
3 | struct tm *localtime(const time_t *time); 此函式返回一個指向表示本地時間的 tm 結構體的指標。 |
4 | clock_t clock(void); 此函式返回一個近似值,表示呼叫程式已執行的時間量。如果時間不可用,則返回 .1。 |
5 | char * asctime ( const struct tm * time ); 此函式返回一個指向字串的指標,該字串包含儲存在 time 指向的結構體中的資訊,轉換為以下格式:day month date hours:minutes:seconds year\n\0 |
6 | struct tm *gmtime(const time_t *time); 此函式返回一個指向以 tm 結構體形式表示的時間的指標。時間以協調世界時 (UTC) 表示,它本質上是格林威治標準時間 (GMT)。 |
7 | time_t mktime(struct tm *time); 此函式返回 time 指向的結構體中找到的時間的日曆時間等效值。 |
8 | double difftime ( time_t time2, time_t time1 ); 此函式計算 time1 和 time2 之間的秒差。 |
9 | size_t strftime(); 此函式可用於以特定格式格式化日期和時間。 |
當前日期和時間
假設您想檢索當前系統日期和時間,無論是本地時間還是協調世界時 (UTC)。
示例
以下是實現相同目標的示例:
#include <iostream> #include <ctime> using namespace std; int main() { // current date/time based on current system time_t now = time(0); // convert now to string form char* dt = ctime(&now); cout << "The local date and time is: " << dt << endl; // convert now to tm struct for UTC tm *gmtm = gmtime(&now); dt = asctime(gmtm); cout << "The UTC date and time is:"<< dt << endl; }
當以上程式碼編譯並執行時,會產生以下結果:
The local date and time is: Sat Jan 8 20:07:41 2011 The UTC date and time is:Sun Jan 9 03:07:41 2011
使用 struct tm 格式化時間
tm 結構體在 C 或 C++ 中處理日期和時間時非常重要。如上所述,此結構體以 C 結構體形式儲存日期和時間。大多數與時間相關的函式都使用 tm 結構體。以下是一個使用各種日期和時間相關函式和 tm 結構體的示例:
在本節中使用結構體時,我假設您對 C 結構體以及如何使用 箭頭 -> 運算子 訪問結構體成員有基本的瞭解。
示例
#include <iostream> #include <ctime> using namespace std; int main() { // current date/time based on current system time_t now = time(0); cout << "Number of sec since January 1,1970 is:: " << now << endl; tm *ltm = localtime(&now); // print various components of tm structure. cout << "Year:" << 1900 + ltm->tm_year<<endl; cout << "Month: "<< 1 + ltm->tm_mon<< endl; cout << "Day: "<< ltm->tm_mday << endl; cout << "Time: "<< 5+ltm->tm_hour << ":"; cout << 30+ltm->tm_min << ":"; cout << ltm->tm_sec << endl; }
當以上程式碼編譯並執行時,會產生以下結果:
Number of sec since January 1,1970 is:: 1588485717 Year:2020 Month: 5 Day: 3 Time: 11:31:57