Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
Loading...
Searching...
No Matches
chrono.h
Go to the documentation of this file.
1// License: Apache 2.0. See LICENSE file in root directory.
2// Copyright(c) 2022 Intel Corporation. All Rights Reserved.
3
4// This is in lieu of the C++20 chrono_io.h
5
6// NOTE: make sure to include this file BEFORE any usage in another header
7// (e.g., Catch should be include after this! Without these definitions, Catch
8// output does not know how to stringify durations...)
9
10#pragma once
11
12#include <chrono>
13#include <string>
14#include <sstream>
15#include <ctime>
16
17
18inline std::string to_string( const std::time_t & time )
19{
20 std::ostringstream os;
21 os << time;
22 return os.str();
23}
24
25
26template< typename Clock, typename Duration = typename Clock::duration >
27std::string to_string( const std::chrono::time_point< Clock, Duration > & tp )
28{
29 auto in_time_t = std::chrono::system_clock::to_time_t( tp );
30 return to_string( in_time_t );
31}
32
33
34template< typename Rep, typename Period = std::ratio< 1 > >
35std::string to_string( const std::chrono::duration< Rep, Period > & duration )
36{
37 auto seconds_as_int = std::chrono::duration_cast< std::chrono::seconds >( duration );
38 if( seconds_as_int == duration )
39 return std::to_string( seconds_as_int.count() ) + "s";
40 auto seconds_as_double = std::chrono::duration_cast< std::chrono::duration< double > >( duration );
41 std::ostringstream os;
42 os << seconds_as_double.count();
43 os << 's';
44 return os.str();
45}
46
47
48template< typename Clock, typename Duration = typename Clock::duration >
49std::ostream & operator<<( std::ostream & o, const std::chrono::time_point< Clock, Duration > & tp )
50{
51 return o << to_string( tp );
52}
53
54
55template< typename Rep, typename Period = std::ratio< 1 > >
56std::ostream & operator<<( std::ostream & o, const std::chrono::duration< Rep, Period > & duration )
57{
58 return o << to_string( duration );
59}
std::ostream & operator<<(std::ostream &o, const std::chrono::time_point< Clock, Duration > &tp)
Definition: chrono.h:49
std::string to_string(const std::time_t &time)
Definition: chrono.h:18