Equivalent of <time.h> in <chrono>

Dec 24, 2019 at 4:53am
Hello!

I was wondering about one thing. I'm reworking on an older project of mine that utilises the header <time.h> and I'm intending on learning about using more recent C++ (so here I'm looking into the <chrono> header)

The code I want to update is the following:
1
2
3
4
5
6
7
8
9
10
11
12
void Clock::display_time()
{
   time_t rawtime;
   struct tm timeinfo;
   char buffer[80];

   time(&rawtime);
   localtime_s(&timeinfo, &rawtime);

   strftime(buffer, 80, "\n %A %d %B ___ %R", &timeinfo);
   puts(buffer);
}


However, with the research I've done for displaying time with only using <chrono> people always use the function localtime() which requires <ctime> or <time.h>, is there any way of doing without it?

Thanks in advance,

Hugo.
Last edited on Dec 24, 2019 at 6:54am
Dec 24, 2019 at 6:32am
Additions to chrono in C++20 has support for calendars and time zones.

There is stream output support for std::chrono::local_time and std::chrono::sys_time
https://en.cppreference.com/w/cpp/chrono/local_t/operator_ltlt
and specialisations of std::formatte for custom formatting of these.
https://en.cppreference.com/w/cpp/chrono/local_t/formatter


There are a few examples in the proposal: https://howardhinnant.github.io/date/d0355r7.html#Description
Dec 24, 2019 at 6:52am
Thanks JLBorges for that info, I didn't know there was a C++20 yet haha.
Is there no way of doing it with earlier versions (I'm currently using VS 19 and that option doesn't work, it says namespace std::chrono doesn't have a member function 'sys_time') ?

EDIT: I can't use std::chrono::local_time() either
Last edited on Dec 24, 2019 at 7:01am
Dec 24, 2019 at 2:19pm
> Is there no way of doing it with earlier versions

No; <chrono> support for time of day, calendar, time zones and i/o was added in C++20.

With C++11, we can use Hinnant's date library (non-viral, MIT License):
https://github.com/HowardHinnant/date

The C++20 additions to <chrono> are based in this library; it provides a stream insertion operator for std::chrono::system_clock::time_point
Dec 24, 2019 at 2:40pm
I see, thank you for the ressource on Hinnant's date library.
One last question if I may, since you're mentioning C++20 I assume there is a way to use it. Is there - to your knowledge - any possible way to use it with VS 19, or VS in general?
Dec 24, 2019 at 3:07pm
To explicitly enable the currently implemented compiler and standard library features proposed for the next draft standard, use /std:c++latest. All C++20 features require /std:c++latest; when the implementation is complete, a new /std:c++20 option will be enabled
https://docs.microsoft.com/en-us/cpp/build/reference/std-specify-language-standard-version?view=vs-2019


As per the compiler status page on cppreference, no compiler/library currently supports C++20 calendar and time zone facilities (clang++/libc++ has partial support).
https://en.cppreference.com/w/cpp/compiler_support
Dec 24, 2019 at 4:14pm
Well have you personally had it work with VS? I included /std::c++latest yet it still doesn't work. RIP. I guess I'll have to stick with that sucky time.h workaround for now...

Thanks a lot for your help regardless JLBorges!
Dec 25, 2019 at 3:42am
> Well have you personally had it work with VS?

No, these C++20 features are not yet implemented in the Microsoft library.
Dec 25, 2019 at 4:03am
Oh I see then, false hope on my part I guess! Thank you for taking the time to answer my questions JLBorges
Topic archived. No new replies allowed.