File Coverage

blib/lib/Date/Format/Generic.pm
Criterion Covered Total %
statement 115 120 95.8
branch 21 28 75.0
condition 4 7 57.1
subroutine 59 63 93.6
pod 0 57 0.0
total 199 275 72.3


line stmt bran cond sub pod time code
1             ##
2             ##
3             ##
4              
5             package Date::Format::Generic;
6              
7 14     14   99 use strict;
  14         30  
  14         520  
8 14     14   63 use warnings;
  14         42  
  14         1181  
9              
10             our ($epoch, $tzname);
11 14     14   6741 use Time::Zone;
  14         66  
  14         1387  
12 14     14   3901 use Time::Local;
  14         17041  
  14         42759  
13              
14             our $VERSION = '2.35'; # VERSION: generated
15             # ABSTRACT: Date formatting subroutines
16              
17             sub ctime
18             {
19 18     18 0 211 my($me,$t,$tz) = @_;
20 18         101 $me->time2str("%a %b %e %T %Y\n", $t, $tz);
21             }
22              
23             sub asctime
24             {
25 0     0 0 0 my($me,$t,$tz) = @_;
26 0         0 $me->strftime("%a %b %e %T %Y\n", $t, $tz);
27             }
28              
29             sub _subs
30             {
31 786     786   1175 my $fn;
32 786         5963 $_[1] =~ s/
33             %(O?[%a-zA-Z])
34             /
35 1685   66 6   11889 ($_[0]->can("format_$1") || sub { $1 })->($_[0]);
  6         28  
36             /sgeox;
37              
38 786         4182 $_[1];
39             }
40              
41             sub strftime
42             {
43 5     5 0 11 my($pkg,$fmt,$time);
44              
45 5         13 ($pkg,$fmt,$time,$tzname) = @_;
46              
47 5 50       14 my $me = ref($pkg) ? $pkg : bless [];
48              
49 5 100       13 if(defined $tzname)
50             {
51 3         4 $tzname = uc $tzname;
52              
53 3 50       22 $tzname = sprintf("%+05d",$tzname)
54             unless($tzname =~ /\D/);
55              
56 3         4 $epoch = timelocal(@{$time}[0..5]);
  3         13  
57              
58 3         204 @$me = gmtime($epoch + tz_offset($tzname));
59             }
60             else
61             {
62 2         7 @$me = @$time;
63 2         4 undef $epoch;
64             }
65              
66 5         14 _subs($me,$fmt);
67             }
68              
69             sub time2str
70             {
71 751     751 0 610418 my($pkg,$fmt,$time);
72              
73 751         2161 ($pkg,$fmt,$time,$tzname) = @_;
74              
75 751 100       2085 my $me = ref($pkg) ? $pkg : bless [], $pkg;
76              
77 751         1278 $epoch = $time;
78              
79 751 100       1531 if(defined $tzname)
80             {
81 731         1489 $tzname = uc $tzname;
82              
83 731 50       3378 $tzname = sprintf("%+05d",$tzname)
84             unless($tzname =~ /\D/);
85              
86 731         2344 $time += tz_offset($tzname);
87 731         3374 @$me = gmtime($time);
88             }
89             else
90             {
91 20         503 @$me = localtime($time);
92             }
93 751         1896 $me->[9] = $time;
94 751         1699 _subs($me,$fmt);
95             }
96              
97             my(@DoW,@MoY,@DoWs,@MoYs,@AMPM,%format,@Dsuf);
98              
99             @DoW = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
100              
101             @MoY = qw(January February March April May June
102             July August September October November December);
103              
104             @DoWs = map { substr($_,0,3) } @DoW;
105             @MoYs = map { substr($_,0,3) } @MoY;
106              
107             @AMPM = qw(AM PM);
108              
109             @Dsuf = (qw(th st nd rd th th th th th th)) x 3;
110             @Dsuf[11,12,13] = qw(th th th);
111             @Dsuf[30,31] = qw(th st);
112              
113             %format = ('x' => "%m/%d/%y",
114             'C' => "%a %b %e %T %Z %Y",
115             'X' => "%H:%M:%S",
116             );
117              
118             my @locale;
119             my $locale = "/usr/share/lib/locale/LC_TIME/default";
120             local *LOCALE;
121              
122             if(open(LOCALE,"$locale"))
123             {
124             chop(@locale = );
125             close(LOCALE);
126              
127             @MoYs = @locale[0 .. 11];
128             @MoY = @locale[12 .. 23];
129             @DoWs = @locale[24 .. 30];
130             @DoW = @locale[31 .. 37];
131             @format{"X","x","C"} = @locale[38 .. 40];
132             @AMPM = @locale[41 .. 42];
133             }
134              
135             sub wkyr {
136 12     12 0 25 my($wstart, $wday, $yday) = @_;
137 12         27 $wday = ($wday + 7 - $wstart) % 7;
138 12         94 return int(($yday - $wday + 13) / 7 - 1);
139             }
140              
141             ##
142             ## these 6 formatting routines need to be *copied* into the language
143             ## specific packages
144             ##
145              
146             my @roman = ('',qw(I II III IV V VI VII VIII IX));
147             sub roman {
148 60     60 0 97 my $n = shift;
149              
150 60         262 $n =~ s/(\d)$//;
151 60         173 my $r = $roman[ $1 ];
152              
153 60 100       196 if($n =~ s/(\d)$//) {
154 44         112 (my $t = $roman[$1]) =~ tr/IVX/XLC/;
155 44         98 $r = $t . $r;
156             }
157 60 100       145 if($n =~ s/(\d)$//) {
158 10         27 (my $t = $roman[$1]) =~ tr/IVX/CDM/;
159 10         18 $r = $t . $r;
160             }
161 60 100       132 if($n =~ s/(\d)$//) {
162 5         13 (my $t = $roman[$1]) =~ tr/IVX/M../;
163 5         11 $r = $t . $r;
164             }
165 60         228 $r;
166             }
167              
168 151     151 0 724 sub format_a { $DoWs[$_[0]->[6]] }
169 2     2 0 8 sub format_A { $DoW[$_[0]->[6]] }
170 151     151 0 468 sub format_b { $MoYs[$_[0]->[4]] }
171 2     2 0 9 sub format_B { $MoY[$_[0]->[4]] }
172 1     1 0 5 sub format_h { $MoYs[$_[0]->[4]] }
173 7 100   7 0 61 sub format_p { $_[0]->[2] >= 12 ? $AMPM[1] : $AMPM[0] }
174 0 0   0 0 0 sub format_P { lc($_[0]->[2] >= 12 ? $AMPM[1] : $AMPM[0]) }
175              
176 29     29 0 153 sub format_d { sprintf("%02d",$_[0]->[3]) }
177 184     184 0 977 sub format_e { sprintf("%2d",$_[0]->[3]) }
178 208     208 0 825 sub format_H { sprintf("%02d",$_[0]->[2]) }
179 17   50 17 0 136 sub format_I { sprintf("%02d",$_[0]->[2] % 12 || 12)}
180 11     11 0 88 sub format_j { sprintf("%03d",$_[0]->[7] + 1) }
181 11     11 0 66 sub format_k { sprintf("%2d",$_[0]->[2]) }
182 11   50 11 0 88 sub format_l { sprintf("%2d",$_[0]->[2] % 12 || 12)}
183 6     6 0 28 sub format_L { $_[0]->[4] + 1 }
184 29     29 0 239 sub format_m { sprintf("%02d",$_[0]->[4] + 1) }
185 214     214 0 700 sub format_M { sprintf("%02d",$_[0]->[1]) }
186 11     11 0 91 sub format_q { sprintf("%01d",int($_[0]->[4] / 3) + 1) }
187             sub format_s {
188 13 100   13 0 35 $epoch = timelocal(@{$_[0]}[0..5])
  2         12  
189             unless defined $epoch;
190 13         223 sprintf("%d",$epoch)
191             }
192 203     203 0 1060 sub format_S { sprintf("%02d",$_[0]->[0]) }
193 6     6 0 26 sub format_U { wkyr(0, $_[0]->[6], $_[0]->[7]) }
194 6     6 0 39 sub format_w { $_[0]->[6] }
195 6     6 0 23 sub format_W { wkyr(1, $_[0]->[6], $_[0]->[7]) }
196 34     34 0 212 sub format_y { sprintf("%02d",$_[0]->[5] % 100) }
197 189     189 0 892 sub format_Y { sprintf("%04d",$_[0]->[5] + 1900) }
198              
199             sub format_Z {
200 175     175 0 512 my $o = tz_local_offset($_[0]->[9]);
201 175 50       656 defined $tzname ? $tzname : uc tz_name($o, $_[0]->[8]);
202             }
203              
204             sub format_z {
205 11     11 0 26 my $t = $_[0]->[9];
206 11 50       65 my $o = defined $tzname ? tz_offset($tzname, $t) : tz_offset(undef,$t);
207 11         99 sprintf("%+03d%02d", int($o / 3600), int(abs($o) % 3600) / 60);
208             }
209              
210 6     6 0 18 sub format_c { &format_x . " " . &format_X }
211 6     6 0 17 sub format_D { &format_m . "/" . &format_d . "/" . &format_y }
212 6     6 0 21 sub format_r { &format_I . ":" . &format_M . ":" . &format_S . " " . &format_p }
213 6     6 0 21 sub format_R { &format_H . ":" . &format_M }
214 179     179 0 311 sub format_T { &format_H . ":" . &format_M . ":" . &format_S }
215 0     0 0 0 sub format_t { "\t" }
216 0     0 0 0 sub format_n { "\n" }
217 2     2 0 18 sub format_o { sprintf("%2d%s",$_[0]->[3],$Dsuf[$_[0]->[3]]) }
218 12     12 0 41 sub format_x { my $f = $format{'x'}; _subs($_[0],$f); }
  12         37  
