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) {
37 $me = IO::Socket::INET->new(
51 unless defined $timeout;
53 IO::Select->new($me)->can_read($timeout)
60 my $s = _socket('time', 37, @_) || return undef;
65 unless defined $s->recv($buf, length(pack("N", 0)));
67 # unpack, we | 0 to ensure we have an unsigned
68 my $time = (unpack("N", $buf))[0] | 0;
70 # the time protocol return time in seconds since 1900, convert
71 # it to a the required format
75 # MacOS return seconds since 1904, 1900 was not a leap year.
76 $offset = (4 * 31536000) | 0;
80 # otherwise return seconds since 1972, there were 17 leap years between
82 $offset = (70 * 31536000 + 17 * 86400) | 0;
90 my $s = _socket('daytime', 13, @_) || return undef;
93 defined($s->recv($buf, 1024))
104 Net::Time - time and daytime network client interface
108 use Net::Time qw(inet_time inet_daytime);
110 print inet_time(); # use default host from Net::Config
111 print inet_time('localhost');
112 print inet_time('localhost', 'tcp');
114 print inet_daytime(); # use default host from Net::Config
115 print inet_daytime('localhost');
116 print inet_daytime('localhost', 'tcp');
120 C<Net::Time> provides subroutines that obtain the time on a remote machine.
124 =item inet_time ( [HOST [, PROTOCOL [, TIMEOUT]]])
126 Obtain the time on C<HOST>, or some default host if C<HOST> is not given
127 or not defined, using the protocol as defined in RFC868. The optional
128 argument C<PROTOCOL> should define the protocol to use, either C<tcp> or
129 C<udp>. The result will be a time value in the same units as returned
130 by time() or I<undef> upon failure.
132 =item inet_daytime ( [HOST [, PROTOCOL [, TIMEOUT]]])
134 Obtain the time on C<HOST>, or some default host if C<HOST> is not given
135 or not defined, using the protocol as defined in RFC867. The optional
136 argument C<PROTOCOL> should define the protocol to use, either C<tcp> or
137 C<udp>. The result will be an ASCII string or I<undef> upon failure.
143 Graham Barr <gbarr@pobox.com>
147 Copyright (c) 1995-2004 Graham Barr. All rights reserved.
148 This program is free software; you can redistribute it and/or modify
149 it under the same terms as Perl itself.