本實例演示如何使用 time()、localtime() 和 asctime() 獲取並格式化輸出當前本地日期時間。
time(&rawtime) 獲取當前時間戳(自 1970-01-01 以來的秒數)。localtime() 將時間戳轉換為本地時間的 struct tm 結構。asctime() 將 struct tm 格式化為可讀字符串(如 "Tue Nov 10 16:28:49 2015")。// Created by www.runoob.com on 15/11/9.
// Copyright © 2015年 菜鳥教程. All rights reserved.
//
#include <stdio.h>
#include <time.h>
int main() {
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("當前本地時間為: %s", asctime(timeinfo));
return 0;
}
當前本地時間為: Tue Nov 10 16:28:49 2015
strftime() 函數。gmtime() 可獲取 UTC 時間。