Home > Backend Development > C++ > How can I extract temporal components from a `std::chrono::time_point` object?

How can I extract temporal components from a `std::chrono::time_point` object?

Susan Sarandon
Release: 2024-11-02 18:58:31
Original
454 people have browsed it

How can I extract temporal components from a `std::chrono::time_point` object?

Extracting Temporal Components from std::chrono::time_point

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>
Copy after login

The next step is converting it to a time_t using:

<code class="cpp">time_t tt = system_clock::to_time_t(now);</code>
Copy after login

Utilize the C library to convert time_t to tm, ensuring the right time zone is chosen:

<code class="cpp">tm utc_tm = *gmtime(&amp;tt);
tm local_tm = *localtime(&amp;tt);</code>
Copy after login

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';
Copy after login

Extracting System Clock Components

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(&amp;tt);
  tm local_tm = *localtime(&amp;tt);
  std::cout << utc_tm.tm_year + 1900 <&lt '-';
  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>
Copy after login

Using a Custom Duration

To model days, create a custom duration:

<code class="cpp">typedef duration<int, ratio_multiply<hours::period, ratio<24> >::type> days;</code>
Copy after login

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>
Copy after login

Truncate it to days and subtract that:

<code class="cpp">days d = duration_cast<days>(tp);
tp -= d;</code>
Copy after login

Repeat this process for hours, minutes, and seconds.

The remainder is the fraction of a second in the units of system_clock::duration.

Using a Date Library

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";
}
Copy after login

C 20 Standard Library Support

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template