STRFTIME - obtain parts of time/date as string.
(ANSI Standard)
Usage:
#include <time.h>
length = strftime(string,maxlen,format,timestruct);
Where:
- char *string;
- points to an area of memory that is big enough to hold
the data produced by "strftime". Information
will be written into this area as a string.
- size_t maxlen;
- is the maximum number of characters that
"strftime" should write into
"*string". This includes the '\0' on the end of
the string.
- const char *format;
- is a format string indicating what pieces of information
"strftime" should write into
"*string". More details are given below.
- const struct tm *timestruct;
- points to a time structure as created by
"gmtime" or "localtime".
"strftime" uses this time structure as the
source of information to be written into
"*string".
- size_t length;
- is the number of characters written into
"*string" (not counting the '\0' character that
ends the string). If "strftime" fails (e.g. if
the requested string would be longer than
"maxlen" characters), the return value will be
zero, and the contents of "*string" will be
undefined.
Description:
"strftime" converts information from a time
structure to a string form, and writes the string into the memory
area pointed to by "string".
The "format" string tells what information is
required and how it should be presented. It is much like the
format string used by "printf". The format string
consists of ordinary characters (which are copied directly into
"*string") and placeholders consisting of a '%'
followed by a letter. The following placeholders are recognized:
- %a
- abbreviated weekday name (Sun, Mon, etc.)
- %A
- full weekday name (Sunday, Monday, etc.)
- %b
- abbreviated month name (Jan, Feb, etc.)
- %B
- full month name (January, February, etc.)
- %c
- full date and time string
- %d
- day of the month as two-digit decimal integer (01-31)
- %D
- date as %m/%d/%y (this is an extension to the ANSI
standard, in accordance with the POSIX.2 standard)
- %e
- day of month (1-31); single digits are preceded by a
blank (this is an extension to the ANSI standard, in
accordance with the POSIX.2 standard)
- %h
- same as %b (this is an extension to the ANSI standard, in
accordance with the POSIX.2 standard)
- %H
- hour as two-digit 24-hour clock decimal integer (00-23)
- %I
- hour as two-digit 12-hour clock decimal integer (01-12)
- %j
- day of the year as three-digit decimal integer (001-366)
- %k
- hour (0-23); single digits are preceded by a blank (this
is an extension to the ANSI standard, in accordance with
the POSIX.2 standard)
- %l
- hour (0-12); single digits are preceded by a blank (this
is an extension to the ANSI standard, in accordance with
the POSIX.2 standard)
- %m
- month as a two-digit decimal integer (01-12)
- %M
- minute as a two-digit decimal integer (00-59)
- %n
- new-line character (\n) (this is an extension to the ANSI
standard, in accordance with the POSIX.2 standard)
- %p
- either "AM" or "PM"
- %r
- time expressed as %I:%M:%S %p (this is an extension to
the ANSI standard, in accordance with the POSIX.2
standard)
- %R
- time expressed as %H:%M (this is an extension to the ANSI
standard, in accordance with the POSIX.2 standard)
- %S
- second as a two-digit decimal integer (00-59)
- %t
- horizontal tab (\t) (this is an extension to the ANSI
standard, in accordance with the POSIX.2 standard)
- %T
- time expressed as %H:%M:%S (this is an extension to the
ANSI standard, in accordance with the POSIX.2 standard)
- %U
- number of week in the year as two-digit decimal integer
(00-52) with Sunday considered as first day of the week
- %w
- weekday as one-digit decimal integer (0-6) with Sunday as
0
- %W
- number of week in the year as two-digit decimal integer
(00-52) with Monday considered as first day of the week
- %x
- full date string (no time); in the C locale, this is
equivalent to "%m/%d/%y".
- %X
- full time string (no date); in the C locale, this is
equivalent to "%H:%M:%S".
- %y
- year without century as two-digit decimal number (00-99)
- %Y
- year with century as four-digit decimal number
- %Z
- time zone name (e.g. EST); null string if no time zone
can be obtained
- %%
- stands for '%' character in output string.
Examples:
#include <time.h>
char s[30];
size_t i;
struct tm tim;
time_t now;
now = time(NULL);
tim = *(localtime(&now));
i = strftime(s,30,"%b %d, %Y; %H:%M:%S\n",&tim);
obtains the current date and time and puts it into
"s" in a format like
Jan 10, 1987; 17:55:55\n
The variable "i" is assigned 23 (the number of
characters, without the '\0' at the end of the string).
See Also:
expl c lib localtime
expl c lib time
expl c lib printf
Copyright © 1996, Thinkage Ltd.