Formats a tm structure.
#include <time.h> size_t strftime(char *s, size_t max, const char *format, const struct tm *ts);
b
A pointer to a character string in which to store the formatted text.
max
The maximum number of characters that may be stored at s.
format
A pointer to a character string that describes how to format the text.
ts
A pointer to time structure to convert to text.
This function converts ts to a null-terminated character array, s, using the format specified by format. The number of characters stored in the formatted string will not exceed the number specified by max.
The format argument points to a character array containing normal text and conversion specifications similar to the format string used by the snprintf() function. Conversion specifiers for date and time values are prefixed with a percent sign ( %). Doubling the percent sign ( %%) will output a single %.
If any of the specified values are outside the normal range, the characters stored are unspecified.
A conversion specifier that has a E prefix specifies that the resulting text should use the locale's alternate textual representation. The O prefix specifies the locale's alternate numeric symbols. In the "C" locale, the E and O modifiers are ignored. Also, some of the formats are dependent on the LC_TIME component of the current locale.
The strftime() function returns the total number of characters that were stored in s if the total number of characters, including the null character, is less than the value of max. If the formatted string cannot fit in s, strftime() returns 0.
| Conversion Specifier | Generates |
|---|---|
| a | Locale's abbreviated weekday name. |
| A | Locale's full weekday name. |
| b | Locale's abbreviated month name. |
| B | Locale's full month name. |
| c | Equivalent to " %A %B %d %T %Y". |
| C | The year divided by 100 and truncated to an integer, as a two-digit decimal number from 00 to 99. |
| d | Day of the month as a 2-digit decimal number from 01 to 31. |
| D | Equivalent to " %m/%d/%y". |
| e | The day of the month as a decimal number. Single digit values are preceded by a space character. |
| F | Equivalent to " %Y-%m-%d". |
| g | The last 2 digits of the week-based year (as described by the ISO/IEC 8601 standard). |
| G | The week-based year (as described by the ISO/IEC 8601 standard). |
| h | Equivalent to " %b". |
| H | The hour of the 24-hour clock as a 2-digit decimal number from 00 to 23. |
| I | The hour of the 12-hour clock as a 2-digit decimal number from 01 to 12. |
| j | The day of the year as a 3-digit decimal number from 001 to 366. |
| m | The month as a 2-digit decimal numberfrom 01 to 12. |
| M | The minute as a 2-digit decimal numberfrom 00 to 59. |
| n | Newline character. |
| p | Locale's representation of the AM or PM designation for a 12-hour clock. |
| r | Locale's 12-hour clock time. |
| R | Equivalent to "%H:%M". |
| S | The second as a 2-digit decimal number from 00 to 60. |
| t | Horizontal tab. |
| T | Equivalent to "%H:%M:%S". |
| u | The weekday as a single-digit decimal number from 1 to 7. |
| U | The week number of the year as a 2-digit decimal number from 00 to 53. Sunday is considered the first day of the week. |
| w | The weekday as a single-digit decimal number from 0 to 6. Sunday is the first day of the week. |
| W | The week of the year as a 2-digit decimal number from 00 to 51. Monday is the first day of the week. |
| x | Equivalent to " %A %B %d %Y" |
| X | Equivalent " %T" |
| y | The last two digits of the year as a decimal number. |
| Y | The year as a 4-digit number. |
| z | The time zone offset from UTC or the empty string if the time zone is unknown. |
| Z | The locale's time zone name, its abbreviation, or the empty string if the time zone is unknown. |
This facility may not be available on some configurations of the EWL.
#include <time.h> #include <stdio.h> #include <string.h> #define MAXTS 1000 int main(void) { time_t lclTime; struct tm *now; char ts[MAXTS]; size_t result; lclTime = time(NULL); now = localtime(&lclTime); result = strftime(ts, MAXTS, "Today's abbreviated name is %a", now); if (result == 0) exit(1); puts(ts); result = strftime(ts, MAXTS, "Today's full name is %A", now); if (result == 0) exit(1); puts(ts); return 0; } Output: Today's abbreviated name is Wed Today's full name is Wednesday