Tutorial Part 2- A Portable Date Library for C++

In the previous part of this tutorial we downloaded and set up Howard Hinnant‘s date library. This is a library written in standard C++ that lets us work with dates much more easily than what we can currently do. We learned how to work around some potential compiler errors and how to create compile-time and run-time dates. In this part of the tutorial we’ll discuss the library’s error handling and efficiency, and then concentrate on doing calculations with the dates.

Continue reading “Tutorial Part 2- A Portable Date Library for C++”

Tutorial Part 1- A Portable Date Library for C++

Introduction

How do we work with dates and times in C++? One way is to use the C date and time library. This library has functions for manipulating times, such as time, clock and difftime; functions for converting formats of times and dates, e.g., gmtime, localtime, mktime; and various types, such as tm for calendar time, time_t for the time since the epoch, and clock_t, which denotes a process running time. In addition, since C++11 we’ve had a nice library for dealing with points in and durations of time, namely, chrono.

These libraries do have their problems though. The C library (not surprisingly) looks like just that – a C library, and not like C++ code. For example, it has no classes, there are no operators for performing such useful functions as determining the amount of time between two dates or whether one date comes before or after another date, and the internals of the library are exposed and often clumsy to work with. The chrono library overcomes these deficiencies because it was designed from the ground up to be in C++. Unfortunately, chrono has no facilities for working with calendar dates.

Continue reading “Tutorial Part 1- A Portable Date Library for C++”