Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::timespec_get

      From cppreference.com
      <cpp‎ |chrono‎ |c
       
       
      Date and time library
       
       
      Defined in header<ctime>
      int timespec_get(std::timespec* ts,int base);
      (1)(since C++17)
      #define TIME_UTC /* implementation-defined */
      (2)(since C++17)
      1) Modifies thestd::timespec object pointed to byts to hold the current calendar time in the time basebase.
      2) Expands to a value suitable for use as thebase argument ofstd::timespec_get.

      Other macro constants beginning withTIME_ may be provided by the implementation to indicate additional time bases.

      Ifbase isTIME_UTC, then

      • ts->tv_sec is set to the number of seconds since an implementation defined epoch, truncated to a whole value,
      • ts->tv_nsec member is set to the integral number of nanoseconds, rounded to the resolution of the system clock.

      Contents

      [edit]Parameters

      ts - pointer to an object of typestd::timespec
      base -TIME_UTC or another nonzero integer value indicating the time base

      [edit]Return value

      The value ofbase if successful, zero otherwise.

      [edit]Notes

      The POSIX functionclock_gettime(CLOCK_REALTIME, ts) may also be used to populate astd::timespec with the time since the Epoch.

      [edit]Example

      Run this code
      #include <ctime>#include <iostream> int main(){std::timespec ts;    std::timespec_get(&ts, TIME_UTC);char buf[100];std::strftime(buf, sizeof buf,"%D %T",std::gmtime(&ts.tv_sec));std::cout<<"Current time: "<< buf<<'.'<< ts.tv_nsec<<" UTC\n";}

      Possible output:

      Current time: 06/24/16 20:07:42.949494132 UTC

      [edit]See also

      (C++17)
      time in seconds and nanoseconds
      (struct)[edit]
      returns the current time of the system as time since epoch
      (function)[edit]
      C documentation fortimespec_get
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/c/timespec_get&oldid=158570"

      [8]ページ先頭

      ©2009-2025 Movatter.jp