Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::chrono::duration

      From cppreference.com
      <cpp‎ |chrono
       
       
      Date and time library
      Time point
      (C++11)
      (C++20)
      Duration
      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)
       
      std::chrono::duration
       
      Defined in header<chrono>
      template<

         class Rep,
         class Period=std::ratio<1>

      >class duration;
      (since C++11)

      Class templatestd::chrono::duration represents a time interval.

      It consists of a count of ticks of typeRep and a tick period, where the tick period is a compile-time rationalfraction representing the time in seconds from one tick to the next.

      The only data stored in aduration is a tick count of typeRep. IfRep is floating point, then theduration can represent fractions of ticks.Period is included as part of the duration's type, and is only used when converting between different durations.

      Contents

      [edit]Member types

      Member type Definition
      repRep, an arithmetic type, or a class emulating an arithmetic type, representing the number of ticks
      periodPeriod(until C++17)typename Period::type(since C++17), astd::ratio representing the tick period (i.e. the number of second's fractions per tick)

      [edit]Member functions

      constructs new duration
      (public member function)[edit]
      assigns the contents
      (public member function)[edit]
      returns the count of ticks
      (public member function)[edit]
      [static]
      returns the special duration value zero
      (public static member function)[edit]
      [static]
      returns the special duration value min
      (public static member function)[edit]
      [static]
      returns the special duration value max
      (public static member function)[edit]
      implements unary + and unary -
      (public member function)[edit]
      increments or decrements the tick count
      (public member function)[edit]
      implements compound assignment between two durations
      (public member function)[edit]

      [edit]Non-member functions

      implements arithmetic operations with durations as arguments
      (function template)[edit]
      (C++11)(C++11)(removed in C++20)(C++11)(C++11)(C++11)(C++11)(C++20)
      compares two durations
      (function template)[edit]
      converts a duration to another, with a different tick interval
      (function template)[edit]
      converts a duration to another, rounding down
      (function template)[edit]
      converts a duration to another, rounding up
      (function template)[edit]
      converts a duration to another, rounding to nearest, ties to even
      (function template)[edit]
      obtains the absolute value of the duration
      (function template)[edit]
      (C++20)
      performs stream output on aduration
      (function template)[edit]
      parses aduration from a stream according to the provided format
      (function template)[edit]

      [edit]Helper types

      A type/* intXX */ used in the table below means a signed integer type of at least XX bits.

      Type Definition
      std::chrono::nanosecondsstd::chrono::duration</* int64 */,std::nano>
      std::chrono::microsecondsstd::chrono::duration</* int55 */,std::micro>
      std::chrono::millisecondsstd::chrono::duration</* int45 */,std::milli>
      std::chrono::secondsstd::chrono::duration</* int35 */>
      std::chrono::minutesstd::chrono::duration</* int29 */,std::ratio<60>>
      std::chrono::hoursstd::chrono::duration</* int23 */,std::ratio<3600>>
      std::chrono::days(since C++20)std::chrono::duration</* int25 */,std::ratio<86400>>
      std::chrono::weeks(since C++20)std::chrono::duration</* int22 */,std::ratio<604800>>
      std::chrono::months(since C++20)std::chrono::duration</* int20 */,std::ratio<2629746>>
      std::chrono::years(since C++20)std::chrono::duration</* int17 */,std::ratio<31556952>>

      Note: each of the predefined duration types up tohours covers a range of at least ±292 years.

      Each of the predefined duration typesdays,weeks,months andyears covers a range of at least ±40000 years.years is equal to 365.2425days (the average length of a Gregorian year).months is equal to 30.436875days (exactly 1/12 ofyears).

      (since C++20)

      [edit]Helper classes

      specializes thestd::common_type trait
      (class template specialization)[edit]
      indicates that a duration is convertible to duration with different tick period
      (class template)[edit]
      constructs zero, min, and max values of a tick count of given type
      (class template)[edit]
      formatting support forduration
      (class template specialization)[edit]
      hash support forstd::chrono::duration
      (class template specialization)

      [edit]Helper specializations

      template<class Rep,class Period>

      constexprbool enable_nonlocking_formatter_optimization<chrono::duration<Rep, Period>>

         = enable_nonlocking_formatter_optimization<Rep>;
      (since C++23)

      This specialization ofstd::enable_nonlocking_formatter_optimization enables efficient implementation ofstd::print andstd::println for printing achrono::duration object when the template parameterRep enables it.

      [edit]Literals

      Defined in inline namespacestd::literals::chrono_literals
      astd::chrono::duration literal representing hours
      (function)[edit]
      astd::chrono::duration literal representing minutes
      (function)[edit]
      astd::chrono::duration literal representing seconds
      (function)[edit]
      astd::chrono::duration literal representing milliseconds
      (function)[edit]
      astd::chrono::duration literal representing microseconds
      (function)[edit]
      astd::chrono::duration literal representing nanoseconds
      (function)[edit]

      Note: the literal suffixesd andy do not refer todays andyears but today andyear, respectively.

      (since C++20)

      [edit]Notes

      The actual time interval (in seconds) that is held by a duration objectd is roughly equal tod.count()* D::period::num/ D::period::den, whereD is of typechrono::duration<> andd is an object of such type.

      Feature-test macroValueStdFeature
      __cpp_lib_chrono_udls201304L(C++14)User-defined literals for time types

      [edit]Example

      This example shows how to define several custom duration types and convert between types:

      Run this code
      #include <chrono>#include <iostream> usingnamespace std::chrono_literals; template<typename T1,typename T2>using mul=std::ratio_multiply<T1, T2>; int main(){using microfortnights= std::chrono::duration<float,        mul<mul<std::ratio<2>, std::chrono::weeks::period>,std::micro>>;using nanocenturies= std::chrono::duration<float,        mul<mul<std::hecto, std::chrono::years::period>,std::nano>>;using fps_24= std::chrono::duration<double,std::ratio<1,24>>; std::cout<<"1 second is:\n"; // integer scale conversion with no precision loss: no caststd::cout<< std::chrono::milliseconds(1s).count()<<" milliseconds\n"<< std::chrono::microseconds(1s).count()<<" microseconds\n"<< std::chrono::nanoseconds(1s).count()<<" nanoseconds\n"; // integer scale conversion with precision loss: requires a caststd::cout<<std::chrono::duration_cast<std::chrono::minutes>(1s).count()<<" minutes\n";// alternative to duration_cast:std::cout<< 1s/ 1min<<" minutes\n"; // floating-point scale conversion: no caststd::cout<< microfortnights(1s).count()<<" microfortnights\n"<< nanocenturies(1s).count()<<" nanocenturies\n"<< fps_24(1s).count()<<" frames at 24fps\n";}

      Output:

      1 second is:1000 milliseconds1000000 microseconds1000000000 nanoseconds0 minutes0 minutes0.82672 microfortnights0.316887 nanocenturies24 frames at 24fps
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/duration&oldid=179838"

      [8]ページ先頭

      ©2009-2025 Movatter.jp