in reply to Setting the time on a Windows machine

Difficult to answer authoratatively without knowing more context as to why you want to set the machines time from within a program but this will do it.

WARNING! Be very, very sure that $time has a valid value for a time before doing this!!

For instance. Using a value of "ab:cd" for $time in the following snippet, or even from the command line (under NT4 sp6a) will result in the time being set to 00:00:00!!!

The unix types are gonna have a field day with that one!

... # $time = the time you want to set as "12:45" or "00:00:01" # derived or verified using system calls or modules as # appropriate system( "cmd /c time $time" ); ...

Replies are listed 'Best First'.
Re^2: Setting the time on a Windows machine
by Aristotle (Chancellor) on Jul 08, 2002 at 13:40 UTC
    Why do you preprend cmd /c when Perl will call the shell to handle a system SCALAR call anyway?

    Makeshifts last the longest.

      Because I read this from perldoc:perlfunc:

      • If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is /bin/sh -c on Unix platforms, but varies on other platforms). If there are no shell metacharacters in the argument, it is split into words and passed directly to execvp, which is more efficient.

      Which, as I know time is a CMD built-in, implied to me that as there are no shell meta characters involved, would mean that I would need to force the use of CMD explicitly.

      I have just tried it without and it works fine so I guess ActiveState have never updated the documentation to reflect the difference on the Win32 platform.

      Thanks for pointing it out. Another peice of 'cargo cult' programming, if I understand the term correctly, bites the dust :^)

        Hrm. Now the question is, can that behaviour be expected to remain as is? Since the docs were not updated, it may well be implementational detail.. I guess the safe way to achieve exactly predictable behaviour would be system "cmd.exe", "/c", "time $time"; as that is guaranteed to call the given program without going through the shell - except we happen to be calling the shell.

        Makeshifts last the longest.