Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      timespec_get

      From cppreference.com
      <c‎ |chrono
       
       
      Date and time utilities
      Functions
      Time manipulation
      timespec_get
      (C11)
      Format conversions
      (deprecated in C23)(C11)
      (deprecated in C23)(C11)
      Constants
      Types
       
      Defined in header<time.h>
      int timespec_get(struct timespec*ts,int base);
      (1)(since C11)
      #define TIME_UTC /* implementation-defined */
      (2)(since C11)
      1) Modifies thetimespec 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 oftimespec_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 typestruct 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 atimespec with the time since the Epoch.

      [edit]Example

      Run this code
      #include <stdio.h>#include <time.h> int main(void){struct timespec ts;    timespec_get(&ts, TIME_UTC);char buff[100];strftime(buff,sizeof buff,"%D %T",gmtime(&ts.tv_sec));printf("Current time: %s.%09ld UTC\n", buff, ts.tv_nsec);}

      Possible output:

      Current time: 02/18/15 14:34:03.048508855 UTC

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.27.2.5 The timespec_get function (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.27.2.5 The timespec_get function (p: 286)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.27.2.5 The timespec_get function (p: 390)

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp