Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/chrono/c/mktime"

From cppreference.com
< cpp‎ | chrono‎ | c
(upd example to show the effects on isdst (and also fix TZ so that it reproduces on coliru))
m (http -> https)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{cpp/title| mktime}}
+
{{cpp/title|mktime}}
 
{{cpp/chrono/c/navbar}}
 
{{cpp/chrono/c/navbar}}
{{ddcl | header=ctime |
+
{{ddcl|header=ctime|
 
std::time_t mktime( std::tm* time );
 
std::time_t mktime( std::tm* time );
 
}}
 
}}
  
Converts local calendar time to a time since epoch as a {{c|time_t}} object. {{tt|time->tm_wday}} and {{tt|time->tm_yday}} are ignored. The values in {{tt|time}} are permitted to be outside their normal ranges.
+
Converts local calendar time to a time since epoch as a {{lc|std::time_t}} object. {{c|time->tm_wday}} and {{c|time->tm_yday}} are ignored. The values in {{c|time}} are permitted to be outside their normal ranges.
  
A negative value of {{tt|time->tm_isdst}} causes {{tt|mktime}} to attempt to determine if Daylight Saving Time was in effect.
+
A negative value of {{c|time->tm_isdst}} causes {{tt|mktime}} to attempt to determine if Daylight Saving Time was in effect.
  
If the conversion is successful, the {{tt|time}} object is modified. All fields of {{tt|time}} are updated to fit their proper ranges. {{tt|time->tm_wday}} and {{tt|time->tm_yday}} are recalculated using information available in other fields.
+
If the conversion is successful, the {{c|time}} object is modified. All fields of {{c|time}} are updated to fit their proper ranges. {{c|time->tm_wday}} and {{c|time->tm_yday}} are recalculated using information available in other fields.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | time | pointer to a {{lc|std::tm}} object specifying local calendar time to convert}}
+
{{par|time|pointer to a {{lc|std::tm}} object specifying local calendar time to convert}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
Time since epoch as a {{lc|std::time_t}} object on success or {{c|-1}} if {{tt|time}} cannot be represented as a {{lc|std::time_t}} object.
+
Time since epoch as a {{lc|std::time_t}} object on success or {{c|-1}} if {{c|time}} cannot be represented as a {{lc|std::time_t}} object.
  
 
===Notes===
 
===Notes===
If the {{lc|std::tm}} object was obtained from {{lc|std::get_time}} or the POSIX [http://pubs.opengroup.org/onlinepubs/009695399/functions/strptime.html strptime], the value of {{tt|tm_isdst}} is indeterminate, and needs to be set explicitly before calling {{tt|mktime}}.
+
If the {{lc|std::tm}} object was obtained from {{lc|std::get_time}} or the POSIX [https://pubs.opengroup.org/onlinepubs/009695399/functions/strptime.html {{tt|strptime}}], the value of {{tt|tm_isdst}} is indeterminate, and needs to be set explicitly before calling {{tt|mktime}}.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| Display the time 100 months ago.  
+
|Construct a local time explicitly.  
| code=
+
|code=
 +
#include <ctime>
 +
#include <iomanip>
 
#include <iostream>
 
#include <iostream>
#include <iomanip>
+
#include <sstream>
#include <ctime>
+
#include <stdlib.h>
+
 
+
 
int main()
 
int main()
 
{
 
{
     setenv("TZ", "/usr/share/zoneinfo/America/New_York", 1); // POSIX-specific
+
     setenv("TZ", "/usr/share/zoneinfo/America/Los_Angeles", 1); // POSIX-specific
 
+
     std::time_t t = std::time(NULL);
+
     std::tm tm{}; // Zero initialise
     std::tm tm = *std::localtime(&t);
+
    tm.tm_year = 2020 - 1900; // 2020
     std::cout << "Today is          " << std::put_time(&tm, "%c %Z")
+
     tm.tm_mon = 2 - 1; // February
              << " and DST is " << (tm.tm_isdst ? "in effect" : "not in effect") << '\n';
+
    tm.tm_mday = 15; // 15th
     tm.tm_mon -= 100; // tm_mon is now outside its normal range
+
     tm.tm_hour = 10;
     std::mktime(&tm); // tm_dst is not set to -1; today's DST status is used
+
    tm.tm_min = 15;
     std::cout << "100 months ago was " << std::put_time(&tm, "%c %Z")
+
     tm.tm_isdst = 0; // Not daylight saving
              << " and DST was " << (tm.tm_isdst ? "in effect" : "not in effect") << '\n';
+
     std::time_t t = std::mktime(&tm);  
 +
    std::tm local = *std::localtime(&t);
 +
 +
     std::cout << "local: " << std::put_time(&local, "%c %Z") << '\n';
 
}
 
}
| output=
+
|p=true
Today is          Fri Apr 22 11:40:36 2016 EDT and DST is in effect
+
|output=
100 months ago was Sat Dec 22 10:40:36 2007 EST and DST was not in effect
+
local: Sat Feb 15 10:15:00 2020 PST
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/chrono/c/dsc localtime}}
+
{{dsc inc|cpp/chrono/c/dsc localtime}}
{{dsc see c | c/chrono/mktime}}
+
{{dsc see c|c/chrono/mktime}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/chrono/c/mktime]]
+
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}
[[es:cpp/chrono/c/mktime]]
+
[[fr:cpp/chrono/c/mktime]]
+
[[it:cpp/chrono/c/mktime]]
+
[[ja:cpp/chrono/c/mktime]]
+
[[pl:cpp/chrono/c/mktime]]
+
[[pt:cpp/chrono/c/mktime]]
+
[[ru:cpp/chrono/c/mktime]]
+
[[zh:cpp/chrono/c/mktime]]
+

Latest revision as of 13:45, 9 September 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
C-style date and time utilities
Functions
Time manipulation
Format conversions
mktime
Constants
Types
(C++17)
 
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 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.

Contents

[edit] Parameters

time - pointer to a std::tm object specifying local calendar time to convert

[edit] 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.

[edit] 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.

[edit] Example

Construct a local time explicitly.

#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

[edit] See also

converts time since epoch to calendar time expressed as local time
(function) [edit]
C documentation for mktime