CppCon 2015: Howard Hinnant “A C++14 approach to dates and times"
Why Stack Overflow recommends it
Stack Overflow users link it as the introductory video for Hinnant's date library whenever someone needs to work with dates, times or time zones in modern C++.
Recommended by 3 people across 4 different months, 2011 to 2020.
What it is
Howard Hinnant, author of the C++ date library that later shaped C++20 chrono, presents a calendar and date/time library built on C++11 std::chrono. It covers year/month/day types, conversions to and from system_clock time points, compile-time checking and constexpr date computations.
What people said
Recommends Drop · 2015-10 · Stack Overflow · on “How can I compare two class with void* in cpp”
You might want to take a look at Howard Hinnant's date library [LINK] and at related video from CppCon 2015 [this video]
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 Matt Johnson-Pint · 2016-01 · Stack Overflow · on “C++ function that converts time(NULL) to local time”
+1 for Howard Hinnant's date module. Check out his CPPCon presentation on YouTube [this video] describing its usage.
Recommends Howard Hinnant · 2016-01 · Stack Overflow · on “Why doesn't the C++11 chrono library implement date?”
Here is a video introduction of Howard Hinnant's date library linked to in TemplateRex's comment above: [this video]
Recommends Howard Hinnant · 2016-01 · Stack Overflow · on “Build/parse UTC date using standard library”
How can I build a std::chrono::system_clock::time_point from the UTC date? Just for this part of it, here [LINK] is a header-only high-performance C++11/14 modern solution. It can be used like this: int y = 2016; int m = 1; int d = 14; int h = 18; int min = 2; int sec = 15; std::chrono::system_clock::time_point tp = date::sys_days{date::year{y}/m/d} + std::chrono::hours{h} + std::chrono::minutes{m} + std::chrono::seconds{sec}; This is an open source, free library on github [LINK] . Here is a video presentation [this video] of the library. This library is portable to all recent implementations of C++11 and C++14 (including VS2013 and VS2015). It offers a better interface than tm inherited from C,
Recommends Howard Hinnant · 2017-01 · Stack Overflow · on “Whats the way to compare dates in c++”
Here is a very good C++11/14 library for handling dates [LINK] 1 . It allows you to have {year, month, day} structures (called date::year_month_day ), and {count-of-days} structures (called sys_days ). It also allows easy and efficient conversions between these two structures. And naturally there are comparison operators. The entire library is essentially an extension of <chrono> into calendar types. Video introduction found here: [this video] Lots of example code here [LINK] . Here is a detailed explanation of the underlying algorithms to convert between the {year, month, day} structure and the {count-of-days} structure: [LINK] Here are some date creation and comparison examples for both field (
Cites Howard Hinnant · 2011-09 · Stack Overflow · on “C++11 alternative to localtime_r”
You're not missing anything. The next C standard (due out probably this year) does have defined in Annex K: struct tm *localtime_s(const time_t * restrict timer, struct tm * restrict result); And this new function is thread safe! But don't get too happy. There's two major problems: localtime_s is an optional extension to C11. C++11 references C99, not C11. local_time_s is not to be found in C++11, optional or not. Update In the 4 years since I answered this question, I have also been frustrated by the poor design of C++ tools in this area. I was motivated to create modern C++ tools to deal with this: [LINK] #include "tz.h" #include <iostream> int main() { using namespace date; auto local_tim
Cites Howard Hinnant · 2015-02 · Stack Overflow · on “How do I parse and convert DateTime to the RFC 3339 with C++”
Here is a complete, but unfortunately unsatisfactory, and yet portable program among the very latest versions of libc++, libstdc++, VS implementations, which parses the string in the format you show into a std::chrono::system_clock::time_point . I could not find a DateTime to which you refer. However std::chrono::system_clock::time_point is a "DateTime" structure. std::chrono::system_clock::time_point is a count of some time duration (seconds, microseconds, nanoseconds, whatever) since some unspecified epoch. And you can query std::chrono::system_clock::time_point to find out what its time duration is. And as it turns out, every implementation measures time since New Years 1970 neglecting le
Cites Howard Hinnant · 2015-12 · Stack Overflow · on “C++ Get ISO8601 day number from week number and week day”
New answer for old question. Using this free, open-source date library: [LINK] 2014, First sunday of March - That would be 2014-03-02 #include "date.h" #include <iostream> int main() { using namespace date; std::cout << year_month_day{sun[1]/mar/2014} << '\n'; } which outputs: 2014-03-02 The type date::year_month_day has getters on it named year , month and day . It is pretty easy to work with, and has full documentation at the link above. 2014, 4th Wednesday of December - That would be 2014-12-24 using namespace date; std::cout << year_month_day{wed[4]/dec/2014} << '\n'; 2014-12-24 But this is not the last Wednesday of December 2014, this is: std::cout << year_month_day{wed[last]/dec/2014}
Cites Howard Hinnant · 2016-01 · Stack Overflow · on “I'm having a hard time getting date/time components from day of year and seconds since midnight”
Here is an argument for using <chrono> . This argument is not without its downsides. However I believe that in the long run, migrating towards this system will have benefits in type safety (correctness), performance and readability. The downsides include the need for an 3rd party open source free header-only (one single header) library (for now): [LINK] This will also require C++11 or forward (naturally because it builds on <chrono> ). I will present this solution in stages: The first stage uses <chrono> only for the conversion, and gains little in terms of type-safety. The input and output at this stage is integral. The second stage starts showing benefits when the input stage adopts <chron