在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++语言中常用的获取系统时间的方法,可以根据实际需求选择适合的方式。