【问题标题】:How to find out the UTC offset of my current location in Qt 5.1?如何在 Qt 5.1 中找出我当前位置的 UTC 偏移量?
【发布时间】:2014-08-08 12:35:02
【问题描述】:

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

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

【问题讨论】:

    标签: qt utc timezone-offset qdatetime


    【解决方案1】:

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

    const QDateTime dateTime1 = QDateTime::currentDateTime();
    const QDateTime dateTime2 = QDateTime(dateTime1.date(), dateTime1.time(), Qt::UTC);
    return dateTime1.secsTo(dateTime2) / 60;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 2014-07-27
      • 2018-03-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      相关资源
      最近更新 更多