WEuropeanStandardTime



  • Hi everybody.
    Is there someone that knows this time of timezone and is there a way to have it with a Qt object?

    WEuropeanStandardTime-1DaylightTime,M3.5.0,M10.5.0/3
    (means +01:00 (Rome) time zone)

    Regards.


  • Qt Champions 2016

    Do you mean QTimeZone?



  • Not exactly. I need to obtain the string above but I don't know how.


  • Qt Champions 2016

    Well I have no idea what the string above means, but any timezone information you might need should be available through the timezone class.


  • Moderators

    @mrdebug said in WEuropeanStandardTime:

    Is there someone that knows this time of timezone

    That looks like a POSIX TZ string: http://www.di-mgt.com.au/wclock/tz.html

    and is there a way to have it with a Qt object?

    Not at the moment.

    How do you wish to use that string?



  • I need that string to implement SetDateTime onvif request


  • Moderators

    @mrdebug said in WEuropeanStandardTime:

    I need that string to implement SetDateTime onvif request

    The string format is described at http://www.di-mgt.com.au/wclock/tz.html



  • @mrdebug said in WEuropeanStandardTime:

    I need that string to implement SetDateTime onvif request

    Hmm. That's a particularly ugly format, IMO... I'd be interested in seeing the documentation that says you need to use it. Maybe that API supports alternative formats too?

    Anyway, to get an idea of how you might do it with Qt...

    #include <QDateTime>
    #include <QTimeZone>
    
    QString quotePosix1StdDst(const QString &stdDst)
    {
        foreach (const QChar c, stdDst) {
            if (!c.isLetter()) {
                return QString::fromLatin1("<%1>").arg(stdDst);
            }
        }
        return stdDst;
    }
    
    QString toPosix1Time(const int offset, const bool useSign = false)
    {
        QString str;
        if (useSign) {
            str += QLatin1Char((offset >= 0) ? '+' : '-');
        }
        str += QString::number(qAbs(offset) / 3600);
        if ((offset % 3600) != 0) {
            str += QString::fromLatin1(":%1").arg((qAbs(offset) % 3600) / 60, 2, 10, QLatin1Char('0'));
        }
        if ((offset % 60) != 0) {
            str += QString::fromLatin1(":%1").arg(qAbs(offset) % 60, 2, 10, QLatin1Char('0'));
        }
        return str;
    }
    
    QString toPosix1Rule(const QDateTime &dt)
    {
        return QString::fromLatin1("M%1.%2.%3/%4")
            .arg(dt.date().month())
            .arg((dt.date().day() - dt.date().dayOfWeek()) / 7 + 1)
            .arg(dt.date().dayOfWeek()%7) // Convert 1-based to 0-based.
            .arg(toPosix1Time(dt.time().msecsSinceStartOfDay()/1000));
    }
    
    QString toPosix1Tz(const QTimeZone &tz,
                       const QTimeZone::NameType nt = QTimeZone::ShortName)
    {
        // std
        QString str = quotePosix1StdDst(tz.displayName(QTimeZone::StandardTime, nt));
    
        // offset (if no transitions)
        if (!tz.hasTransitions()) {
            str += toPosix1Time(tz.offsetFromUtc(QDateTime::currentDateTime()), true);
            return str;
        }
    
        // Get the first DST and non-DST transitions.
        QTimeZone::OffsetData dst, nonDst;
        {
            const QDateTime dt = QDateTime::currentDateTimeUtc(); // Any valid, unambiguous timestamp.
            const QTimeZone::OffsetData data1 = tz.previousTransition(dt);
            const QTimeZone::OffsetData data2 = tz.previousTransition(data1.atUtc);
            Q_ASSERT(data1.atUtc.isValid() && data2.atUtc.isValid());
            Q_ASSERT((data1.daylightTimeOffset == 0) || (data2.daylightTimeOffset == 0));
            Q_ASSERT((data1.daylightTimeOffset != 0) || (data2.daylightTimeOffset != 0));
            dst = (data1.daylightTimeOffset == 0) ? data2 : data1;
            nonDst = (data1.daylightTimeOffset == 0) ? data1 : data2;
        }
    
        // offset
        str += toPosix1Time(dst.standardTimeOffset, true);
    
        // dst
        str += dst.abbreviation;
    
        // dst-offset
        str += toPosix1Time(dst.daylightTimeOffset, true);
    
        // start
        str += QLatin1Char(',') + toPosix1Rule(dst.atUtc.toTimeZone(tz));
    
        // end
        str += QLatin1Char(',') + toPosix1Rule(nonDst.atUtc.toTimeZone(tz));
    
        return str;
    }
    
    void tryIt()
    {
        foreach (const QByteArray &ianaId, QTimeZone::availableTimeZoneIds()) {
            qDebug() << ianaId << toPosix1Tz(QTimeZone(ianaId));
        }
    }
    

    That code seems to work mostly - that is, the output looks okay at a quick glance. I guarantee it will have bugs, issues, and failed corner cases, but it does give you an idea of what might be required.

    Good luck! :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.