Previous Page
Next Page


Converts an integer time value into a date and time string

#include <time.h>
char *ctime ( const time_t *seconds  );

The argument passed to the ctime( ) function is a pointer to a number interpreted as a number of seconds elapsed since the epoch (on Unix systems, January 1, 1970).

The function converts this value into a human-readable character string showing the local date and time, and returns a pointer to that string. The string is exactly 26 bytes long, including the terminating null character, and has the following format:

Thu Apr 28 15:50:56 2005\n

The argument's type, time_t, is defined in time.h, usually as a long or unsigned long integer.

The function call ctime(&seconds) is equivalent to asctime(localtime(&seconds)). A common way to obtain the argument value passed to ctime( ) is by calling the time( ) function, which returns the current time in seconds.


void logerror(int errorcode)
  time_t eventtime;

  fprintf( stderr, "%s: Error number %d occurred.\n",
           ctime(&eventtime), errorcode );

This code produces output like the following:

Fri Sep  9 14:58:03 2005
: Error number 23 occurred.

The output contains a line break because the string produced by ctime( ) ends in a newline character.

See Also

asctime( ), difftime( ), gmtime( ), localtime( ), mktime( ), strftime( ), time( )

Previous Page
Next Page