asctime_r() — Convert Time to Character String (Restartable)

Format

#include <time.h>
char *asctime_r(const struct tm *tm, char *buf);

Language Level: XPG4

Threadsafe: Yes.

Description

This function is the restartable version of the asctime() function.

The asctime_r() function converts time, stored as a structure pointed to by tm, to a character string. You can obtain the tm value from a call to gmtime_r(), gmtime64_r(), localtime_r(), or localtime64_r().

The string result that asctime_r() 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_r() 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_r() function returns a pointer to the resulting character string. If the function is unsuccessful, it returns NULL.

Example that uses asctime_r()

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

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

Related Information



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