2

我必须编写一个函数来返回与我当前位置的 UTC 的偏移量。令我最惊讶的是,以下代码返回 0:

const QDateTime now = QDateTime::currentDateTime();
return now.toUTC().secsTo(now) / 60;
4

1 回答 1

6

这并不像看起来那么容易,因为 QDateTime::secsTo 在转换为 UTC 后计算偏移量。我在这里找到了答案,但我真的不喜欢转换为字符串并返回。所以我的解决方案是:

const QDateTime dateTime1 = QDateTime::currentDateTime();
const QDateTime dateTime2 = QDateTime(dateTime1.date(), dateTime1.time(), Qt::UTC);
return dateTime1.secsTo(dateTime2) / 60;
于 2014-06-18T09:21:41.613 回答