在C/C++語言中,獲取系統時間的幾種方式包括:
#include <stdio.h>
#include <time.h>
int main() {
time_t currentTime;
time(¤tTime);
printf("Current time: %s", ctime(¤tTime));
return 0;
}
#include <stdio.h>
#include <time.h>
int main() {
clock_t start = clock();
// 在這里放置要計時的代碼
clock_t end = clock();
double timeUsed = (double)(end - start) / CLOCKS_PER_SEC;
printf("Time used: %lf seconds", timeUsed);
return 0;
}
#include <stdio.h>
#include <sys/time.h>
int main() {
struct timeval currentTime;
gettimeofday(¤tTime, NULL);
printf("Current time: %ld seconds, %ld microseconds", currentTime.tv_sec, currentTime.tv_usec);
return 0;
}
#include <stdio.h>
#include <time.h>
int main() {
time_t currentTime;
time(¤tTime);
struct tm* localTime = localtime(¤tTime);
printf("Current time: %04d-%02d-%02d %02d:%02d:%02d",
localTime->tm_year + 1900, localTime->tm_mon + 1, localTime->tm_mday,
localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
return 0;
}
這些是C/C++語言中常用的獲取系統時間的方法,可以根據實際需求選擇適合的方式。