我必须编写一个函数来返回与我当前位置的 UTC 的偏移量。令我最惊讶的是,以下代码返回 0:
const QDateTime now = QDateTime::currentDateTime();
return now.toUTC().secsTo(now) / 60;
我必须编写一个函数来返回与我当前位置的 UTC 的偏移量。令我最惊讶的是,以下代码返回 0:
const QDateTime now = QDateTime::currentDateTime();
return now.toUTC().secsTo(now) / 60;
这并不像看起来那么容易,因为 QDateTime::secsTo 在转换为 UTC 后计算偏移量。我在这里找到了答案,但我真的不喜欢转换为字符串并返回。所以我的解决方案是:
const QDateTime dateTime1 = QDateTime::currentDateTime();
const QDateTime dateTime2 = QDateTime(dateTime1.date(), dateTime1.time(), Qt::UTC);
return dateTime1.secsTo(dateTime2) / 60;