CppCon 2016: Howard Hinnant “Welcome To The Time Zone"
Why Stack Overflow recommends it
Stack Overflow users link it as the video introduction to Hinnant's date and tz library whenever answers involve time zones, parsing, or zoned_time in C++.
Recommended by 2 people across 6 different months, 2015 to 2020.
What it is
A CppCon 2016 talk by Howard Hinnant, lead author of the date/tz library that became C++20's chrono calendar and time zone support. It shows how to convert between time zones in a type-safe, chrono-based way, drawing a clear line between calendars and times and building on his 2015 date library talk.
What people said
Recommends Howard Hinnant · 2015-12 · Stack Overflow · on “Get current number of hours and minutes using chrono::time_point”
Here is a free, open-source date library [LINK] which will do this for you. Feel free to inspect the code if you want to know exactly how it is done. You can use it to get the current hours and minutes since midnight in the UTC timezone like this: #include "date/date.h" #include <iomanip> #include <iostream> int main() { auto now = date::floor<std::chrono::minutes>(std::chrono::system_clock::now()); auto dp = date::floor<date::days>(now); auto time = date::make_time(now - dp); int hours = time.hours().count(); int minutes = time.minutes().count(); std::cout.fill('0'); std::cout << std::setw(2) << hours << ':' << std::setw(2) << minutes << '\n'; } If you want the information in some other tim
Recommends Howard Hinnant · 2016-10 · Stack Overflow · on “Converting date, time and UTC offset values to milliseconds since unix epoch?”
Here is a free, open-source C++11/14 header-only library [LINK] which will do the job. It is fully documented [LINK] , and portable across all C++11/14 implementations. There is even a video presentation [this video] of it. #include "chrono_io.h" #include "date.h" #include <iostream> #include <sstream> int main() { using namespace std; using namespace std::chrono; using namespace date; istringstream in{"2016-10-10 23:48:59.456 -4"}; sys_time<milliseconds> tp; int ih; in >> parse("%F %T", tp) >> ih; tp -= hours{ih}; cout << tp.time_since_epoch() << '\n'; } This outputs: 1476157739456ms Which is the correct number of Unix Time [LINK] milliseconds since the epoch. If you need to include leap seconds
Recommends Howard Hinnant · 2016-10 · Stack Overflow · on “Sending signal in Special Time like hh:mm with Qt”
If you wish to fix the part about being off when crossing a daylight saving boundary, you can use this library for the time computation: [this video] And here are links showing how to convert back and forth between this library and `QDate`: [LINK] Extending that conversion to include time-of-day should be straightforward.
Recommends Howard Hinnant · 2016-11 · Stack Overflow · on “How can I convert IANA time zone name to UTC offset at present in Ubuntu C/C++”
You could use this free open source C++11/14 library [LINK] to do it like this: #include "chrono_io.h" #include "tz.h" #include <iostream> int main() { using namespace date; using namespace std::chrono; auto zt = make_zoned("America/Los_Angeles", system_clock::now()); std::cout << zt.get_info().offset << '\n'; } This currently outputs: -25200s Or you could format it differently like this: std::cout << make_time(zt.get_info().offset) << '\n'; which currently outputs: -07:00:00 The factory function make_zoned creates a zoned_time using the IANA name "America/Los_Angeles" and the current time from std::chrono::system_clock . A zoned_time has a member getter to get the information [LINK] about t
Recommends Howard Hinnant · 2016-11 · Stack Overflow · on “Get the Local Time in C++ Visual Studio 2015 Community”
Feel free to use this free, open-source modern C++ library [LINK] : #include "tz.h" #include <iostream> int main() { std::cout << date::make_zoned(date::current_zone(), std::chrono::system_clock::now()) << '\n'; } See the installation section for details on how to install this on Windows. It is known to work with VS-2015. The library is thread safe and uses no deprecated functionality. It also does a lot more things [this video] than just getting the current local time.
Recommends Howard Hinnant · 2017-03 · Stack Overflow · on “How do you get info for an arbitrary time zone in Linux / POSIX?”
New answer for an old question. Rationale for new answer: There is now a modern, free, open-source, C++11/14/17 library [LINK] to do this. 1 It does require some installation [LINK] . But it is portable across Linux/macOS/Windows. And it has full documentation [LINK] , and even a video introduction [this video] . Here is an example program to get information about a specific time zone. I'm using my own timezone just as an example. This library supports the full IANA timezone database [LINK] : #include "tz.h" #include <iostream> int main() { auto zone = date::locate_zone("America/New_York"); std::cout << *zone << '\n'; std::cout << zone->get_info(std::chrono::system_clock::now()) << '\n'; } The fi
Recommends Howard Hinnant · 2017-08 · Stack Overflow · on “Time Lookup Table in C++11”
To support local times in different time zones with <chrono> I recommend Howard Hinnant's free, open-source time zone library [LINK] . This library is built on top of <chrono> , and uses the IANA time zone database [LINK] to manage time zones. Also, to handle time zones, you will need to store more than just time-of-day. You will need to store the entire date, as a time zone's UTC offset often varies with date. I.e. 09:05 Australia/Sydney doesn't really nail down a moment in time. But 2017-08-16 09:05 Australia/Sydney does. Here is how you could create such a time stamp with <chrono> and the time zone library: using namespace date; using namespace std::chrono; auto zt = make_zoned("Australia
Recommends JohnFilleau · 2020-06 · Stack Overflow · on “How do I parse with respect to char array indices (C++)”
Unrelated to your specific question, but maybe related to a related underlying issue, watch this youtube video about zoned time, a whole library for parsing and using time. [this video] builds upon the chrono talk [LINK] watch the latter if you don't have an understanding of `chrono`.
Cites Howard Hinnant · 2016-12 · Stack Overflow · on “Creating a CTime @ 2am doesn't return the correct value (Windows CE, C++)”
Actually (assuming a US timezone) The clock jumps forward , not back at 2017-03-12 02:00:00, making CTime 's output quite curious. Assuming a modern compiler (e.g. VS-2013 or better), here is a C++11/14 library [LINK] which can be used to clarify the situation. #include "chrono_io.h" #include "tz.h" #include <iostream> #include <exception> int main() { try { using namespace date; using namespace std::chrono_literals;; for (auto myHour = 1h; myHour < 13h; ++myHour) { auto myTime = make_zoned(current_zone(), local_days{2017_y/mar/12} + myHour); std::cout << "Hour: " << myHour << '\n'; std::cout << "Time: " << myTime << '\n'; } } catch (const std::exception& e) { std::cout << e.what() << '\n';
Cites Qaz · 2019-05 · Stack Overflow · on “std::chrono::from_stream correct usage”
First, from_stream works on streams, not strings. If you want to use a string, you have to go through a stream first: std::istringstream iss("2018-12-09T00:00:00"); Now the function you were expecting is actually parse [LINK] , not from_stream . It works like other manipulators such as get_time , and delegates its work to from_stream : std::chrono::year_month_day date; if (not (iss >> std::chrono::parse("%FT%T", date))) { // Handle error } This is equivalent to from_stream(iss, "%FT%T", date) . You can see some usage of parsing in Howard's CppCon talk on time zones [this video] . He goes on to show examples that use the extra parameters to determine which time zone abbreviation and offset were ac