function
<ctime>

asctime

char* asctime (const struct tm * timeptr);
Convert tm structure to string
Interprets the contents of the tm structure pointed by timeptr as a calendar time and converts it to a C-string containing a human-readable version of the corresponding date and time.

The returned string has the following format:

Www Mmm dd hh:mm:ss yyyy


Where Www is the weekday, Mmm the month (in letters), dd the day of the month, hh:mm:ss the time, and yyyy the year.

The string is followed by a new-line character ('\n') and terminated with a null-character.

It is defined with a behavior equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char* asctime(const struct tm *timeptr)
{
  static const char wday_name[][4] = {
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  };
  static const char mon_name[][4] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };
  static char result[26];
  sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
    wday_name[timeptr->tm_wday],
    mon_name[timeptr->tm_mon],
    timeptr->tm_mday, timeptr->tm_hour,
    timeptr->tm_min, timeptr->tm_sec,
    1900 + timeptr->tm_year);
  return result;
}

For an alternative with custom date formatting, see strftime.

Parameters

timeptr
Pointer to a tm structure that contains a calendar time broken down into its components (see struct tm).

Return Value

A C-string containing the date and time information in a human-readable format.

The returned value points to an internal array whose validity or value may be altered by any subsequent call to asctime or ctime.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* asctime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, time, localtime, asctime */

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "The current date/time is: %s", asctime (timeinfo) );

  return 0;
}

Output:

The current date/time is: Wed Feb 13 15:46:11 2013


Data races

The function accesses the object pointed by timeptr.
The function also accesses and modifies a shared internal buffer, which may cause data races on concurrent calls to asctime or ctime. Some libraries provide an alternative function that avoids this data race: asctime_r (non-portable).

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.

See also