Namespaces
Variants
Views
Actions

localtime

From cppreference.com
< c‎ | chrono
Revision as of 08:15, 17 June 2014 by Newatthis (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Defined in header <time.h>
tm* localtime( const time_t *time );

Converts given time since epoch as time_t value into calendar time, expressed in local time.

Contents

[edit] Parameters

time - pointer to a time_t object to convert

[edit] Return value

pointer to a static internal tm object on success, or NULL otherwise. The structure may be shared between gmtime, localtime, and ctime, and may be overwritten on each invocation.

[edit] Notes

This function may not be thread-safe.

POSIX requires that this function sets errno to EOVERFLOW if it fails because the argument is too large.

[edit] Example

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
 
int main(void)
{
    time_t t = time(NULL);
    printf("UTC:   %s", asctime(gmtime(&t)));
    struct tm *tmptr = localtime(&t);
    if (tmptr == NULL)
    {
       fprintf(stderr,"localtime() failed in file %s at line # %d\n", __FILE__,__LINE__-3);
       exit(EXIT_FAILURE);
    }
    printf("local: %s", asctime(tmptr));
 
    return EXIT_SUCCESS;
}

Output:

UTC:   Wed Oct  9 10:37:39 2013
local: Wed Oct  9 17:37:39 2013

[edit] See also

converts time since epoch to calendar time expressed as Coordinated Universal Time (UTC)
(function) [edit]
C++ documentation for localtime