Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <time.h>
- #ifdef WIN32
- #include <windows.h>
- #else
- #include <unistd.h>
- #include <sys/time.h>
- #endif
- // 時間計測関数の使用前処理
- void InitTimer(void)
- {
- #ifdef WIN32
- timeBeginPeriod(1);
- #endif /* WIN32 */
- }
- // 時間計測関数の使用後処理
- void UnInitTimer(void)
- {
- #ifdef WIN32
- timeEndPeriod(1);
- #endif /* WIN32 */
- }
- // 時間取得関数(秒)
- double GetTimeVal(void)
- {
- #ifdef WIN32
- return (double)(timeGetTime()) / 1000;
- #else
- struct timeval t;
- gettimeofday(&t, NULL);
- return (double)t.tv_sec + (double)t.tv_usec * 1e-6;
- #endif
- }
- // 時間待ち関数(秒)
- void SysSleep(double tsleep)
- {
- #ifdef WIN32
- Sleep((DWORD)(tsleep * 1000));
- #else
- struct timespec treq, trem;
- treq.tv_sec = (time_t)tsleep;
- treq.tv_nsec = (long)((tsleep - treq.tv_sec) * 1000000000.0);
- memset(&trem, 0, sizeof(trem));
- nanosleep(&treq, &trem);
- #endif
- }
- // 現在時刻出力関数
- void PrintTime(void)
- {
- #ifdef WIN32
- SYSTEMTIME t_st;
- GetLocalTime(&t_st);
- printf("%d-%02d-%02d %02d:%02d:%02d.%03d\n",
- t_st.wYear,
- t_st.wMonth,
- t_st.wDay,
- t_st.wHour,
- t_st.wMinute,
- t_st.wSecond,
- t_st.wMilliseconds);
- #else
- struct timeval tv;
- struct tm *t_st;
- /* 現在時刻の取得 */
- gettimeofday(&tv, NULL);
- /* 現在時刻を構造体に変換 */
- t_st = localtime(&tv.tv_sec);
- printf("%d-%02d-%02d %02d:%02d:%02d.%03d\n",
- t_st->tm_year + 1900,
- t_st->tm_mon + 1,
- t_st->tm_mday,
- t_st->tm_hour,
- t_st->tm_min,
- t_st->tm_sec,
- (int)(tv.tv_usec / 10000));
- #endif /* WIN32 */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement