Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QStringList can replace int back?
Forum Updated to NodeBB v4.3 + New Features

QStringList can replace int back?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 440 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.
  • D Offline
    D Offline
    DhanyaRaghav
    wrote on 14 Mar 2023, 03:12 last edited by
    #1

    Hi,
    I have QStringList that hold time (say 03:13:23 PM). I shall split and change this to 24 hour format and then replace back to same QString.
    QString str("03:13:23 PM");
    QStringList hrs = qstrData.split(QRegExp("\ "));
    int hours = hrs[1].split(":").first().toInt();
    if(str.contains("PM") && hours < 12)
    {
    hours += 12;
    }
    hrs[1].replace(hrs[1].split(":").first().length(), str.split(":").first().length(), hours);

    This replaces hrs[1][0] = 0; hrs[1][1] = 12; but expected result is hrs[1][0] = 1; and hrs[1][1] = 2;

    Can please any one help to get the desired output.
    Thanks in advance.

    B 1 Reply Last reply 14 Mar 2023, 03:38
    0
    • D DhanyaRaghav
      14 Mar 2023, 03:12

      Hi,
      I have QStringList that hold time (say 03:13:23 PM). I shall split and change this to 24 hour format and then replace back to same QString.
      QString str("03:13:23 PM");
      QStringList hrs = qstrData.split(QRegExp("\ "));
      int hours = hrs[1].split(":").first().toInt();
      if(str.contains("PM") && hours < 12)
      {
      hours += 12;
      }
      hrs[1].replace(hrs[1].split(":").first().length(), str.split(":").first().length(), hours);

      This replaces hrs[1][0] = 0; hrs[1][1] = 12; but expected result is hrs[1][0] = 1; and hrs[1][1] = 2;

      Can please any one help to get the desired output.
      Thanks in advance.

      B Offline
      B Offline
      Bonnie
      wrote on 14 Mar 2023, 03:38 last edited by
      #2

      @DhanyaRaghav
      You should do some debugging, then you'll find hrs[1] is "PM", not "03:13:23".
      Your replace statement is too complicated I haven't check that yet.
      Why not use QLocale + QTime? That's much easier, just one line

      QLocale::c().toTime(str, "hh:mm:ss A").toString("HH:mm:ss")
      
      1 Reply Last reply
      1
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 14 Mar 2023, 04:24 last edited by
        #3

        you are reinventing the wheel. Use QTime to read the QString time text into the object, then use the class methods to extract or print in the format you wish.

        D 1 Reply Last reply 14 Mar 2023, 05:10
        4
        • K Kent-Dorfman
          14 Mar 2023, 04:24

          you are reinventing the wheel. Use QTime to read the QString time text into the object, then use the class methods to extract or print in the format you wish.

          D Offline
          D Offline
          DhanyaRaghav
          wrote on 14 Mar 2023, 05:10 last edited by
          #4

          @Kent-Dorfman
          Thanks for the reply.
          Could you please elaborate much more on it.

          J 1 Reply Last reply 14 Mar 2023, 05:12
          0
          • D DhanyaRaghav
            14 Mar 2023, 05:10

            @Kent-Dorfman
            Thanks for the reply.
            Could you please elaborate much more on it.

            J Online
            J Online
            jsulm
            Lifetime Qt Champion
            wrote on 14 Mar 2023, 05:12 last edited by
            #5

            @DhanyaRaghav
            https://doc.qt.io/qt-6/qtime.html#fromString - use it to convert your string into QTime
            https://doc.qt.io/qt-6/qtime.html#toString - use it to convert QTime into a QString in format you wish

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply 14 Mar 2023, 05:26
            1
            • J jsulm
              14 Mar 2023, 05:12

              @DhanyaRaghav
              https://doc.qt.io/qt-6/qtime.html#fromString - use it to convert your string into QTime
              https://doc.qt.io/qt-6/qtime.html#toString - use it to convert QTime into a QString in format you wish

              D Offline
              D Offline
              DhanyaRaghav
              wrote on 14 Mar 2023, 05:26 last edited by DhanyaRaghav
              #6

              @jsulm
              I have different set of format assigned to qDateTime based on data I receive.

                  QDateTime qDateTime, qDateTimeResult;
                      qDateTime = QDateTime::fromString(qstrData, "MM/dd/yyyy HH:mm:ss AP");
                  qDateTimeResult = QDateTime::fromString(qDateTime.toString(), "MM/dd/yyyy HH:mm:ss");
              

              DateTimeUtc dateTimeUpdate(qDateTimeResult);
              if (qDateTimeResult.isValid())
              {
              }

              qDateTimeResult is always invalid, why?

              J B 2 Replies Last reply 14 Mar 2023, 05:38
              0
              • D DhanyaRaghav
                14 Mar 2023, 05:26

                @jsulm
                I have different set of format assigned to qDateTime based on data I receive.

                    QDateTime qDateTime, qDateTimeResult;
                        qDateTime = QDateTime::fromString(qstrData, "MM/dd/yyyy HH:mm:ss AP");
                    qDateTimeResult = QDateTime::fromString(qDateTime.toString(), "MM/dd/yyyy HH:mm:ss");
                

                DateTimeUtc dateTimeUpdate(qDateTimeResult);
                if (qDateTimeResult.isValid())
                {
                }

                qDateTimeResult is always invalid, why?

                J Online
                J Online
                jsulm
                Lifetime Qt Champion
                wrote on 14 Mar 2023, 05:38 last edited by
                #7

                @DhanyaRaghav said in QStringList can replace int back?:

                qDateTimeResult is always invalid, why?

                Probably because your qDateTime.toString() returns a different format than "MM/dd/yyyy HH:mm:ss". Why don't you check that? Simply specify the format you need when you call https://doc.qt.io/qt-6/qtime.html#toString

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • D DhanyaRaghav
                  14 Mar 2023, 05:26

                  @jsulm
                  I have different set of format assigned to qDateTime based on data I receive.

                      QDateTime qDateTime, qDateTimeResult;
                          qDateTime = QDateTime::fromString(qstrData, "MM/dd/yyyy HH:mm:ss AP");
                      qDateTimeResult = QDateTime::fromString(qDateTime.toString(), "MM/dd/yyyy HH:mm:ss");
                  

                  DateTimeUtc dateTimeUpdate(qDateTimeResult);
                  if (qDateTimeResult.isValid())
                  {
                  }

                  qDateTimeResult is always invalid, why?

                  B Offline
                  B Offline
                  Bonnie
                  wrote on 14 Mar 2023, 07:08 last edited by Bonnie
                  #8

                  @DhanyaRaghav You shouldn't use HH with AP, change it to hh.
                  Also it will be invalid if the running program's default QLocale::amText()/QLocale::pmText() is not AM/PM.
                  So in my above code I use QLocale::c() which is a simplified English locale to make sure that wouldn't happen.
                  Here you can use

                  QLocale::c().toDateTime(qstrData, "MM/dd/yyyy hh:mm:ss AP")
                  
                  1 Reply Last reply
                  0

                  1/8

                  14 Mar 2023, 03:12

                  • Login

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