I have a function which prints time_t values.
void Logger::describe()
{
cout << m_start_time << " " << m_end_time << "\n";
if (ctime(&m_start_time) == ctime(&m_end_time))
{
cout << "check1\n";
}
cout << m_vehicle->getPlate() << " " << ctime(&m_start_time) << " " << ctime(&m_end_time) << "\n";
}
// m_start_time and m_end_time are private variables of type time_t of the class Logger
For a sample output after waiting a couple of seconds I get
1634907786 1634907791
check1
bike1 Fri Oct 22 18:33:06 2021
Fri Oct 22 18:33:06 2021
As can be seen m_start_time and m_end_time are different but ctime returns the the same value. Can anyone help explain why ?
I'm using gcc 6.3.0 if it helps.
ctimereturns achar*, i.e. a pointer to a string buffer. The function does not have to be thread safe. Therefore the same buffer might be reused. You should print the first result before you call again. Also: You only compare buffers, not string content. At least when it comes to C which you have tagged your question with.