2013-04-03 8 views
34

나는 시간 지점을 만들었지 만 터미널에 그것을 인쇄하려고 애를 썼다. 나는 그가 time_point를 인쇄 찾을 수 있습니다어떻게 C++ 11 time_point를 인쇄합니까?

#include <iostream> 
#include <chrono> 

int main(){ 

    //set time_point to current time 
    std::chrono::time_point<std::chrono::system_clock,std::chrono::nanoseconds> time_point; 
    time_point = std::chrono::system_clock::now(); 

    //print the time 
    //... 

    return 0; 
} 

유일한 문서는 여기있다 : http://en.cppreference.com/w/cpp/chrono/time_point

그러나, 나는 (예 등) 내 time_point을 기반으로 time_t를 만들조차 수 아니에요.

std::time_t now_c = std::chrono::system_clock::to_time_t(time_point); //does not compile 

오류 :

/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono: In instantiation of ‘constexpr std::chrono::time_point<_Clock, _Dur>::time_point(const std::chrono::time_point<_Clock, _Dur2>&) [with _Dur2 = std::chrono::duration<long int, std::ratio<1l, 1000000000l> >; _Clock = std::chrono::system_clock; _Dur = std::chrono::duration<long int, std::ratio<1l, 1000000l> >]’: 
time.cpp:13:69: required from here 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:540:32: error: no matching function for call to ‘std::chrono::duration<long int, std::ratio<1l, 1000000l> >::duration(std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > >::duration)’ 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:540:32: note: candidates are: 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:247:14: note: template<class _Rep2, class _Period2, class> constexpr std::chrono::duration::duration(const std::chrono::duration<_Rep2, _Period2>&) 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:247:14: note: template argument deduction/substitution failed: 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:243:46: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’ 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:240:23: note: template<class _Rep2, class> constexpr std::chrono::duration::duration(const _Rep2&) 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:240:23: note: template argument deduction/substitution failed: 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:236:27: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’ 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:234:12: note: constexpr std::chrono::duration<_Rep, _Period>::duration(const std::chrono::duration<_Rep, _Period>&) [with _Rep = long int; _Period = std::ratio<1l, 1000000l>] 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:234:12: note: no known conversion for argument 1 from ‘std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > >::duration {aka std::chrono::duration<long int, std::ratio<1l, 1000000000l> >}’ to ‘const std::chrono::duration<long int, std::ratio<1l, 1000000l> >&’ 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:232:12: note: constexpr std::chrono::duration<_Rep, _Period>::duration() [with _Rep = long int; _Period = std::ratio<1l, 1000000l>] 
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/chrono:232:12: note: candidate expects 0 arguments, 1 provided 
+5

'libstdC++'(즉, STL의 GCC 구현체)는'std :: put_time'을 아직 지원하지 않으며 cppreference의 예제가 컴파일되지 않는다고 생각합니다. http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html –

+0

참고로 '이 코드 실행'을 클릭하고 컴파일러를 'clang 3.4 (C + +)'로 변경하면 cppreference의 예제가 컴파일됩니다. +11)' – Cubbi

답변

36

