localtime_r() — Convert Time (Restartable)

Format

#include <time.h>
struct tm *localtime_r(const time_t *timeval, struct tm *result);

Language Level

XPG4

Threadsafe

Yes

Locale Sensitive

The behavior of this function might be affected by the LC_TOD category of the current locale.

Description

This function is the restartable version of localtime(). It is the same as localtime() except that it passes in the place to store the returned structure result.

Return Value

The localtime_r() returns a pointer to the structure result. There is no error return value.

Example

This example queries the system clock and displays the local time.
#include <time.h>
#include <stdio.h>
 
int main(void)
{
   struct tm newtime;
   time_t ltime;
   char buf[50];
 
   ltime=time(&ltime);
   localtime_r(&ltime, &newtime);
   printf("The date and time is %s", asctime_r(&newtime, buf));
}
 
/**************  If the local time is 3 p.m. February 15, 2008,  **********
*************************  the output should be:  *********************
 
The date and time is Fri Feb 15 15:00:00 2008
*/

Related Information