QDateTime function toSecsSinceEpoch() returning 0
-
Hello,
I would like to retrieve the number of seconds since Epoch from a date so I try to use the toSecsSinceEpoch given by the QDateTime class.
But it always returns 0, do you have any idea on what could generate this problem? (Maybe the fact that my date is not in UTC? But I use toUtc() to try, change nothing)
Here is the source sample :
QDateTime newDate = QDateTime::fromString(dateString, Qt::ISODate); std::cout << "New date reception : " << qPrintable(newDate.toString()) << std::endl; std::cout << "New date secs" << newDate.toSecsSinceEpoch() << std::endl;
Output:
New date reception Mon Apr 25 12:26:02 2022 GMT New date secs : 0
Thank for your answer and sorry for my approximative english :)
-
@Pantoufle Should work.
What does https://doc.qt.io/qt-5/qdatetime.html#isValid return? -
@Pantoufle said in QDateTime function toSecsSinceEpoch() returning 0:
Here is the source sample :
QDateTime newDate = QDateTime::fromString(dateString, Qt::ISODate); std::cout << "New date reception : " << qPrintable(newDate.toString()) << std::endl; std::cout << "New date secs" << newDate.toSecsSinceEpoch() << std::endl;
As output and code do not totally match, I am not sure which part is the correct one: code or output.
Please check if parsing string has really worked:QDateTime newDate = QDateTime::fromString(dateString, Qt::ISODate); std::cout << "New date reception: " << (newDate.isValid() ? "OK" : "INVALID") << std::endl; std::cout << "Unix time: " << newDate.toSecsSinceEpoch() << "/" << newDate.toTime_t() << std::endl;
-
@Pantoufle Should work.
What does https://doc.qt.io/qt-5/qdatetime.html#isValid return?It return true
As output and code do not totally match, I am not sure which part is the correct one: code or output.
It does not tottaly match cause I use a printf custom function in my embedded system so I translate it so you can see clearly.
Also to check if it was not my string i test with this :
QDateTime::currentDateTime().toSecsSinceEpoch()
and it also doesn't work :(