##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# time

From cppreference.com
< c‎ | chrono

Defined in header `<time.h>` |  |
time_t time( time_t *time ); |  |

Returns the current calendar time encoded as a time_t object.

### [edit] Parameters

time | - | pointer to a time_t object to store the time in or NULL

### [edit] Return value

Current calendar time encoded as time_t object on success, (time_t)(-1) on error. If the argument is not NULL, the return value is equal to the value stored in the object pointed to by the argument.

### [edit] Notes

The encoding of calendar time in time_t is unspecified, but most systems conform to POSIX specification and return a value of integral type holding the number of seconds since the Epoch. Implementations in which time_t is a 32-bit signed integer (many historical implementations) fail in the year 2038.

### [edit] Example

Run this code

    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>

    int main(void)
    {
        time_t result = time(NULL);
        if (result == ((time_t)-1))
        {
           fprintf(stderr,"time() failed in file %s at line # %d\n", __FILE__,__LINE__-3);
           exit(EXIT_FAILURE);
        }

        printf("%s", asctime(localtime(&result)));
        printf("%d seconds since the Epoch", (int)result);
        return EXIT_SUCCESS;
    }

Possible output:

    Wed Oct  9 10:49:31 2013
    1381315771 seconds since the Epoch

### [edit] See also

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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/chrono/time&oldid=71641"