-2

I am trying to create a timestamp and at the moment I am trying to output it to the screen.

The function which I am attempting to use to do this is localtime_s.

However I cannot get it to work and I cannot find any working examples of how to use this.

I am encountering the following errors:

  1. Error 1 error C2660: 'localtime_s' : function does not take 1 arguments
  2. a value of type "errno_t" cannot be assigned to an entity of type "tm *"
  3. argument of type "time_t *" is incompatible with parameter of type "tm *"
  4. too few arguments in function call

        time_t rawtime;
        struct tm * timeinfo;
    
        time(&rawtime);
        timeinfo = localtime_s(&rawtime);
        asctime(timeinfo);
        cout << "Current local time and date: " << timeinfo << endl;
    

Can anyone please advise?

Edit

Using the answer to cover the cout problem, visual studio won't let me run asctime. So instead I tried using asctime_s which then displays "no instance of overloaded function "asctime_s" matches the argument list, argument types are : (tm *, time_t *)"

However using a pointer to timeinfo and rawtime causes further errors.

The code is now:

    time_t rawtime;
    tm timeinfo;
    errno_t result = localtime_s(&timeinfo, &rawtime);
    cout << "Current local time and date: " << asctime_s(&timeinfo, &rawtime) << endl;

Kind Regards

PapaSmurf
  • 161
  • 4
  • 15
  • pay attention to the error messages, they are quite clear what the problem is. Also read the [documentation](http://msdn.microsoft.com/en-us/library/a442x3ye.aspx) for the API calls you are having problems with, that will help clear things up as well. – Captain Obvlious Dec 29 '14 at 14:35
  • 1
    Check this: https://stackoverflow.com/questions/14386923/localtime-vs-localtime-s-and-appropriate-input-arguments – Super-intelligent Shade Dec 29 '14 at 14:37

2 Answers2

1

The first problem is you are not passing enough arguments to localtime_s. It requires two arguments but you are only passing one. The first argument should be a pointer to tm and the second a pointer to a time_t. The second problem is that the return type is errno_t not a pointer to tm. Your code for acquiring the current time should actually be:

time_t rawtime;
tm timeinfo;
errno_t result = localtime_s(&timeinfo, &rawtime);

There is also a problem with sending the current time to cout. Since there is no overloaded version of operator<< that takes a type tm you will get the value of the pointer instead of the text representation of the time. After applying the above changes you will also need to change the stream out line from:

cout << "Current local time and date: " << timeinfo << endl;

to:

cout << "Current local time and date: " << asctime(&timeinfo) << endl;

Any time you encounter a problem like this you should either look at the function signature or refer to the documentation to determine how to properly call the function. The documentation usually details what the arguments are, what values they can be (i.e. if NULL is acceptable for pointers), and what their return values are (if any). in this case the return value represents an error condition and should be checked to determine if the call was successful.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • @CaptainOblivious Thanks that is a great help, I was unsure what the second parameter was. Also now when I output the result I am getting 22 rather than the localtime. In Addition I am currently using http://www.cplusplus.com/reference/ for my documentation is there any other detailed documentation that I should be looking at? Once again I appreciate your detailed answer. – PapaSmurf Dec 29 '14 at 14:52
  • Updated my answer to cover problems with sending the time to `cout`. – Captain Obvlious Dec 29 '14 at 14:57
  • @CaptainOblivious Using the answer to cover the cout problem, visual studio won't let me run asctime. So instead I tried using asctime_s which then displays "no instance of overloaded function "asctime_s" matches the argument list, argument types are : (tm *, time_t *)" However using a pointer to timeinfo and rawtime causes further errors. – PapaSmurf Dec 29 '14 at 15:09
0

You didn't pass second parameter to localtime_s.

time_t rawtime;
struct tm timeinfo;
errno_t error = localtime_s(&timeinfo, &rawtime);

It returns zero if successful. The return value is an error code if there is a failure. Error codes are defined in Errno.h.

ravi
  • 10,994
  • 1
  • 18
  • 36