StarShine
StarShine

Reputation: 2060

The mess that is ctime, time.h, sys/time.h

I hope some Linux die-hard can answer me how I should write portable (POSIX) code when working with time functions.

error: field ‘st_atim’ has incomplete type ‘timespec’, struct timespec st_atim;

error: ‘::time’ has not been declared

etc..

So, for a C++ setup linking with many 3rd party dependencies, what is the correct way to make sure everything keeps compiling w.r.t. including time.h? Should I pin the time.h include for the system to the specific platform I'm compiling to? Should I go over all targets that possibly need time.h? Dancing elephants and confetti lie ahead.


Update: The issue seems to be related to the version of C++ as was hinted at in the comments below. I've since updated gcc to 8.3.0 (from 5.4), and I've given up on supporting older c++ compatibility below c++11 on linux. After updating and rebuilding all 3rd party packages (including ffmpeg), I now no longer have the issue I described, but that doesn't mean it's fixed in the sense that it can't occur again for someone else. In fact I think the issue is mainly with how ffmpeg compiles on older compilers and without c++11 explicitly requested, so I'm leaving it open.

Upvotes: 18

Views: 12488

Answers (1)

Galik
Galik

Reputation: 48635

I recommend you consider Howard Hinnant's date library that has been accepted into the next version of the C++ standard library (C++20):

https://github.com/HowardHinnant/date

It should work on any platform that supports C++11 onwards.

The Standard version is documented here: https://en.cppreference.com/w/cpp/chrono

Upvotes: 8

Related Questions