0

All is in the title! I develop on Qt 4.7.1 and it's for on a Nokia N8.

I think that I need to use: QDateTime and timeSpec (Qt::OffsetFromUTC).

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
Fabrice
  • 105
  • 2
  • 7

2 Answers2

0

Below is a function that returns the UTC/GMT offset for any timezone. For negative UTC offsets you must override this function and check the boolean "isNegative". I use this for sending requests to a server, if I want to check that its not the day the clock moves forwards or backwards I just call the function twice, once with today's date and then with tomorrow's date. If they both return the same then we know that the clock isn't switching in the next 24 hrs for Daylight Savings time.

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;
}

My solution is also posted here: Timezone offset

stackunderflow
  • 10,122
  • 4
  • 20
  • 29
0

Yes and no. You're correct that Qt::OffsetFromUTC gives you the currently-in-use value.

But this will change given daylight saving time rules for the timezone you're in. It's a long-outstanding (not yet implemented) request to add full timezone support to QDateTime:

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

I.e. right now, if you're using a device in France and ask for the UTC offset, you'll get one hour, but when switching to DST in March that'll change to two hours. Please keep that in mind.

rene
  • 41,474
  • 78
  • 114
  • 152
FrankH.
  • 17,675
  • 3
  • 44
  • 63
  • 1
    I think the new link for that now is https://bugreports.qt-project.org/browse/QTBUG-29666 – Vadim Peretokin Sep 10 '13 at 07:30
  • the bugreports URL has changed agreed, the bug IDs haven't; the above is still https://bugreports.qt-project.org/browse/QTBUG-71 - if anyone cares (a lot), close the -29666 one as duplicate (-71 is still unresolved). – FrankH. Sep 11 '13 at 14:40