| 1 | package Net::Analysis::Time; | ||||||||||||||||||||||||||||
| 2 | # $Id: Time.pm 131 2005-10-02 17:24:31Z abworrall $ | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use 5.008000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Carp qw(carp croak confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | use overload | ||||||||||||||||||||||||||||
| 11 | 3 | ▶ | q("") => \&as_string, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | q(0+) => \&as_number, | ||||||||||||||||||||||||||||
| 13 | q(+=) => \&inceq, | ||||||||||||||||||||||||||||
| 14 | q(-=) => \&deceq, #) | ||||||||||||||||||||||||||||
| 15 | q(+) => \&addition, | ||||||||||||||||||||||||||||
| 16 | q(-) => \&subtraction, #) | ||||||||||||||||||||||||||||
| 17 | 3 | q(<=>) => \&numerical_cmp; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | our $Default_format = ''; # No format, raw epoch | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # {{{ POD | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Net::Analysis::Time - value object for [tv_sec, tv_usec] times | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | use Net::Analysis::Time; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | my $t1 = Net::Analysis::Time->new(10812345, 123456); | ||||||||||||||||||||||||||||
| 32 | my $t2 = Net::Analysis::Time->new(10812356, 123456); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | my $diff = $t2-$t1; # == new Time Object | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | print "$diff\n"; # == "11.000000" | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | $t1->round_usec(10000); # "$t1" == "10812345.120000"; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Can't believe I've found myself implementing a date/time module. The shame of | ||||||||||||||||||||||||||||
| 43 | it. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | This is a heavily overloaded object, so '+', '-' do what you expect. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | There is some format stuff to change how it stringfies, and some stuff for | ||||||||||||||||||||||||||||
| 48 | rounding off values, used elsewhere for time-boxing. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | This stuff should probably all be junked as soon as someone wants some | ||||||||||||||||||||||||||||
| 51 | efficiency. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # }}} | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | #### Public methods | ||||||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||||||
| 59 | # {{{ new | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # {{{ POD | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head2 new ($sec [, $usec] ) | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | If passed a single floating point arg, does what it can, but don't blame me if | ||||||||||||||||||||||||||||
| 66 | rounding errors knacker things up. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Best to pass two ints, one seconds and one microseconds. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # }}} | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 7 | my ($class, $s, $us) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # If it looks like we've been passed floating point seconds, sort it out | ||||||||||||||||||||||||||||
| 78 | 7 | ▶ | if (!defined $us && ($s - int($s))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $us and $s - int($s)
MC/DC0%0/2
not defined $us and $s - int($s)
not defined $us
$s - int($s)
Truth table33%1/3
not(A) and B
A= not defined $usB= $s - int($s)
| |||||||||||||||||||||||||||||
| 79 | 0 | ($s, $us) = _breakup_float ($s); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 7 | ▶ | $us = 0 if (!defined $us); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 7 | return bless ({'s'=>$s, us=>$us}, $class); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # }}} | ||||||||||||||||||||||||||||
| 88 | # {{{ clone | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # {{{ POD | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 clone () | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Returns a new object, holding the same time value as the invocant. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =cut | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # }}} | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 3 | ▶ | sub clone { | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 3 | my ($self) = shift; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | my $new = { %$self }; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 3 | return bless $new => ref($self); # Copy class over | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # }}} | ||||||||||||||||||||||||||||
| 109 | # {{{ numbers | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | sub numbers { | ||||||||||||||||||||||||||
|
Subroutine
numbers: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 113 | return (wantarray) | ||||||||||||||||||||||||||||
| 114 | ? ( $self->{'s'}, $self->{us} ) | ||||||||||||||||||||||||||||
| 115 | 1 | ▶ | : [ $self->{'s'}, $self->{us} ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # }}} | ||||||||||||||||||||||||||||
| 119 | # {{{ round_usec | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head2 round_usec ($usec_step [, $round_up_not_down]) | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Rounds the time down to the nearest usec_step value. Valid values between 10 | ||||||||||||||||||||||||||||
| 124 | and 1000000. A value of 1000000 will round to the nearest second. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Optional argument, if true, causes rounding to go up, not down. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 4 | ▶ | sub round_usec { | ||||||||||||||||||||||||||
|
Subroutine
round_usec: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 4 | my ($self, $val, $up) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 4 | ▶ | if ($val < 10 || $val > 1000000) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$val < 10 or $val > 1000000
MC/DC0%0/2
$val < 10 or $val > 1000000
$val < 10
$val > 1000000
Truth table33%1/3
A or B
A= $val < 10B= $val > 1000000
| |||||||||||||||||||||||||||||
| 134 | 0 | croak ("round_usec([10-1000000]), not '$val'\n"); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 4 | $self->{rem} = $self->{us} % $val; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 4 | $self->{us} -= $self->{rem}; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 4 | ▶ | if ($up && $self->{rem}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$up and $self->{"rem"}
MC/DC50%1/2
$up and $self->{"rem"}
$up
$self->{"rem"}
Truth table66%2/3
A and B
A= $upB= $self->{"rem"}
| |||||||||||||||||||||||||||||
| 142 | 2 | ▶ | if ($val == 1000000) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 1 | $self->{'s'}++; | |||||||||||||||||||||||||||
| 144 | } else { | ||||||||||||||||||||||||||||
| 145 | 1 | $self->{us} += $val ; # round up, not down | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | $self->{rem} = $val - $self->{rem}; # Allow for one level of restore | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # }}} | ||||||||||||||||||||||||||||
| 153 | # {{{ usec | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | ▶ | sub usec { | ||||||||||||||||||||||||||
|
Subroutine
usec: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 1 | return $self->{'s'} * 1000000 + $self->{us}; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # }}} | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | #### Overload methods | ||||||||||||||||||||||||||||
| 164 | # | ||||||||||||||||||||||||||||
| 165 | # {{{ as_string | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 18 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | 18 | my ($self, $fmt) = @_; | |||||||||||||||||||||||||||
| 169 | 18 | my $ret = ''; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # If we've been passed an explicit format, override the default for | ||||||||||||||||||||||||||||
| 172 | # the scope of this execution. | ||||||||||||||||||||||||||||
| 173 | 18 | local $Default_format = $Default_format; | |||||||||||||||||||||||||||
| 174 | 18 | ▶ | Net::Analysis::Time->set_format($fmt) if ($fmt); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 18 | ▶ | if ($Default_format) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 6 | $ret = strftime($Default_format, gmtime($self->{'s'})); | |||||||||||||||||||||||||||
| 178 | } else { | ||||||||||||||||||||||||||||
| 179 | 12 | $ret = $self->{'s'}; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 18 | return $ret . sprintf (".%06d", $self->{us}); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # }}} | ||||||||||||||||||||||||||||
| 186 | # {{{ as_number | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 3 | ▶ | sub as_number { | ||||||||||||||||||||||||||
|
Subroutine
as_number: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 189 | 3 | my ($self) = shift; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 3 | return $self->{'s'} + ($self->{us} / 1000000); | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # }}} | ||||||||||||||||||||||||||||
| 195 | # {{{ numerical_cmp | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 5 | ▶ | sub numerical_cmp { | ||||||||||||||||||||||||||
|
Subroutine
numerical_cmp: called CC=4 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 198 | # If the seconds agree, it's down to the microseconds ... | ||||||||||||||||||||||||||||
| 199 | 5 | ▶ | if (ref($_[0]) ne 'Net::Analysis::Time' || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_[0] ne "Net::Analysis::Time" or ref $_[1] ne "Net::Analysis::Time"
MC/DC0%0/2
ref $_[0] ne "Net::Analysis::Time" or ref $_[1] ne "Net::Analysis::Time"
ref $_[0] ne "Net::Analysis::Time"
ref $_[1] ne "Net::Analysis::Time"
Truth table33%1/3
A or B
A= ref $_[0] ne "Net::Analysis::Time"B= ref $_[1] ne "Net::Analysis::Time"
| |||||||||||||||||||||||||||||
| 200 | ref($_[1]) ne 'Net::Analysis::Time') | ||||||||||||||||||||||||||||
| 201 | { | ||||||||||||||||||||||||||||
| 202 | 0 | confess "Time<=> args bad: ".ref($_[0]).", ".ref($_[1])."\n"; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 5 | ▶ | if ($_[0]->{'s'} == $_[1]->{'s'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 5 | return ($_[0]->{'us'} <=> $_[1]->{'us'}); | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 0 | return ($_[0]->{'s'} <=> $_[1]->{'s'}); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # }}} | ||||||||||||||||||||||||||||
| 212 | # {{{ inceq | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 4 | ▶ | sub inceq { | ||||||||||||||||||||||||||
|
Subroutine
inceq: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 215 | 4 | my ($arg1, $arg2, $arg3) = @_; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # Should really work out what to do here .. | ||||||||||||||||||||||||||||
| 218 | 4 | ▶ | die "we have arg3 1 !\n".Data::Dumper::Dumper(\@_) if ($arg3); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 4 | $arg1->_add (_arg_to_nums ($arg2)); | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 4 | return $arg1; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | # }}} | ||||||||||||||||||||||||||||
| 226 | # {{{ deceq | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 4 | ▶ | sub deceq { | ||||||||||||||||||||||||||
|
Subroutine
deceq: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 229 | 4 | my ($arg1, $arg2, $arg3) = @_; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # Should really work out what to do here .. | ||||||||||||||||||||||||||||
| 232 | 4 | ▶ | die "we have arg3 2 !\n".Data::Dumper::Dumper(\@_) if ($arg3); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 4 | $arg1->_subtract (_arg_to_nums ($arg2)); | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 4 | return $arg1; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # }}} | ||||||||||||||||||||||||||||
| 240 | # {{{ addition | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 1 | ▶ | sub addition { | ||||||||||||||||||||||||||
|
Subroutine
addition: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 243 | 1 | my ($arg1, $arg2, $arg3) = @_; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # Should really work out what to do here .. | ||||||||||||||||||||||||||||
| 246 | 1 | ▶ | die "we have arg3 3!\n".Data::Dumper::Dumper(\@_) if ($arg3); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 1 | my $new = $arg1->clone(); | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 1 | $new->_add (_arg_to_nums ($arg2)); | |||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 1 | return $new; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | # }}} | ||||||||||||||||||||||||||||
| 256 | # {{{ subtraction | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 1 | ▶ | sub subtraction { | ||||||||||||||||||||||||||
|
Subroutine
subtraction: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 259 | 1 | my ($arg1, $arg2, $arg3) = @_; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # Should really work out what to do here .. | ||||||||||||||||||||||||||||
| 262 | 1 | ▶ | confess "we have arg3 4!\n".Data::Dumper::Dumper(\@_) if ($arg3); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 1 | my $new = $arg1->clone(); | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 1 | $new->_subtract (_arg_to_nums ($arg2)); | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 1 | return $new; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | # }}} | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | #### Class methods | ||||||||||||||||||||||||||||
| 274 | # | ||||||||||||||||||||||||||||
| 275 | # {{{ set_format | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =head2 set_format ($format) | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | Set the default output format for stringification of the date/time. | ||||||||||||||||||||||||||||
| 282 | The parameter is either a C<strftime(3)> compliant string, or a named | ||||||||||||||||||||||||||||
| 283 | format: | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | raw - 1100257189.123456 | ||||||||||||||||||||||||||||
| 286 | time - 10:59:49.123456 | ||||||||||||||||||||||||||||
| 287 | full - 2004/11/12 10:59:49.123456 | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | Returns the old format. | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =cut | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 7 | ▶ | sub set_format { | ||||||||||||||||||||||||||
|
Subroutine
set_format: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 294 | 7 | my ($class, $fmt) = @_; | |||||||||||||||||||||||||||
| 295 | 7 | my (%format_shortcuts) = (raw => '', | |||||||||||||||||||||||||||
| 296 | full => '%Y/%m/%d %T', | ||||||||||||||||||||||||||||
| 297 | time => '%T', | ||||||||||||||||||||||||||||
| 298 | ); | ||||||||||||||||||||||||||||
| 299 | 7 | my $old_format = $Default_format; | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 7 | ▶ | if (exists $format_shortcuts{$fmt}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 7 | $Default_format = $format_shortcuts{$fmt}; | |||||||||||||||||||||||||||
| 303 | } else { | ||||||||||||||||||||||||||||
| 304 | 0 | $Default_format = $fmt; | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 7 | return $old_format; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # }}} | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | #### Helpers | ||||||||||||||||||||||||||||
| 313 | # | ||||||||||||||||||||||||||||
| 314 | # {{{ _breakup_float | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 2 | ▶ | sub _breakup_float { | ||||||||||||||||||||||||||
|
Subroutine
_breakup_float: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 317 | 2 | my ($f) = shift; | |||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | # Break up float with: int (rounds down), and sprintf (rounds closest) | ||||||||||||||||||||||||||||
| 320 | 2 | my $s = int($f); | |||||||||||||||||||||||||||
| 321 | 2 | my $us = sprintf ("%6d", ($f - $s) * 1000000); | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 2 | ▶ | return (wantarray) ? ($s,$us) : [$s,$us]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | # }}} | ||||||||||||||||||||||||||||
| 327 | # {{{ _arg_to_nums | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 10 | ▶ | sub _arg_to_nums { | ||||||||||||||||||||||||||
|
Subroutine
_arg_to_nums: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 330 | 10 | my ($arg) = @_; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 10 | ▶ | if (! ref($arg)) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 333 | 2 | return _breakup_float($arg); | |||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | } elsif (ref ($arg) eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 336 | 5 | return (@$arg); | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | } elsif (ref ($arg) eq 'Net::Analysis::Time') { | ||||||||||||||||||||||||||||
| 339 | 3 | return ($arg->{'s'}, $arg->{us}); | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | } else { | ||||||||||||||||||||||||||||
| 342 | 0 | die "could not make arg '$arg' into time: ".Data::Dumper::Dumper($arg); | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | # }}} | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # {{{ _add | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 5 | ▶ | sub _add { | ||||||||||||||||||||||||||
|
Subroutine
_add: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 351 | 5 | my ($self, $s, $us) = @_; | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 5 | $self->{'s'} += $s; | |||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | # Catch overflows | ||||||||||||||||||||||||||||
| 356 | 5 | ▶ | if (($self->{'us'} += $us) > 1000000) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 357 | 1 | $self->{'s'}++; | |||||||||||||||||||||||||||
| 358 | 1 | $self->{'us'} -= 1000000; | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | # }}} | ||||||||||||||||||||||||||||
| 363 | # {{{ _subtract | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 5 | ▶ | sub _subtract { | ||||||||||||||||||||||||||
|
Subroutine
_subtract: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 366 | 5 | my ($self, $s, $us) = @_; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 5 | $self->{'s'} -= $s; | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | # Catch underflows | ||||||||||||||||||||||||||||
| 371 | 5 | ▶ | if (($self->{'us'} -= $us) < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 2 | $self->{'s'}--; | |||||||||||||||||||||||||||
| 373 | 2 | $self->{'us'} += 1000000; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | # }}} | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 3 | 1; | |||||||||||||||||||||||||||
| 380 | __END__ | ||||||||||||||||||||||||||||