File: Rule.pm

package info (click to toggle)
libdatetime-timezone-perl 1%3A2.60-1%2B2024a
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,188 kB
  • sloc: perl: 2,732; sh: 59; makefile: 10
file content (87 lines) | stat: -rw-r--r-- 2,104 bytes parent folder | download
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
package DateTime::TimeZone::OlsonDB::Rule;

use strict;
use warnings;
use namespace::autoclean;

our $VERSION = '2.60';

use DateTime::TimeZone::OlsonDB;

sub new {
    my $class = shift;
    my %p     = @_;

    $p{letter} ||= q{};

    my $save = $p{save};

    # The handling of q{-} and q{1} are to account for new syntax introduced
    # in 2009u (and hopefully gone in future versions).
    if ( $save && $save ne q{-} ) {
        if ( $save =~ /^\d+$/ ) {
            $p{offset_from_std} = 3600 * $save;
        }
        else {
            $p{offset_from_std}
                = DateTime::TimeZone::offset_as_seconds($save);
        }
    }
    else {
        $p{offset_from_std} = 0;
    }

    return bless \%p, $class;
}

sub name            { $_[0]->{name} }
sub offset_from_std { $_[0]->{offset_from_std} }
sub letter          { $_[0]->{letter} }
sub min_year        { $_[0]->{from} }

sub offset_from_std_as_hm {
    my $offset = $_[0]->offset_from_std;
    my $h      = int( $offset / 3600 );
    my $m      = ( $offset % 3600 ) / 60;
    return sprintf( '%02d:%02d', $h, $m );
}

sub max_year {
          $_[0]->{to} eq 'only' ? $_[0]->min_year
        : $_[0]->{to} eq 'max'  ? undef
        :                         $_[0]->{to};
}

sub is_infinite { $_[0]->{to} eq 'max' ? 1 : 0 }

sub month { $DateTime::TimeZone::OlsonDB::MONTHS{ $_[0]->{in} } }
sub on    { $_[0]->{on} }
sub at    { $_[0]->{at} }

sub utc_start_datetime_for_year {
    my $self            = shift;
    my $year            = shift;
    my $offset_from_utc = shift;

    # should be the offset of the _previous_ rule
    my $offset_from_std = shift;

    my ( $month, $day ) = DateTime::TimeZone::OlsonDB::parse_day_spec(
        $self->on,
        $self->month,
        $year,
    );

    my $utc = DateTime::TimeZone::OlsonDB::utc_datetime_for_time_spec(
        spec            => $self->at,
        year            => $year,
        month           => $month,
        day             => $day,
        offset_from_utc => $offset_from_utc,
        offset_from_std => $offset_from_std,
    );

    return $utc;
}

1;