C++20 calendars and time zones

A couple years ago I wrote a post called A better date and time C++ library about Howard Hinnant’s date library (I actually planned for several posts, but only the first was materialized). A slightly modified version of the library has been voted in for C++ 20 at the ISO committee meeting in Jacksonville this month. You can find the actual proposal here D0355R7: Extending <chrono> to Calendars and Time Zones.

This addition to the chrono library provides two things:

  • calendar support: on one hand, types that represent the year, month and day, and various combinations, such as a specific day in a month (month_day) or the last day in a month (month_day_last), a specific month of the year (year_month) and others, and on the other hand operations with dates for the Gregorian calendar.
  • time zones support: the ability the represent time points in a specific time zone (from the IANA time zones database).

The library is not implemented yet by any compiler but you can find it on Github and you can try it on Wandbox. Mind that the namespace of this implementation is date, but it will actually be part of std::chrono.

Here are just some random examples of using the new features:

  • Representing dates:

  • Determining the last day of February the current year:

  • Number of days between two dates:

  • Printing the current time in the current time zone:

    The result is something like this (my local time zone is Europe/Bucharest):

  • Printing the current time in different time zones (localtime is the object from example above):

    The result looks like this:

The date and time utilities page at http://en.cppreference.com/w/cpp/chrono is already updated with references to the added extensions, although most of the documentation is just empty for now. However, you can find all that you need in the project documentation on Github.

1 Reply to “C++20 calendars and time zones”

  1. Great. Please publish full and working programs without using auto. You are trying to educate, than don’t hide half of the code.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.