Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::chrono::time_point

      From cppreference.com
      <cpp‎ |chrono
       
       
      Date and time library
      Time point
      time_point
      (C++11)
      (C++20)
      Duration
      (C++11)
      Clocks
      (C++20)
      (C++20)
      (C++20)
      (C++20)
      (C++20)
      (C++20)
      Time of day
      (C++20)(C++20)
      (C++20)(C++20)
      (C++20)
       
       
      Defined in header<chrono>
      template<

         class Clock,
         class Duration=typename Clock::duration

      >class time_point;
      (since C++11)

      Class templatestd::chrono::time_point represents a point in time. It is implemented as if it stores a value of typeDuration indicating the time interval from the start of theClock's epoch.

      Clock must meet the requirements forClockor bestd::chrono::local_t(since C++20).

      (until C++23)

      Contents

      [edit]Member types

      Type Description
      Clockclock
      the clock on which this time point is measured
      (typedef)
      Durationduration
      astd::chrono::duration type used to measure the time since epoch
      (typedef)
      duration::reprep
      an arithmetic type representing the number of ticks of the duration
      (typedef)
      duration::periodperiod
      astd::ratio type representing the tick period of the duration
      (typedef)

      [edit]Member functions

      constructs a new time point
      (public member function)[edit]
      returns the time point as duration since the start of its clock
      (public member function)[edit]
      modifies the time point by the given duration
      (public member function)[edit]
      increments or decrements the duration
      (public member function)[edit]
      [static]
      returns the time point corresponding to the smallest duration
      (public static member function)[edit]
      [static]
      returns the time point corresponding to the largest duration
      (public static member function)[edit]

      [edit]Non-member functions

      performs add and subtract operations involving a time point
      (function template)[edit]
      (C++11)(C++11)(removed in C++20)(C++11)(C++11)(C++11)(C++11)(C++20)
      compares two time points
      (function template)[edit]
      converts a time point to another time point on the same clock, with a different duration
      (function template)[edit]
      converts a time_point to another, rounding down
      (function template)[edit]
      converts a time_point to another, rounding up
      (function template)[edit]
      converts a time_point to another, rounding to nearest, ties to even
      (function template)[edit]

      [edit]Helper classes

      specializes thestd::common_type trait
      (class template specialization)[edit]
      hash support forstd::chrono::time_point
      (class template specialization)

      [edit]Example

      Run this code
      #include <algorithm>#include <chrono>#include <ctime>#include <iomanip>#include <iostream> void slow_motion(){staticint a[]{1,2,3,4,5,6,7,8,9,10,11,12};// Generate Γ(13) == 12! permutations:while(std::ranges::next_permutation(a).found){}} int main(){usingnamespace std::literals;// enables literal suffixes, e.g. 24h, 1ms, 1s. const std::chrono::time_point<std::chrono::system_clock> now=std::chrono::system_clock::now(); conststd::time_t t_c=std::chrono::system_clock::to_time_t(now- 24h);std::cout<<"24 hours ago, the time was "<<std::put_time(std::localtime(&t_c),"%F %T.\n")<<std::flush; const std::chrono::time_point<std::chrono::steady_clock> start=std::chrono::steady_clock::now(); std::cout<<"Different clocks are not comparable:\n""  System time: "<< now.time_since_epoch()<<"\n""  Steady time: "<< start.time_since_epoch()<<'\n';     slow_motion(); constauto end=std::chrono::steady_clock::now();std::cout<<"Slow calculations took "<<std::chrono::duration_cast<std::chrono::microseconds>(end- start)<<" ≈ "<<(end- start)/ 1ms<<"ms ≈ "// almost equivalent form of the above, but<<(end- start)/ 1s<<"s.\n";// using milliseconds and seconds accordingly}

      Possible output:

      24 hours ago, the time was 2021-02-15 18:28:52.Different clocks are not comparable:  System time: 1666497022681282572ns  Steady time: 413668317434475nsSlow calculations took 2090448µs ≈ 2090ms ≈ 2s.

      [edit]See also

      (C++11)
      a time interval
      (class template)[edit]
      represents a specificyear,month, andday
      (class)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/time_point&oldid=182528"

      [8]ページ先頭

      ©2009-2025 Movatter.jp