Names specified here
Name Description Notes Source Availability
asctime() Convert time to string (·) <time.h> C89 C90 C95 C99 C11
asctime_s() Convert time to string (·) <time.h> C11
ctime() Convert time to string (·) <time.h> C89 C90 C95 C99 C11
ctime_s() Convert time to string (·) <time.h> C11
strftime() Convert time to string (·) <time.h> C89 C90 C95 C99 C11
wcsftime() Convert time to string (·) <wchar.h> C95 C99 C11
#include <time.h>
size_t strftime(char *s,
                size_t slen,
                const char *fmt,
                const struct tm *tp); 
#include <wchar.h>
size_t wcsftime(wchar_t *s,
                size_t slen,
                const wchar_t *fmt,
                const struct tm *tp); 

The functions strftime and wcsftime take a null-terminated string fmt, and write it to an array of slen bytes or wide characters at s, converting the following sequences to textual representations of the fields in *tp:

Sequence Field of struct tm Meaning Range/example
%a tm_wday abbreviated weekday name Mon
%A tm_wday full weekday name Monday
%b tm_mon abbreviated month name Jan
%B tm_mon full month name January
%c all local date and time representation
%C tm_year the year divided by 100 0099
%d tm_mday day of the month 0131
%D tm_year, tm_mon, tm_mday same as %m/%d/%Y
%e tm_mday day of the month 131
%F tm_year, tm_mon, tm_mday ISO 8601 date, same as %Y-%m-%d
%g tm_year, tm_wday, tm_yday last two digits of the week-based year 0099
%G tm_year, tm_wday, tm_yday the week-based year
%h tm_mon abbreviated month name Jan
%H tm_hour hour (24-hour clock) 0023
%I tm_hour hour (12-hour clock) 0112
%j tm_yday day of year 001366
%m tm_mon month number 0112
%M tm_min minute 0059
%n the newline character
%p tm_hour morning/afternoon designator am
%r tm_hour, tm_min, tm_sec local 12-hour clock time
%R tm_hour, tm_min ISO 8601 time without seconds, same as %H:%M 14:34
%S tm_sec seconds 0060
%t the horizontal-tab character
%T tm_hour, tm_min, tm_sec ISO 8601 time, same as %H:%M:%S 14:34:49
%u tm_wday weekday number (Monday is 1) 17
%U tm_year, tm_wday, tm_yday week number of the year (with the first Sunday as the first day of week 1) 0053
%V tm_year, tm_wday, tm_yday ISO 8601 week number 0153
%w tm_wday weekday number (Sunday is 0) 06
%W tm_year, tm_wday, tm_yday week number of the year (with the first Monday as the first day of week 1) 0053
%x all local date representation
%X all local time representation
%y tm_year year without century 0099
%Y tm_year year with century 1992
%z tm_isdst timezone offset from UTC -0100
%Z tm_isdst timezone name, if any GMT
%% a literal %

The output is affected by the locale category LC_TIME. The functions return the number of bytes or characters written to the array at s, excluding the terminating null character. If the array is too small, the functions return zero.

#include <time.h>
char *asctime(const struct tm *tp);
#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>
errno_t asctime_s(char *buf,
                  rsize_t buflen,
                  const struct tm *tp);

The functions asctime and asctime_s convert a structural time *tp into a null-terminated string of the form Sun Sep 16 01:02:33 1973\n. asctime stores this in an internal buffer, and returns a pointer to its start, while asctime_s writes the string into the array of buflen bytes starting at buf. It returns zero on success, and non-zero on failure (also setting buf[0] to '\0' if buflen is at least 1).

#include <time.h>
char *ctime(const time_t *tp);
#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>
errno_t ctime_s(char *buf,
                rsize_t buflen,
                const time_t *tp);

The function ctime is equivalent to:

asctime(localtime(*tp));

The function ctime_s is equivalent to:

struct tm tmp;
asctime_s(buf, buflen, localtime_s(*tp, &tmp));

There is no standard function to convert from a textual time to a struct tm. However, a function called strptime exists on many systems for this purpose.

HTTP times

HTTP messages often carry dates and timestamps. The following formats are permitted, expressed for compatibility with strftime and wcsftime:

  • %a, %d %b %Y %H:%M:%S %Z (recommended)
  • %A, %d-%b-%y %H:%M:%S %Z
  • %a %b %d %H:%M:%S %Y

Use the C locale when performing conversions. [Right?]


CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2022-06-17T21:43:05.000+0000