3 # Copyright (C) 1995-2004 Graham Barr. All rights reserved.
4 # Copyright (C) 2014, 2020 Steve Hay. All rights reserved.
5 # This module is free software; you can redistribute it and/or modify it under
6 # the same terms as Perl itself, i.e. under the terms of either the GNU General
7 # Public License or the Artistic License, as specified in the F<LICENCE> file.
22 our @ISA = qw(Exporter);
23 our @EXPORT_OK = qw(inet_time inet_daytime);
25 our $VERSION = "3.12";
30 my ($pname, $pnum, $host, $proto, $timeout) = @_;
34 my $port = (getservbyname($pname, $proto))[2] || $pnum;
36 my $hosts = defined $host ? [$host] : $NetConfig{$pname . '_hosts'};
40 foreach my $addr (@$hosts) {
41 $me = IO::Socket::INET->new(
55 unless defined $timeout;
57 IO::Select->new($me)->can_read($timeout)
64 my $s = _socket('time', 37, @_) || return;
69 unless defined $s->recv($buf, length(pack("N", 0)));
71 # unpack, we | 0 to ensure we have an unsigned
72 my $time = (unpack("N", $buf))[0] | 0;
74 # the time protocol return time in seconds since 1900, convert
75 # it to a the required format
79 # MacOS return seconds since 1904, 1900 was not a leap year.
80 $offset = (4 * 31536000) | 0;
84 # otherwise return seconds since 1972, there were 17 leap years between
86 $offset = (70 * 31536000 + 17 * 86400) | 0;
94 my $s = _socket('daytime', 13, @_) || return;
97 defined($s->recv($buf, 1024))
108 Net::Time - time and daytime network client interface
112 use Net::Time qw(inet_time inet_daytime);
114 print inet_time(); # use default host from Net::Config
115 print inet_time('localhost');
116 print inet_time('localhost', 'tcp');
118 print inet_daytime(); # use default host from Net::Config
119 print inet_daytime('localhost');
120 print inet_daytime('localhost', 'tcp');
124 C<Net::Time> provides subroutines that obtain the time on a remote machine.
130 =item C<inet_time([$host[, $protocol[, $timeout]]])>
132 Obtain the time on C<$host>, or some default host if C<$host> is not given
133 or not defined, using the protocol as defined in RFC868. The optional
134 argument C<$protocol> should define the protocol to use, either C<tcp> or
135 C<udp>. The result will be a time value in the same units as returned
136 by time() or I<undef> upon failure.
138 =item C<inet_daytime([$host[, $protocol[, $timeout]]])>
140 Obtain the time on C<$host>, or some default host if C<$host> is not given
141 or not defined, using the protocol as defined in RFC867. The optional
142 argument C<$protocol> should define the protocol to use, either C<tcp> or
143 C<udp>. The result will be an ASCII string or I<undef> upon failure.
149 The following symbols are, or can be, exported by this module:
153 =item Default Exports
157 =item Optional Exports
174 Graham Barr E<lt>L<gbarr@pobox.com|mailto:gbarr@pobox.com>E<gt>.
176 Steve Hay E<lt>L<shay@cpan.org|mailto:shay@cpan.org>E<gt> is now maintaining
177 libnet as of version 1.22_02.
181 Copyright (C) 1995-2004 Graham Barr. All rights reserved.
183 Copyright (C) 2014, 2020 Steve Hay. All rights reserved.
187 This module is free software; you can redistribute it and/or modify it under the
188 same terms as Perl itself, i.e. under the terms of either the GNU General Public
189 License or the Artistic License, as specified in the F<LICENCE> file.
201 See the F<Changes> file.