Need time representation that cannot be adjusted and can be serialized

I'm looking for something that can hold the current time, but is independent of the user adjusting the OS's time, and that can be converted to a std::string and back and that meets the requirements of LessThanComparable and EqualityComparable, I search online but I couldn't find anything suitable.

I can't use The C++ Standard Chrono Library because:
std::chrono::system_clock you can change the OS's time
std::chrono::steady_clock and std::chrono::high_resolution_clock cannot be converted to a std::string and back

Does anyone have any idea?

Thanks in advance to anyone who replies!
Last edited on
std::chrono::high_resolution_clock can only be used to get time deltas, generally for the purposes of benchmarking/profiling. It can't be used for timestamping because it's not a real time clock.

If you want to get the actual current time your only real option is Network Time Protocol. Note:
1. You will have to hardcode the servers you want to use.
2. NTP doesn't have any provisions to guarantee that the server you're talking to is actually the one you wanted, so there's no guarantee that the system is not configured to resolve names or route IP addresses to itself and run an NTP server that lies. If this is a problem, you will have to make your own time protocol that does provide authentication, and you will have to run your own server.
std::chrono::time_point<> is used to represent a point in time (internally holds a time interval from the start of the clock's epoch). std::chrono::time_point<> is a NumericType that is LessThanComparable and EqualityComparable.
http://en.cppreference.com/w/cpp/chrono/time_point/operator_cmp

The clocks in std::chrono provide the static member function now() which returns the current time point.

The only clock in std::chrono that is guaranteed to be monotonic and the one most suitable for measuring time intervals is std::chrono::steady_clock. The time points of this clock always move forward in physical time and it is not affected by changes to system wall clock time.

(std::chrono::system_clock follows the system wall clock; if the user adjusts the OS's current time, it will follow the adjustment. And std::chrono::high_resolution_clock need not be steady.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <chrono>
#include <string>
#include <thread>

int main()
{
    using namespace std::chrono ;

    const auto time_pt = steady_clock::now() ;
    const auto milliseconds_since_epoch_begin = duration_cast<milliseconds>( time_pt.time_since_epoch() ).count() ;
    const std::string str = std::to_string(milliseconds_since_epoch_begin) + " milliseconds." ;
    std::cout << str << '\n' ;
    
    std::this_thread::sleep_for( seconds(2) ) ;
    std::cout << duration_cast<milliseconds>( steady_clock::now().time_since_epoch() ).count() << " milliseconds.\n" ;
}

http://coliru.stacked-crooked.com/a/ec86e020bcbe5dd2
Thank you all for the quick replies!

@JLBorges

OK, I see that I can convert a time point to milliseconds to a std::string, but how do I convert a std::string that contains milliseconds to a time point?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <chrono>
#include <string>
#include <thread>

int main()
{
    using clock = std::chrono::steady_clock ;
    using milliseconds = std::chrono::milliseconds ;

    const std::string str_msecs = std::to_string( std::chrono::duration_cast<milliseconds>(
                                                                clock::now().time_since_epoch() ).count() ) ;
                                                                
    const unsigned long long millisecs = std::stoll(str_msecs) ; // string => unsigned long long
    const milliseconds duration = milliseconds(millisecs) ; // unsigned long long => std::chrono::milliseconds
    const std::chrono::time_point<clock> time_pt(duration) ; // duration => time point of clock

    std::cout << std::chrono::duration_cast<milliseconds>( clock::now() - time_pt ).count() << '\n' ;
    std::this_thread::sleep_for( milliseconds(100) ) ;
    std::cout << std::chrono::duration_cast<milliseconds>( clock::now() - time_pt ).count() << '\n' ;
}

http://coliru.stacked-crooked.com/a/14bd6e9dc643357a
@JLBorges

Thank you so so much for helping me!
Topic archived. No new replies allowed.