219 12     12 0 37 sub format_X { my $f = $format{'X'}; _subs($_[0],$f); }
  12         38  
220 6     6 0 32 sub format_C { my $f = $format{'C'}; _subs($_[0],$f); }
  6         17  
221              
222 5     5 0 25 sub format_Od { roman(format_d(@_)) }
223 5     5 0 21 sub format_Oe { roman(format_e(@_)) }
224 5     5 0 18 sub format_OH { roman(format_H(@_)) }
225 5     5 0 19 sub format_OI { roman(format_I(@_)) }
226 5     5 0 22 sub format_Oj { roman(format_j(@_)) }
227 5     5 0 20 sub format_Ok { roman(format_k(@_)) }
228 5     5 0 18 sub format_Ol { roman(format_l(@_)) }
229 5     5 0 17 sub format_Om { roman(format_m(@_)) }
230 5     5 0 23 sub format_OM { roman(format_M(@_)) }
231 5     5 0 20 sub format_Oq { roman(format_q(@_)) }
232 5     5 0 19 sub format_Oy { roman(format_y(@_)) }
233 5     5 0 22 sub format_OY { roman(format_Y(@_)) }
234              
235 3     3 0 24 sub format_G { int(($_[0]->[9] - 315993600) / 604800) }
236              
237             1;
238              
239             __END__