std::gmtime(3)
π std::gmtime(3) β C++ Standard Library, bestie edition β¨
okay so we're doing std::gmtime today, from the <ctime> header, and honestly? she's iconic. πβ¨
π NAME π
std::gmtime β literally std::gmtime, that's it, that's the name π
πΈ SYNOPSIS πΈ
Defined in header <ctime>:
std::tm* gmtime( const std::time_t* time );
So what does it do? It converts a given time since epoch (as a std::time_t value) into calendar time, expressed in Coordinated Universal Time (UTC). π No cap, that's the whole vibe.
π¦ PARAMETERS π¦
timeβ a pointer to atime_tobject to convert π«Ά
That's it. One little pointer. She's low maintenance, we love that for her π
π RETURN VALUE π
You get back a pointer to a static internal std::tm object on success, or a null pointer otherwise. π
β οΈ okay listen bestie, this part matters: that structure may be shared betweenstd::gmtime,std::localtime, andstd::ctime, and may be overwritten on each invocation. So likeβ¦ don't get too attached to it? It's giving "main character shared with three other functions" energy. π Copy it if you need it to survive.
π NOTES π
- This function may not be thread-safe. fr fr, be careful out there π§
- POSIX requires that this function sets
errnotoEOVERFLOWif it fails because the argument is too large. πβ¨
π₯ EXAMPLE π₯
// Run this code
#include <ctime>
#include <iomanip>
#include <iostream>
#include <sstream>
int main()
{
setenv("TZ", "/usr/share/zoneinfo/Europe/London", 1); // POSIX-specific
std::tm tm{}; // get_time does not set all fields hence {}
tm.tm_year = 2020 - 1900; // 2020
tm.tm_mon = 7 - 1; // July
tm.tm_mday = 15; // 15th
tm.tm_hour = 10;
tm.tm_min = 15;
tm.tm_isdst = 1; // Daylight saving in London
std::time_t t = std::mktime(&tm);
std::cout << "UTC: " << std::put_time(std::gmtime(&t), "%c %Z") << '\n';
std::cout << "local: " << std::put_time(std::localtime(&t), "%c %Z") << '\n';
}
π Possible output π
UTC: Wed Jul 15 09:15:00 2020 GMT
local: Wed Jul 15 10:15:00 2020 BST
see how UTC and local are one hour apart? that's the whole point, bestie β same moment, two timezones, and gmtime is the UTC one πβππ«
π SEE ALSO π
localtimeβ converts time since epoch to calendar time expressed as local time (function) π- C documentation for
gmtimeπ
k that's the whole page, mwah π β this lil gem comes from http://cppreference.com, dated 2024.06.10 (and it's literally titled std::gmtime(3) in case you forgot where you were π). stay hydrated, set your errno, and go convert some timestamps πβ¨π¦
π¦ 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