The reason those functions have different parmaters is that the lack of safety is caused by only have a single parameter. In particular, asctime()
uses a single buffer to return the time. So if you do something like:
char *s1 = asctime((time_t)0); // 1-Jan-1970 00:00:00 or something like that.
time_t t = time();
char *s2 = asctime(t);
cout << "1970: " << s1 << " now:" << s2 << endl;
Then you will not see two different times printed, but the current time printed twice, as both s1
and s2
point to the same string.
The same applies to localtime
, which returns a pointer to struct tm
- but it's always the same struct tm
, so you if you do:
struct tm* t1 = localtime(0);
struct tm* t2 = localtime(time());
you will get the same values in t1
and t2
(with the "current" time, not 1970).
So, to fix this problem, asctime_s
and localtime_s
have an extra parameter that is used to store the data into. asctime_s
also have a second extra parameter to tell the function how much space there is in the storage buffer, as otherwise, it could overflow that buffer.