3 |
use 5.001; |
use 5.001; |
4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
6 |
|
use Carp; |
7 |
|
|
8 |
require Exporter; |
require Exporter; |
9 |
|
|
60 |
$self->{ARGS} = {@_}; |
$self->{ARGS} = {@_}; |
61 |
$debug = $self->{ARGS}->{DEBUG}; |
$debug = $self->{ARGS}->{DEBUG}; |
62 |
|
|
63 |
die("need start time") if (! $self->{ARGS}->{start}); |
croak("need start time") if (! $self->{ARGS}->{start}); |
64 |
|
|
65 |
# calc start and stop seconds |
# calc start and stop seconds |
66 |
my ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{start},3); |
my ($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{start},3); |
67 |
print STDERR "new: start time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug); |
print STDERR "new: start time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug); |
68 |
my $s = $hh * 3600 || die("need at least hour specified for start time"); |
my $s = $hh * 3600 || croak("need at least hour specified for start time"); |
69 |
$s += $mm * 60 if ($mm); |
$s += $mm * 60 if ($mm); |
70 |
$s += $ss if ($ss); |
$s += $ss if ($ss); |
71 |
$self->{start} = $s; |
$self->{start} = $s; |
72 |
|
|
73 |
die("need end time") if (! $self->{ARGS}->{end}); |
croak("need end time") if (! $self->{ARGS}->{end}); |
74 |
|
|
75 |
($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{end},3); |
($hh,$mm,$ss) = split(/:/,$self->{ARGS}->{end},3); |
76 |
print STDERR "new: end time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug); |
print STDERR "new: end time ",$hh||0,":",$mm||0,":",$ss||0,"\n" if ($debug); |
77 |
$s = $hh * 3600 || die("need at least hour specified for end time"); |
$s = $hh * 3600 || croak("need at least hour specified for end time"); |
78 |
$s += $mm * 60 if ($mm); |
$s += $mm * 60 if ($mm); |
79 |
$self->{end} = $s; |
$self->{end} = $s; |
80 |
|
|
81 |
die("need dayMask specified") if (! $self->{ARGS}->{dayMask}); |
croak("need dayMask specified") if (! $self->{ARGS}->{dayMask}); |
82 |
|
|
83 |
$self->{dayMask} = $self->{ARGS}->{dayMask}; |
$self->{dayMask} = $self->{ARGS}->{dayMask}; |
84 |
|
|
129 |
sub uptime { |
sub uptime { |
130 |
my $self = shift; |
my $self = shift; |
131 |
|
|
132 |
my $time = shift || die "need uptime timestamp to calculate uptime"; |
my $time = shift || croak "need uptime timestamp to calculate uptime"; |
133 |
|
|
134 |
# calculate offset -- that is number of seconds since midnight |
# calculate offset -- that is number of seconds since midnight |
135 |
my @lt = gmtime($time); |
my @lt = gmtime($time); |
183 |
sub downtime { |
sub downtime { |
184 |
my $self = shift; |
my $self = shift; |
185 |
|
|
186 |
my $time = shift || die "need downtime timestamp to calculate uptime"; |
my $time = shift || croak "need downtime timestamp to calculate uptime"; |
187 |
|
|
188 |
# calculate offset -- that is number of seconds since midnight |
# calculate offset -- that is number of seconds since midnight |
189 |
my @lt = gmtime($time); |
my @lt = gmtime($time); |
257 |
|
|
258 |
sub interval { |
sub interval { |
259 |
my $self = shift; |
my $self = shift; |
260 |
my $from = shift || die "need start time for interval"; |
my $from = shift || croak "need start time for interval"; |
261 |
my $to = shift || die "need end time for interval"; |
my $to = shift || croak "need end time for interval"; |
262 |
|
|
263 |
print STDERR "from:\t$from\t",scalar gmtime($from),"\n" if ($debug); |
print STDERR "from:\t$from\t",scalar gmtime($from),"\n" if ($debug); |
264 |
print STDERR "to:\t$to\t",scalar gmtime($to),"\n" if ($debug); |
print STDERR "to:\t$to\t",scalar gmtime($to),"\n" if ($debug); |
290 |
# add rest of last day |
# add rest of last day |
291 |
print STDERR "t:\t$to\t",scalar gmtime($to),"\n" if ($debug); |
print STDERR "t:\t$to\t",scalar gmtime($to),"\n" if ($debug); |
292 |
|
|
293 |
$total -= $self->downtime($to); |
$total = abs($total - $self->downtime($to)); |
294 |
print STDERR "total: $total (final)\n" if ($debug); |
print STDERR "total: $total (final)\n" if ($debug); |
295 |
|
|
296 |
return $total; |
return $total; |
303 |
sub day_in_interval { |
sub day_in_interval { |
304 |
my $self = shift; |
my $self = shift; |
305 |
|
|
306 |
my $time = shift || die "need timestamp to check if day is in interval"; |
my $time = shift || croak "need timestamp to check if day is in interval"; |
307 |
|
|
308 |
my @lt = gmtime($time); |
my @lt = gmtime($time); |
309 |
return $self->_dayOk($lt[6]); |
return $self->_dayOk($lt[6]); |
430 |
=over 8 |
=over 8 |
431 |
|
|
432 |
=item * |
=item * |
|
Use croak and not die in module for better error handling |
|
|
|
|
|
=item * |
|
433 |
Allow arbitary (array?) of holidays to be included. |
Allow arbitary (array?) of holidays to be included. |
434 |
|
|
435 |
=back |
=back |