| 1 |
## |
| 2 |
## |
| 3 |
## |
| 4 |
|
| 5 |
package Date::Format::Generic; |
| 6 |
|
| 7 |
use strict; |
| 8 |
use warnings; |
| 9 |
|
| 10 |
our ($epoch, $tzname); |
| 11 |
use Time::Zone; |
| 12 |
use Time::Local; |
| 13 |
|
| 14 |
our $VERSION = '2.35'; # VERSION: generated |
| 15 |
# ABSTRACT: Date formatting subroutines |
| 16 |
|
| 17 |
sub ctime |
| 18 |
{ |
| 19 |
my($me,$t,$tz) = @_; |
| 20 |
$me->time2str("%a %b %e %T %Y\n", $t, $tz); |
| 21 |
} |
| 22 |
|
| 23 |
sub asctime |
| 24 |
{ |
| 25 |
my($me,$t,$tz) = @_; |
| 26 |
$me->strftime("%a %b %e %T %Y\n", $t, $tz); |
| 27 |
} |
| 28 |
|
| 29 |
sub _subs |
| 30 |
{ |
| 31 |
my $fn; |
| 32 |
$_[1] =~ s/ |
| 33 |
%(O?[%a-zA-Z]) |
| 34 |
/ |
| 35 |
($_[0]->can("format_$1") || sub { $1 })->($_[0]); |
| 36 |
/sgeox; |
| 37 |
|
| 38 |
$_[1]; |
| 39 |
} |
| 40 |
|
| 41 |
sub strftime |
| 42 |
{ |
| 43 |
my($pkg,$fmt,$time); |
| 44 |
|
| 45 |
($pkg,$fmt,$time,$tzname) = @_; |
| 46 |
|
| 47 |
my $me = ref($pkg) ? $pkg : bless []; |
| 48 |
|
| 49 |
if(defined $tzname) |
| 50 |
{ |
| 51 |
$tzname = uc $tzname; |
| 52 |
|
| 53 |
$tzname = sprintf("%+05d",$tzname) |
| 54 |
unless($tzname =~ /\D/); |
| 55 |
|
| 56 |
$epoch = timelocal(@{$time}[0..5]); |
| 57 |
|
| 58 |
@$me = gmtime($epoch + tz_offset($tzname)); |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
@$me = @$time; |
| 63 |
undef $epoch; |
| 64 |
} |
| 65 |
|
| 66 |
_subs($me,$fmt); |
| 67 |
} |
| 68 |
|
| 69 |
sub time2str |
| 70 |
{ |
| 71 |
my($pkg,$fmt,$time); |
| 72 |
|
| 73 |
($pkg,$fmt,$time,$tzname) = @_; |
| 74 |
|
| 75 |
my $me = ref($pkg) ? $pkg : bless [], $pkg; |
| 76 |
|
| 77 |
$epoch = $time; |
| 78 |
|
| 79 |
if(defined $tzname) |
| 80 |
{ |
| 81 |
$tzname = uc $tzname; |
| 82 |
|
| 83 |
$tzname = sprintf("%+05d",$tzname) |
| 84 |
unless($tzname =~ /\D/); |
| 85 |
|
| 86 |
$time += tz_offset($tzname); |
| 87 |
@$me = gmtime($time); |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
@$me = localtime($time); |
| 92 |
} |
| 93 |
$me->[9] = $time; |
| 94 |
_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 = <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 |
my($wstart, $wday, $yday) = @_; |
| 137 |
$wday = ($wday + 7 - $wstart) % 7; |
| 138 |
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 |
my $n = shift; |
| 149 |
|
| 150 |
$n =~ s/(\d)$//; |
| 151 |
my $r = $roman[ $1 ]; |
| 152 |
|
| 153 |
if($n =~ s/(\d)$//) { |
| 154 |
(my $t = $roman[$1]) =~ tr/IVX/XLC/; |
| 155 |
$r = $t . $r; |
| 156 |
} |
| 157 |
if($n =~ s/(\d)$//) { |
| 158 |
(my $t = $roman[$1]) =~ tr/IVX/CDM/; |
| 159 |
$r = $t . $r; |
| 160 |
} |
| 161 |
if($n =~ s/(\d)$//) { |
| 162 |
(my $t = $roman[$1]) =~ tr/IVX/M../; |
| 163 |
$r = $t . $r; |
| 164 |
} |
| 165 |
$r; |
| 166 |
} |
| 167 |
|
| 168 |
sub format_a { $DoWs[$_[0]->[6]] } |
| 169 |
sub format_A { $DoW[$_[0]->[6]] } |
| 170 |
sub format_b { $MoYs[$_[0]->[4]] } |
| 171 |
sub format_B { $MoY[$_[0]->[4]] } |
| 172 |
sub format_h { $MoYs[$_[0]->[4]] } |
| 173 |
sub format_p { $_[0]->[2] >= 12 ? $AMPM[1] : $AMPM[0] } |
| 174 |
sub format_P { lc($_[0]->[2] >= 12 ? $AMPM[1] : $AMPM[0]) } |
| 175 |
|
| 176 |
sub format_d { sprintf("%02d",$_[0]->[3]) } |
| 177 |
sub format_e { sprintf("%2d",$_[0]->[3]) } |
| 178 |
sub format_H { sprintf("%02d",$_[0]->[2]) } |
| 179 |
sub format_I { sprintf("%02d",$_[0]->[2] % 12 || 12)} |
| 180 |
sub format_j { sprintf("%03d",$_[0]->[7] + 1) } |
| 181 |
sub format_k { sprintf("%2d",$_[0]->[2]) } |
| 182 |
sub format_l { sprintf("%2d",$_[0]->[2] % 12 || 12)} |
| 183 |
sub format_L { $_[0]->[4] + 1 } |
| 184 |
sub format_m { sprintf("%02d",$_[0]->[4] + 1) } |
| 185 |
sub format_M { sprintf("%02d",$_[0]->[1]) } |
| 186 |
sub format_q { sprintf("%01d",int($_[0]->[4] / 3) + 1) } |
| 187 |
sub format_s { |
| 188 |
$epoch = timelocal(@{$_[0]}[0..5]) |
| 189 |
unless defined $epoch; |
| 190 |
sprintf("%d",$epoch) |
| 191 |
} |
| 192 |
sub format_S { sprintf("%02d",$_[0]->[0]) } |
| 193 |
sub format_U { wkyr(0, $_[0]->[6], $_[0]->[7]) } |
| 194 |
sub format_w { $_[0]->[6] } |
| 195 |
sub format_W { wkyr(1, $_[0]->[6], $_[0]->[7]) } |
| 196 |
sub format_y { sprintf("%02d",$_[0]->[5] % 100) } |
| 197 |
sub format_Y { sprintf("%04d",$_[0]->[5] + 1900) } |
| 198 |
|
| 199 |
sub format_Z { |
| 200 |
my $o = tz_local_offset($_[0]->[9]); |
| 201 |
defined $tzname ? $tzname : uc tz_name($o, $_[0]->[8]); |
| 202 |
} |
| 203 |
|
| 204 |
sub format_z { |
| 205 |
my $t = $_[0]->[9]; |
| 206 |
my $o = defined $tzname ? tz_offset($tzname, $t) : tz_offset(undef,$t); |
| 207 |
sprintf("%+03d%02d", int($o / 3600), int(abs($o) % 3600) / 60); |
| 208 |
} |
| 209 |
|
| 210 |
sub format_c { &format_x . " " . &format_X } |
| 211 |
sub format_D { &format_m . "/" . &format_d . "/" . &format_y } |
| 212 |
sub format_r { &format_I . ":" . &format_M . ":" . &format_S . " " . &format_p } |
| 213 |
sub format_R { &format_H . ":" . &format_M } |
| 214 |
sub format_T { &format_H . ":" . &format_M . ":" . &format_S } |
| 215 |
sub format_t { "\t" } |
| 216 |
sub format_n { "\n" } |
| 217 |
sub format_o { sprintf("%2d%s",$_[0]->[3],$Dsuf[$_[0]->[3]]) } |
| 218 |
sub format_x { my $f = $format{'x'}; _subs($_[0],$f); } |
| 219 |
sub format_X { my $f = $format{'X'}; _subs($_[0],$f); } |
| 220 |
sub format_C { my $f = $format{'C'}; _subs($_[0],$f); } |
| 221 |
|
| 222 |
sub format_Od { roman(format_d(@_)) } |
| 223 |
sub format_Oe { roman(format_e(@_)) } |
| 224 |
sub format_OH { roman(format_H(@_)) } |
| 225 |
sub format_OI { roman(format_I(@_)) } |
| 226 |
sub format_Oj { roman(format_j(@_)) } |
| 227 |
sub format_Ok { roman(format_k(@_)) } |
| 228 |
sub format_Ol { roman(format_l(@_)) } |
| 229 |
sub format_Om { roman(format_m(@_)) } |
| 230 |
sub format_OM { roman(format_M(@_)) } |
| 231 |
sub format_Oq { roman(format_q(@_)) } |
| 232 |
sub format_Oy { roman(format_y(@_)) } |
| 233 |
sub format_OY { roman(format_Y(@_)) } |
| 234 |
|
| 235 |
sub format_G { int(($_[0]->[9] - 315993600) / 604800) } |
| 236 |
|
| 237 |
1; |
| 238 |
|
| 239 |
__END__ |
| 240 |
|
| 241 |
=pod |
| 242 |
|
| 243 |
=encoding UTF-8 |
| 244 |
|
| 245 |
=head1 NAME |
| 246 |
|
| 247 |
Date::Format::Generic - Date formatting subroutines |
| 248 |
|
| 249 |
=head1 VERSION |
| 250 |
|
| 251 |
version 2.35 |
| 252 |
|
| 253 |
=head1 AUTHOR |
| 254 |
|
| 255 |
Graham <[email protected]> |
| 256 |
|
| 257 |
=head1 COPYRIGHT AND LICENSE |
| 258 |
|
| 259 |
This software is copyright (c) 2020 by Graham Barr. |
| 260 |
|
| 261 |
This is free software; you can redistribute it and/or modify it under |
| 262 |
the same terms as the Perl 5 programming language system itself. |
| 263 |
|
| 264 |
=cut |