Names specified here
Name |
Description |
Notes
|
Source |
Availability |
asctime() |
Convert time to string |
|
|
|
(·) |
|
C89
|
C90
|
C95
|
C99
|
C11
|
asctime_s() |
Convert time to string |
|
|
|
(·) |
|
|
|
|
|
C11
|
ctime() |
Convert time to string |
|
|
|
(·) |
|
C89
|
C90
|
C95
|
C99
|
C11
|
ctime_s() |
Convert time to string |
|
|
|
(·) |
|
|
|
|
|
C11
|
strftime() |
Convert time to string |
|
|
|
(·) |
|
C89
|
C90
|
C95
|
C99
|
C11
|
wcsftime() |
Convert time to string |
|
|
|
(·) |
|
|
|
C95
|
C99
|
C11
|
#include
size_t strftime(char *s,
size_t slen,
const char *fmt,
const struct tm *tp);
#include
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 |
00–99 |
%d |
tm_mday |
day of the month |
01–31 |
%D |
tm_year , tm_mon , tm_mday |
same as %m/%d/%Y |
%e |
tm_mday |
day of the month |
1–31 |
%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 |
00–99 |
%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) |
00–23 |
%I |
tm_hour |
hour (12-hour clock) |
01–12 |
%j |
tm_yday |
day of year |
001–366 |
%m |
tm_mon |
month number |
01–12 |
%M |
tm_min |
minute |
00–59 |
%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 |
00–60 |
%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) |
1–7 |
%U |
tm_year , tm_wday , tm_yday |
week number of the year (with the first Sunday as the
first day of week 1) |
00–53 |
%V |
tm_year , tm_wday , tm_yday |
ISO 8601 week number |
01–53 |
%w |
tm_wday |
weekday number (Sunday is 0) |
0–6 |
%W |
tm_year , tm_wday , tm_yday |
week number of the year (with the first Monday as the
first day of week 1) |
00–53 |
%x |
all |
local date representation |
%X |
all |
local time representation |
%y |
tm_year |
year without century |
00–99 |
%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
char *asctime(const struct tm *tp);
#define __STDC_WANT_LIB_EXT1__ 1
#include
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
char *ctime(const time_t *tp);
#define __STDC_WANT_LIB_EXT1__ 1
#include
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?]