3

Is there any date time module in c++?
I researched a bit in google and came to know about <ctime> header file but it just went above my head.

Any other thing that can do my work?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • BTW [Boost.Date_Time library](https://www.boost.org/doc/libs/1_38_0/doc/html/date_time.html) might help. – KaiserKatze Dec 25 '20 at 13:55
  • @KaiserKatze: If I'm not mistaken, Boost's DateTime library [hasn't](https://www.boost.org/doc/libs/1_75_0/doc/html/date_time.html) been updated in 15 years. – einpoklum Dec 25 '20 at 17:01

1 Answers1

7

In the C++ standard: <chrono>

C++11

Starting with C++11, the standard library contains date & time utilities, available in the <chrono> header, with constructs within the std::chrono namespace.

Among these you will find:

  • Clocks
  • Time points
  • Durations
  • A flexible mechanism for parsing date & time values from strings

the library is heavily templated, so that you can use a type of your choice for the raw representation (even floating-point types), and set your own resolution.

Example of use: How to get current time and date in C++?

Note that std::chrono facilities are somewhat-compatible with the old <ctime> header types. For example, the "system clock" class has methods for converting to and from the <ctime> time representation, time_t. This will be useful to you if you're combining C and C++ code, or need to expose a C interface somewhere; otherwise - try avoiding <ctime>-based code.

C++20

The new standard version approved in 2020 added:

  • Time zones
  • Calendars
  • More Duration types
  • More time point types
  • More clocks

Beyond the standard: Howard Hinnant's Date libraries

As commenters @AndyG and @doug suggest, Howard Hinnant is the "C++ date&time guy", and much of his work has actually gone into the standard - but not all of it.

Howard maintains a library/set-of-libraries of his own, named "Date".

It is based on C++11 <chrono>, but its changes are not exactly what's been added in C++20; so especially useful if you're using C++17 or earlier. It adds:

  • Timezones
  • Calendars, including Julian and Islamic
  • More Duration types
  • More time point types
  • Weeks

I'm assuming that eventually, all the good stuff from here will get standardized, but that hasn't happened yet.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • @KaiserKatze: I thought GNU and LLVM already have all of C++20 implemented? For example, according to [this page](https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017) libstdc++ has these since July 2019. – einpoklum Dec 25 '20 at 15:03
  • See https://github.com/HowardHinnant/date/issues/565 for a list of differences between Howard Hinnant's date lib and C++20. – Howard Hinnant Dec 25 '20 at 15:52
  • I appreciate the effort that went into writing this answer. However, this type of answer is also why questions _Seeking recommendations for books, tools, software libraries, and more_ are off-topic. – Trenton McKinney Jan 20 '21 at 04:23