(이 게시물에서는 명확성을 위해 std::chrono::의 자격을 생략합니다.

코드 예제가 컴파일되지 않는 이유는 반환 유형이 system_clock::now()과이 변수를 할당하려고 시도하는 변수 유형 (time_point<system_clock, nanoseconds>)간에 불일치가 있기 때문입니다.

문서화 된 반환 값 system_clock::now()은 의 typedef 인 system_clock::time_point입니다. system_clock::duration은 구현 정의이며 microsecondsnanoseconds이 일반적으로 사용됩니다. 구현시 microseconds을 사용하고 있으므로 system_clock::now()의 반환 유형은 time_point<system_clock, microseconds>입니다.

time_point 다른 기간은 암시 적으로 서로 변환 할 수 없으므로 컴파일러 오류가 발생합니다.

할 수 있습니다 다양한 기간이 time_point_cast을 사용하기 때문에 시스템에 컴파일 할 다음과 명시 적으로 변환 시점 : time_point_cast에 명시 적으로 템플릿 매개 변수가 대상 기간 유형,하지

time_point<system_clock, nanoseconds> time_point; 
time_point = time_point_cast<nanoseconds>(system_clock::now()); 

공지 사항 목표 time_point 유형 시계 유형은 time_point_cast과 일치해야하므로 전체 time_point 유형 (시계 유형과 기간 유형 모두에 템플리트 화됨)을 지정하면 중복 될 수 있습니다.

당연히 시간 지점을 인쇄하려고하기 때문에 특정 해상도가 필요하지 않으므로 time_pointsystem_clock::now()이 반환하는 것과 동일한 유형으로 선언 할 수 있습니다. ~로 시작하십시오.C++ 11, 당신은 또한 단지 auto 사용할 수있다이 있기 때문에

system_clock::time_point time_point; 
time_point = system_clock::now(); // no time_point_cast needed 

: 그렇게하는 간단한 방법은 system_clock::time_point 형식 정의를 사용하는 것입니다이 컴파일러 오류를 해결 데

auto time_point = system_clock::now(); 

를 변환 잘 time_t 작품 :

std::time_t now_c = std::chrono::system_clock::to_time_t(time_point); 

당신이 지금 time_t 값을 표시하는 표준 방법을 사용할 수 있습니다, 난 ike std::ctime 또는 std::strftime. (Cassio Neri 귀하의 질문에 대한 의견을 지적 더 C++ - y를 std::put_time 기능은 아직 GCC에서 지원되지 않습니다).

+1

+1 훨씬 더 철저한 설명과'auto' (실제로는 C++입니다. 아무도 변수를 선언하지 않고'auto '가없는 경우에도 바로 다음 행에 할당합니다.)를 언급합니다. –

+4

@ HighCommander4 :'time_point'는 다른 지속 시간을 가지고 있습니다 ** ** 동일한 클럭을 공유하고 rhs'Duration'이 암시 적으로'Duration' lhs로 변환 될 수 있다면 ** 암시 적으로 서로 변환 가능합니다. 이 예제에서'system_clock :: duration'이 마이크로 초 또는 나노초라면 [time.point.cons]/p3에 따라'time_point '로 암시 적으로 변환됩니다. –

8

nanoseconds이 문서에서 내가 얻을 수 있었다 조금 찾고, 문제의 일부가 될 것으로 보인다은이 작동하려면 :

#include <iostream> 
#include <chrono> 
#include <ctime> 


int main(){ 

    //set time_point to current time 
    std::chrono::time_point<std::chrono::system_clock> time_point; 
    time_point = std::chrono::system_clock::now(); 

    std::time_t ttp = std::chrono::system_clock::to_time_t(time_point); 
    std::cout << "time: " << std::ctime(&ttp); 

    return 0; 
} 

std::chrono::microseconds처럼 보이지만 그래도 작동합니다.

std::chrono::time_point<std::chrono::system_clock,std::chrono::microseconds> time_point; 
+1

그건 그렇고 훨씬 더 간결한'auto time_point = std :: chrono :: system_clock :: now();'도 아마 그렇게했을 것입니다. –

+1

@ChristianRau 물론 틀림없이 잘못된 코드가 아니라면 가능한 한 원래 게시 된 코드에 최대한 가까운 코드를 유지하는 것이 좋습니다. –

10

이 조각은 당신을 도울 수 있습니다

#include <iostream> 
#include <chrono> 
#include <ctime> 

template<typename Clock, typename Duration> 
std::ostream &operator<<(std::ostream &stream, 
    const std::chrono::time_point<Clock, Duration> &time_point) { 
    const time_t time = Clock::to_time_t(time_point); 
#if __GNUC__ > 4 || \ 
    ((__GNUC__ == 4) && __GNUC_MINOR__ > 8 && __GNUC_REVISION__ > 1) 
    // Maybe the put_time will be implemented later? 
    struct tm tm; 
    localtime_r(&time, &tm); 
    return stream << std::put_time(&tm, "%c"); // Print standard date&time 
#else 
    char buffer[26]; 
    ctime_r(&time, buffer); 
    buffer[24] = '\0'; // Removes the newline that is added 
    return stream << buffer; 
#endif 
} 

int main() { 
    std::cout << std::chrono::system_clock::now() << std::endl; 
    // Wed May 22 14:17:03 2013 
} 
+0

'std :: chrono :: system_clock'만이'to_time_t'를 지원합니다. 그래서 이것을 템플릿으로 사용하는 것이 타당하지 않다고 생각합니다. 대신 time_point는'const system_clock :: time_point & time_point'로 직접 전달 될 수 있습니다. –

+1

사용자가 'to_time_t'을 가진 사용자 정의 된 시계를 작성하는지에 따라 다릅니다. –

+0

또한 #include '해야합니다. – thiagowfx

1

ctime이() Visual C++에서 작동하지 않습니다. MS Visual Studio 2013을 사용합니다. MSVC 컴파일러의 지시에 따라 위의 코드를 ctime_s (...)를 사용하도록 변경했습니다. 그것은 효과가 있었다. 오래된 질문에 대한

//set time_point to current time 
std::chrono::time_point<std::chrono::system_clock> time_point; 
time_point = std::chrono::system_clock::now(); 

std::time_t ttp = std::chrono::system_clock::to_time_t(time_point); 
char chr[50]; 
errno_t e = ctime_s(chr, 50, &ttp); 
if (e) std::cout << "Error." << endl; 
else std::cout << chr << std::endl; 
6

업데이트 대답하십시오 std::chrono::time_point<std::chrono::system_clock, some-duration>를 들어

지금 당신에게 더 나은 제어 할 수 있도록 제 3 자 라이브러리가있다. 다른 시계를 기반으로 한 time_points의 경우 내부 표현을 가져 와서 인쇄하는 것보다 더 나은 해결책이 아직 없습니다. this library을 사용

그러나 system_clock에 대한

은이는 쉽게로 : 나를 위해

#include "date.h" 
#include <iostream> 

int 
main() 
{ 
    using namespace date; 
    using namespace std::chrono; 
    std::cout << system_clock::now() << " UTC\n"; 
} 

단지 출력 :

현재 UTC 날짜와 정밀도를 마이크로 시간이다
2016-07-19 03:21:01.910626 UTC 

. 플랫폼에 system_clock::time_point의 나노초 정밀도가있는 경우 나노초 정밀도가 인쇄됩니다. time_point<steady_clock> (안 time_point<system_clock>)와 협력 사람들을위한

+4

흠, 이것은 표준 imo에 있어야합니다. –

+3

@AbhinavGunnal : 작업 중 (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0355r1.html). 이것은 ** 귀하의 ** 매우 보컬 도움이 필요할 것입니다. 위원회에 막대한 영향력을 행사하는 사람들은이 제안을 좋아하지 않으며 대중이 그들을 외면하지 않으면 승리 할 것입니다. –

+0

어떤 종류의 논쟁이 주어질 수 있습니까 ??? – akim

1

:

#include <chrono> 
#include <iostream> 

template<std::intmax_t resolution> 
std::ostream &operator<<(
    std::ostream &stream, 
    const std::chrono::duration< 
     std::intmax_t, 
     std::ratio<std::intmax_t(1), resolution> 
    > &duration) 
{ 
    const std::intmax_t ticks = duration.count(); 
    stream << (ticks/resolution) << '.'; 
    std::intmax_t div = resolution; 
    std::intmax_t frac = ticks; 
    for (;;) { 
     frac %= div; 
     if (frac == 0) break; 
     div /= 10; 
     stream << frac/div; 
    } 
    return stream; 
} 

template<typename Clock, typename Duration> 
std::ostream &operator<<(
    std::ostream &stream, 
    const std::chrono::time_point<Clock, Duration> &timepoint) 
{ 
    typename Duration::duration ago = timepoint.time_since_epoch(); 
    return stream << ago; 
} 

int main(){ 
    // print time_point 
    std::chrono::time_point<std::chrono::steady_clock> now = 
     std::chrono::steady_clock::now(); 
    std::cout << now << "\n"; 

    // print duration (such as the difference between 2 time_points) 
    std::chrono::steady_clock::duration age = now - now; 
    std::cout << age << "\n"; 
} 

진수 수치 포매터가 가장 효율적인 아니지만, 당신이 resolution을 원하는 경우 알 수없는 소수의 수, 전혀 사전 지식이 필요합니다 ceil(log10(resolution))에 대한 상수 표현식을 생각해 낼 수 없다면 템플릿으로 작성해야합니다.