Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it so convoluted to get the date and/or time in C++?

I fully expect this to be closed within a day or two since it's a kinda subjective topic, but here goes anyway: Why does it take at least 5 lines of code to get the date/time in C++?

This was one of the first things I learnt how to do in C, but that was a long time ago...and I remember it taking me a while to grasp the whole concept back then. Now I'm much more experienced, but after having gotten used to higher-level languages like C# and Java, it really irritates me that something this simple requires all of this:

#include <iostream>
#include <chrono>
#include <ctime>

using namespace std::chrono;

// First get an epoch value
auto time = system_clock::to_time_t(system_clock::now());

// Now we need a char buffer to hold the output
char timeString[20] = "";

// Oh and a tm struct too! Gotta have that, just to make it more complicated!
tm tmStruct;

// Oh and BTW, you can't just get your local time directly;
// you need to call localtime_s with the tm AND the time_t!
// Can't use localtime anymore since it's "unsafe"
localtime_s(&tmStruct, &time);

// Hurray! We finally have a readable string!!
strftime(timeString, 20, "%d-%m-%Y %T", &tmp);

cout << timeString << "Phew! I'm tired, I guess the time must be bedtime!"

Now compare that to C# (for example):

Console.WriteLine(DateTime.Now.ToString("%d-%m-%Y %T")); // Well that was easy!

Is there a good reason for this nonsense, or does it just boil down to the general idea that C++ is for low-level stuff where the dev wants/needs more control?

As an avid code golfer, I'll take the second option over the first any day of the week, since shorter code is generally cleaner, more readable, easier to debug and just generally better IMHO. So is there a shorter method in C++ that I'm missing? MTIA!

like image 1000
Kenny83 Avatar asked Dec 28 '19 10:12

Kenny83


1 Answers

All of the tm stuff is inherited from C. C code work with functions with output parameters and return codes, so the code tends to be convoluted. Let's just take file I/O as an example:

FILE *file;
file = fopen("foo", "w");
fprintf(file, "%d", /* some number */);
fclose(file);

vs

std::ofstream ofs{"foo"};
ofs << /* some number */;

In this case, the C++ standard library just doesn't happen to contain the date functionalities, which is a shame ...


... until C++20, where Howard Hinnant's date library is voted into the standard library! The library is pretty lightweight, so don't wait until C++20 to try it out! Here's the example in the README file:

#include "date.h"
#include <iostream>

int
main()
{
    using namespace date;
    using namespace std::chrono;
    auto now = system_clock::now();
    std::cout << "The current time is " << now << " UTC\n";
    auto current_year = year_month_day{floor<days>(now)}.year();
    std::cout << "The current year is " << current_year << '\n';
    auto h = floor<hours>(now) - sys_days{January/1/current_year};
    std::cout << "It has been " << h << " since New Years!\n";
}

(live demo)

like image 151
L. F. Avatar answered Oct 21 '22 21:10

L. F.