Question concerning QTimeZone/QDateTime Class for World Time Clock



  • I plan to program a world time clock on my raspberry with QT.
    For beginning I watched this video on Youtube

    With one digital clock and the local time zone I have a good function, but I have many problems and errors when I add other timezones (different from the local time, for example New York City):

    Here a code-snippet:

       
        //Germany
        QDateTime time = QDateTime::currentDateTime();
        //New York
        QDateTime time2 = time.toTimeZone("America/New_York");
    
        //Convert
        QString time_text = time.toString("hh:mm");
        QString time_text2 = time2.toString("hh:mm");
    
        //Output 
        ui->lcdNumber->display(time_text);
        ui->lcdNumber_2->display(time_text2);
    

    After building with QT-Designer I get the following error:
    Error: no matching function for call to 'QDateTime::toTimeZone(const char [17])'
    Note: no known conversion for argument 1 from 'const char [17]' to 'const QTimeZone&'

    Question
    I do not unterstand the QDateTime::toTimeZone(parameter). Which parameter do I have to insert?
    I read a lot, but i found not so many examples. Can you help me?


  • Moderators

    @Donni_Duck

    There is no automatic conversion from QString to QTimeZone as the compiler says.

    From where did you get the string used?

    You need to specify QTimeZone, but there you cannot use directly the string you used.



  • @koahnig
    Thank you for the quick answer.
    I have the string from the following website: clock.zone

    Can you please give me an example how to specify QTimeZone? Which one is the best for my world time clock?
    Thank you.


  • Moderators

    @Donni_Duck

    Either you use the QTimeZone constructor, but I am at loss of possible values there. You need to check with the lists given later in the documentation.

    Or you are using the constructor QTimeZone with the offset of seconds to UTC.

    For your example it should be:

    QDateTime time2 = time.toTimeZone( QTimeZone (-5 * 3600) );
    

    Check the sign, if I got it right.


  • Lifetime Qt Champion

    Hi,

    Another possibility is to use:

    time.toTimeZone(QTimeZone(QByteArrayLiteral("America/New_York")));
    

    Note that QTimeZone provides the list of available time zones on the system so you could leverage that and use the content of its output to generate the conversion(s) you want.


Log in to reply
 

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