C++: location of localtime_s in GCC

Following the documentation and compiling with gcc-11.2.0 like so

g++ -std=c++17 -pthread -O3 -flto -fPIC ...

I am not able to use localtime_s in my program:

#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>

using SystemClock = std::chrono::system_clock;
const auto in_time_t = SystemClock::to_time_t(SystemClock::now());

struct tm buf; localtime_s(&in_time_t, &buf);
// error: there are no arguments to ‘localtime_s’ that depend on a template parameter, 
//        so a declaration of ‘localtime_s’ must be available [-fpermissive]

struct tm buf; std::localtime_s(&in_time_t, &buf);
// error: ‘localtime_s’ is not a member of ‘std’; did you mean ‘localtime’?

Am I doing something wrong or localtime_s is not available in GCC? Or it is only available for pure C programs (for example, compiled with gcc -std=c11)?

Thank you very much for your help!

>Solution :

Many of the functions specified in the standard ending with _s were originally developed by Microsoft as alternates to functions ending in _r.

On Linux systems, localtime_s is not defined but localtime_r is, and has the same parameters/return type, so use that instead.

Leave a Reply