char *fdate(buffer,format,tm)
char *buffer, *format;
struct tm *tm;
The percent sign indicates that a field of the tm structure is to be inserted into the result at that point. The fields are named as follows:
Simply naming the field will insert it in the result. Thus,
will result in
on Tuesday. Of course, it would be better if the weekday was indicated with a capital letter. The alphabetic fields are capitalised in the same manner as the field was named, so %Wday inserts Tuesday and %WDAY inserts TUESDAY.
You can also specify variable formats, allowing the format of the result to vary depending on the date/time being formatted. This is done by surrounding part of the format string in brackets or braces. If a format string is enclosed in square brackets, then the entire string will produce no result if any field enclosed within the brackets itself failed to produce a result. A field will fail to produce a result if it is unspecified in the date and cannot be computed from the date (e.g. when the time is not specified).
Thus, for example, in the format string,
the result will be simply
if either seconds or minutes are undefined. Otherwise, it will result in something like
Square brackets function almost identically, except that the entire string is not inserted only if all of the fields are undefined. Thus,
will result in
if both seconds and minutes are undefined, but will otherwise insert the middle part as well.
Within brackets, alternatives can be specified by separating them with vertical bars ("|"). The first alternative to succeed is the one that is used.
While %field generally gives the value of the field, %?field may be used to query the nature of the field; if it has a reasonable default value, then the field fails. Reasonable defaults are zero for the minutes and seconds, and the current year.
Additionally, a numeric field width may be specified between the % and the field name. If the field width begins with a zero, the field will be inserted with leading zeroes. For the hour field, a leading zero also serves to indicate 24-hour notation.
will result in something like
Similarly,
will result in something like
As a final, more complex example, if the format given is:
This will insert "00:00" if it is midnight, or "noon" if it is noon. Otherwise, if the seconds are defined it will insert the hour (in 24-hour format because of the field width zero), minute, and second, separated by colons. If the seconds are not defined or are equal to zero, it will insert the hour (in 12 hour format), the minutes if they are defined (omitting the relevant colon if they aren't defined), and "am" or "pm". This format, incidentally, is precisely what you can get with the rather simpler field "%time".