std::mktime
From cppreference.com
C++
Date and time library
Time point | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Duration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Clocks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Time of day | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Calendar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Time zone | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
chrono I/O | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++20)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C-style date and time |
Defined in header <ctime>
|
||
std::time_t mktime(
std::tm
* time )
;
|
||
Converts local calendar time to a time since epoch as a std::time_t object. time->tm_wday and time->tm_yday are ignored. The values in time
A negative value of time->tm_isdst causes mktime
If the conversion is successful, the time object is modified. All fields of time are updated to fit their proper ranges. time->tm_wday and time->tm_yday
Parameters
time | - | pointer to a std::tm object specifying local calendar time to convert |
Return value
Time since epoch as a std::time_t object on success or -1 if time cannot be represented as a std::time_t
Notes
If the std::tm object was obtained from std::get_time or the POSIX strptime
, the value of tm_isdst
is indeterminate, and needs to be set explicitly before calling mktime
Example
Construct a local time explicitly.
Run this code
#include <ctime> #include <iomanip> #include <iostream> #include <sstream> int main() { setenv("TZ", "/usr/share/zoneinfo/America/Los_Angeles", 1); // POSIX-specific std::tm tm{}; // Zero initialise tm.tm_year = 2020 - 1900; // 2020 tm.tm_mon = 2 - 1; // February tm.tm_mday = 15; // 15th tm.tm_hour = 10; tm.tm_min = 15; tm.tm_isdst = 0; // Not daylight saving std::time_t t = std::mktime(&tm); std::tm local = *std::localtime(&t); std::cout << "local: " << std::put_time(&local, "%c %Z") << '\n'; }
Possible output:
local: Sat Feb 15 10:15:00 2020 PST
See also
converts time since epoch to calendar time expressed as local time (function) |
|
C documentation for mktime
|