File: Unix.pm

package info (click to toggle)
libdatetime-timezone-perl 1%3A1.75-2%2B2018e
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-updates
  • size: 191,260 kB
  • sloc: perl: 2,767; sh: 42; makefile: 14
file content (323 lines) | stat: -rw-r--r-- 7,637 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package DateTime::TimeZone::Local::Unix;
$DateTime::TimeZone::Local::Unix::VERSION = '1.75';
use strict;
use warnings;

use Cwd 3;
use parent 'DateTime::TimeZone::Local';

sub Methods {
    return qw(
        FromEnv
        FromEtcLocaltime
        FromEtcTimezone
        FromEtcTIMEZONE
        FromEtcSysconfigClock
        FromEtcDefaultInit
    );
}

sub EnvVars { return 'TZ' }

our $EtcDir = '/etc';

sub _EtcFile {
    shift;
    return File::Spec->catfile( $EtcDir, @_ );
}

sub FromEtcLocaltime {
    my $class = shift;

    my $lt_file = $class->_EtcFile('localtime');
    return unless -r $lt_file && -s _;

    my $real_name;
    if ( -l $lt_file ) {

        # The _Readlink sub exists so the test suite can mock it.
        $real_name = $class->_Readlink($lt_file);
    }

    $real_name ||= $class->_FindMatchingZoneinfoFile($lt_file);

    if ( defined $real_name ) {
        my ( $vol, $dirs, $file ) = File::Spec->splitpath($real_name);

        my @parts
            = grep { defined && length } File::Spec->splitdir($dirs), $file;

        foreach my $x ( reverse 0 .. $#parts ) {
            my $name = (
                $x < $#parts
                ? join '/', @parts[ $x .. $#parts ]
                : $parts[$x]
            );

            my $tz;
            {
                local $@;
                local $SIG{__DIE__};
                $tz = eval { DateTime::TimeZone->new( name => $name ) };
            }

            return $tz if $tz;
        }
    }
}

sub _Readlink {
    my $link = $_[1];

    # Using abs_path will resolve multiple levels of link indirection,
    # whereas readlink just follows the link to the next target.
    return Cwd::abs_path($link);
}

our $ZoneinfoDir = '/usr/share/zoneinfo';
# for systems where /etc/localtime is a copy of a zoneinfo file
sub _FindMatchingZoneinfoFile {
    my $class         = shift;
    my $file_to_match = shift;

    return unless -d $ZoneinfoDir;

    require File::Basename;
    require File::Compare;
    require File::Find;

    my $size = -s $file_to_match;

    my $real_name;
    local $@;
    local $SIG{__DIE__};
    local $_;
    eval {
        File::Find::find(
            {
                wanted => sub {
                    if (
                           !defined $real_name
                        && -f $_
                        && !-l $_
                        && $size == -s _

                        # This fixes RT 24026 - apparently such a
                        # file exists on FreeBSD and it can cause a
                        # false positive
                        && File::Basename::basename($_) ne 'posixrules'
                        && File::Compare::compare( $_, $file_to_match ) == 0
                        ) {
                        $real_name = $_;

                        # File::Find has no mechanism for bailing in the
                        # middle of a find.
                        die { found => 1 };
                    }
                },
                no_chdir => 1,
            },
            $ZoneinfoDir,
        );
    };

    if ($@) {
        return $real_name if ref $@ && $@->{found};
        die $@;
    }
}

sub FromEtcTimezone {
    my $class = shift;

    my $tz_file = $class->_EtcFile('timezone');
    return unless -f $tz_file && -r _;

    open my $fh, '<', $tz_file
        or die "Cannot read $tz_file: $!";
    my $name = join '', <$fh>;
    close $fh;

    $name =~ s/^\s+|\s+$//g;

    return unless $class->_IsValidName($name);

    local $@;
    local $SIG{__DIE__};
    return eval { DateTime::TimeZone->new( name => $name ) };
}

sub FromEtcTIMEZONE {
    my $class = shift;

    my $tz_file = $class->_EtcFile('TIMEZONE');
    return unless -f $tz_file && -r _;

    open my $fh, '<', $tz_file
        or die "Cannot read $tz_file: $!";

    my $name;
    while ( defined( $name = <$fh> ) ) {
        if ( $name =~ /\A\s*TZ\s*=\s*(\S+)/ ) {
            $name = $1;
            last;
        }
    }

    close $fh;

    return unless $class->_IsValidName($name);

    local $@;
    local $SIG{__DIE__};
    return eval { DateTime::TimeZone->new( name => $name ) };
}

# RedHat uses this
sub FromEtcSysconfigClock {
    my $class = shift;

    my $clock_file = $class->_EtcFile('sysconfig/clock');
    return unless -r $clock_file && -f _;

    my $name = $class->_ReadEtcSysconfigClock($clock_file);

    return unless $class->_IsValidName($name);

    local $@;
    local $SIG{__DIE__};
    return eval { DateTime::TimeZone->new( name => $name ) };
}

# this is a separate function so that it can be overridden in the test suite
sub _ReadEtcSysconfigClock {
    my $class      = shift;
    my $clock_file = shift;

    open my $fh, '<', $clock_file
        or die "Cannot read $clock_file: $!";

    local $_;
    while (<$fh>) {
        return $1 if /^(?:TIME)?ZONE="([^"]+)"/;
    }
}

