std::ctime
來自 cppreference.com
定義於標頭檔案 <ctime> |
||
char* ctime( const std::time_t* time ); |
||
將給定自紀元以來的時間轉換為日曆本地時間,然後轉換為文字表示,如同透過呼叫 std::asctime(std::localtime(time))。結果字串具有以下格式:
Www Mmm dd hh:mm:ss yyyy\n
-
Www
- 星期幾(Mon
,Tue
,Wed
,Thu
,Fri
,Sat
,Sun
之一)。 -
Mmm
- 月份(Jan
,Feb
,Mar
,Apr
,May
,Jun
,Jul
,Aug
,Sep
,Oct
,Nov
,Dec
之一)。 -
dd
- 月份中的日期。 -
hh
- 小時。 -
mm
- 分鐘。 -
ss
- 秒。 -
yyyy
- 年份。
該函式不支援本地化。
目錄 |
[編輯] 引數
time | - | 指向 std::time_t 物件的指標,指定要列印的時間 |
[編輯] 返回值
指向一個靜態的以 null 結尾的字元字串的指標,該字串包含日期和時間的文字表示。該字串可能在 std::asctime 和 std::ctime
之間共享,並且可能在這些函式每次呼叫時被覆蓋。
[編輯] 注意
此函式返回指向靜態資料的指標,並且不是執行緒安全的。此外,它會修改靜態的 std::tm 物件,該物件可能與 std::gmtime 和 std::localtime 共享。POSIX 將此函式標記為已過時,並推薦使用 std::strftime。
對於導致字串長度超過 25 個字元的 std::time_t 值(例如,年份 10000),行為可能是未定義的。
[編輯] 示例
執行此程式碼
#include <cassert> #include <cstring> #include <ctime> #include <iostream> int main() { std::time_t result = std::time(nullptr); std::cout << std::ctime(&result); char buffer[32]; std::strncpy(buffer, std::ctime(&result), 26); assert('\n' == buffer[std::strlen(buffer) - 1]); std::cout << buffer; }
可能的輸出
Mon Oct 11 17:10:55 2021 Mon Oct 11 17:10:55 2021
[編輯] 參閱
將 std::tm 物件轉換為文字表示 (函式) | |
將 std::tm 物件轉換為自定義文字表示 (函式) | |
(C++11) |
根據指定格式格式化並輸出日期/時間值 (函式模板) |
C 文件 關於 ctime
|