Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ date and time

Tags:

c++

datetime

I am developing an Appointment application in C++ and want to use some Date and Time features.

Is it easier to just use strings when talking about Date and Time, or should I write or get a Date/Time class?

I am wanting to code an appointment class that holds both the time and date of an appointment. Once I have coded the class file, I am wanting to integrate it into a forms application in C++ builder.

I see that there is a TMonthCalendar control. I would like to use this control when making the forms application. As such, what format for the date does this control use? I would like to use the same type as the control when making the class so that I can easily integrate it together.

UPDATE

I have found that it uses the TDateTime type. My question is this: What include statement do I need to use to use this in a console application?

like image 361
Darryl Janecek Avatar asked Sep 10 '12 06:09

Darryl Janecek


2 Answers

C++11 includes convenience data types and functions for date/time representations, as well as their conversion to strings.

With that, you can do things like this (pretty self-explanatory, I think):

#include <iostream>
#include <iomanip>
#include <ctime>

int main()
{
    std::time_t t =  std::time(NULL);
    std::tm tm    = *std::localtime(&t);
    std::cout << "Time right now is " << std::put_time(&tm, "%c %Z") << '\n';
}

In particular, there are data types std::time_t and std::tm, and a very nice IO manipulator std::put_time for pretty printing. The format strings used by it are well-documented at cppreference.

This is also supposed to work together well with locales, e.g. for a Japanese time/date format:

std::cout.imbue(std::locale("ja_JP.utf8"));
std::cout << "ja_JP: " << std::put_time(&tm, "%c %Z") << '\n';

The chrono library included in the C++11 standard library also allows you to do simple time/date arithmetic conveniently:

std::chrono::time_point<std::chrono::system_clock> now;
now = std::chrono::system_clock::now();
/* The day before today: */
std::time_t now_c = std::chrono::system_clock::to_time_t(
         now - std::chrono::hours(24));

Unfortunately, not all of this is available in all compilers yet. In particular, the std::put_time function does not seem to be available in GCC 4.7.1 yet. To get the code I gave initially to work, I had to use the slightly less elegant std::strftime function:

#include <iostream>
#include <iomanip>
#include <ctime>

int main()
{
    std::time_t t =  std::time(NULL);
    std::tm tm    = *std::localtime(&t);

    constexpr int bufsize = 100;
    char buf[bufsize];
    if (std::strftime(buf,bufsize,"%c %Z",&tm) != 0)
      std::cout << "Time right now is " << buf << std::endl;
}
like image 70
jogojapan Avatar answered Sep 28 '22 16:09

jogojapan


Is it easier to just use strings when talking about Date and Time?

No. Even simple things like calculating a duration if you have a meeting start and end date need to account for complex things such as leap years.

should I write or get a Date/Time class?

No. Writing a Date/Time Library seems simple, but it's rather difficult to get right and extremely easy to get wrong. Also, others have done it before - boost is a collection of free libraries with stellar reputation. So many others in fact, that it's become a cliche that newbie programmers want to write a Date/Time library, failing horribly at it.

I see that there is a TMonthCalendar control. [...] I have found that it uses the TDateTime type.

Relying on the same Date/Time class as your GUI framework is ok, but if you later change the GUI framework it can become an issue. Since it's not terribly hard to swap a sensible Date/Time library for another sensible Date/Time library later on, just use one that you find easy to use.

like image 27
Peter Avatar answered Sep 28 '22 17:09

Peter