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.