Is the localtime s function supported in the GCC translator?
Good afternoon.
I mostly use Visual Studio, and for some time now Visual Studio has been insisting with maniacal persistence that everyone use localtime_s instead of localtime because of the vulnerability of localtime from use in multithreaded applications. In Visual Studio 2017, this warning has been upgraded to a compiler error.
Everything would be fine, but sometimes you need the code to be translated under GCC. So I decided to check if it is supported localtime_s in GCC. I took the GCC online translator http://cpp.sh/ and wrote the program:
// Example program
#include <iostream>
#include <string>
#include <time.h>
int main()
{
struct tm *ptr;
struct tm structtime;
time_t lt;
lt = time(NULL);
ptr = localtime(<);
printf(asctime(ptr));
ptr = localtime_s(&structtime,<);
printf(asctime(&structtime));
std::string name;
std::cout << "What is your name? ";
getline (std::cin, name);
std::cout << "Hello, " << name << "!\n";
}
When trying to translate in the line
ptr = localtime_s(&structtime,<);
The translator informed me that:
17:34: error: 'localtime_s' was not declared in this scope
Relevant questions:
- Is localtime_s supported in GCC?
- Maybe you need a different header, not time.h?
Thanks.
UPD1:
On other online GCC translators such as:
Https://www.onlinegdb.com/online_c++_compiler
Localtime_s doesn't work either. Even if you define all the specified macros.
2 answers
As stated on cppreference:
As with all bounds-checked functions,
localtime_s
is only guaranteed to be available if__STDC_LIB_EXT1__
is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__
to the integer constant 1 before includingtime.h
.
It is required to check for the presence of the define __STDC_LIB_EXT1__
and then set the custom define __STDC_WANT_LIB_EXT1__
to one. All this must be done before connecting the header file time.h
.
Otherwise, the function can not present in the implementation of a particular compiler.
In modern versions of Visual C++, the CRT function localtime
(like most other similar functions) has no problems with access from different threads: the buffer used is not a real static variable, but is actually located in the thread's local storage. This does not mean that it is good to use it - it is just a very poorly designed API, with which it is easy to make mistakes. But there are a lot of them in C/C++, this does not mean anything.
As far as I know, the studio is capable of such functions gives a warning, not an error. You probably have the compiler option "treat warnings as errors" enabled. You can disable the warning with the #pragma warning(disable : 4996)
directive, if you have the discipline not to use really unsafe functions from the CRT, like strcpy
. Or rewrite the code using safe functions available in specific compilers.
As for localtime_s
, it is introduced in the C11 standard, which is considered "exotic", and not all implementations they fully support it. In C++11, it is not present. GLIBC (similar to CRT in GCC) contains localtime_r
from POSIX instead. To write code that will be processed by different compilers, you will have to use conditional preprocessor directives and write different code branches for different compilers. There's no other way.