C++ 프로그래밍

C++ system_clock을 이용하여 64bit tick값 저장 및 로드 예제

하늘흐늘 2022. 3. 28. 16:40
반응형

게임의 아이템 등 여러 곳에서 데이터의 시간 값을 저장하고 로드합니다.
여기서는 system_clock을 이용하여 시간 값을 64bit 변수로 저장하고 로드하는 예제를 살펴보도록 하겠습니다. 참고로 변수 값은 보통 실무 프로젝트에서는 tick이라고 불리웁니다. 예제에서는 실제로 값을 저장하지는 않지만 long long 값인 tick 값을 64bit 변수 값으로 DB 등에 저장하고 로드하면 됩니다.

#include <iostream>
#include <chrono>


using namespace std;
using namespace chrono;


int main(int argc, char* argv[])
{
	system_clock::time_point now = system_clock::now();
	long long tick = now.time_since_epoch().count();

	wcout << "save value... "<< endl;
	wcout << "tick: " << tick << endl;
	wcout << endl;

	system_clock::duration dur = system_clock::duration(tick);
	system_clock::time_point time = system_clock::time_point(dur);

	time_t tt = system_clock::to_time_t(time);
	tm local;
	localtime_s(&local, &tt);

	wcout << "load value... " << endl;
	wcout << "time: " << 1900 + local.tm_year << "." << local.tm_mon + 1 << "." << local.tm_mday << " ";
	wcout << local.tm_hour << ":" << local.tm_min << ":" << local.tm_sec << endl;

	return 0;
}

실행 결과는 아래와 같습니다. 참고로 결과값은 실행시킨 시간에 따라 달라집니다.

save value...
tick: 16484532111110594

load value...
time: 2022.3.28 16:40:11
반응형