Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      ctime, ctime_s

      From cppreference.com
      <c‎ |chrono
       
       
      Date and time utilities
      Functions
      Time manipulation
      Format conversions
      (deprecated in C23)(C11)
      ctimectime_s
      (deprecated in C23)(C11)
      Constants
      Types
       
      Defined in header<time.h>
      (1)
      char*                ctime(consttime_t* timer);
      (until C23)
      [[deprecated]]char* ctime(consttime_t* timer);
      (since C23)
      errno_t ctime_s(char*buf, rsize_t bufsz,consttime_t* timer);
      (2)(since C11)
      1) Converts given time since epoch to a calendar local time and then to a textual representation, as if by callingasctime(localtime(timer)) orasctime(localtime_r(timer,&(structtm){0}))(since C23).This function is deprecated and should not be used in new code.(since C23)
      2) Same as(1), except that the function is equivalent toasctime_s(buf, bufsz, localtime_s(timer,&(structtm){0})), and the following errors are detected at runtime and call the currently installedconstraint handler function:
      • buf ortimer is a null pointer
      • bufsz is less than26 or greater thanRSIZE_MAX
      As with all bounds-checked functions,ctime_s is only guaranteed to be available if__STDC_LIB_EXT1__ is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__ to the integer constant1 before including<time.h>.

      The resulting string has the following format:

      Www Mmm dd hh:mm:ss yyyy\n
      • Www - the day of the week (one ofMon,Tue,Wed,Thu,Fri,Sat,Sun).
      • Mmm - the month (one ofJan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec).
      • dd - the day of the month
      • hh - hours
      • mm - minutes
      • ss - seconds
      • yyyy - years

      The function does not support localization.

      Contents

      [edit]Parameters

      timer - pointer to atime_t object specifying the time to print
      buf - pointer to the first element of a char array of size at leastbufsz
      bufsz - max number of bytes to output, typically the size of the buffer pointed to bybuf

      [edit]Return value

      1) pointer to a static null-terminated character string holding the textual representation of date and time. The string may be shared betweenasctime andctime, and may be overwritten on each invocation of any of those functions.
      2) zero on success (in which case the string representation of time has been written out to the array pointed to bybuf), or non-zero on failure (in which case, the terminating null character is always written tobuf[0] unlessbuf is a null pointer orbufsz is zero or greater thanRSIZE_MAX.

      [edit]Notes

      ctime returns a pointer to static data and is not thread-safe. In addition, it modifies the statictm object which may be shared withgmtime andlocaltime. POSIX marks this function obsolete and recommendsstrftime instead. The C standard also recommendsstrftime instead ofctime andctime_s becausestrftime is more flexible and locale-sensitive.

      The behavior ofctime is undefined for the values oftime_t that result in the string longer than 25 characters (e.g. year 10000).

      [edit]Example

      Run this code
      #define __STDC_WANT_LIB_EXT1__ 1#include <time.h>#include <stdio.h> int main(void){time_t result=time(NULL);printf("%s", ctime(&result)); #ifdef __STDC_LIB_EXT1__char str[26];    ctime_s(str,sizeof str,&result);printf("%s", str);#endif}

      Possible output:

      Tue May 26 21:51:03 2015Tue May 26 21:51:03 2015

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 7.27.3.2 The ctime function (p: 287-288)
      • K.3.8.2.2 The ctime_s function (p: 454)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.27.3.2 The ctime function (p: 393)
      • K.3.8.2.2 The ctime_s function (p: 626)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.23.3.2 The ctime function (p: 342)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.12.3.2 The ctime function

      [edit]See also

      (deprecated in C23)(C11)
      converts atm object to a textual representation
      (function)[edit]
      converts atm object to custom textual representation
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/chrono/ctime&oldid=142715"

      [8]ページ先頭

      ©2009-2025 Movatter.jp