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.
  • R Offline
    R Offline
    Ramkumar Mohan
    wrote on 5 Aug 2022, 10:23 last edited by
    #1

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

    J 1 Reply Last reply 5 Aug 2022, 10:56
    0
    • R Ramkumar Mohan
      5 Aug 2022, 10:23

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

      J Offline
      J Offline
      JonB
      wrote on 5 Aug 2022, 10:56 last edited by
      #2

      @Ramkumar-Mohan
      This depends on the OS, regardless of C++ or Qt.

      R 1 Reply Last reply 5 Aug 2022, 11:40
      0
      • J JonB
        5 Aug 2022, 10:56

        @Ramkumar-Mohan
        This depends on the OS, regardless of C++ or Qt.

        R Offline
        R Offline
        Ramkumar Mohan
        wrote on 5 Aug 2022, 11:40 last edited by
        #3

        @JonB ok, but how to set the RTC date and time in (Linux OS) programmatically?

        M 1 Reply Last reply 5 Aug 2022, 12:20
        0
        • R Ramkumar Mohan
          5 Aug 2022, 11:40

          @JonB ok, but how to set the RTC date and time in (Linux OS) programmatically?

          M Offline
          M Offline
          mpergand
          wrote on 5 Aug 2022, 12:20 last edited by
          #4

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

          C R 2 Replies Last reply 5 Aug 2022, 13:33
          0
          • M mpergand
            5 Aug 2022, 12:20

            @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 5 Aug 2022, 13:33 last edited by
            #5

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

            M J 2 Replies Last reply 5 Aug 2022, 13:40
            0
            • C candy76041820
              5 Aug 2022, 13:33

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

              M Offline
              M Offline
              mpergand
              wrote on 5 Aug 2022, 13:40 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
                5 Aug 2022, 13:33

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

                J Offline
                J Offline
                JonB
                wrote on 5 Aug 2022, 14:31 last edited by JonB 8 May 2022, 14:36
                #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
                  5 Aug 2022, 12:20

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

                  R Offline
                  R Offline
                  Ramkumar Mohan
                  wrote on 26 Aug 2022, 12:36 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?

                  J 1 Reply Last reply 26 Aug 2022, 12:40
                  0
                  • R Ramkumar Mohan
                    26 Aug 2022, 12:36

                    @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?

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 26 Aug 2022, 12:40 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

                    R 1 Reply Last reply 26 Aug 2022, 12:51
                    0
                    • M Offline
                      M Offline
                      mchinand
                      wrote on 26 Aug 2022, 12:41 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

                      R 1 Reply Last reply 26 Aug 2022, 16:47
                      0
                      • J jsulm
                        26 Aug 2022, 12:40

                        @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");
                        
                        R Offline
                        R Offline
                        Ramkumar Mohan
                        wrote on 26 Aug 2022, 12:51 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 26 Aug 2022, 12:57 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.

                          R 1 Reply Last reply 27 Aug 2022, 07:29
                          0
                          • M mchinand
                            26 Aug 2022, 12:41

                            @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

                            R Offline
                            R Offline
                            Ramkumar Mohan
                            wrote on 26 Aug 2022, 16:47 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 26 Aug 2022, 17:22 last edited by
                              #14

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

                              1 Reply Last reply
                              0
                              • M mchinand
                                26 Aug 2022, 12:57

                                @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.

                                R Offline
                                R Offline
                                Ramkumar Mohan
                                wrote on 27 Aug 2022, 07:29 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.

                                J 1 Reply Last reply 29 Aug 2022, 05:48
                                0
                                • R Ramkumar Mohan
                                  27 Aug 2022, 07:29

                                  @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.

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 29 Aug 2022, 05:48 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