在Linux中,可以使用C語言的time.h
庫來獲取系統(tǒng)時間
#include<stdio.h>
#include <time.h>
int main() {
time_t currentTime;
struct tm *localTime;
// 獲取當(dāng)前的系統(tǒng)時間(以秒為單位)
currentTime = time(NULL);
// 將系統(tǒng)時間轉(zhuǎn)換為本地時間(struct tm格式)
localTime = localtime(¤tTime);
// 打印本地時間
printf("當(dāng)前本地時間:%s", asctime(localTime));
return 0;
}
這個程序首先獲取當(dāng)前的系統(tǒng)時間(以秒為單位),然后將其轉(zhuǎn)換為本地時間(struct tm
格式),最后使用asctime()
函數(shù)將本地時間轉(zhuǎn)換為可讀的字符串并打印出來。