asctime() — Convert Time to Character String

Format

#include <time.h>
char *asctime(const struct tm *time);

Language Level: ANSI

Threadsafe: No. Use asctime_r() instead.

Description

The asctime() function converts time, stored as a structure pointed to by time, to a character string. You can obtain the time value from a call to the gmtime(), gmtime64(), localtime(), or localtime64() function.

The string result that asctime() produces contains exactly 26 characters and has the format:

   "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n"

The following are examples of the string returned:

   Sat Jul 16 02:03:55 1994\n\0
or
   Sat Jul 16  2:03:55 1994\n\0

The asctime() function uses a 24-hour-clock format. The days are abbreviated to: Sun, Mon, Tue, Wed, Thu, Fri, and Sat. The months are abbreviated to: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, and Dec. All fields have constant width. Dates with only one digit are preceded either with a zero or a blank space. The new-line character (\n) and the null character (\0) occupy the last two positions of the string.

The time and date functions begin at 00:00:00 Universal Time, January 1, 1970.

Return Value

The asctime() function returns a pointer to the resulting character string. If the function is unsuccessful, it returns NULL.

Note:
The asctime(), ctime() functions, and other time functions can use a common, statically allocated buffer to hold the return string. Each call to one of these functions might destroy the result of the previous call. The asctime_r(), ctime_r(), gmtime_r(), and localtime_r() functions do not use a common, statically allocated buffer to hold the return string. These functions can be used in place of the asctime(), ctime(), gmtime(), and localtime() functions if reentrancy is desired.

Example that uses asctime()

This example polls the system clock and prints a message that gives the current time.

#include <time.h>
#include <stdio.h>
 
int main(void)
{
    struct tm *newtime;
    time_t ltime;
 
/* Get the time in seconds */
    time(&ltime);
/* Convert it to the structure tm */
    newtime = localtime(&ltime);
 
        /* Print the local time as a string */
    printf("The current date and time are %s",
             asctime(newtime));
}
 
/****************  Output should be similar to:  ******************
The current date and time are Fri Sep 16 13:29:51 1994
*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]