| 1 | package perl5i::2::DateTime; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # A file to contain the Datetime work for perl5i to get it out of perl5i.pm | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 109 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 109 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 109 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # Determine if we need Time::y2038 and only load if necessary. | ||||||||||||||||||||||||||||
| 10 | # XXX This is a bit of a hack and should go into a config file. | ||||||||||||||||||||||||||||
| 11 | 109 | ▶ | use constant NEEDS_y2038 => ( | ||||||||||||||||||||||||||
|
Condition42%3/7
((gmtime 140737488355327)[5] || 0) + 1900 != 4461763 || ((gmtime -62135510400)[5] || 0) + 1900 != 1
(gmtime 140737488355327)[5] || 0
(gmtime -62135510400)[5] || 0
MC/DC0%0/2
((gmtime 140737488355327)[5] || 0) + 1900 != 4461763 || ((gmtime -62135510400)[5] || 0) + 1900 != 1
(gmtime 140737488355327)[5]
(gmtime -62135510400)[5]
Truth table0%0/3
A || B
A= (gmtime 140737488355327)[5]B= (gmtime -62135510400)[5]
| |||||||||||||||||||||||||||||
| 12 | ((((CORE::gmtime(2**47-1))[5] || 0) + 1900) != 4461763) || | ||||||||||||||||||||||||||||
| 13 | ((((CORE::gmtime(-62135510400))[5] || 0) + 1900) != 1) | ||||||||||||||||||||||||||||
| 14 | 109 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | BEGIN { | ||||||||||||||||||||||||||||
| 17 | 109 | ▶ | if( NEEDS_y2038 ) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | require Time::y2038; | ||||||||||||||||||||||||||||
| 19 | Time::y2038->import; | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ## no critic (Subroutines::ProhibitSubroutinePrototypes) | ||||||||||||||||||||||||||||
| 24 | sub dt_gmtime (;$) { | ||||||||||||||||||||||||||||
| 25 | 5 | ▶ | my $time = @_ ? shift : time; | ||||||||||||||||||||||||||
|
Subroutine
dt_gmtime: called CC=3 SCAR=11.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 5 | ▶ | return gmtime($time) if wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | my($sec, $min, $hour, $mday, $mon, $year) = gmtime($time); | |||||||||||||||||||||||||||
| 29 | 4 | $mon++; | |||||||||||||||||||||||||||
| 30 | 4 | $year += 1900; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 4 | require DateTime; | |||||||||||||||||||||||||||
| 33 | 4 | return perl5i::2::DateTime::y2038->new( | |||||||||||||||||||||||||||
| 34 | year => $year, | ||||||||||||||||||||||||||||
| 35 | month => $mon, | ||||||||||||||||||||||||||||
| 36 | day => $mday, | ||||||||||||||||||||||||||||
| 37 | hour => $hour, | ||||||||||||||||||||||||||||
| 38 | minute => $min, | ||||||||||||||||||||||||||||
| 39 | second => $sec, | ||||||||||||||||||||||||||||
| 40 | formatter => "perl5i::2::DateTime::Format::CTime" | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub _get_datetime_timezone { | ||||||||||||||||||||||||||||
| 46 | 2 | ▶ | state $local_tzfile = "/etc/localtime"; | ||||||||||||||||||||||||||
|
Subroutine
_get_datetime_timezone: called CC=6 SCAR=29.9
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Always be sure to honor the TZ environment var | ||||||||||||||||||||||||||||
| 49 | 2 | ▶ | return "local" if $ENV{TZ}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Work around a bug in DateTime::TimeZone on FreeBSD where it | ||||||||||||||||||||||||||||
| 52 | # can't determine the time zone if /etc/localtime is not a link. | ||||||||||||||||||||||||||||
| 53 | # Tzfile is also faster to do localtime calculations. | ||||||||||||||||||||||||||||
| 54 | 2 | ▶ | if( -e $local_tzfile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | # Could go through more effort to figure it out. Meh. | ||||||||||||||||||||||||||||
| 56 | 0 | my $tzname = "Local"; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | if( -l $local_tzfile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if( my $real_tzfile = eval { readlink $local_tzfile } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $tzname = $real_tzfile; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 0 | require DateTime::TimeZone::Tzfile; | |||||||||||||||||||||||||||
| 63 | 0 | my $tz = DateTime::TimeZone::Tzfile->new( | |||||||||||||||||||||||||||
| 64 | name => $tzname, | ||||||||||||||||||||||||||||
| 65 | filename => $local_tzfile | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | return $tz if $tz; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | return "local"; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | ## no critic (Subroutines::ProhibitSubroutinePrototypes) | ||||||||||||||||||||||||||||
| 74 | sub dt_localtime (;$) { | ||||||||||||||||||||||||||||
| 75 | 2 | ▶ | my $time = @_ ? shift : time; | ||||||||||||||||||||||||||
|
Subroutine
dt_localtime: called CC=3 SCAR=11.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 2 | ▶ | return localtime($time) if wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | my($sec, $min, $hour, $mday, $mon, $year) = localtime($time); | |||||||||||||||||||||||||||
| 79 | 2 | $mon++; | |||||||||||||||||||||||||||
| 80 | 2 | $year += 1900; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | state $tz = _get_datetime_timezone(); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | require DateTime; | |||||||||||||||||||||||||||
| 85 | 2 | return perl5i::2::DateTime::y2038->new( | |||||||||||||||||||||||||||
| 86 | year => $year, | ||||||||||||||||||||||||||||
| 87 | month => $mon, | ||||||||||||||||||||||||||||
| 88 | day => $mday, | ||||||||||||||||||||||||||||
| 89 | hour => $hour, | ||||||||||||||||||||||||||||
| 90 | minute => $min, | ||||||||||||||||||||||||||||
| 91 | second => $sec, | ||||||||||||||||||||||||||||
| 92 | time_zone => $tz, | ||||||||||||||||||||||||||||
| 93 | formatter => "perl5i::2::DateTime::Format::CTime" | ||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | ## no critic (Subroutines::ProhibitSubroutinePrototypes) | ||||||||||||||||||||||||||||
| 99 | sub dt_time () { | ||||||||||||||||||||||||||||
| 100 | 4 | ▶ | require DateTime::Format::Epoch; | ||||||||||||||||||||||||||
|
Subroutine
dt_time: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 101 | 4 | state $formatter = DateTime::Format::Epoch->new( epoch => DateTime->from_epoch( epoch => 0 ) ); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 4 | require DateTime; | |||||||||||||||||||||||||||
| 104 | 4 | return perl5i::2::DateTime::time->from_epoch( | |||||||||||||||||||||||||||
| 105 | epoch => time, | ||||||||||||||||||||||||||||
| 106 | formatter => $formatter | ||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | package perl5i::2::DateTime::y2038; | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Don't load DateTime until we need it. | ||||||||||||||||||||||||||||
| 115 | 109 | our @ISA = qw(DateTime); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | use overload | ||||||||||||||||||||||||||||
| 118 | "eq" => sub { | ||||||||||||||||||||||||||||
| 119 | 2 | ▶ | my($dt1, $dt2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 120 | 2 | ▶ | return "$dt1" eq "$dt2" if !eval { $dt2->isa("DateTime") }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | return $dt1 eq $dt2; | |||||||||||||||||||||||||||
| 122 | 109 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | sub say { | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | CORE::say("$_[0]"); | ||||||||||||||||||||||||||
|
Subroutine
say: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub print { | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | CORE::print("$_[0]"); | ||||||||||||||||||||||||||
|
Subroutine
print: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub from_epoch { | ||||||||||||||||||||||||||||
| 133 | 4 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
from_epoch: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 4 | if( perl5i::2::DateTime::NEEDS_y2038 ) { | |||||||||||||||||||||||||||
| 136 | 109 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 137 | local *CORE::GLOBAL::gmtime = \&Time::y2038::gmtime; | ||||||||||||||||||||||||||||
| 138 | local *CORE::GLOBAL::localtime = \&Time::y2038::localtime; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | return $class->SUPER::from_epoch(@_); | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | else { | ||||||||||||||||||||||||||||
| 143 | 4 | return $class->SUPER::from_epoch(@_); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # Copy of DateTime's own epoch() function. | ||||||||||||||||||||||||||||
| 149 | 109 | if( perl5i::2::DateTime::NEEDS_y2038 ) { | |||||||||||||||||||||||||||
| 150 | *epoch = sub { | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | my $zone = $self->time_zone; | |||||||||||||||||||||||||||
| 154 | 0 | $self->set_time_zone("UTC"); | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | require Time::y2038; | |||||||||||||||||||||||||||
| 157 | 0 | my $time = Time::y2038::timegm( | |||||||||||||||||||||||||||
| 158 | $self->sec, $self->min, $self->hour, $self->mday, | ||||||||||||||||||||||||||||
| 159 | $self->mon - 1, | ||||||||||||||||||||||||||||
| 160 | $self->year - 1900, | ||||||||||||||||||||||||||||
| 161 | ); | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | $self->set_time_zone($zone); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | return $time; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 109 | package perl5i::2::DateTime::time; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 109 | ▶ | use parent -norequire, qw(perl5i::2::DateTime::y2038); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | use overload | ||||||||||||||||||||||||||||
| 177 | 0 | ▶ | "0+" => sub { $_[0]->epoch }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 178 | "-" => sub { | ||||||||||||||||||||||||||||
| 179 | 2 | ▶ | my( $a, $b, $reverse ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.8
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 2 | ▶ | if($reverse) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | ( $b, $a ) = ( $a, $b ); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | ▶ | my $time_a = eval { $a->isa("DateTime") } ? $a->epoch : $a; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 2 | ▶ | my $time_b = eval { $b->isa("DateTime") } ? $b->epoch : $b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 2 | return $time_a - $time_b; | |||||||||||||||||||||||||||
| 189 | }, | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | "+" => sub { | ||||||||||||||||||||||||||||
| 192 | 0 | ▶ | my( $a, $b, $reverse ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | if($reverse) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | ( $b, $a ) = ( $a, $b ); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | ▶ | my $time_a = eval { $a->isa("DateTime") } ? $a->epoch : $a; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | my $time_b = eval { $b->isa("DateTime") } ? $b->epoch : $b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | return $time_a + $time_b; | |||||||||||||||||||||||||||
| 202 | }, | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | "==" => sub { | ||||||||||||||||||||||||||||
| 205 | 0 | ▶ | my($a, $b) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | return $a+0 == $b+0 if !eval { $b->isa("DateTime") }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 0 | return $a == $b; | |||||||||||||||||||||||||||
| 208 | }, | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 109 | ▶ | fallback => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | { | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 109 | package perl5i::2::DateTime::Format::CTime; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 109 | ▶ | use CLASS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | sub new { | ||||||||||||||||||||||||||||
| 221 | 0 | ▶ | return bless {}, $CLASS; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | sub format_datetime { | ||||||||||||||||||||||||||||
| 225 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
format_datetime: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 226 | 3 | my $dt = shift; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # Straight from the Open Group asctime() docs. | ||||||||||||||||||||||||||||
| 229 | 3 | return sprintf "%.3s %.3s%3d %.2d:%.2d:%.2d %d", | |||||||||||||||||||||||||||
| 230 | $dt->day_abbr, | ||||||||||||||||||||||||||||
| 231 | $dt->month_abbr, | ||||||||||||||||||||||||||||
| 232 | $dt->mday, | ||||||||||||||||||||||||||||
| 233 | $dt->hour, | ||||||||||||||||||||||||||||
| 234 | $dt->min, | ||||||||||||||||||||||||||||
| 235 | $dt->sec, | ||||||||||||||||||||||||||||
| 236 | $dt->year, | ||||||||||||||||||||||||||||
| 237 | ; | ||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 109 | 1; | |||||||||||||||||||||||||||