File Coverage

blib/lib/OpenPlugin/Utility.pm
Criterion Covered Total %
statement 16 19 84.2
branch 3 6 50.0
condition 3 7 42.8
subroutine 4 4 100.0
pod 0 2 0.0
total 26 38 68.4


line stmt bran cond sub pod time code
1             package OpenPlugin::Utility;
2              
3             # $Id: Utility.pm,v 1.7 2003/04/03 01:51:24 andreychek Exp $
4              
5 7     7   40 use strict;
  7         12  
  7         513  
6              
7             $OpenPlugin::Utility::VERSION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
8 7     7   38 use Digest::MD5();
  7         11  
  7         4126  
9              
10             # Given an expiration in one of various formats, convert it to seconds
11             # This code was inspired by, and largely taken from, CGI.pm
12              
13             # The format for expire can be in any of the forms...
14             # "now" -- expire immediately
15             # "+180s" -- in 180 seconds
16             # "+2m" -- in 2 minutes
17             # "+12h" -- in 12 hours
18             # "+1d" -- in 1 day
19             # "+3M" -- in 3 months
20             # "+2y" -- in 2 years
21             # "-3m" -- 3 minutes ago(!)
22             # You may also sent in an exact time in seconds. Anything else is
23             # considered invalid.
24             sub expire_calc {
25 1     1 0 6 my ( $self, $expire, $time ) = @_;
26              
27 1   33     5 $time ||= time;
28              
29 1         9 my ( %mult ) = ('s'=>1,
30             'm'=>60,
31             'h'=>60*60,
32             'd'=>60*60*24,
33             'M'=>60*60*24*30,
34             'y'=>60*60*24*365);
35              
36 1         2 my ( $offset );
37              
38 1 50       15 if (lc( $expire ) eq 'now') {
    50          
    50          
39 0         0 $offset = 0;
40             }
41             elsif ( $expire =~ /^\d+/ ) {
42 0         0 return $expire;
43             }
44             elsif ( $expire =~ /^([+-]?(?:\d+|\d*\.\d*))([mhdMy]?)/ ) {
45 1   50     33 $offset = ($mult{$2} || 1) * $1;
46             }
47             else {
48 0         0 return 0;
49             }
50              
51 1         7 return ( $time + $offset );
52             }
53              
54             # Generate a random id
55             # Not perfect, but it'll give us reasonable ID's
56             sub generate_rand_id {
57 1     1 0 74 my ( $self, $length ) = @_;
58              
59 1   50     11 $length ||= '32';
60              
61 1         92 return substr(Digest::MD5::md5_hex(Digest::MD5::md5_hex(time(). {}. rand(). $$)), 0, $length);
62              
63             }
64              
65             1;
66              
67             __END__