The chrono types of C ++ 11 use only one number to represent time from a given era, unlike the timeval (or timespec ) structure, which uses two numbers to represent time accurately. So, with C ++ 11 chrono you don't need the combine() method.
The contents of the timestamp returned by now() depends on the clock you use; There is a tree clock described at http://en.cppreference.com/w/cpp/chrono :
system_clock wall clock time from the system-wide realtime clock steady_clock monotonic clock that will never be adjusted high_resolution_clock the clock with the shortest tick period available
If you want consistent timestamps to always be different, use a constant clock:
auto t1 = std::chrono::steady_clock::now(); ... auto t2 = std::chrono::steady_clock::now(); assert (t2 > t1);
Edit: Reply to comment
#include <iostream> #include <chrono> #include <cstdint> int main() { typedef std::chrono::duration< uint32_t, std::ratio<1> > s32_t; typedef std::chrono::duration< uint32_t, std::milli > ms32_t; s32_t first_part; ms32_t second_part; auto t1 = std::chrono::nanoseconds( 2500000000 ); // 2.5 secs first_part = std::chrono::duration_cast<s32_t>(t1); second_part = std::chrono::duration_cast<ms32_t>(t1-first_part); std::cout << "first part = " << first_part.count() << " s\n" << "seconds part = " << second_part.count() << " ms" << std::endl; auto t2 = std::chrono::nanoseconds( 2800000000 ); // 2.8 secs first_part = std::chrono::duration_cast<s32_t>(t2); second_part = std::chrono::duration_cast<ms32_t>(t2-first_part); std::cout << "first part = " << first_part.count() << " s\n" << "seconds part = " << second_part.count() << " ms" << std::endl; }
Output:
first part = 2 s seconds part = 500 ms first part = 2 s seconds part = 800 ms
source share