Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Calling Windows "time /t" command

by JFarr (Sexton)
on Jul 27, 2006 at 14:36 UTC ( #564113=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I have a small issues with calling the "time /t" command on the windows platform.

In my script I trying to get the values from "time /t" into a variable, let me preface this with, this works on all of our servers except one or two.
So calling system("time \/t") produces this error on our servers in question:
time: cannot run /t: No such file or directory Command exited with non-zero status 127 0.03user 0.00system 0:00.03elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (0major+0minor)pagefaults 0swaps

BUT, if I type this same command from the command line, it works fine.
Has anyone else seen a problem like this? Could it be a permission setting on the server in question? If so, what would I need to look at?

Thanks in advance for any suggestions.

Replies are listed 'Best First'.
Re: Calling Windows "time /t" command
by Corion (Patriarch) on Jul 27, 2006 at 14:43 UTC

    It sounds that on "one or two" of your servers, there is a program time.exe installed, most likely from the Cygwin suite of programs, which preempts the shell builtin time command.

    To solve your problem, maybe you want to look at the Perl builtin functions of localtime and gmtime() together with the POSIX::strftime() function:

    use strict; use warnings; use POSIX qw(strftime); print strftime "%Y:%m:%d %H:%M:%S", localtime();

    There are lots of format strings that strftime knows to insert into your string, like the weekday and more, should you need it.

      Another solution would be to alter your PATH environment variable to make sure the system's time command comes before any others.
Re: Calling Windows "time /t" command
by liverpole (Monsignor) on Jul 27, 2006 at 14:42 UTC
    Hi JFarr,

    The problem is that the "time /t" command is a Windows command.  You can't use it to get the time on a Linux/Unix system.

    However, you can use Perl's time function to get the current time (specified in seconds since the epoch), which will work on any system.  Then, use localtime to return the time in a readable format:

    #!/usr/bin/perl -w use strict; use warnings; my $time = time; my $timestr = localtime($time); print "Current date/time is $timestr\n";

    Oh, and one further note ... you can't assign a variable to the results from a system call.  If you really wanted to assign a variable, say $x, to the results of the time /t system call in Windows, you would use the backtick "`" operator (and then likely chomp to remove the newline):

    my $x = `time /t`; # Assign $x to the system time (only in Windows) chomp $x; # $x now holds something like "10:51 AM"

Re: Calling Windows "time /t" command
by VSarkiss (Monsignor) on Jul 27, 2006 at 14:45 UTC
Re: Calling Windows "time /t" command
by machinecraig (Monk) on Jul 27, 2006 at 14:45 UTC
    Hello, It's not the answer to your question - but rather than using an external command to get time, why not use the localtime() function? Something like this might work nicely for you:
    use strict; my $now = scalar(localtime); print "It is $now\n";
    Update: Argh - beaten to the punch by several other good posts!
Re: Calling Windows "time /t" command
by JFarr (Sexton) on Jul 27, 2006 at 15:02 UTC
    I should also have mentioned that we are using windowns not Linux/Unix. I should have clarified that to populate the variable I am using the `` operators. I could use localtime, but our legacy code is already using the time /t command, and I was looking for a simpler solution for this, just getting lazy I guess. Its a lot of code to change for only a hand full of servers in a praticular region that are not working.....
    I'd rather be racing my Daytona
      There's a neat little trick you can use here - just invoke some (pointless) redirection:
      system("time /t <nul");
      That should work on all of your Windows boxes. Another option that I think will work everywhere for you is to specifically invoke the cmd.exe shell:
      system("cmd /C time /t");

      As backticks spawn a new shell each time, the simplest solution would be to set the path to empty prior to invoking the (builtin) time command:

      path='' & time /t

      The change to the environment will only last for the duration of the spawned shell, so the will be no knock on effect for subsequent spawned shells.

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      It may be Windows, but you have unix tools installed, and the PATH is setup such that these are found before Windows's tools. Fix that and you won't have to change your code.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2023-05-31 00:01 GMT
Find Nodes?
    Voting Booth?

    No recent polls found