0

一切尽在标题中!我在 Qt 4.7.1 上开发,它是在诺基亚 N8 上开发的。

我认为我需要使用:QDateTimetimeSpec (Qt::OffsetFromUTC)

4

2 回答 2

0

是和不是。您是正确的,它Qt::OffsetFromUTC为您提供了当前使用的价值。

但是,鉴于您所在时区的夏令时规则,这将发生变化。将完整时区支持添加到以下位置是一项长期未决(尚未实施)的请求QDateTime

http://bugreports.qt-project.org/browse/QTBUG-71

即现在,如果您在法国使用设备并要求 UTC 偏移量,您将获得一小时,但在三月份切换到 DST 时,这将变为两小时。请记住这一点。

于 2010-12-06T12:16:09.050 回答
0

下面是一个返回任何时区的 UTC/GMT 偏移量的函数。对于负 UTC 偏移量,您必须覆盖此函数并检查布尔值“isNegative”。我用它来向服务器发送请求,如果我想检查它不是时钟向前或向后移动的那一天,我只需调用该函数两次,一次是今天的日期,然后是明天的日期。如果它们都返回相同的值,那么我们就知道在接下来的 24 小时内时钟没有切换到夏令时。

QTime Calendar::getTimeZoneDiff(QDateTime midnightDateTime, bool &isNegative) {
    midnightDateTime.setTime(QTime(0,0));
    QDateTime utc   = midnightDateTime.toUTC();
    QDateTime local = midnightDateTime; 
    local.setTimeSpec(Qt::LocalTime);
    QDateTime offset = local.toUTC();
    QTime properTimeOffset = QTime(offset.time().hour(), offset.time().minute());
    offset.setTimeSpec(Qt::LocalTime);
    utc.setTimeSpec(Qt::UTC);

    if(offset.secsTo(utc) < 0){
        isNegative = true;
    }else{
        isNegative = false;
       properTimeOffset.setHMS(24 - properTimeOffset.hour() - (properTimeOffset.minute()/60.0) - (properTimeOffset.second()/3600.0), properTimeOffset.minute() - (properTimeOffset.second()/60.0), properTimeOffset.second());
        if(!properTimeOffset.isValid()){ //Midnight case
            properTimeOffset.setHMS(0,0,0);
        }
    }
   return properTimeOffset;
}

我的解决方案也在这里发布:时区偏移

于 2012-09-19T16:50:07.403 回答