mirror of
https://github.com/boostorg/uuid.git
synced 2026-01-19 04:42:16 +00:00
Add uuid::time_point_v1(), uuid_clock::from_timestamp, uuid_clock::to_timestamp
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
// 02 Dec 2009 - removed BOOST_STATIC_CONSTANT - not all compilers like it
|
||||
// 29 Apr 2013 - added support for noexcept and constexpr, added optimizations for SSE/AVX
|
||||
|
||||
#include <boost/uuid/uuid_clock.hpp>
|
||||
#include <boost/uuid/detail/endian.hpp>
|
||||
#include <boost/uuid/detail/hash_mix.hpp>
|
||||
#include <boost/uuid/detail/config.hpp>
|
||||
@@ -81,6 +82,8 @@ public:
|
||||
|
||||
// accessors
|
||||
|
||||
// variant
|
||||
|
||||
enum variant_type
|
||||
{
|
||||
variant_ncs, // NCS backward compatibility
|
||||
@@ -106,6 +109,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
// version
|
||||
|
||||
enum version_type
|
||||
{
|
||||
version_unknown = -1,
|
||||
@@ -136,6 +141,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
// timestamp
|
||||
|
||||
using timestamp_type = std::uint64_t;
|
||||
|
||||
timestamp_type timestamp_v1() const BOOST_NOEXCEPT
|
||||
@@ -147,6 +154,15 @@ public:
|
||||
return time_low | static_cast<std::uint64_t>( time_mid ) << 32 | static_cast<std::uint64_t>( time_hi ) << 48;
|
||||
}
|
||||
|
||||
// time_point
|
||||
|
||||
uuid_clock::time_point time_point_v1() const BOOST_NOEXCEPT
|
||||
{
|
||||
return uuid_clock::from_timestamp( timestamp_v1() );
|
||||
}
|
||||
|
||||
// clock_seq
|
||||
|
||||
using clock_seq_type = std::uint16_t;
|
||||
|
||||
clock_seq_type clock_seq() const BOOST_NOEXCEPT
|
||||
@@ -154,6 +170,8 @@ public:
|
||||
return detail::load_big_u16( this->data + 8 ) & 0x3FFF;
|
||||
}
|
||||
|
||||
// node_identifier
|
||||
|
||||
using node_type = std::array<std::uint8_t, 6>;
|
||||
|
||||
node_type node_identifier() const BOOST_NOEXCEPT
|
||||
|
||||
@@ -27,6 +27,9 @@ public:
|
||||
|
||||
static time_point from_sys( std::chrono::system_clock::time_point const& tp ) noexcept;
|
||||
static std::chrono::system_clock::time_point to_sys( time_point const& tp ) noexcept;
|
||||
|
||||
static time_point from_timestamp( std::uint64_t timestamp ) noexcept;
|
||||
static std::uint64_t to_timestamp( time_point const& tp ) noexcept;
|
||||
};
|
||||
|
||||
inline uuid_clock::time_point uuid_clock::now() noexcept
|
||||
@@ -54,6 +57,16 @@ inline std::chrono::system_clock::time_point uuid_clock::to_sys( time_point cons
|
||||
return std::chrono::system_clock::time_point( sys_since );
|
||||
}
|
||||
|
||||
inline uuid_clock::time_point uuid_clock::from_timestamp( std::uint64_t timestamp ) noexcept
|
||||
{
|
||||
return time_point( duration( timestamp ) );
|
||||
}
|
||||
|
||||
inline std::uint64_t uuid_clock::to_timestamp( time_point const& tp ) noexcept
|
||||
{
|
||||
return tp.time_since_epoch().count();
|
||||
}
|
||||
|
||||
} // uuids
|
||||
} // boost
|
||||
|
||||
|
||||
@@ -13,14 +13,6 @@
|
||||
|
||||
using namespace boost::uuids;
|
||||
|
||||
uuid_clock::time_point get_time_point_v1( uuid const& u )
|
||||
{
|
||||
auto t = u.timestamp_v1();
|
||||
auto d = uuid_clock::duration( t );
|
||||
|
||||
return uuid_clock::time_point( d );
|
||||
}
|
||||
|
||||
uuid generate_and_test( time_generator_v1& gen )
|
||||
{
|
||||
auto sys_before = std::chrono::time_point_cast<uuid_clock::duration>( std::chrono::system_clock::now() );
|
||||
@@ -32,7 +24,7 @@ uuid generate_and_test( time_generator_v1& gen )
|
||||
|
||||
auto sys_after = std::chrono::time_point_cast<uuid_clock::duration>( std::chrono::system_clock::now() );
|
||||
|
||||
auto uuid_time_point = get_time_point_v1( u );
|
||||
auto uuid_time_point = u.time_point_v1();
|
||||
|
||||
auto sys_time_point = uuid_clock::to_sys( uuid_time_point );
|
||||
|
||||
|
||||
@@ -16,7 +16,9 @@ int main()
|
||||
|
||||
std::uint64_t uuid_timestamp = 0x1ef018cddaa7790;
|
||||
|
||||
auto uuid_time = uuid_clock::time_point( uuid_clock::duration( uuid_timestamp ) );
|
||||
auto uuid_time = uuid_clock::from_timestamp( uuid_timestamp );
|
||||
|
||||
BOOST_TEST_EQ( uuid_clock::to_timestamp( uuid_time ), uuid_timestamp );
|
||||
|
||||
auto sys_time = uuid_clock::to_sys( uuid_time );
|
||||
|
||||
@@ -26,7 +28,8 @@ int main()
|
||||
|
||||
auto uuid_time_2 = uuid_clock::from_sys( sys_time );
|
||||
|
||||
BOOST_TEST_EQ( uuid_time_2.time_since_epoch().count(), uuid_timestamp );
|
||||
BOOST_TEST( uuid_time == uuid_time_2 );
|
||||
BOOST_TEST_EQ( uuid_clock::to_timestamp( uuid_time_2 ), uuid_timestamp );
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user