forked from ryhmrt/bassy-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPullTimer.pm
46 lines (40 loc) · 1.09 KB
/
PullTimer.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package PullTimer;
use strict;
use warnings;
sub new {
my $class = shift;
my @local_epoch = localtime(0);
my $self = bless {
_LOCAL_DIFF => $local_epoch[0] + $local_epoch[1]*60 + $local_epoch[2]*60*60,
_TARGETS => {}
}, $class;
$self->{_LAST_TIME} = $self->time();
return $self;
}
sub time {
my $self = shift;
return (time() + $self->{_LOCAL_DIFF}) % (24*60*60);
}
sub add_target {
my $self = shift;
my $time = shift;
my $content = shift;
my ($h, $m) = split(/\:/, $time);
$self->{_TARGETS}{$h*60*60 + $m*60} = $content;
# print "timer updated: ", %{$self->{_TARGETS}}, "\n";
}
sub pull {
my $self = shift;
my $time = $self->time();
my $last_time = $self->{_LAST_TIME};
# print "search timer between $last_time and $time\n";
my @current_times = grep {
($_ > $last_time and $_ <= $time)
or ($time < $last_time and $_ > $last_time)
or ($time < $last_time and $_ <= $time)
} keys %{$self->{_TARGETS}};
# print "result :", join(",", @current_times), "\n";
$self->{_LAST_TIME} = $time;
return map { $self->{_TARGETS}{$_} } @current_times;
}
1;