ftime()
関数を<sys/time.h>
のgettimeofday()
関数で代替する場合、次のようなコードを使用できます。
#include <stdio.h>
#include <sys/time.h>
int main() {
struct timeval tv;
gettimeofday(&tv, NULL);
printf("Seconds since epoch: %ld\n", tv.tv_sec);
printf("Microseconds: %ld\n", tv.tv_usec);
return 0;
}
このコードは、現在の時間を秒単位で取得し、エポックからの経過秒数とマイクロ秒単位の時間を表示します。
もし、精度の高い時間情報が必要な場合は、<time.h>
のclock_gettime()
関数を使用できます。
#include <stdio.h>
#include <time.h>
int main() {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
printf("Seconds since epoch: %ld\n", ts.tv_sec);
printf("Nanoseconds: %ld\n", ts.tv_nsec);
return 0;
}
このコードは、CLOCK_REALTIME
を使用して、実時間の時間情報を取得します。