strftime

Converts the specified date and time into a formatted string.

Library:LibC
Classification:ANSI
Service:Time

Syntax

  #include <time.h> 
   
  size_t strftime (
     char              *s,
     size_t             maxsize, 
     const char        *format,
     const struct tm   *localtime);
  

Parameters

s

(OUT) Points to the character array for the converted string.

maxsize

(IN) Specifies the maximum number of characters that can be placed in the array.

format

(IN) Points to the format control string. See Remarks.

localtime

(IN) Points to the tm structure to convert.

Return Values

If the number of characters to be placed into the array is less than the value specified by the maxsize parameter, the function returns the number of characters placed into the array pointed to by the s parameter (not including the null terminating character). Otherwise, it returns 0 and the contents of the array are unspecified.

Remarks

The strftime function formats the time in the localtime parameter into the array pointed to by the s parameter according to the format parameter.

Local timezone information is used.

The format parameter string is a null-terminated string. It consists of zero or more directives and ordinary characters. A directive consists of a % character followed by a character that determines the substitution that is to take place. All ordinary characters are copied unchanged into the array. No more than the number of characters specified by the maxsize parameter are placed in the array.

The following conversion specifications are supported:

Directive

Substitution

%a

Replaced by the locale’s abbreviated weekday name

%A

Replaced by the locale’s full weekday name

%b

Replaced by the locale’s abbreviated month name

%B

Replaced by the locale’s full month name

%c

Replaced by the locale’s appropriate date and time representation

%C

Replaced by the century number (the year divided by 100 and trucated to an integer) as a decimal number from 00 through 99.

%d

Replaced by the day of the month as a decimal number (01-31)

%D

Replaced by the date in the format mm/dd/yy (POSIX)

%h

Replaced by the locale’s abbreviated month name (POSIX)

%H

Replaced by the hour (24-hour clock) as a decimal number (00-23)

%I

Replaced by the hour (12-hour clock) as a decimal number (01-12)

%j

Replaced by the day of the year as a decimal number (001-366)

%m

Replaced by the month as a decimal number (01-12)

%M

Replaced by the minute as a decimal number (00-59)

%n

Replaced by the newline character (POSIX)

%p

Replaced by the locale’s equivalent of either a.m. or p.m. notation

%r

Replaced by the time in A.M. and P.M. notation

%R

Replaced by time in 24-hour notation.

%S

Replaced by the second as a decimal number (00-59)

%t

Replaced by the tab character (POSIX)

%T

Replaced by the 24-hour clock time in the format hh:mm:ss (POSIX)

%U

Replaced by the week number of the year as a decimal number (00-53) where the first Sunday in January is the first day of the week 1

%w

Replaced by the weekday as a decimal number (0-6) where 0 is Sunday

%W

Replaced by the week number of the year as a decimal number (00-53). If the week containing January 1 has four or more days of the new year, it is considered week 1. Otherwise it is the last week of the previous year, and the next week is week 1.

%x

Replaced by the locale’s appropriate date representation

%X

Replaced by the locale’s appropriate time representation

%y

Replaced by the last two digits of the year as a decimal number (00-99)

%Y

Replaced by the year as a four-digit decimal number

%z

Replaced by the offset from UTC. The value depends on the tm_isdst field in tm:

  • If positive, the daylight savings time offset is used.

  • If zero, the standard offset is used.

  • If negative, no characters are returned.

%Z

Replaced by the timezone name or abbreviation, or by no characters if no timezone exists

%%

Replaced by the % character

See Also