LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Code for changing system's local date & time (http://www.linuxquestions.org/questions/programming-9/code-for-changing-systems-local-date-and-time-538448/)

g4j31a5 03-18-2007 03:21 AM

Code for changing system's local date & time
 
Hi, I planned to make a program to change the local date and time of my machine permanently (just like in yast). So if I rebooted it after changing the date & time with that application, it will be the current date & time of the machine. But the truth is I don't know how. Can anybody help me? Thanks a lot.

gnashley 03-18-2007 03:58 AM

Why not use hwclock?

g4j31a5 03-18-2007 06:56 AM

Quote:

Originally Posted by gnashley
Why not use hwclock?

I've looked at the man page of hwclock. So basically I've just to call
Code:

system ("hwclock --set --date="blabla..."");
Right?

gnashley 03-18-2007 11:52 AM

UTC=`grep UTC /etc/adjtime`
if [ "$UTC" == "UTC" ] ; then
UTC="--utc"
fi

To set the hardware clock:
/sbin/hwclock --set $UTC --date "$NEWDATE $NEWTIME"
To set the system time:
/sbin/hwclock --hctosys $UTC

g4j31a5 03-18-2007 01:03 PM

Thanks. FYI, I'm trying make a simple GUI for changing the time with SDL & SDL ttf. One more thing though, it seems like hwclock can only be called from root. But I needed everyone to be able to change the date and time with my application. How do I do that?

indienick 03-18-2007 02:45 PM

If you're trying to allow any low-end user to change the time using an executable runnable to only root, you have several options:
Option 1: In the installation/setup procedures, "chmod" the hwclock executable so it can be executed by all (eg. "chmod a+x /path/to/hwclock").
Option 2: Utilize "sudo".
Option 3: Force the user to provide the root password if they wish to apply any changes they've made.

There's more than likely a reason why the executable has default permissions of being root-only, and it's more than likely best not to tamper with it; if you need to be root to run it, there's a reason for it. This is UNIX/Linux, not Windows. :)

g4j31a5 03-20-2007 08:30 PM

Quote:

Originally Posted by indienick
If you're trying to allow any low-end user to change the time using an executable runnable to only root, you have several options:
Option 1: In the installation/setup procedures, "chmod" the hwclock executable so it can be executed by all (eg. "chmod a+x /path/to/hwclock").
Option 2: Utilize "sudo".
Option 3: Force the user to provide the root password if they wish to apply any changes they've made.

There's more than likely a reason why the executable has default permissions of being root-only, and it's more than likely best not to tamper with it; if you need to be root to run it, there's a reason for it. This is UNIX/Linux, not Windows. :)

Thanks. Will do that. Yeah, I know it's Linux, but I needed it to behave more like a Windows nevertheless. The requirement needed so.

gnashley 03-21-2007 03:59 AM

The code I posted is just a part of an example program included with Xdialog, which uses the gtk libs. Why not just use that instead of sdl?

g4j31a5 03-21-2007 05:02 AM

Quote:

Originally Posted by gnashley
The code I posted is just a part of an example program included with Xdialog, which uses the gtk libs. Why not just use that instead of sdl?

Well I needed sdl because that's the requirement. So whether I like it or not, I must use a system call to hwclock with C++. :D


All times are GMT -5. The time now is 08:38 PM.