QDateTime.toTime_t() output a wrong result
-
Hello,
I am trying to get a Unix time stamp in my QT application but it outputs a wrong date.QDateTime time = QDateTime::fromString("1966-06-21 00:00:00", "yyyy-MM-dd hh:mm:ss"); qDebug() << time.toString("yyyy-MM-dd hh:mm:ss.zzz"); uint t = time.toTime_t(); qDebug() << QDateTime::fromTime_t(t).toString("yyyy-MM-dd hh:mm:ss.zzz");it outputs:
"1966-06-21 00:00:00.000" "2106-02-07 14:28:15.000"Why does it output 2106? how do I get the Unix time stamp?
-
Hello,
I am trying to get a Unix time stamp in my QT application but it outputs a wrong date.QDateTime time = QDateTime::fromString("1966-06-21 00:00:00", "yyyy-MM-dd hh:mm:ss"); qDebug() << time.toString("yyyy-MM-dd hh:mm:ss.zzz"); uint t = time.toTime_t(); qDebug() << QDateTime::fromTime_t(t).toString("yyyy-MM-dd hh:mm:ss.zzz");it outputs:
"1966-06-21 00:00:00.000" "2106-02-07 14:28:15.000"Why does it output 2106? how do I get the Unix time stamp?
@wuze said in QDateTime.toTime_t() output a wrong result:
Why does it output 2106? how do I get the Unix time stamp?
A UNIX time stamp is a signed number to allow for dates before the epoch start. Usually time_t is defined the same way.
This long-obsolete function returns an unsigned integer number of seconds after 1 Jan 1970 as @Christian-Ehrlicher points out. How it behaves for dates outside this range is clearly documented. This page also directs you to replacements.
The function you are looking for is
QDateTime::toSecsSinceEpoch() -
Hello,
I am trying to get a Unix time stamp in my QT application but it outputs a wrong date.QDateTime time = QDateTime::fromString("1966-06-21 00:00:00", "yyyy-MM-dd hh:mm:ss"); qDebug() << time.toString("yyyy-MM-dd hh:mm:ss.zzz"); uint t = time.toTime_t(); qDebug() << QDateTime::fromTime_t(t).toString("yyyy-MM-dd hh:mm:ss.zzz");it outputs:
"1966-06-21 00:00:00.000" "2106-02-07 14:28:15.000"Why does it output 2106? how do I get the Unix time stamp?
@wuze said in QDateTime.toTime_t() output a wrong result:
how do I get the Unix time stamp?
You won't since the unix time starts at 01.01.1970
-
Hello,
I am trying to get a Unix time stamp in my QT application but it outputs a wrong date.QDateTime time = QDateTime::fromString("1966-06-21 00:00:00", "yyyy-MM-dd hh:mm:ss"); qDebug() << time.toString("yyyy-MM-dd hh:mm:ss.zzz"); uint t = time.toTime_t(); qDebug() << QDateTime::fromTime_t(t).toString("yyyy-MM-dd hh:mm:ss.zzz");it outputs:
"1966-06-21 00:00:00.000" "2106-02-07 14:28:15.000"Why does it output 2106? how do I get the Unix time stamp?
@wuze said in QDateTime.toTime_t() output a wrong result:
Why does it output 2106? how do I get the Unix time stamp?
A UNIX time stamp is a signed number to allow for dates before the epoch start. Usually time_t is defined the same way.
This long-obsolete function returns an unsigned integer number of seconds after 1 Jan 1970 as @Christian-Ehrlicher points out. How it behaves for dates outside this range is clearly documented. This page also directs you to replacements.
The function you are looking for is
QDateTime::toSecsSinceEpoch() -
@wuze said in QDateTime.toTime_t() output a wrong result:
Why does it output 2106? how do I get the Unix time stamp?
A UNIX time stamp is a signed number to allow for dates before the epoch start. Usually time_t is defined the same way.
This long-obsolete function returns an unsigned integer number of seconds after 1 Jan 1970 as @Christian-Ehrlicher points out. How it behaves for dates outside this range is clearly documented. This page also directs you to replacements.
The function you are looking for is
QDateTime::toSecsSinceEpoch() -
A aha_1980 has marked this topic as solved on