Date and time not updating

Hi all,
for a program of mine I have to get the current date and time.

By looking around I found the 2 lines below:
1
2
time_t now = std::time(0);
std::string currentTime = std::ctime(&now);


Of course with the addition of
#include <ctime>

Since I have to do it in different points of the program, I have put them in a function:
1
2
3
4
5
6
7
8
9
10
11
// Return current time
std::string NowTime(){
// current date/time based on current system
time_t now = std::time(0);
std::string currentTime = std::ctime(&now);

// Remove eventual \n or \r from time string
currentTime = RemoveNewLine(currentTime);

return currentTime;
}


The problem is that the above function works correctly only the fist time, but from the second forward it always return the same value.

Is it there something I am doing wrong?
Can't see anything wrong with that function, but how are you calling it and what does the RemoveNewLine function look like, just need the other code to see if there's an error there.
I commented out your RemoveNewLine() function, added a call to Sleep() to make sure the seconds have changed and NowTime () appeared to work for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <ctime>
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

// Return current time
std::string NowTime()
{   // current date/time based on current system
    time_t now = std::time(0);
    std::string currentTime = std::ctime(&now);

    // Remove eventual \n or \r from time string
    //  currentTime = RemoveNewLine(currentTime);

    return currentTime;
}

int main ()
{   cout << NowTime() << endl;
    Sleep (1000);  // Make sure seconds change
    cout << NowTime() << endl;
    system ("pause");
}
Thu Oct 06 15:40:24 2016

Thu Oct 06 15:40:25 2016

Press any key to continue . . .


So if your time is not changing, you're probably not waiting for the seconds to change.
Last edited on
RemoveNewLine just take the string passed and check and eventually remove the last character. I used it elsewhere without issues, so I don't think it is the problem.
1
2
3
4
5
6
7
8
9
10
// Strips eventual \n and \r from a line
std::string RemoveNewLine(std::string lineToCheck)
{
	// TODO: check for a more efficient way
	while((lineToCheck[lineToCheck.size() - 1] == '\r') || (lineToCheck[lineToCheck.size() - 1] == '\n')){
		lineToCheck.resize(lineToCheck.size() - 1);
	}

	return lineToCheck;
}



For the moment NowTime() is called only by one other function:
1
2
3
4
5
6
// Save the information to the log file
void Log (std::string logInfo){
	// Print the item to log and save it to the log file
	std::cout << "[" << NowTime() << "] " << logInfo << std::endl;
	logFile <<  "[" << NowTime() << "] " << logInfo << std::endl;
}



As for AbstractionAnon post and example, I tried to use the Sleep command to test it and still nothing:
Thu Oct 06 22:56:25 2016
Thu Oct 06 22:56:25 2016
Hi all,
I solved the problem and ... it is kind of awkward. :D

In the original implementation of NowTime() I declared the variables as "static" thinking that in this way they would not be deleted and recreated every time the function is called (eventually later a friend of mine explained it wasn't exactly the typical "use case" for the static anyway).

When I was making the original post I started to suspect that those "static" may have been the cause so I removed them (it is for this reason they don't appear in my original post!!!) and I tried the application ... it still didn't work (reason in the next paragraph). To compound the problem, since it didn't seems to solve the issue I have putted the "static" back in and I have tested your answers with those "static" in the code.

Today I tried again to remove the "static" and suddenly the function started to work correctly!!! Why didn't it work while I was making this post???? Because when I removed those "static" I forgot to recompile the application before testing it; therefore the "static" wasn't in the code any longer, but the binary still did.

Sorry to have wasted your time. :(
Be aware that initialization of a static is only done once, not each time you enter the function. The following would have worked with static variables, although there is no need for static variables.

1
2
3
4
5
6
7
8
9
// Return current time
std::string NowTime()
{  static time_t now;
    static std::string currentTime;

    now = std::time(0);
    currentTime = std::ctime(&now);
    return currentTime;
}


I hope you've also learned the importance of posting the actual code that is not working.

Topic archived. No new replies allowed.