File: ptktime

package info (click to toggle)
ptknettools 0.2.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 96 kB
  • ctags: 19
  • sloc: perl: 649; makefile: 34
file content (146 lines) | stat: -rwxr-xr-x 4,856 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/perl
# ptktime v0.2
# Perl/Tk Time Service (TCP port 37) Client
# Copyright Alan Ford <alan@whirlnet.co.uk>  08/04/1999
# Distributed with no warranty under the GNU Public License
#
# Changelog:
# 08/04/1999 Initial Release Version (0.1)
# 06/06/1999 Added support for command-line specification of timeserver (0.2)
#
# Based on example in The Camel Book
# (There Is No Camel [TM])

require 5.002;
#use strict;
use Tk;
#use Tk::DialogBox;
use Socket;
sub comparetimes ;
sub getlocal ;
sub help ;

my $version = "0.2";

my $MW = MainWindow->new;

$MW->title("Perl/Tk Time Client");
$MW->Label(-text => "Version $version - Written by Alan Ford\n<alan\@whirlnet.co.uk>")->pack(-side => 'bottom');

my $lookup_frame = $MW->Frame()->pack(-expand => '1', -fill => 'both', -side => 'top');

my $button_frame = $MW->Frame()->pack(-expand => '1', -fill => 'both', -side => 'top');

my $local_frame = $MW->Frame()->pack(-expand => '1', -fill => 'both', -side => 'top');

my $remote_frame = $MW->Frame()->pack(-expand => '1', -fill => 'both', -side => 'top');

my $diff_frame = $MW->Frame()->pack(-expand => '1', -fill => 'both', -side => 'top');

$lookup_frame->Label(-text => "Remote Timeserver:")->pack(-side => 'left');
my $host_entry = $lookup_frame->Entry(-width => '25', -relief => 'sunken')->pack(-side => 'left', -expand => '1', -fill => 'x');

$local_frame->Label(-text => "Local Time:")->pack(-side => 'left');
my $local_time = $local_frame->Entry(-width => '25', -relief => 'sunken', -justify => 'right')->pack(-side => 'left', -expand => '1', -fill => 'x');

$remote_frame->Label(-text => "Remote Time:")->pack(-side => 'left');
my $remote_time = $remote_frame->Entry(-width => '25', -relief => 'sunken', -justify => 'right')->pack(-side => 'left', -expand => '1', -fill => 'x');

$diff_frame->Label(-text => "Difference in Seconds:")->pack(-side => 'left');
my $diff_secs = $diff_frame->Entry(-width => '25', -relief => 'sunken', -justify => 'right')->pack(-side => 'left', -expand => '1', -fill => 'x');

my $comparetimes = $button_frame->Button(-text => 'Compare Times',
                                         -command => sub
                                         {
                                          comparetimes;
	                                 });
$comparetimes->pack(-side => 'left', -expand => '1', -fill => 'both');

my $getlocal = $button_frame->Button(-text => 'Get Local Time',
                                     -command => sub
                                     {
                                      getlocal;
	                             });
$getlocal->pack(-side => 'left', -expand => '1', -fill => 'both');

my $exit = $button_frame->Button(-text => 'Exit',
                                 -command => sub
                                 {
                                  exit;
                                 });
$exit->pack(-side => 'left', -expand => '1', -fill => 'both');

if ($ARGV[0]) {
   # Are they asking for help?
   if ($ARGV[0] =~ /help$/) {
       help;
   }
   if ($ARGV[0] eq "-?") {
       help;
   }
   if ($ARGV[0] eq "-h") {
       help;
   }
   # Timeserver (probably) given as command-line option
   my $cmdserver = $ARGV[0];
   $host_entry->insert('0', $cmdserver);
   comparetimes;
}

MainLoop;


sub comparetimes {
    my $SECS_of_70_YEARS = 2208988800;
    sub ctime { scalar localtime(shift) }

    my $iaddr = gethostbyname('localhost');
    my $proto = getprotobyname('tcp');
    my $port = getservbyname('time', 'tcp');
    my $paddr = sockaddr_in(0, $iaddr);
    my $host = $host_entry->get;

    $| = 1;
    printf "%-24s %8s %s\n", "localhost", 0, ctime(time());
    $local_time->delete(0, 'end');
    $remote_time->delete(0, 'end');

    printf "%-24s ", $host;
    my $hisiaddr = inet_aton($host) or die "Unknown Host: $host";
    my $hispaddr = sockaddr_in($port, $hisiaddr);
    socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
                                         or die "Socket Error: $!";
    connect(SOCKET, $hispaddr)           or die "Bind Error for $host: $!";
    my $rtime = '   ';
    read(SOCKET, $rtime, 4);
    close(SOCKET);
    my $histime = unpack("N", $rtime) - $SECS_of_70_YEARS;
    printf "%8d %s\n", $histime - time, ctime($histime);
    $local_time->insert('end', ctime(time()));
    $remote_time->insert('end', ctime($histime));
    $diff_secs->delete(0, 'end');
    $diff_secs->insert('end', $histime - time);
}

sub getlocal {
    $local_time->delete('0', 'end');
    $local_time->insert('end', ctime(time()));
}

sub help {
   print <<EOF

ptktime - Perk/Tk Time Client for X version $version (requires Perl/Tk)

Compares local time with timeserver.

Remote timeserver can (but is not required) be specified as a parameter.

Report bugs to the author, Alan Ford <alan\@whirlnet.co.uk>

For more information: `man ptktime'

EOF
;
exit;
}