std::hash<std::chrono::time_point>
From cppreference.com
 
 
 < cpp | chrono | time point 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
Date and time library 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
(C++11)
(C++20)
  (C++20)
(C++11)
(C++11)
(C++11)
(C++20)
(C++20)
(C++20)
(C++11)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
  (C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
std::chrono::time_point 
 
 
 
 
 
 
 Member functions
(C++20)(C++20)
 Non-member functions
(until C++20)(C++20)
(C++17)
(C++17)
(C++17)
 Helper classes
hash<std::chrono::time_point>
(C++26)
Defined in header 
 
 
<chrono> 
 template< class Clock, class Duration >
struct hash<std::chrono::time_point <Clock, Duration>>;
 
 (since C++26) 
struct hash<std::chrono::time_point <Clock, Duration>>;
The template specialization of std::hash for std::chrono::time_point allows users to obtain hashes of objects of type std::chrono::time_point <Clock, Duration>. This specialization is enabled if and only if std::hash <Duration> is enabled.
[edit] Notes
| Feature-test macro | Value | Std | Feature | 
|---|---|---|---|
| __cpp_lib_chrono | 202306L | (C++26) | Hashing support for std::chronovalue classes | 
Example
Run this code
#include <chrono> #include <cstddef> #include <iostream> #include <string> #include <thread> #include <unordered_map> struct my_system_clock : std::chrono::system_clock { using time_point = std::chrono::time_point <my_system_clock>; static time_point now() noexcept { return time_point{std::chrono::system_clock::now ().time_since_epoch()}; } template<class CharT, class Traits> friend auto operator<<(std::basic_ostream <CharT, Traits>& os, const time_point& tp) -> decltype(os) { return os << std::chrono::system_clock::time_point{tp.time_since_epoch()}; } }; using my_system_clock_tp = std::chrono::time_point <my_system_clock>; #if __cpp_lib_chrono < 202306L // custom specialization of std::hash can be injected in namespace std template<> struct std::hash <my_system_clock_tp> { std::size_t operator()(const my_system_clock_tp& d) const noexcept { return d.time_since_epoch().count(); } }; #endif int main() { using namespace std::chrono_literals; std::unordered_map <my_system_clock_tp, std::string > log; for (int i{}; i != 4; ++i) { std::this_thread::sleep_for (100ms); log[my_system_clock::now()] = "event #" + std::to_string (i); } for (auto const& [time, message] : log) std::cout << '[' << time << "], message: " << message << '\n'; }
Possible output:
[2024年03月22日 10:47:14.966238436], message: event #3 [2024年03月22日 10:47:14.866096194], message: event #2 [2024年03月22日 10:47:14.765965786], message: event #1 [2024年03月22日 10:47:14.665817365], message: event #0