여기 (밀리 초) 시대의 시간과 시차를 얻을 수있는 C++ (11)의 방법 (물론, std::literals
는 C++ 14입니다하지만 당신은 것을 사용할 필요가 없습니다) :
#include <iostream>
#include <chrono>
using namespace std::literals;
int main()
{
using Clock = std::chrono::system_clock;
auto point1 = Clock::now();
int64_t epoch = point1.time_since_epoch()/1ms;
std::cout << "Time since epoch: " << epoch << std::endl;
auto point2 = Clock::now();
std::cout << "Time difference in milliseconds: " << ((point2 - point1)/1ms) << std::endl;
std::cout << "Time difference in nanoseconds: " << ((point2 - point1)/1ns) << std::endl;
}
system_clock demo
Time since epoch: 1486930917677
Time difference in milliseconds: 0
Time difference in nanoseconds: 102000
표준이
chrono::system_clock
보다 높은 정밀도를 제공 할 수
chrono::high_resolution_clock
을 가지고 높은 해상도 시점의 차이를
만의 시대는 종종 시스템 부팅시 시작되지 1-1-1970에
high_resolution_clock demo
Time since "epoch": 179272927
Time difference in milliseconds: 0
Time difference in nanoseconds: 74980
그것은 2015+ Visual Studio에서 100ns의 정밀도를 가지고 2015 년 전에 high_resolution_clock
여전히 비주얼 스튜디오에 일초 정밀도를 가지고 있다는 사실을 숙지하고, 는는에 적어도 1ms의 정밀도를 가져야한다 다른 플랫폼.
PS std::chrono
은 32 비트 및 64 비트 시스템에서 정확히 동일하게 작동합니다.
에포크 시간이 밀리 초 단위로 필요합니까? 아니면 두 시간 단위 사이에 밀리 초 단위로 차이가 필요합니까? – rustyx
@RustyX 밀리 세컨드의 두 시간 지점 사이에 차이가 필요하지만 뭔가 빠뜨린 것이 아니라면 다른 시간대의 밀리 초 단위로 에포크 시간을 얻을 수 있다고 가정합니다. – dk13
에포크 시간을 밀리 초 단위로 가져 오는 표준적인 휴대용 방법은 없습니다. 'chrono :: system_clock'은 종종 1 초의 정밀도를 가지고 있으며,'chrono :: high_resolution_clock'은 그 시대에 시작하지 않습니다. – rustyx