3 # Versions up to 2.10 Copyright (c) 1995-2004 Graham Barr <gbarr@pobox.com>.
5 # Changes in Version 2.11 onwards Copyright (C) 2014 Steve Hay. All rights
7 # This module is free software; you can redistribute it and/or modify it under
8 # the same terms as Perl itself, i.e. under the terms of either the GNU General
9 # Public License or the Artistic License, as specified in the F<LICENCE> file.
24 our @ISA = qw(Exporter);
25 our @EXPORT_OK = qw(inet_time inet_daytime);
27 our $VERSION = "3.08";
32 my ($pname, $pnum, $host, $proto, $timeout) = @_;
36 my $port = (getservbyname($pname, $proto))[2] || $pnum;
38 my $hosts = defined $host ? [$host] : $NetConfig{$pname . '_hosts'};
42 foreach my $addr (@$hosts) {
43 $me = IO::Socket::INET->new(
57 unless defined $timeout;
59 IO::Select->new($me)->can_read($timeout)
66 my $s = _socket('time', 37, @_) || return;
71 unless defined $s->recv($buf, length(pack("N", 0)));
73 # unpack, we | 0 to ensure we have an unsigned
74 my $time = (unpack("N", $buf))[0] | 0;
76 # the time protocol return time in seconds since 1900, convert
77 # it to a the required format
81 # MacOS return seconds since 1904, 1900 was not a leap year.
82 $offset = (4 * 31536000) | 0;
86 # otherwise return seconds since 1972, there were 17 leap years between
88 $offset = (70 * 31536000 + 17 * 86400) | 0;
96 my $s = _socket('daytime', 13, @_) || return;
99 defined($s->recv($buf, 1024))
110 Net::Time - time and daytime network client interface
114 use Net::Time qw(inet_time inet_daytime);
116 print inet_time(); # use default host from Net::Config
117 print inet_time('localhost');
118 print inet_time('localhost', 'tcp');
120 print inet_daytime(); # use default host from Net::Config
121 print inet_daytime('localhost');
122 print inet_daytime('localhost', 'tcp');
126 C<Net::Time> provides subroutines that obtain the time on a remote machine.
130 =item 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 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 Graham Barr E<lt>F<gbarr@pobox.com>E<gt>
151 Steve Hay E<lt>F<shay@cpan.org>E<gt> is now maintaining libnet as of version
156 Versions up to 2.11 Copyright (c) 1995-2004 Graham Barr. All rights reserved.
157 Changes in Version 2.11 onwards Copyright (C) 2014 Steve Hay. All rights
160 This module is free software; you can redistribute it and/or modify it under the
161 same terms as Perl itself, i.e. under the terms of either the GNU General Public
162 License or the Artistic License, as specified in the F<LICENCE> file.