function
<ctime>
localtime
struct tm * localtime (const time_t * timer);
Convert time_t to tm as local time
Uses the value pointed by timer to fill a tm structure with the values that represent the corresponding time, expressed for the local timezone.
Parameters
- timer
- Pointer to an object of type time_t that contains a time value.
time_t is an alias of a fundamental arithmetic type capable of representing times as returned by function time.
Return Value
A pointer to a tm structure with its members filled with the values that correspond to the local time representation of timer.
The returned value points to an internal object whose validity or value may be altered by any subsequent call to gmtime or localtime.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
/* localtime example */
#include <stdio.h> /* puts, printf */
#include <time.h> /* time_t, struct tm, time, localtime */
int main ()
{
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
printf ("Current local time and date: %s", asctime(timeinfo));
return 0;
}
| |
Output:
Current local time and date: Wed Feb 13 17:17:11 2013
|
Data races
The function accesses the object pointed by timer.
The function also accesses and modifies a shared internal object, which may introduce data races on concurrent calls to gmtime and localtime. Some libraries provide an alternative function that avoids this data race: localtime_r (non-portable).
Exceptions (C++)
No-throw guarantee: this function never throws exceptions.
See also
- asctime
- Convert tm structure to string (function
)
- ctime
- Convert time_t value to string (function
)
- gmtime
- Convert time_t to tm as UTC time (function
)
- mktime
- Convert tm structure to time_t (function
)
- time
- Get current time (function
)