Share this page

# localtime

Date and Time Functions

Converts the calendar time into local time.

    struct tm *localtime( const time_t *calTime);

calTime | The name of the value for the calendar time.

Under Linux, localtime is not thread-safe. Use localtime_r, instead. Refer to your platform-specific documentation for details.

## Return Values

A pointer to a tm structure.

## Example

The following example uses localtime to convert the time_t variable, t, to a tm structure, now. It then changes the 24-hour field, tm_hour, to be 12-hour based and prints out the AM/PM time. The example then prints out details of the tm structure using strftime.

Copy code

        #include <time.h>
        time_t t;
        struct tm * now;
        char tmp[128], ampm[] = "AM";
    // Sets variables used by localtime
        _tzset();
        time(&t);
    // Convert to time structure and adjust for PM if necessary
        now = (struct tm *)localtime(&t);
        if (now->tm_hour > 12) {
            strcpy (ampm, "PM");
            now->tm_hour -= 12;
         }
    // Adjust if midnight hour
        if (now->tm_hour == 0)
            now->tm_hour = 12;
        lr_message ("12-hour time: %.8s %s", asctime(now) + 11, ampm);
    // Use strftime to build a customized time string
        strftime (tmp, 128, "Today is %A, day %d of %B in the year %Y.", now);
        lr_message (tmp);

    Example: Output:
    12-hour time: 03:31:35 PM
    Today is Sunday, day 21 of April in the year 2002.

Tips to improve your search results... VuGen Function Reference Version 26.1-26.3 Function Reference 26.1-26.3 All View Local Help Center View Online Help Center Version: