CPP > gettimeofday

Redjini WiKi
< cpp


1970년 1월 1일 부터 현재 까지 시간 구하기

마이크로초 단위의 시간 까지 확인 가능.

Linux
#include <sys/time.h>
#include <unistd.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);

struct timeval {
    long tv_sec;    // 초
    long tv_usec;   // 마이크로초
};

struct timezone {
    int  tz_minuteswest; // 시차(minutes)
    int  tz_dsttime;     // DST 보정 타입 , DST_NONE : DST 사용하지 않음
};
Win32
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
  #define DELTA_EPOCH_IN_MICROSECS  116444736000000000Ui64
								//  1164447360010000000
#else
  #define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
#endif

// for timezone
struct timezone{
  int  tz_minuteswest; //- minutes W of Greenwich *-
  int  tz_dsttime;     //- type of dst correction *-
};

// gettimeofday in windows
int gettimeofday(struct timeval *tv, struct timezone *tz){
	FILETIME			ft;		
	ULARGE_INTEGER		ull			= {0, 0};
	static int			tzflag		= 0;
	time_t				ttttt		= time(NULL);

	if (NULL != tv){
		// system time을 구하기
		GetSystemTimeAsFileTime(&ft);

		//time_t tf = FILEFileTimeToUnixTime(ft, NULL);

		ull.HighPart = ft.dwHighDateTime;
		ull.LowPart  = ft.dwLowDateTime;
		
		// UNIX TIME으로 변환하기
		ull.QuadPart-= DELTA_EPOCH_IN_MICROSECS;

		// 100 nano -> 1micro로 변환하기
		ull.QuadPart/= 10;

		tv->tv_sec = (LONG)((ull.QuadPart / 1000000UL));
		tv->tv_usec= (LONG)((ull.QuadPart % 1000000UL));
	}

	// timezone 처리
	if (NULL != tz){
		LONG nTimezone	= 0;
		INT	 nDaylight	= 0;

		if (!tzflag){
			_tzset();
			tzflag++;
		}

		_get_timezone(&nTimezone);
		_get_daylight(&nDaylight);

		tz->tz_minuteswest	= nTimezone;
		tz->tz_dsttime		= nDaylight;
	}

	return 0;
}
반환값

성공 : 0, 실패 : -1

참고