asctime, _wasctime
Converts a tm time structure to a character string.
char*asctime(conststructtm*timeptr);
wchar_t*_wasctime(conststructtm*timeptr);
Routine | Required Header | Compatibility |
asctime | <time.h> | ANSI, Win 95, Win NT |
_wasctime | <time.h> or <wchar.h> | Win 95, Win NT |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB | Single thread static library, retail version |
LIBCMT.LIB | Multithread static library, retail version |
MSVCRT.LIB | Import library for MSVCRT.DLL, retail version |
Return Value
asctime returns a pointer to the character string result; _wasctime returns a pointer to the wide-character string result. There is no error return value.
Parameter
timeptr
Time/date structure
Remarks
The asctime function converts a time stored as a structure to a character string. The timeptr value is usually obtained from a call to gmtime or localtime, which both return a pointer to a tm structure, defined in TIME.H.
timeptr Field | Value |
tm_hour | Hours since midnight (0 – 23) |
tm_isdst | Positive if daylight saving time is in effect; 0 if daylight saving time is not in effect; negative if status of daylight saving time is unknown. The C run-time library assumes the United States’s rules for implementing the calculation of Daylight Saving Time (DST). |
tm_mday | Day of month (1 – 31) |
tm_min | Minutes after hour (0 – 59) |
tm_mon | Month (0 – 11; January = 0) |
tm_sec | Seconds after minute (0 – 59) |
tm_wday | Day of week (0 – 6; Sunday = 0) |
tm_yday | Day of year (0 – 365; January 1 = 0) |
tm_year | Year (current year minus 1900) |
The converted character string is also adjusted according to the local time zone settings. See the time, _ftime, and localtime functions for information on configuring the local time and the _tzset function for details about defining the time zone environment and global variables.
The string result produced by asctime contains exactly 26 characters and has the form Wed Jan 02 02:03:55 1980\n\0
. A 24-hour clock is used. All fields have a constant width. The newline character and the null character occupy the last two positions of the string. asctime uses a single, statically allocated buffer to hold the return string. Each call to this function destroys the result of the previous call.
_wasctime is a wide-character version of asctime. _wasctime and asctime behave identically otherwise.
Generic-Text Routine Mapping:
TCHAR.H Routine | _UNICODE & _MBCS Not Defined | _MBCS Defined | _UNICODE Defined |
_tasctime | asctime | asctime | _wasctime |
Example
/* ASCTIME.C: This program places the system time
* in the long integer aclock, translates it into the
* structure newtime and then converts it to string
* form for output, using the asctime function.
*/
#include <time.h>
#include <stdio.h>
struct tm *newtime;
time_t aclock;
void main( void )
{
time( &aclock ); /* Get time in seconds */
newtime = localtime( &aclock ); /* Convert time to struct */
/* tm form */
/* Print local time as a string */
printf( "The current date and time are: %s", asctime( newtime ) );
}
Output
The current date and time are: Sun May 01 20:27:01 1994