std::mktime
Defined in header <ctime>
|
||
std::time_t mktime( std::tm* time ); |
||
Converts local calendar time to a time since epoch as a time_t object. time->tm_wday
and time->tm_yday
are ignored. The values in time
are permitted to be outside their normal ranges.
A negative value of time->tm_isdst
causes mktime
to attempt to determine if Daylight Saving Time was in effect.
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
are recalculated using information available in other fields.
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 object.
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
Display the time 100 months ago.
#include <iostream> #include <iomanip> #include <ctime> #include <stdlib.h> int main() { setenv("TZ", "/usr/share/zoneinfo/America/New_York", 1); // POSIX-specific std::time_t t = std::time(NULL); std::tm tm = *std::localtime(&t); std::cout << "Today is " << std::put_time(&tm, "%c %Z") << " and DST is " << (tm.tm_isdst ? "in effect" : "not in effect") << '\n'; tm.tm_mon -= 100; // tm_mon is now outside its normal range std::mktime(&tm); // tm_dst is not set to -1; today's DST status is used std::cout << "100 months ago was " << std::put_time(&tm, "%c %Z") << " and DST was " << (tm.tm_isdst ? "in effect" : "not in effect") << '\n'; }
Output:
Today is Fri Apr 22 11:40:36 2016 EDT and DST is in effect 100 months ago was Sat Dec 22 10:40:36 2007 EST and DST was not in effect
See also
converts time since epoch to calendar time expressed as local time (function) | |
C documentation for mktime
|