TimeZone/Local/Unix.pm untested

filter: Statementstmt 0.0% 0 / 129 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 55 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 285 CC 20 SCAR 26.6
CC
20
Cov
0%
FromEtcLocaltime
42.0
_FindMatchingZoneinfoFile
20.0
FromEtcTIMEZONE
20.0
CRAP 14.2
1 package DateTime::TimeZone::Local::Unix;
2  
3 use strict;
4 use warnings;
5 use namespace::autoclean;
6  
7 our $VERSION = '2.72';
8  
9 use Cwd 3;
10 use Try::Tiny;
11  
12 use parent 'DateTime::TimeZone::Local';
13  
14 sub Methods {
15 return qw(
16 FromEnv
17 FromEtcTimezone
18 FromEtcLocaltime
19 FromEtcTIMEZONE
20 FromEtcSysconfigClock
21 FromEtcDefaultInit
22 );
23 }
24  
25 sub EnvVars { return 'TZ' }
26  
27 ## no critic (Variables::ProhibitPackageVars)
28 our $EtcDir = '/etc';
29 ## use critic
30  
31 sub _EtcFile {
32 shift;
33 return File::Spec->catfile( $EtcDir, @_ );
34 }
35  
36 sub FromEtcLocaltime {
37 my $class = shift;
38  
39 my $lt_file = $class->_EtcFile('localtime');
40 return unless -r $lt_file && -s _;
41  
42 my $real_name;
43 if ( -l $lt_file ) {
44  
45 # The _Readlink sub exists so the test suite can mock it.
46 $real_name = $class->_Readlink($lt_file);
47 }
48  
49 $real_name ||= $class->_FindMatchingZoneinfoFile($lt_file);
50  
51 if ( defined $real_name ) {
52 my ( undef, $dirs, $file ) = File::Spec->splitpath($real_name);
53  
54 my @parts = grep { defined && length } File::Spec->splitdir($dirs),
55 $file;
56  
57 foreach my $x ( reverse 0 .. $#parts ) {
58 my $name = (
59 $x < $#parts
60 ? join '/', @parts[ $x .. $#parts ]
61 : $parts[$x]
62 );
63  
64 my $tz = try {
65 ## no critic (Variables::RequireInitializationForLocalVars)
66 local $SIG{__DIE__};
67 DateTime::TimeZone->new( name => $name );
68 };
69  
70 return $tz if $tz;
71 }
72 }
73 }
74  
75 sub _Readlink {
76 my $link = $_[1];
77  
78 # Using abs_path will resolve multiple levels of link indirection,
79 # whereas readlink just follows the link to the next target.
80 return Cwd::abs_path($link);
81 }
82  
83 ## no critic (Variables::ProhibitPackageVars)
84 our $ZoneinfoDir = '/usr/share/zoneinfo';
85 ## use critic
86  
87 # for systems where /etc/localtime is a copy of a zoneinfo file
88 sub _FindMatchingZoneinfoFile {
89 shift;
90 my $file_to_match = shift;
91  
92 # For some reason, under at least macOS 10.13 High Sierra,
93 # /usr/share/zoneinfo is a link to a link to a directory. And no, I didn't
94 # stutter. This is fine, and it passes the -d below. But File::Find does
95 # not understand a link to be a directory, so rather than incur the
96 # overhead of telling File::Find::find() to follow symbolic links, we just
97 # resolve it here.
98 my $zone_info_dir = $ZoneinfoDir;
99 $zone_info_dir = readlink $zone_info_dir while -l $zone_info_dir;
100  
101 return unless -d $zone_info_dir;
102  
103 require File::Basename;
104 require File::Compare;
105 require File::Find;
106  
107 my $size = -s $file_to_match;
108  
109 my $real_name;
110 try {
111 ## no critic (Variables::RequireInitializationForLocalVars)
112 local $SIG{__DIE__};
113 local $_;
114  
115 File::Find::find(
116 {
117 wanted => sub {
118 if (
119 !defined $real_name
120 && -f $_
121 && !-l $_
122 && $size == -s _
123  
124 # This fixes RT 24026 - apparently such a
125 # file exists on FreeBSD and it can cause a
126 # false positive
127 && File::Basename::basename($_) ne 'posixrules'
128 && File::Compare::compare( $_, $file_to_match ) == 0
129 ) {
130 $real_name = $_;
131  
132 # File::Find has no mechanism for bailing in the
133 # middle of a find.
134 die { found => 1 };
135 }
136 },
137 no_chdir => 1,
138 },
139 $zone_info_dir,
140 );
141 }
142 catch {
143 die $_ unless ref $_ && $_->{found};
144 };
145  
146 return $real_name;
147 }
148  
149 sub FromEtcTimezone {
150 my $class = shift;
151  
152 my $tz_file = $class->_EtcFile('timezone');
153 return unless -f $tz_file && -r _;
154  
155 open my $fh, '<', $tz_file
156 or die "Cannot read $tz_file: $!";
157 my $name = do { local $/ = undef; <$fh> };
158 close $fh or die $!;
159  
160 $name =~ s/^\s+|\s+$//g;
161  
162 return unless $class->_IsValidName($name);
163  
164 return try {
165 ## no critic (Variables::RequireInitializationForLocalVars)
166 local $SIG{__DIE__};
167 DateTime::TimeZone->new( name => $name );
168 };
169 }
170  
171 sub FromEtcTIMEZONE {
172 my $class = shift;
173  
174 my $tz_file = $class->_EtcFile('TIMEZONE');
175 return unless -f $tz_file && -r _;
176  
177 ## no critic (InputOutput::RequireBriefOpen)
178 open my $fh, '<', $tz_file
179 or die "Cannot read $tz_file: $!";
180  
181 my $name;
182 while ( defined( $name = <$fh> ) ) {
183 if ( $name =~ /\A\s*TZ\s*=\s*(\S+)/ ) {
184 $name = $1;
185 last;
186 }
187 }
188  
189 close $fh or die $!;
190  
191 return unless $class->_IsValidName($name);
192  
193 return try {
194 ## no critic (Variables::RequireInitializationForLocalVars)
195 local $SIG{__DIE__};
196 DateTime::TimeZone->new( name => $name );
197 };
198 }
199  
200 # RedHat uses this
201 sub FromEtcSysconfigClock {
202 my $class = shift;
203  
204 my $clock_file = $class->_EtcFile('sysconfig/clock');
205 return unless -r $clock_file && -f _;
206  
207 my $name = $class->_ReadEtcSysconfigClock($clock_file);
208  
209 return unless $class->_IsValidName($name);
210  
211 return try {
212 ## no critic (Variables::RequireInitializationForLocalVars)
213 local $SIG{__DIE__};
214 DateTime::TimeZone->new( name => $name );
215 };
216 }
217  
218 # this is a separate function so that it can be overridden in the test suite
219 sub _ReadEtcSysconfigClock {
220 shift;
221 my $clock_file = shift;
222  
223 open my $fh, '<', $clock_file
224 or die "Cannot read $clock_file: $!";
225  
226 ## no critic (Variables::RequireInitializationForLocalVars)
227 local $_;
228 while (<$fh>) {
229 return $1 if /^(?:TIME)?ZONE="([^"]+)"/;
230 }
231  
232 close $fh or die $!;
233 }
234  
235 sub FromEtcDefaultInit {
236 my $class = shift;
237  
238 my $init_file = $class->_EtcFile('default/init');
239 return unless -r $init_file && -f _;
240  
241 my $name = $class->_ReadEtcDefaultInit($init_file);
242  
243 return unless $class->_IsValidName($name);
244  
245 return try {
246 ## no critic (Variables::RequireInitializationForLocalVars)
247 local $SIG{__DIE__};
248 DateTime::TimeZone->new( name => $name );
249 };
250 }
251  
252 # this is a separate function so that it can be overridden in the test
253 # suite
254 sub _ReadEtcDefaultInit {
255 shift;
256 my $init_file = shift;
257  
258 open my $fh, '<', $init_file
259 or die "Cannot read $init_file: $!";
260  
261 ## no critic (Variables::RequireInitializationForLocalVars)
262 local $_;
263 while (<$fh>) {
264 return $1 if /^TZ=(.+)/;
265 }
266  
267 close $fh or die $!;
268 }
269  
270 1;
271  
272 # ABSTRACT: Determine the local system's time zone on Unix
273  
274 __END__