cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : C Library : ctime (time.h) : asctime
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
ctime (time.h)
functions:
· asctime
· clock
· ctime
· difftime
· gmtime
· localtime
· mktime
· strftime
· time
macros:
· CLOCKS_PER_SEC
· NULL
types:
· clock_t
· size_t
· time_t
· struct tm

-

asctime function
char * asctime ( const struct tm * timeptr );
<ctime>

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 the terminating null-character.

Parameters

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

Return Value

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

The array which holds this string is statically allocated and shared by both the ctime and asctime functions. Each time either one of these functions is called the content of this array is overwritten.

Example

/* asctime example */
#include <stdio.h>
#include <time.h>

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: Sat May 20 15:21:51 2000

See also

ctime Convert time_t value to string (function)
gmtime Convert time_t to tm as UTC time (function)
localtime Convert time_t to tm as local time (function)
time Get current time (function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us