sub FromEtcDefaultInit {
    my $class = shift;

    my $init_file = $class->_EtcFile('default/init');
    return unless -r $init_file && -f _;

    my $name = $class->_ReadEtcDefaultInit($init_file);

    return unless $class->_IsValidName($name);

    local $@;
    local $SIG{__DIE__};
    return eval { DateTime::TimeZone->new( name => $name ) };
}

# this is a separate function so that it can be overridden in the test
# suite
sub _ReadEtcDefaultInit {
    my $class     = shift;
    my $init_file = shift;

    open my $fh, '<', $init_file
        or die "Cannot read $init_file: $!";

    local $_;
    while (<$fh>) {
        return $1 if /^TZ=(.+)/;
    }
}

1;

# ABSTRACT: Determine the local system's time zone on Unix

__END__

=pod

=encoding UTF-8

=head1 NAME

DateTime::TimeZone::Local::Unix - Determine the local system's time zone on Unix

=head1 VERSION

version 1.75

=head1 SYNOPSIS

  my $tz = DateTime::TimeZone->new( name => 'local' );

  my $tz = DateTime::TimeZone::Local->TimeZone();

=head1 DESCRIPTION

This module provides methods for determining the local time zone on a
Unix platform.

=head1 HOW THE TIME ZONE IS DETERMINED

This class tries the following methods of determining the local time
zone:

=over 4

=item * $ENV{TZ}

It checks C<< $ENV{TZ} >> for a valid time zone name.

=item * F</etc/localtime>

If this file is a symlink to an Olson database time zone file (usually
in F</usr/share/zoneinfo>) then it uses the target file's path name to
determine the time zone name. For example, if the path is
F</usr/share/zoneinfo/America/Chicago>, the time zone is
"America/Chicago".

Some systems just copy the relevant file to F</etc/localtime> instead
of making a symlink.  In this case, we look in F</usr/share/zoneinfo>
for a file that has the same size and content as F</etc/localtime> to
determine the local time zone.

=item * F</etc/timezone>

If this file exists, it is read and its contents are used as a time
zone name.

=item * F</etc/TIMEZONE>

If this file exists, it is opened and we look for a line starting like
"TZ = ...". If this is found, it should indicate a time zone name.

=item * F</etc/sysconfig/clock>

If this file exists, it is opened and we look for a line starting like
"TIMEZONE = ..." or "ZONE = ...". If this is found, it should indicate
a time zone name.

=item * F</etc/default/init>

If this file exists, it is opened and we look for a line starting like
"TZ=...". If this is found, it should indicate a time zone name.

=back

=head1 AUTHOR

Dave Rolsky <autarch@urth.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Dave Rolsky.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut