从Epoch(1970)到2000年开始的Postgresql时间戳的Qt时间戳。如何添加偏移量并从毫秒转换为微秒?
Qt timestamps from Epoch(1970) to Postgresql timestamps that starts in 2000. How to add an offset and convert from milliseconds to microseconds?
我需要计算从 1970 年到 2000 年的偏移量以将数据插入 postgresql 数据库,因为 postgresql 从 2000 年开始,分辨率为微秒,而 currentMSecsSinceEpoch 从 1970 年开始,以毫秒为单位。 ¿如何添加偏移量才能在 postgresql 数据库中显示相同的日期?
显示计算偏移量并转换为微秒的解决方案:
QString timestamp = "2000-01-01 00:00:00"; //postgres starts here and uses microseconds resolution
QString timestampEpoch = "1970-01-01 00:00:00"; //epoch starts here, Qt has seconds/milliseconds resolution.
//need to compute the interval between these dates in microseconds
QString format = "yyyy-MM-dd HH:mm:ss";//TZD";
QDateTime origin = QDateTime::fromString(timestampEpoch, format);
QDateTime end = QDateTime::fromString(timestamp, format);
qint64 offset = origin.msecsTo(end);
qint64 timeWithOffset = 1000 * (QDateTime::currentMSecsSinceEpoch() - offset); //mseconds to microseconds for postgresql timestamp(8 bytes)
希望对您有所帮助!
我需要计算从 1970 年到 2000 年的偏移量以将数据插入 postgresql 数据库,因为 postgresql 从 2000 年开始,分辨率为微秒,而 currentMSecsSinceEpoch 从 1970 年开始,以毫秒为单位。 ¿如何添加偏移量才能在 postgresql 数据库中显示相同的日期?
显示计算偏移量并转换为微秒的解决方案:
QString timestamp = "2000-01-01 00:00:00"; //postgres starts here and uses microseconds resolution
QString timestampEpoch = "1970-01-01 00:00:00"; //epoch starts here, Qt has seconds/milliseconds resolution.
//need to compute the interval between these dates in microseconds
QString format = "yyyy-MM-dd HH:mm:ss";//TZD";
QDateTime origin = QDateTime::fromString(timestampEpoch, format);
QDateTime end = QDateTime::fromString(timestamp, format);
qint64 offset = origin.msecsTo(end);
qint64 timeWithOffset = 1000 * (QDateTime::currentMSecsSinceEpoch() - offset); //mseconds to microseconds for postgresql timestamp(8 bytes)
希望对您有所帮助!