X-Git-Url: http://dxcluster.org/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FDXUtil.pm;h=b60b0b0b1de4c444e301f86aa04404ed3d666b61;hb=a13f69ce9f26ea2b95be1e6de1f14f50ab7065d8;hp=b8ff16d1b3ba50605316a2f645f85e9929e6d5a2;hpb=78fcd88c563bc8f05c9f590cdfc0739129c7efda;p=spider.git diff --git a/perl/DXUtil.pm b/perl/DXUtil.pm index b8ff16d1..b60b0b0b 100644 --- a/perl/DXUtil.pm +++ b/perl/DXUtil.pm @@ -15,7 +15,6 @@ use File::Copy; use Data::Dumper; use Time::HiRes qw(gettimeofday tv_interval); use Text::Wrap; - use strict; use vars qw(@month %patmap $pi $d2r $r2d @ISA @EXPORT); @@ -29,7 +28,7 @@ require Exporter; is_qra is_freq is_digits is_pctext is_pcflag insertitem deleteitem is_prefix dd is_ipaddr $pi $d2r $r2d localdata localdata_mv diffms _diffms _diffus difft parraydifft is_ztime basecall - normalise_call + normalise_call is_numeric ); @@ -46,6 +45,24 @@ $d2r = ($pi/180); $r2d = (180/$pi); +# BEGIN { +# our $enable_ptonok = 0; +# our $ptonok; + + +# if ($enable_ptonok && !$main::is_win) { +# eval {require Socket; Socket->import(qw(AF_INET6 AF_INET inet_pton)); }; +# unless ($@) { +# $ptonok = !defined inet_pton(AF_INET, '016.17.184.1') +# && !defined inet_pton(AF_INET6, '2067::1:') +# # Some old versions of Socket are hopelessly broken +# && length(inet_pton(AF_INET, '1.1.1.1')) == 4; +# } +# } +# } + + + # a full time for logging and other purposes sub atime { @@ -222,7 +239,7 @@ sub phash my $ref = shift; my $out; - while (my $k = sort keys %$ref) { + foreach my $k (sort keys %$ref) { $out .= "${k}=>$ref->{$k}, "; } $out =~ s/, $// if $out; @@ -380,8 +397,8 @@ sub filecopy sub unpad { my $s = shift; - $s =~ s/\s+$//; - $s =~ s/^\s+//; + $s =~ s/^\s*//; + $s =~ s/\s*$//; return $s; } @@ -448,7 +465,25 @@ sub is_latlong # is it an ip address? sub is_ipaddr { - return $_[0] =~ /^\d+\.\d+\.\d+\.\d+$/ || $_[0] =~ /^[0-9a-f:,]+$/; + $_[0] =~ s|/\d+$||; + # if ($ptonok) { + # if ($_[0] =~ /:/) { + # if (inet_pton(AF_INET6, $_[0])) { + # return ($_[0] =~ /([:0-9a-f]+)/); + # } + # } else { + # if (inet_pton(AF_INET, $_[0])) { + # return ($_[0] =~ /([\.\d]+)/); + # } + # } + # } else { + if ($_[0] =~ /:/) { + return ($_[0] =~ /^((?:\:?\:?[0-9a-f]{0,4}){1,8}\:?\:?)$/i); + } else { + return ($_[0] =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/); + } +# } + return undef; } # is it a zulu time hhmmZ @@ -551,7 +586,7 @@ sub diffms sub difft { my $b = shift; - my $adds = shift; + my $adds = shift // 0; my $t; if (ref $b eq 'ARRAY') { @@ -565,8 +600,11 @@ sub difft } } return '-(ve)' if $t < 0; - my ($d,$h,$m,$s); + my ($y,$d,$h,$m,$s); my $out = ''; + $y = int $t / (86400*365); + $out .= sprintf ("%s${y}y", $adds?' ':'') if $y; + $t -= $y * 86400 * 365; $d = int $t / 86400; $out .= sprintf ("%s${d}d", $adds?' ':'') if $d; $t -= $d * 86400; @@ -575,7 +613,7 @@ sub difft $t -= $h * 3600; $m = int $t / 60; $out .= sprintf ("%s${m}m", $adds?' ':'') if $m; - if ($d == 0 && $adds || $adds == 2) { + if (($d == 0 && $adds) || (int $adds && $adds == 2)) { $s = int $t % 60; $out .= sprintf ("%s${s}s", $adds?' ':'') if $s; $out ||= sprintf ("%s0s", $adds?' ':''); @@ -611,3 +649,10 @@ sub normalise_call $ncall .= "-$ssid" if $ssid; return $ncall; } + +sub is_numeric +{ + return $_[0] =~ /^[\.\d]+$/; +} + +1;