Questions

using C++ to change windows system clock

Tags:
+
0 Votes
Locked

using C++ to change windows system clock

fsposaro
Does anybody know of a way to change the windows system clock using C++? I want to run a command line program that takes in a time and sets the clock to that arg.

Thanks.
  • +
    0 Votes
    alan williams

    eg time 11:13 sets the time to 11:13

    +
    0 Votes
    Duriana jones

    how do i edit this "time" file? thank u. i need to modify this for my school project. thanks. durianajones@yahoo.com

    +
    0 Votes
    alan williams

    why re-invent the wheel?

    Time is one of the built in commands of cmd.

    +
    0 Votes
    nighthawk808

    Besides, I don't think that was the question he was asking.

    Calling shell commands from inside a program (unless the program is a simple shell script) is almost always a bad idea, especially when there are facilities in the language available to do the same thing. It adds a needless layer of complication because if there is some strange behavior, you don't know if it's something in your program or a problem in its interaction with the external function. Furthermore, if something in "time" changes, then your program may get broken because that is code you have no control over. If you write the little bit of code yourself, it's also much easier to debug. Relying on the crutch of the command line is a VERY bad habit to get into.

    I'm all for reusing code and taking advantage of prewritten modules and other features to make life simpler and productive. (A good engineer is a lazy engineer.) However, the disadvantages strongly suggest against your approach here.

    +
    0 Votes
    alan williams

    First, what was wanted was a command line utility to change the time. Time is a built in command line command to do this and has been available since at least DOS 3 and the internals are used to set the system clock from the CMOS clock on startup. So I don't think that it is likely to change fundamentally in the same way as Dir, MD etc are basics.

    Second, a shell script is a collection of Internal and External commands executed by cmd line by line, so the time command is essentially a simple shell script.

    Third, the MSDN method is useful to change the time from within a program. I've used it or similar on a PDA which doesn't have a Shell option.

    Fourth, the API calls are more likely to change between OSs than the command line interpreter and a missing DLL would also cause similar problems.

    So why write a simple command line program, when that command line program is already written.

    +
    0 Votes
    nighthawk808

    "Using C++", not "Is there a way to change the time and I don't care how?" If that's all he was asking, I'd agree that "time" would be the way to do it, but he specifically asked about C++.

    A missing DLL would be a problem with the OS, not the code. If you're missing one that contains something as fundamental as getting the system time, chances are you're going to have problems long before you even have a chance to fire up the IDE, much less write the application. Depending on the purpose and environment, statically linking could also avoid that problem in any case. If you're cross-compiling or are writing for a kiosk or other device where the user is limited to interacting with the program and never gets to touch the OS itself, this probably wouldn't be a factor at all.

    And, as you have seen for yourself, keeping the code self-contained has advantages when you don't have the option of falling back on the command interpreter.

    +
    0 Votes
    fsposaro

    Thanks for all your input. The best way was to use the MSDN functions. The cmd time command would not have worked because it was not possible to use. This program is going to be used by pilots and they have a GUI. The GUI gives them the option to adjust their system time to the time on the bus on the time on their GPS. Both times would be stored in an external file or streamed directly to the program (the details on this are still a little shaky).

    Once again thanks for the advice.

  • +
    0 Votes
    alan williams

    eg time 11:13 sets the time to 11:13

    +
    0 Votes
    Duriana jones

    how do i edit this "time" file? thank u. i need to modify this for my school project. thanks. durianajones@yahoo.com

    +
    0 Votes
    alan williams

    why re-invent the wheel?

    Time is one of the built in commands of cmd.

    +
    0 Votes
    nighthawk808

    Besides, I don't think that was the question he was asking.

    Calling shell commands from inside a program (unless the program is a simple shell script) is almost always a bad idea, especially when there are facilities in the language available to do the same thing. It adds a needless layer of complication because if there is some strange behavior, you don't know if it's something in your program or a problem in its interaction with the external function. Furthermore, if something in "time" changes, then your program may get broken because that is code you have no control over. If you write the little bit of code yourself, it's also much easier to debug. Relying on the crutch of the command line is a VERY bad habit to get into.

    I'm all for reusing code and taking advantage of prewritten modules and other features to make life simpler and productive. (A good engineer is a lazy engineer.) However, the disadvantages strongly suggest against your approach here.

    +
    0 Votes
    alan williams

    First, what was wanted was a command line utility to change the time. Time is a built in command line command to do this and has been available since at least DOS 3 and the internals are used to set the system clock from the CMOS clock on startup. So I don't think that it is likely to change fundamentally in the same way as Dir, MD etc are basics.

    Second, a shell script is a collection of Internal and External commands executed by cmd line by line, so the time command is essentially a simple shell script.

    Third, the MSDN method is useful to change the time from within a program. I've used it or similar on a PDA which doesn't have a Shell option.

    Fourth, the API calls are more likely to change between OSs than the command line interpreter and a missing DLL would also cause similar problems.

    So why write a simple command line program, when that command line program is already written.

    +
    0 Votes
    nighthawk808

    "Using C++", not "Is there a way to change the time and I don't care how?" If that's all he was asking, I'd agree that "time" would be the way to do it, but he specifically asked about C++.

    A missing DLL would be a problem with the OS, not the code. If you're missing one that contains something as fundamental as getting the system time, chances are you're going to have problems long before you even have a chance to fire up the IDE, much less write the application. Depending on the purpose and environment, statically linking could also avoid that problem in any case. If you're cross-compiling or are writing for a kiosk or other device where the user is limited to interacting with the program and never gets to touch the OS itself, this probably wouldn't be a factor at all.

    And, as you have seen for yourself, keeping the code self-contained has advantages when you don't have the option of falling back on the command interpreter.

    +
    0 Votes
    fsposaro

    Thanks for all your input. The best way was to use the MSDN functions. The cmd time command would not have worked because it was not possible to use. This program is going to be used by pilots and they have a GUI. The GUI gives them the option to adjust their system time to the time on the bus on the time on their GPS. Both times would be stored in an external file or streamed directly to the program (the details on this are still a little shaky).

    Once again thanks for the advice.