LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2007, 03:21 AM   #1
g4j31a5
Member
 
Registered: Sep 2006
Distribution: open SuSE 10.0
Posts: 116

Rep: Reputation: 15
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.
 
Old 03-18-2007, 03:58 AM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Why not use hwclock?
 
Old 03-18-2007, 06:56 AM   #3
g4j31a5
Member
 
Registered: Sep 2006
Distribution: open SuSE 10.0
Posts: 116

Original Poster
Rep: Reputation: 15
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?
 
Old 03-18-2007, 11:52 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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
 
Old 03-18-2007, 01:03 PM   #5
g4j31a5
Member
 
Registered: Sep 2006
Distribution: open SuSE 10.0
Posts: 116

Original Poster
Rep: Reputation: 15
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?
 
Old 03-18-2007, 02:45 PM   #6
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
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.
 
Old 03-20-2007, 08:30 PM   #7
g4j31a5
Member
 
Registered: Sep 2006
Distribution: open SuSE 10.0
Posts: 116

Original Poster
Rep: Reputation: 15
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.
 
Old 03-21-2007, 03:59 AM   #8
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
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?
 
Old 03-21-2007, 05:02 AM   #9
g4j31a5
Member
 
Registered: Sep 2006
Distribution: open SuSE 10.0
Posts: 116

Original Poster
Rep: Reputation: 15
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++.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Date & time as dir name Pyrate_02 Linux - General 6 03-18-2006 03:53 AM
Help With Date & Time Jude Lucien Linux - General 3 10-11-2005 02:46 PM
Newbie Help With Date & Time Jude Lucien Debian 2 10-11-2005 10:21 AM
Problem with date & time kosmonaft Linux - General 3 08-17-2005 11:23 AM
Changing system date and time from KDE yapp Linux - General 4 04-17-2005 03:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration