2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
16 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
17 parray parraypairs shellregex
18 print_all_fields cltounix iscallsign
21 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
29 # a full time for logging and other purposes
33 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
35 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
39 # get a zulu time in cluster format (2300Z)
43 my ($sec,$min,$hour) = gmtime((defined $t) ? $t : time);
45 my $buf = sprintf "%02d%02dZ", $hour, $min;
50 # get a cluster format date (23-Jun-1998)
54 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
56 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
60 # return a cluster style date time
64 my $date = cldate($t);
69 # return a unix date from a cluster date and time
74 my ($thisyear) = (gmtime)[5] + 1900;
76 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
77 return 0 if $3 > 2036;
78 return 0 unless abs($thisyear-$3) <= 1;
80 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
81 $time = "$1:$2 +0000";
82 my $r = str2time("$date $time");
84 return $r == -1 ? undef : $r;
87 # turn a latitude in degrees into a string
91 my ($deg, $min, $let);
92 $let = $n >= 0 ? 'N' : 'S';
95 $min = int ((($n - $deg) * 60) + 0.5);
96 return "$deg $min $let";
99 # turn a longitude in degrees into a string
103 my ($deg, $min, $let);
104 $let = $n >= 0 ? 'E' : 'W';
107 $min = int ((($n - $deg) * 60) + 0.5);
108 return "$deg $min $let";
111 # turn a true into 'yes' and false into 'no'
115 return $n ? $main::yes : $main::no;
118 # format a prompt with its current value and return it with its privilege
121 my ($line, $value) = @_;
122 my ($priv, $prompt, $action) = split ',', $line;
124 # if there is an action treat it as a subroutine and replace $value
126 my $q = qq{\$value = $action(\$value)};
129 $prompt = sprintf "%15s: %s", $prompt, $value;
130 return ($priv, $prompt);
133 # take an arg as an array list and print it
137 return join(', ', @{$ref});
140 # take the arg as an array reference and print as a list of pairs
147 for ($i = 0; $i < @$ref; $i += 2) {
149 my $r2 = @$ref[$i+1];
152 chop $out; # remove last space
153 chop $out; # remove last comma
157 # print all the fields for a record according to privilege
159 # The prompt record is of the format '<priv>,<prompt>[,<action>'
160 # and is expanded by promptf above
164 my $self = shift; # is a dxchan
165 my $ref = shift; # is a thingy with field_prompt and fields methods defined
167 my @fields = $ref->fields;
170 foreach $field (sort @fields) {
171 if (defined $ref->{$field}) {
172 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
173 push @out, $ans if ($self->priv >= $priv);
179 # generate a regex from a shell type expression
180 # see 'perl cookbook' 6.9
184 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
185 return '^' . $in . "\$";
188 # start an attempt at determining whether this string might be a callsign
192 return 1 if $call =~ /^\w+\s+/;
193 return 1 if $call =~ /^\d+\w+/;