3 # Copyright (c) 1995-2004 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
10 use vars qw($VERSION @ISA @EXPORT_OK $TIMEOUT);
18 @EXPORT_OK = qw(inet_time inet_daytime);
26 my($pname,$pnum,$host,$proto,$timeout) = @_;
30 my $port = (getservbyname($pname, $proto))[2] || $pnum;
32 my $hosts = defined $host ? [ $host ] : $NetConfig{$pname . '_hosts'};
36 foreach $host (@$hosts)
38 $me = IO::Socket::INET->new(PeerAddr => $host,
50 unless defined $timeout;
52 IO::Select->new($me)->can_read($timeout)
59 my $s = _socket('time',37,@_) || return undef;
64 unless defined $s->recv($buf, length(pack("N",0)));
66 # unpack, we | 0 to ensure we have an unsigned
67 my $time = (unpack("N",$buf))[0] | 0;
69 # the time protocol return time in seconds since 1900, convert
70 # it to a the required format
73 # MacOS return seconds since 1904, 1900 was not a leap year.
74 $offset = (4 * 31536000) | 0;
77 # otherwise return seconds since 1972, there were 17 leap years between
79 $offset = (70 * 31536000 + 17 * 86400) | 0;
87 my $s = _socket('daytime',13,@_) || return undef;
90 defined($s->recv($buf, 1024)) ? $buf
100 Net::Time - time and daytime network client interface
104 use Net::Time qw(inet_time inet_daytime);
106 print inet_time(); # use default host from Net::Config
107 print inet_time('localhost');
108 print inet_time('localhost', 'tcp');
110 print inet_daytime(); # use default host from Net::Config
111 print inet_daytime('localhost');
112 print inet_daytime('localhost', 'tcp');
116 C<Net::Time> provides subroutines that obtain the time on a remote machine.
120 =item inet_time ( [HOST [, PROTOCOL [, TIMEOUT]]])
122 Obtain the time on C<HOST>, or some default host if C<HOST> is not given
123 or not defined, using the protocol as defined in RFC868. The optional
124 argument C<PROTOCOL> should define the protocol to use, either C<tcp> or
125 C<udp>. The result will be a time value in the same units as returned
126 by time() or I<undef> upon failure.
128 =item inet_daytime ( [HOST [, PROTOCOL [, TIMEOUT]]])
130 Obtain the time on C<HOST>, or some default host if C<HOST> is not given
131 or not defined, using the protocol as defined in RFC867. The optional
132 argument C<PROTOCOL> should define the protocol to use, either C<tcp> or
133 C<udp>. The result will be an ASCII string or I<undef> upon failure.
139 Graham Barr <gbarr@pobox.com>
143 Copyright (c) 1995-2004 Graham Barr. All rights reserved.
144 This program is free software; you can redistribute it and/or modify
145 it under the same terms as Perl itself.