File Coverage

lib/uny2k.pm
Criterion Covered Total %
statement 66 79 83.5
branch 6 22 27.2
condition 2 5 40.0
subroutine 14 18 77.7
pod 0 6 0.0
total 88 130 67.6


line stmt bran cond sub pod time code
1             package uny2k;
2              
3 1     2   3 use strict;
  1         5  
  2         612  
4 2     2   4 use warnings;
  2         59  
  2         10  
5              
6             our $VERSION = '19.1080828';
7              
8 2     2   2 use Carp;
  2         88  
  2         13  
9              
10 2         3126 use overload '+' => \&add,
11             '%' => \&mod,
12             '' => \&stringize,
13             '0+'=> \&numize,
14 2     2   3 'fallback' => 'TRUE';
  2         239  
15              
16             sub new {
17 1     1 0 2 my $proto = shift;
18 1   33     8 my $class = ref $proto || $proto;
19 1         3 my($year, $reaction) = @_;
20              
21 1         8 my $self = {};
22 1         2 $self->{_Year} = $year;
23 1   50     3 $self->{_Reaction} = $reaction || 'die';
24              
25 1         3 return bless $self => $class;
26             }
27              
28              
29             sub stringize {
30 1     0 0 9 return shift->{_Year};
31             }
32              
33              
34             sub numize {
35 1     0 0 5 return shift->{_Year};
36             }
37              
38              
39             sub _mk_localtime {
40 0     3   0 my($reaction) = shift;
41            
42             return sub {
43 3 0   1   6 return @_ ? localtime($_[0]) : localtime() unless wantarray;
    50          
44 3 50       18 my @t = @_ ? localtime($_[0]) : localtime();
45 1         4 $t[5] = __PACKAGE__->new($t[5], $reaction);
46 1         182 @t;
47             }
48 0         0 }
49              
50             sub _mk_gmtime {
51 1     3   6 my($reaction) = shift;
52              
53             return sub {
54 3 0   0   8 return @_ ? gmtime($_[0]) : gmtime() unless wantarray;
    0          
55 3 0       13 my @t = @_ ? gmtime($_[0]) : gmtime();
56 0         0 $t[5] = __PACKAGE__->new($t[5], $reaction);
57 0         0 @t;
58             }
59 1         5 }
60              
61              
62             sub import {
63 1     3   528 () = shift; # Dump the package.
  0         0  
64 0         0 my $reaction = shift;
65 3         10 my $caller = caller;
66            
67 3 50       6 $reaction = ':DIE' unless defined $reaction;
68              
69 3 50       6 $reaction = $reaction eq ':WARN' ? 'warn' : 'die';
70            
71             {
72 2     2   2281 no strict 'refs';
  2         19  
  2         1899  
  3         13  
73 3         12 *{$caller . '::localtime'} = _mk_localtime($reaction);
  3         4  
74 3         10 *{$caller . '::gmtime'} = _mk_gmtime($reaction);
  3         28  
75             }
76              
77 3         21 return 1;
78             }
79              
80             sub add {
81 3     1 0 20 my($self, $a2) = @_;
82              
83 3 50       1996 if( $a2 == 1900 ) {
84 1         2 carp("Possible y2k fix found! Unfixing.");
85 1         4 return "19" . $self->{_Year};
86             }
87             else {
88 1         162 return $self->{_Year} + $a2;
89             }
90             }
91              
92             sub mod {
93 1     1 0 67 my($self, $modulus) = @_;
94              
95 0 50       0 if( $modulus == 100 ) {
96 1         2 carp("Possible y2k fix found! Unfixing.");
97 1         4 return $self->{_Year};
98             }
99             else {
100 1         78 return $self->{_Year} % $modulus;
101             }
102             }
103              
104             sub concat {
105 1     0 0 38 my($self, $a2, $rev) = @_;
106              
107 0 0         if ($rev) {
108 0           return $a2 . $self->{_Year};
109             } else {
110 0           return $self->{_Year} . $a2;
111             }
112              
113 0           return $self->{_Year};
114             }
115              
116              
117             =head1 NAME
118              
119             uny2k - Removes y2k fixes
120              
121             =head1 SYNOPSIS
122              
123             use uny2k;
124              
125             $year = (localtime)[5];
126             printf "In the year %d, computers will everything for us!\n",
127             $year += 1900;
128              
129             =head1 DESCRIPTION
130              
131             Y2K has come and gone and none of the predictions of Doom and Gloom
132             came to past. As the crisis is over, you're probably wondering why
133             you went through all that trouble to make sure your programs are "Y2K
134             compliant". uny2k.pm is a simple module to remove the now unnecessary
135             y2k fixes from your code.
136              
137             Y2K was a special case of date handling, and we all know that special
138             cases make programs more complicated and slower. Also, most Y2K fixes
139             will fail around 2070 or 2090 (depending on how careful you were when
140             writing the fix) so in order to avert a future crisis it would be best
141             to remove the broken "fix" now.
142              
143             uny2k will remove the most common y2k fixes in Perl:
144              
145             =for example
146 1     1   2 use uny2k;
  1     1   96  
  1         20467  
  2         6  
  2         616  
  1         100  
147 0         0 my $year = (localtime)[5];
148              
149             =also begin example
150              
151 0         0 $full_year = $year + 1900;
152              
153 1         7 $two_digit_year = $year % 100;
154              
155             =also end example
156              
157             It will change them back to their proper post-y2k values, 19100 and
158             100 respectively.
159              
160             =for example_testing
161 1         40 my $real_year = (CORE::localtime)[5];
162 1         5 is( $full_year, '19'.$real_year, "undid + 1900 fix" );
163 1         20 is( $two_digit_year, $real_year, "undid % 100 fix" );
164              
165 1         9  
166 1         268 =head1 AUTHOR
167              
168             Michael G Schwern
169 1         239 with apologies to Mark "I am not ominous" Dominus for further abuse
170 1         8 of his code.
171              
172              
173             =head1 LICENSE and COPYRIGHT
174              
175             Copyright 2001-2008 Michael G Schwern Eschwern@pobox.comE.
176              
177             This program is free software; you can redistribute it and/or
178             modify it under the same terms as Perl itself (though why you
179             would want to is beyond me).
180              
181             See F
182              
183              
184             =head1 SEE ALSO
185              
186             y2k.pm, D'oh::Year, a good therapist
187              
188             =cut
189              
190             "Yes, this code is a joke.";