Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Setting the time on a Windows machine

by Anonymous Monk
on Jul 08, 2002 at 08:02 UTC ( [id://180115]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a machine running Windows 98. I am stuck on a program I am writing because I don't know how to use Perl to set the time. Getting the time is quite easy of course, but I can't find any information on changing the time. Will I have to use a Win32 module or is the answer so simple that I have overlooked it?
Thanks in advance.

Replies are listed 'Best First'.
(wil) Re: Setting the time on a Windows machine
by wil (Priest) on Jul 08, 2002 at 09:03 UTC
    I've never actually done this myself, but I think this should be possible using the Win32::API module available on CPAN.

    You will need to specifically look at the GetSystemTime and SetSystemTime which I know will work for a Windows NT machine, but I don't see why it shouldn't work for your 98 machine, too.

    If you have sufficent permissions, you might also be able to directly run the date program. You can read more about how to do this in Perl FAQ 8

    Failing that, however, it might be worth looking into a few networking modules on CPAN. These modules allow you to remotely set your system time either off a time server or as you define.

    Hope this helps

    - wil
Re: Setting the time on a Windows machine
by dree (Monsignor) on Jul 08, 2002 at 13:16 UTC
    This code works for me: it sets new time for my Win98 box.
    use strict; use Win32::API; my $GetSystemTime = new Win32::API('kernel32', 'GetSystemTime', 'P', ' +V'); #you can also use GetLocalTime my $lpSystemtime = pack('S8',0,0,0,0,0,0,0,0); $GetSystemTime->Call($lpSystemtime); my ($wYear,$wMonth,$wDayOfWeek,$wDay,$wHour,$wMinute,$wSecond,$wMillis +econds) = unpack('S8', $lpSystemtime); print "Actual time is: $wHour:$wMinute:$wSecond\n"; $wHour++; $wMinute++; $wSecond++; $lpSystemtime = pack('S8',$wYear,$wMonth,$wDayOfWeek,$wDay,$wHour,$wMi +nute,$wSecond,$wMilliseconds); my $SetSystemTime = new Win32::API('kernel32', 'SetSystemTime', 'P', ' +I'); #you can also use SetLocalTime if ($SetSystemTime->Call($lpSystemtime)) { print "The new time is correctly setted: $wHour:$wMinute:$wSecond\ +n" } else { print "Error setting the time\n" }
Re: Setting the time on a Windows machine
by BrowserUk (Patriarch) on Jul 08, 2002 at 12:34 UTC

    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" ); ...
      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 :^)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://180115]
Approved by IlyaM
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found