Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Time Conversion
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Time Conversion

Scheduled Pinned Locked Moved QML and Qt Quick
15 Posts 7 Posters 17.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    giesbert
    wrote on last edited by
    #5

    You can use QString::arg like here:

    @
    QString text = QString("%1").arg(intVal, // value
    6, // field width
    10, // base for conversion
    QLatin1Char('0')); // fill char
    @

    Nokia Certified Qt Specialist.
    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

    1 Reply Last reply
    0
    • D Offline
      D Offline
      david.luggen
      wrote on last edited by
      #6

      Thx a lot. I tried to implement the QTimeSpan class into my creator but it isn't recognized by it. Anyway, if it's within QT 4.8, I can wait, until then I solved it like this:

      @QString timeConversion::timeConversion(int msecs)
      {
      QString formattedTime;

      int hours = msecs/(1000*60*60);
      int minutes = (msecs-(hours*1000*60*60))/(1000*60);
      int seconds = (msecs-(minutes*1000*60)-(hours*1000*60*60))/1000;
      int milliseconds = msecs-(seconds*1000)-(minutes*1000*60)-(hours*1000*60*60);
      
      formattedTime.append(QString("%1").arg(hours, 2, 10, QLatin1Char('0')) + ":" +
                           QString( "%1" ).arg(minutes, 2, 10, QLatin1Char('0')) + ":" +
                           QString( "%1" ).arg(seconds, 2, 10, QLatin1Char('0')) + ":" +
                           QString( "%1" ).arg(milliseconds, 3, 10, QLatin1Char('0')));
      
      return formattedTime;
      

      }@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #7

        No, QTimeSpan is not in Qt yet, but you can simply copy/paste the code out. I do that too in my current project. I can make the code available for easy inclusion in current projects, if you like.

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #8
          • If the idea is to get time and date like "Thu May 12 11:50:21 CEST 2011", I'd use time(...) to get the time, then ctime(...) to get it as a char*, then QString(...) to bring it back to Qt.

          • If some other format is needed, I'd use time(...), then gmtime(...) or localtime(...), then sprintf(...), then QString(...).

          --

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #9

            No, the idea was to get a good format for a period of time, not for a point in time. Hours might be more than 24, for instance.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              david.luggen
              wrote on last edited by
              #10

              Well, I don't know much "around" QT and the Creator. I've tried to copy the code where your link leads to in my QT Folder.

              src/corelib/global/qnamespace.h <- added the necessary code
              src/corelib/tools/qtimespan.cpp <- downloaded and copied into "tools"
              src/corelib/tools/qtimespan.h <- downloaded and copied into "tools"
              src/corelib/tools/tools.pri <- added necessary code

              Finally tried to #include "qtimespan.h" in my project but it didn't recognized anything.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #11

                OK, I have created an "easily downloadable link":http://dl.dropbox.com/u/16442531/qtimespan.zip for a package you can just use until the time it is in part of Qt proper.

                You can just put the .h and the .cpp files in your sources directory, and add them to your .pro file. Then, where you want to use QTimeSpan, just #include "qtimespan.h".

                Note that doing this, will generate a compilation warning about a redefinition of Q_EXPORT. It is save to ignore that in this case. \

                Instead of including the files directly in your .pro file, you can also use the include() directive to include the .pri file in your project.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  david.luggen
                  wrote on last edited by
                  #12

                  There is just to add, thx!

                  1 Reply Last reply
                  0
                  • NIvil WilsonN Offline
                    NIvil WilsonN Offline
                    NIvil Wilson
                    wrote on last edited by
                    #13

                    QTime n(0,0,0,0);
                    QTime t=n.addMSecs(value);
                    QString result=t.toString("hh:mm:ss.zzz");

                    __mk___ 1 Reply Last reply
                    0
                    • NIvil WilsonN NIvil Wilson

                      QTime n(0,0,0,0);
                      QTime t=n.addMSecs(value);
                      QString result=t.toString("hh:mm:ss.zzz");

                      __mk___ Offline
                      __mk___ Offline
                      __mk__
                      wrote on last edited by
                      #14

                      @NIvil-Wilson said in [SOLVED] Time Conversion:

                      QTime n(0,0,0,0);
                      QTime t=n.addMSecs(value);
                      QString result=t.toString("hh:mm:ss.zzz");

                      This is the best way to do it in modern Qt versions.
                      Unfortunately, you cannot construct a QTime object directly from a millisecond value and we have to rely on the addMSecs method.

                      Heres my slightly shorter take:

                      QString result = QTime(0,0).addMSecs(value).toString("hh:mm:ss.zzz");
                      
                      JonBJ 1 Reply Last reply
                      0
                      • __mk___ __mk__

                        @NIvil-Wilson said in [SOLVED] Time Conversion:

                        QTime n(0,0,0,0);
                        QTime t=n.addMSecs(value);
                        QString result=t.toString("hh:mm:ss.zzz");

                        This is the best way to do it in modern Qt versions.
                        Unfortunately, you cannot construct a QTime object directly from a millisecond value and we have to rely on the addMSecs method.

                        Heres my slightly shorter take:

                        QString result = QTime(0,0).addMSecs(value).toString("hh:mm:ss.zzz");
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #15

                        @__mk__ said in [SOLVED] Time Conversion:

                        Unfortunately, you cannot construct a QTime object directly from a millisecond value

                        What about (untested) QTime QTime::fromMSecsSinceStartOfDay(int msecs)

                        Returns a new QTime instance with the time set to the number of msecs since the start of the day, i.e. since 00:00:00.

                        ?

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved