asctime, _wasctime
Converts a tm time structure to a character string.
char *asctime( const struct tm *timeptr ); wchar_t *_wasctime( const struct tm *timeptr );
Parameter
- timeptr
- Time/date structure.
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.
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' 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 |
Requirements
| Routine | Required header | Compatibility |
|---|---|---|
| asctime | <time.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
| _wasctime | <time.h> or <wchar.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
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.
// crt_asctime.c
#include <time.h>
#include <stdio.h>
struct tm *newtime;
time_t aclock;
int main( void )
{
time( &aclock ); // Get time in seconds
newtime = localtime( &aclock ); // Convert time to struct tm form
/* Print local time as a string */
printf( "Current date and time: %s", asctime( newtime ) );
}
Sample Output
Current date and time: Sun Feb 03 11:38:58 2002
See Also
Time Management Routines | ctime | _ftime | gmtime, localtime | time | _tzset | Run-Time Routines and .NET Framework Equivalents