std::mktime(3)
π NAME π
std::mktime - std::mktime β okay bestie, this is the std::mktime(3) page from the C++ Standard Libary manual. The name is literally std::mktime - std::mktime, self-titled icon behavior, no cap. π
β¨ Synopsis β¨
First things first: she's defined in header <ctime> π. Here's the signature, exact:
std::time_t mktime( std::tm* time );
She converts local calendar time to a time since epoch as a std::time_t object. Obsessed with epochs, honestly.
time->tm_wday and time->tm_yday are ignored. The values in time are permitted to be outside their normal ranges. Totally allowed to be a little unhinged. π
A negative value of time->tm_isdst causes mktime to attempt to determine if Daylight Saving Time was in effect. She's doing her detective era π΅οΈββοΈ.
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. Glow-up, but make it calendar math. β
π«Ά Parameters π«Ά
timeβ pointer to astd::tmobject specifying local calendar time to convert. That's the whole parameter, bestie, she only needs one. π«Ά
π 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. So either you get the epoch glow or a sad little -1. π
π§ 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.
Real talk: indeterminatetm_isdstis not the vibe. Set it explicitly before you callmktime, bestie. π¨
πΈ Example πΈ
Goal: construct a local time explicitly. Cute, direct, we love a purposeful example. π
// Run this code β yes, run it, bestie πββοΈπ¨
#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 π¦
localtimeβ converts time since epoch to calendar time expressed as local time. It's a function. π°οΈ- C documentation for
mktimeβ the C edition, for when you need the samemktimeenergy but in C. π
Source/version http://cppreference.com, date 2024.06.10, page std::mktime(3). That's the whole footer vibe, bestie β mwah, go slay some epochs πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms