Namespaces
Variants
Actions

std::time

From cppreference.com
< cpp‎ | chrono‎ | c
 
C++
 
Date and time library
Time point
(C++11)
(C++20)
Duration
(C++11)
Clocks
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Time of day
(C++20)(C++20)
(C++20)(C++20)
(C++20)
Calendar
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
chrono I/O
(C++20)

 
C-style date and time utilities
Functions
Time manipulation
time
Format conversions
Constants
Types
(C++17)
 
Defined in header <ctime>
std::time_t time( std::time_t* arg );

Returns the current calendar time encoded as a std::time_t object, and also stores it in the object pointed to by arg, unless arg is a null pointer.

Contents

[edit] Parameters

arg - pointer to a std::time_t object to store the time, or a null pointer

[edit] Return value

Current calendar time encoded as std::time_t object on success, (std::time_t)(-1) on error. If arg is not null, the return value is also stored in the object pointed to by arg.

[edit] Notes

The encoding of calendar time in std::time_t is unspecified, but most systems conform to the 2038.

[edit] Example

#include <ctime>
#include <iostream>
 
int main()
{
    std::time_t result = std::time(nullptr);
    std::cout << std::asctime(std::localtime(&result))
              << result << " seconds since the Epoch\n";
}

Possible output:

Wed Sep 21 10:27:52 2011
1316615272 seconds since the Epoch

[edit] See also

returns the calendar time in seconds and nanoseconds based on a given time base
(function) [edit]
converts time since epoch to calendar time expressed as local time
(function) [edit]
converts time since epoch to calendar time expressed as Universal Coordinated Time
(function) [edit]
wall clock time from the system-wide realtime clock
(class) [edit]

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant