Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );

This returns: warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead.

time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime_s ( &rawtime );

When I change localtime to localtime_s I get: error C2660: 'localtime_s' : function does not take 1 arguments

Here is what I think is going on in the first block of code:

  • create an empty time_t variable.
  • create a pointer to timeinfo which is defined in ctime
  • write the rawtime into a rawtime reference
  • convert the rawtime into something meaningful to pedestrians

    1. Am I right?
    2. What second input parameter does localtime_s need?
    3. What's the worst that could happen if I just ignore the whole localtime safety issue.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
293 views
Welcome To Ask or Share your Answers For Others

1 Answer

localtime returns a pointer to a statically allocated struct tm.

With localtime_s, you pass in a pointer to a struct tm, and localtime_s writes its result data into that, so your code would change from:

struct tm *timeinfo;
timeinfo = localtime(&rawtime);

to something like:

struct tm timeinfo;
localtime_s(&timeinfo, &rawtime);

This way, it's writing to your buffer instead of having a buffer of its own.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...