how to change the RTC date and time in c++?
-
wrote on 5 Aug 2022, 10:23 last edited by
how to change the RTC date and time in c++?
-
how to change the RTC date and time in c++?
wrote on 5 Aug 2022, 10:56 last edited by@Ramkumar-Mohan
This depends on the OS, regardless of C++ or Qt. -
@Ramkumar-Mohan
This depends on the OS, regardless of C++ or Qt.wrote on 5 Aug 2022, 11:40 last edited by@JonB ok, but how to set the RTC date and time in (Linux OS) programmatically?
-
@JonB ok, but how to set the RTC date and time in (Linux OS) programmatically?
wrote on 5 Aug 2022, 12:20 last edited by@Ramkumar-Mohan
You need to execute some commands with QProcess.
see: https://www.systutorials.com/docs/linux/man/8-clock/ -
@Ramkumar-Mohan
You need to execute some commands with QProcess.
see: https://www.systutorials.com/docs/linux/man/8-clock/wrote on 5 Aug 2022, 13:33 last edited by@mpergand Or, if you prefer the hard way, call clock_settime(). Maybe you need to sudo.
-
@mpergand Or, if you prefer the hard way, call clock_settime(). Maybe you need to sudo.
wrote on 5 Aug 2022, 13:40 last edited by@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..
-
@mpergand Or, if you prefer the hard way, call clock_settime(). Maybe you need to sudo.
wrote on 5 Aug 2022, 14:31 last edited by JonB 8 May 2022, 14:36@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/489865It 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!
-
@Ramkumar-Mohan
You need to execute some commands with QProcess.
see: https://www.systutorials.com/docs/linux/man/8-clock/wrote on 26 Aug 2022, 12:36 last edited by Ramkumar MohanQString 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’
256Any ideas on how to solve it?
-
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’
256Any ideas on how to solve it?
@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");
-
wrote on 26 Aug 2022, 12:41 last edited by 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-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");
wrote on 26 Aug 2022, 12:51 last edited by Ramkumar Mohanhttps://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 .
-
wrote on 26 Aug 2022, 12:57 last edited by
@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-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
wrote on 26 Aug 2022, 16:47 last edited byQProcess 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
-
wrote on 26 Aug 2022, 17:22 last edited by
Did you try fixing your dateTime string in your original code?
-
@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.wrote on 27 Aug 2022, 07:29 last edited byQProcess 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.
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.
-
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.
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.
@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?