Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Atomic Time and Date

by providencia (Pilgrim)
on Jun 04, 2000 at 06:28 UTC ( [id://16259]=sourcecode: print w/replies, xml ) Need Help??
Category: Time Utilities
Author/Contact Info providencia
e-mail me
Description: I was inspired by reptile's NIST Atomic Clock Time and wanted to see if
I could make something simpler that would set the time and date as well.
<var>I did this for a Win2K machine</var>
#!/usr/bin/perl -w

use strict;
use Net::Time qw(inet_time);

my %month_number = qw(Jan 01 Feb 02 Mar 03 Apr 04 May 05 Jun 06 Jul 07 Aug 08 Sep 09 Oct 10 Nov 11 Dec 12);

my $date_and_time = inet_time('cesium.clock.org','tcp');
$date_and_time = localtime($date_and_time);

my ($day_of_week,$month,$day_of_month,$time,$year) = split /\s+/,$date_and_time;
$day_of_month = 0 . $day_of_month if ($day_of_month < 10);

my $new_date = "$month_number{$month}/$day_of_month/$year";

system("time $time");
system("date $new_date");
Replies are listed 'Best First'.
RE: Atomic Time and Date
by mdillon (Priest) on Jun 04, 2000 at 07:33 UTC
    here's a version that doesn't rely on parsing the output of the scalar version of localtime:
    #!/usr/bin/perl -w use strict; use Net::Time qw(inet_time); my $date_and_time = inet_time('cesium.clock.org', 'tcp'); my ($sec, $min, $hour, $day, $month, $year) = (localtime($date_and_time))[0..5]; my $new_date = sprintf "%02d/%02d/%04d", $month + 1, $day, $year + 190 +0; my $new_time = sprintf "%02d:%02d:%02d", $hour, $min, $sec; system('time', $new_time) or die "Couldn't set time: $!"; system('date', $new_date) or die "Couldn't set date: $!";
      I keep getting:
      C:\WINDOWS\COMMAND.COM /c perl time-date-other.pl
      Couldn't set time: No such file or directory at time-date-other.pl line 14.
      Hit any key to close this window...

      I changed the last two lines to:
      system("time $new_time"); system("date $new_date");

      and it works. It seems to be the:
      or die "Couldn't set time: $!"; or die "Couldn't set date: $!";

      It doesn't work with those.
      I don't have a clue as to why. ??!!??
      I like it though.
        no, i think it's the change from system(CMD, ARGS) back to system(EXPR) that fixed it. die doesn't do anything with a file or directory in that code, so i can't see it being the problem. maybe ActivePerl only likes the monadic (one argument) form of system.

        update: i may have figured it out. i think to do it my way, you would have to say 'TIME.EXE' or whatever the full name of the executable is because it isn't passed to COMMAND.COM to be resolved.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-19 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found