Objective: Acquiring the year, month, day, hour, minute, second, and milliseconds from an std::chrono::time_point object.
Solution:
To extract this information, you specifically require a system_clock::time_point, providing a relationship to the civil calendar system.
<code class="cpp">using namespace std::chrono; system_clock::time_point now = system_clock::now();</code>
The next step is converting it to a time_t using:
<code class="cpp">time_t tt = system_clock::to_time_t(now);</code>
Utilize the C library to convert time_t to tm, ensuring the right time zone is chosen:
<code class="cpp">tm utc_tm = *gmtime(&tt); tm local_tm = *localtime(&tt);</code>
Now, you can extract the components of tm:
<code class="cpp">std::cout << local_tm.tm_year + 1900 << '\n'; std::cout << local_tm.tm_mon + 1 << '\n'; std::cout << local_tm.tm_mday << '\n';
In addition, if you prefer, you can rely on the following non-guaranteed approach:
Every implementation of system_clock known to most is based on Unix time (the number of seconds since January 1, 1970 UTC), usually with a finer precision than seconds.
The following comprehensive program provides an example of extracting all temporal components:
<code class="cpp">#include <chrono> #include <ctime> #include <iostream> int main() { using namespace std::chrono; typedef duration<int, ratio_multiply<hours::period, ratio<24> >::type> 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; std::cout << d.count() << "d " << h.count() << ':' << m.count() << ':' << s.count(); std::cout << " " << tp.count() << "[" << system_clock::duration::period::num << '/' << system_clock::duration::period::den << "]\n"; time_t tt = system_clock::to_time_t(now); tm utc_tm = *gmtime(&tt); tm local_tm = *localtime(&tt); std::cout << utc_tm.tm_year + 1900 << '-'; std::cout << utc_tm.tm_mon + 1 << '-'; std::cout << utc_tm.tm_mday << ' '; std::cout << utc_tm.tm_hour << ':'; std::cout << utc_tm.tm_min << ':'; std::cout << utc_tm.tm_sec << '\n'; }</code>
To model days, create a custom duration:
<code class="cpp">typedef duration<int, ratio_multiply<hours::period, ratio<24> >::type> days;</code>
Now you can get the time since the epoch with the system_clock::duration's precision:
<code class="cpp">system_clock::duration tp = now.time_since_epoch();</code>
Truncate it to days and subtract that:
<code class="cpp">days d = duration_cast<days>(tp); tp -= d;</code>
Repeat this process for hours, minutes, and seconds.
The remainder is the fraction of a second in the units of system_clock::duration.
This C 11/14 library streamlines the above process:
<code class="cpp">#include "date.h" #include <iostream> int main() { using C = std::chrono; using D = date; using S = std; auto tp = C::system_clock::now(); { using namespace date; S::cout << tp << '\n'; } 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"; }
C 20 introduces a standardized syntax for extracting these fields from a system_clock::time_point:
<code class="cpp">#include <chrono> int main() { using namespace std::chrono; 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 can I extract temporal components from a `std::chrono::time_point` object?. For more information, please follow other related articles on the PHP Chinese website!