| 1 | package SPOPS::Utility; | ||
| 2 | |||
| 3 | # $Id: Utility.pm,v 3.5 2004/06/02 00:48:22 lachoy Exp $ | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Log::Log4perl qw( get_logger ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use SPOPS; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | |||
| 9 | my $log = get_logger(); | ||
| 10 | |||
| 11 | $SPOPS::Utility::VERSION = sprintf("%d.%02d", q$Revision: 3.5 $ =~ /(\d+)\.(\d+)/); | ||
| 12 | |||
| 13 | |||
| 14 | # initialize limit tracking vars -- the limit passed in can be: | ||
| 15 | # limit => 'x,y' --> 'offset = x, max = y' | ||
| 16 | # limit => 'x' --> 'max = x' | ||
| 17 | |||
| 18 | sub determine_limit { | ||
| 19 | my ( $class, $limit ) = @_; | ||
| 20 | return ( 0, 0 ) unless ( $limit ); | ||
| 21 | if ( $limit =~ /,/ ) { | ||
| 22 | my ( $offset, $max ) = split /\s*,\s*/, $limit; | ||
| 23 | $max += $offset; | ||
| 24 | $log->is_info && | ||
| 25 | $log->info( "Limit set: Start $offset to $max" ); | ||
| 26 | return ( $offset, $max ); | ||
| 27 | } | ||
| 28 | else { | ||
| 29 | $log->is_info && | ||
| 30 | $log->info( "Limit set: Start 0 to $limit" ); | ||
| 31 | return ( 0, $limit ); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | # Return a random code of length $length. If $opt is 'mixed', then the | ||
| 37 | # code is filled with both lower- and upper-case charaters. | ||
| 38 | # | ||
| 39 | # Signature: $code = $class->generate_random_code( $length, [ 'mixed' ] ); | ||
| 40 | |||
| 41 | sub generate_random_code { | ||
| 42 | my ( $class, $length, $opt ) = @_; | ||
| 43 | $opt ||= ''; | ||
| 44 | return undef unless ( $length ); | ||
| 45 | if ( $opt eq 'mixed' ) { | ||
| 46 | return join '', map { ( $_ % 2 == 0 ) | ||
| 47 | ? chr( int( rand(26) ) + 65 ) | ||
| 48 | : chr( int( rand(26) ) + 97 ) } ( 1 .. $length ); | ||
| 49 | } | ||
| 50 | return join '', map { chr( int( rand(26) ) + 65 ) } ( 1 .. $length ); | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | # Return a 'crypt'ed version of $text | ||
| 55 | # | ||
| 56 | # Signature: $crypted = $class->crypt_it( $text ); | ||
| 57 | |||
| 58 | sub crypt_it { | ||
| 59 | my ( $class, $text ) = @_; | ||
| 60 | return undef unless ( $text ); | ||
| 61 | my $salt = $class->generate_random_code( 2 ); | ||
| 62 | return crypt( $text, $salt ); | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | ######################################## | ||
| 67 | # DATE/TIME | ||
| 68 | ######################################## | ||
| 69 | |||
| 70 | # Return a { time } (or the current time) formatted with { format } | ||
| 71 | # | ||
| 72 | # Signature: $time_string = $class->now( [ { format => $strftime_format, | ||
| 73 | # time => $time_in_seconds } ] ); | ||
| 74 | |||
| 75 | sub now { | ||
| 76 | my ( $class, $p ) = @_; | ||
| 77 | require Class::Date; | ||
| 78 | $p->{format} ||= '%Y-%m-%d %T'; | ||
| 79 | $p->{time} ||= time; | ||
| 80 | return Class::Date->new( $p->{time} )->strftime( $p->{format} ); | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | # Return the current time formatted 'yyyy-mm-dd' | ||
| 85 | # | ||
| 86 | # Signature: $date_string = $class->today(); | ||
| 87 | |||
| 88 | sub today { return $_[0]->now( { format => '%Y-%m-%d' } ); } | ||
| 89 | |||
| 90 | |||
| 91 | # Return a true value if right now is between two other dates | ||
| 92 | |||
| 93 | # Signature: | ||
| 94 | # DATE_FORMAT is [ yyyy,mm,dd ] or 'yyyy-mm-dd' | ||
| 95 | # $rv = $class->date_between_dates( { begin => DATE_FORMAT, | ||
| 96 | # end => DATE_FORMAT } ); | ||
| 97 | |||
| 98 | sub now_between_dates { | ||
| 99 | my ( $class, $p ) = @_; | ||
| 100 | return undef unless ( $p->{begin} or $p->{end} ); | ||
| 101 | |||
| 102 | require Class::Date; | ||
| 103 | my $now = Class::Date->now; | ||
| 104 | my ( $begin_date, $end_date ); | ||
| 105 | |||
| 106 | if ( $p->{begin} ) { | ||
| 107 | if ( ref $p->{begin} eq 'ARRAY' ) { | ||
| 108 | $begin_date = Class::Date->new( $p->{begin} ); | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | $begin_date = Class::Date->new([ split /\D+/, $p->{begin} ]); | ||
| 112 | } | ||
| 113 | return undef if ( $now < $begin_date ); | ||
| 114 | } | ||
| 115 | |||
| 116 | if ( $p->{end} ) { | ||
| 117 | if ( ref $p->{end} eq 'ARRAY' ) { | ||
| 118 | $end_date = Class::Date->new( $p->{end} ); | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | $end_date = Class::Date->new([ split /\D+/, $p->{end} ]); | ||
| 122 | } | ||
| 123 | return undef if ( $now > $end_date ); | ||
| 124 | } | ||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | # Pass in \@existing and \@new and get back a hashref with: | ||
| 130 | # add => \@: items in \@new but not in \@existing, | ||
| 131 | # keep => \@: items in \@new and in \@existing, | ||
| 132 | # remove => \@: items not in \@new but in \@existing | ||
| 133 | |||
| 134 | sub list_process { | ||
| 135 | my ( $class, $exist, $new ) = @_; | ||
| 136 | |||
| 137 | # Create a hash of the existing items | ||
| 138 | |||
| 139 | my %existing = map { $_ => 1 } @{ $exist }; | ||
| 140 | my ( @k, @a ); | ||
| 141 | |||
| 142 | # Go through the new items... | ||
| 143 | |||
| 144 | foreach my $new_id ( @{ $new } ) { | ||
| 145 | |||
| 146 | #... if it's existing, track it as a keeper and remove it | ||
| 147 | # from the existing pile | ||
| 148 | |||
| 149 | if ( $existing{ $new_id } ) { | ||
| 150 | delete $existing{ $new_id }; | ||
| 151 | push @k, $new_id; | ||
| 152 | } | ||
| 153 | |||
| 154 | # otherwise, track it as an add | ||
| 155 | |||
| 156 | else { | ||
| 157 | push @a, $new_id; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | # now, the only items left in %existing are the ones | ||
| 162 | # that were not specified in the new list; therefore, | ||
| 163 | # these should be removed | ||
| 164 | |||
| 165 | return { add => \@a, keep => \@k, remove => [ keys %existing ] }; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; | ||
| 169 | |||
| 170 | __END__ | ||