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. how to change the RTC date and time in c++?
QtWS25 Last Chance

how to change the RTC date and time in c++?

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 2.8k Views
  • 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.
  • M mpergand

    @Ramkumar-Mohan
    You need to execute some commands with QProcess.
    see: https://www.systutorials.com/docs/linux/man/8-clock/

    C Offline
    C Offline
    candy76041820
    wrote on last edited by
    #5

    @mpergand Or, if you prefer the hard way, call clock_settime(). Maybe you need to sudo.

    M JonBJ 2 Replies Last reply
    0
    • C candy76041820

      @mpergand Or, if you prefer the hard way, call clock_settime(). Maybe you need to sudo.

      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #6

      @candy76041820 said in how to change the RTC date and time in c++?:

      Maybe you need to sudo.

      I wouldn't be surprised if that was the case..

      1 Reply Last reply
      0
      • C candy76041820

        @mpergand Or, if you prefer the hard way, call clock_settime(). Maybe you need to sudo.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #7

        @candy76041820 said in how to change the RTC date and time in c++?:

        @mpergand Or, if you prefer the hard way, call clock_settime(). Maybe you need to sudo.

        closk_settime(): https://stackoverflow.com/a/66894317/489865

        It is also possible that OP's " RTC date and time " might require using Linux hwclock, q.v..

        OP will certainly need some kind of su privilege!

        1 Reply Last reply
        0
        • M mpergand

          @Ramkumar-Mohan
          You need to execute some commands with QProcess.
          see: https://www.systutorials.com/docs/linux/man/8-clock/

          Ramkumar MohanR Offline
          Ramkumar MohanR Offline
          Ramkumar Mohan
          wrote on last edited by Ramkumar Mohan
          #8

          @mpergand

          QString string = ui->dateTime->date(). toString(""yyyy-MM-dd hh: mm");
          QString dateTimeString ("date-s");
          dateTimeString.append(string);
          int systemDateTimeStatus = system(dateTimeString.toStdString().c_str());
          if (systemDateTimeStatus ==-1)
          {
          qDebug() "Failed to change date time";
          }
          int systemHwClockStatus = system("sudo hwclock-w");
          if (systemHwClockStatus == -1),
          {
          qDebug() "Failed to sync hardware clock";
          }

          I wrote this code but it doesn't work properly. It shows the error below,

          ERROR ::

          date: invalid date ‘2022-08-26 hh:mm’
          256

          Any ideas on how to solve it?

          jsulmJ 1 Reply Last reply
          0
          • Ramkumar MohanR Ramkumar Mohan

            @mpergand

            QString string = ui->dateTime->date(). toString(""yyyy-MM-dd hh: mm");
            QString dateTimeString ("date-s");
            dateTimeString.append(string);
            int systemDateTimeStatus = system(dateTimeString.toStdString().c_str());
            if (systemDateTimeStatus ==-1)
            {
            qDebug() "Failed to change date time";
            }
            int systemHwClockStatus = system("sudo hwclock-w");
            if (systemHwClockStatus == -1),
            {
            qDebug() "Failed to sync hardware clock";
            }

            I wrote this code but it doesn't work properly. It shows the error below,

            ERROR ::

            date: invalid date ‘2022-08-26 hh:mm’
            256

            Any ideas on how to solve it?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #9

            @Ramkumar-Mohan said in how to change the RTC date and time in c++?:

            QString string = ui->dateTime->date(). toString(""yyyy-MM-dd hh: mm");

            Why do you call date() here? Date does not contain time.

            QString string = ui->dateTime->toString(""yyyy-MM-dd hh: mm");
            

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

            Ramkumar MohanR 1 Reply Last reply
            0
            • M Offline
              M Offline
              mchinand
              wrote on last edited by mchinand
              #10

              @Ramkumar-Mohan said in how to change the RTC date and time in c++?:

              QString string = ui->dateTime->date(). toString(""yyyy-MM-dd hh: mm");

              You are extracting the date from your QDateTime before converting it to a string, so you no longer have the time. Use QDateTime::toString() instead

              Ramkumar MohanR 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Ramkumar-Mohan said in how to change the RTC date and time in c++?:

                QString string = ui->dateTime->date(). toString(""yyyy-MM-dd hh: mm");

                Why do you call date() here? Date does not contain time.

                QString string = ui->dateTime->toString(""yyyy-MM-dd hh: mm");
                
                Ramkumar MohanR Offline
                Ramkumar MohanR Offline
                Ramkumar Mohan
                wrote on last edited by Ramkumar Mohan
                #11

                @jsulm

                https://developer.toradex.com/linux-bsp/how-to/linux-features/real-time-clock-rtc-linux/

                To set system time, use the date command:

                date -s "2013-11-19 15:11:40"

                Setting the system time using the date command does not automatically synchronize the RTCs. Use the hwclock command after entering the date command to synchronize an RTC with the updated system time:

                hwclock -w

                I did as per above link.

                I'm not sure if this is the correct way or not .

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mchinand
                  wrote on last edited by
                  #12

                  @Ramkumar-Mohan said in how to change the RTC date and time in c++?:

                  date: invalid date ‘2022-08-26 hh:mm’

                  The error and @jsulm and my replies are telling you what is wrong, you formed the string you are using to set the time incorrectly. Your string has 'hh:mm' at the end because those have no special meaning when formatting a QDate, which you are doing.

                  Ramkumar MohanR 1 Reply Last reply
                  0
                  • M mchinand

                    @Ramkumar-Mohan said in how to change the RTC date and time in c++?:

                    QString string = ui->dateTime->date(). toString(""yyyy-MM-dd hh: mm");

                    You are extracting the date from your QDateTime before converting it to a string, so you no longer have the time. Use QDateTime::toString() instead

                    Ramkumar MohanR Offline
                    Ramkumar MohanR Offline
                    Ramkumar Mohan
                    wrote on last edited by
                    #13

                    @Ramkumar-Mohan

                    QProcess Dateprocess (this);
                    Dateprocess.start("sh",QStringList()<<"-c"<<"sudo timedatectl set-timezone Asia/Kolkata");
                    if (!Dateprocess.waitForStarted())
                    {
                    qDebug() << "Error : " << Dateprocess.errorString();
                    }
                    Dateprocess.waitForFinished(-1);

                    QDate curdate=QDate::currentDate();
                    QString datetxt= curdate.toString("yyyy / MM / dd");
                    ui->SysDate_Lbl->setText(datetxt);
                    
                    int systemHwClockStatus = system("sudo hwclock -w");
                    if (systemHwClockStatus == -1 )
                    {
                       qDebug() << "Failed to sync hardware clock";
                    

                    }

                    I have written another code. Check if this code works. How to do? I don't know

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mchinand
                      wrote on last edited by
                      #14

                      Did you try fixing your dateTime string in your original code?

                      1 Reply Last reply
                      0
                      • M mchinand

                        @Ramkumar-Mohan said in how to change the RTC date and time in c++?:

                        date: invalid date ‘2022-08-26 hh:mm’

                        The error and @jsulm and my replies are telling you what is wrong, you formed the string you are using to set the time incorrectly. Your string has 'hh:mm' at the end because those have no special meaning when formatting a QDate, which you are doing.

                        Ramkumar MohanR Offline
                        Ramkumar MohanR Offline
                        Ramkumar Mohan
                        wrote on last edited by
                        #15

                        @mchinand

                        QProcess Dateprocess (this);
                        Dateprocess.start("sh",QStringList()<<"-c"<<"sudo timedatectl set-timezone Asia/Kolkata");
                        if (!Dateprocess.waitForStarted())
                        {
                        qDebug() << "Error : " << Dateprocess.errorString();
                        }
                        Dateprocess.waitForFinished(-1);

                        QDate curdate=QDate::currentDate();
                        QString datetxt= curdate.toString("yyyy / MM / dd");
                        ui->SysDate_Lbl->setText(datetxt);
                        
                        int systemHwClockStatus = system("sudo hwclock -w");
                        if (systemHwClockStatus == -1 )
                        {
                           qDebug() << "Failed to sync hardware clock";
                        }
                        system("sudo timedatectl set-local-rtc 1");
                        

                        In this code, I have written using the shell command. and set the time to RTC using the last line in it.

                        system("sudo timedatectl set-local-rtc 1");

                        I checked with the timedatectl command in the Linux terminal, it shows a warning message.

                        error.JPG

                        tells to set UTC time to RTC. I want to reset the RTC time if it changes. Is this way correct?

                        If I do RTC reset, I want to set the Date and time in a label, if I do this I will get which time update. UTC time or local time . I don't know that. tell me if you know.

                        Thank you.

                        jsulmJ 1 Reply Last reply
                        0
                        • Ramkumar MohanR Ramkumar Mohan

                          @mchinand

                          QProcess Dateprocess (this);
                          Dateprocess.start("sh",QStringList()<<"-c"<<"sudo timedatectl set-timezone Asia/Kolkata");
                          if (!Dateprocess.waitForStarted())
                          {
                          qDebug() << "Error : " << Dateprocess.errorString();
                          }
                          Dateprocess.waitForFinished(-1);

                          QDate curdate=QDate::currentDate();
                          QString datetxt= curdate.toString("yyyy / MM / dd");
                          ui->SysDate_Lbl->setText(datetxt);
                          
                          int systemHwClockStatus = system("sudo hwclock -w");
                          if (systemHwClockStatus == -1 )
                          {
                             qDebug() << "Failed to sync hardware clock";
                          }
                          system("sudo timedatectl set-local-rtc 1");
                          

                          In this code, I have written using the shell command. and set the time to RTC using the last line in it.

                          system("sudo timedatectl set-local-rtc 1");

                          I checked with the timedatectl command in the Linux terminal, it shows a warning message.

                          error.JPG

                          tells to set UTC time to RTC. I want to reset the RTC time if it changes. Is this way correct?

                          If I do RTC reset, I want to set the Date and time in a label, if I do this I will get which time update. UTC time or local time . I don't know that. tell me if you know.

                          Thank you.

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

                          @Ramkumar-Mohan Did you actually read what I wrote?! And also the error message you get?
                          Again: you're trying to set "2022-08-26 hh:mm" as datetime: do you think this is valid datetime?
                          I told you what to change: did you try?

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

                          1 Reply Last reply
                          3

                          • Login

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