Home >Backend Development >C++ >How do I extract temporal components from a std::chrono::time_point?
Extracting Temporal Components from a std::chrono::time_point
For system_clock::time_points, the std::chrono library does not directly provide functionality to extract temporal components such as year, month, and day. However, it's possible to retrieve these components by exploiting the underlying relationship between system clocks and the civil calendar.
Extracting Components Using C Library
One method involves converting the time_point to a time_t, representing the number of seconds since New Years 1970 in UTC timezone. Using the C library's time manipulation functions, you can then convert the time_t to a tm structure, which contains components like year, month, and day. The following code demonstrates this approach:
<code class="cpp">system_clock::time_point now = system_clock::now(); time_t tt = system_clock::to_time_t(now); tm utc_tm = *gmtime(&tt); // Convert to UTC time std::cout << utc_tm.tm_year + 1900 << '\n'; std::cout << utc_tm.tm_mon + 1 << '\n'; std::cout << utc_tm.tm_mday << '\n';
Extracting Unix Epoch Time Components
Precise components can be obtained by recognizing that most system clocks used in modern systems are based on Unix epoch time, which represents the number of seconds since January 1, 1970 UTC. While std::chrono does not provide methods to directly access this value, it is possible to extract components by subtracting from the total time elapsed since the epoch.
<code class="cpp">typedef duration<int, ratio_multiply<hours::period, ratio<24>>> days; system_clock::time_point now = system_clock::now(); system_clock::duration tp = now.time_since_epoch(); days d = duration_cast<days>(tp); tp -= d; hours h = duration_cast<hours>(tp); tp -= h; minutes m = duration_cast<minutes>(tp); tp -= m; seconds s = duration_cast<seconds>(tp); tp -= s; // Remaining portion of a second with system_clock::duration precision std::cout << d.count() << "d " << h.count() << ':' << m.count() << ':' << s.count() << '\n';
Using a Header-Only C 11/14 Library
The date-only header-only library offers a more convenient solution, allowing you to extract these components easily:
<code class="cpp">#include "date.h" #include <iostream> int main() { namespace C = std::chrono; namespace D = date; namespace S = std; auto tp = C::system_clock::now(); auto dp = D::floor<D::days>(tp); auto ymd = D::year_month_day{dp}; auto time = D::make_time(C::duration_cast<C::milliseconds>(tp - dp)); S::cout << "year = " << ymd.year() << '\n'; S::cout << "month = " << ymd.month() << '\n'; S::cout << "day = " << ymd.day() << '\n'; S::cout << "hour = " << time.hours().count() << "h\n"; S::cout << "minute = " << time.minutes().count() << "min\n"; S::cout << "second = " << time.seconds().count() << "s\n"; S::cout << "millisecond = " << time.subseconds().count() << "ms\n"; }</code>
C 20 Standard Library Features (Coming Soon)
In the upcoming C 20 standard, direct extraction of temporal components from system_clock::time_point is supported. The syntax is as follows:
<code class="cpp">auto tp = system_clock::now(); auto dp = floor<days>(tp); year_month_day ymd{dp}; hh_mm_ss time{floor<milliseconds>(tp - dp)}; auto y = ymd.year(); auto m = ymd.month(); auto d = ymd.day(); auto h = time.hours(); auto M = time.minutes(); auto s = time.seconds(); auto ms = time.subseconds();</code>
The above is the detailed content of How do I extract temporal components from a std::chrono::time_point?. For more information, please follow other related articles on the PHP Chinese website!