[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


strftime

Format a tm structure.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


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

Parameters for this facility are:

s  
char *  
A string to hold the formatted time  
maxsize  
size_t  
Max length of formatted string  
format  
const char *  
The format string  
timeptr  
const struct tm*  
The address of the time structure  

Remarks:

The strftime() function converts a tm structure to a character array using a programmer supplied format.

The s argument is a pointer to the array to hold the formatted time.

The maxsize argument specifies the maximum length of the formatted character array.

The timeptr argument points to a tm structure containing the calendar time to convert and format.

The format argument points to a character array containing normal text and format specifications similar to a printf() function format string. Format specifiers 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.


NOTE

Refer to "strftime() conversion characters" for a list of format specifiers.
strftime() conversion characters:

Char
Description
a  
Abbreviated weekday name.  
A  
Full weekday name.  
b  
Abbreviated month name.  
B  
Full month name.  
c  
The locale's appropriate date and time representation equivalent to the format string of "%A %B %d %T %Y".  
C  
The year divided by 100 and truncated to an integer, as a decimal number [00 - 99]  
d  
Day of the month as a decimal number [01 - 31].  
D  
The month date year, equivalent to '%m/%d/%y''  
e  
The day of the month as a decimal number; a single digit is preceded by a space.  
F  
The year, month and day separated by hyphens, the equivalent to ''%Y-%m-%d''  
g  
The last 2 digits of the week-based year as a   decimal number. For example: 03 99  
G  
The week-based year as a decimal number  
h  
The month name, equivalent to ''%b''  
H  
The hour (24-hour clock) as a decimal number from 00 to 23.  
I  
The hour (12-hour clock) as a decimal number from 01 to 12  
j  
The day of the year as a decimal number from 001 to 366  
m  
The month as a decimal number from 01 to 12.  
M  
The minute as a decimal number from 00 to 59.  
n  
A newline character  
p  
"am" or "pm".  
r  
The locale's 12-hour clock time, equivalent of "%I:%M:%S %p"  
R  
The hour, minute, equivalent to ''%H:%M  
S  
The second as a decimal number from 00 to 59.  
t  
A horizontal-tab character  
T  
The hour minute second, equivalent to ''%H:%M:%S'  
u  
The weekday as a decimal number 1 to 7, where Monday is 1.  
U  
The week number of the year as a decimal number from 00 to 53. Sunday is considered the first day of the week.  
w  
The weekday as a decimal number from 0 to 6. Sunday is (0) zero.  
W  
The week of the year as a decimal number from 00 to 51. Monday is the first day of the week.  
x  
The date representation of the current locale, equivalent to "%A %B %d %Y"  
X  
The time representation of the current locale, equivalent to "%T"  
y  
The last two digits of the year as a decimal number.  
Y  
The year as a four digit decimal number.  
z  
The time zone offset from UTC. for example, -0430 is 4 hours 30 minutes behind UTC. Or nothing if the time zone is unknown.  
Z  
The locale's time zone name or abbreviation, or by no characters if no time zone is unknown.  
%  
The percent sign is displayed.  

Return:

The strftime() function returns the total number of characters in the argument `s' if the total number of characters including the null character in the string argument `s' is less than the value of `maxlen' argument. If it is greater, strftime() returns 0.

Example of strftime() usage.:
#include <time.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
 time_t lclTime;
 struct tm *now;
 char ts[256];  /* time string */
 
 lclTime = time(NULL);
 now = localtime(&lclTime);
 
 strftime(ts, 256, 
	"Today's abr.name is %a", now);
 puts(ts);

 strftime(ts, 256, 
	"Today's full name is %A", now);
 puts(ts);

 strftime(ts, 256, 
	"Today's aabr.month name is %b", now);
 puts(ts);

 strftime(ts, 256, 
	"Today's full month name is %B", now);
 puts(ts);

 strftime(ts, 256, 
	"Today's date and time is %c",now);
 puts(ts);
 strftime(ts, 256, 
"The day of the month is %d", now);
 puts(ts);

 strftime(ts, 256, 
"The 24-hour clock hour is %H",now);
 puts(ts);

 strftime(ts, 256, 
"The 12-hour clock hour is %H", now);
 puts(ts);

 strftime(ts, 256, 
"Today's day number is %j", now);
 puts(ts);

 strftime(ts, 256, 
"Today's month number is %m", now);
 puts(ts);

 strftime(ts, 256, 
"The minute is %M", now);
 puts(ts);

 strftime(ts, 256, 
"The AM/PM is %p", now);
 puts(ts);

 strftime(ts, 256, 
"The second is %S", now);
 puts(ts);

 strftime(ts, 256, 
"The week number of the year,\
starting on a Sunday is %U", now);
 puts(ts);

 strftime(ts, 256, 
"The number of the week is %w", now);
 puts(ts);

 strftime(ts, 256, "The week number of the year,\
starting on a Monday is %W", now);
 puts(ts);

 strftime(ts, 256, "The date is %x", now);
 puts(ts);

 strftime(ts, 256, "The time is %X", now);
 puts(ts);

 strftime(ts, 256, 
	"The last two digits of the year are %y", now);
 puts(ts);

 strftime(ts, 256, "The year is %Y", now);
 puts(ts);

 strftime(ts, 256, "%Z", now);
 if (strlen(ts) == 0)
  printf("The time zone cannot be determined\n");
 else
  printf("The time zone is %s\n", ts);

	return 0;
}

Results
Today's abr.name is Wed
Today's full name is Wednesday
Today's aabr.month name is Apr
Today's full month name is April
Today's date and time is Wednesday April 05 10:50:44 2000
The day of the month is 05
The 24-hour clock hour is 10
The 12-hour clock hour is 10
Today's day number is 096
Today's month number is 04
The minute is 50
The AM/PM is am
The second is 44
The week number of the year,starting on a Sunday is 14
The number of the week is 3
The week number of the year,starting on a Monday is 14
The date is Wednesday April 05 2000
The time is 10:50:44
The last two digits of the year are 00
The year is 2000
The time zone cannot be determined


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000