File Coverage

blib/lib/Mo/utils/TimeZone.pm
Criterion Covered Total %
statement 27 27 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 8 8 100.0
pod 1 1 100.0
total 45 45 100.0


line stmt bran cond sub pod time code
1             package Mo::utils::TimeZone;
2              
3 3     3   163780 use base qw(Exporter);
  3         7  
  3         545  
4 3     3   22 use strict;
  3         5  
  3         97  
5 3     3   25 use warnings;
  3         6  
  3         164  
6              
7 3     3   3076 use DateTime::TimeZone;
  3         547726  
  3         146  
8 3     3   1754 use Error::Pure qw(err);
  3         22947  
  3         76  
9 3     3   236 use Readonly;
  3         6  
  3         1339  
10              
11             Readonly::Array our @EXPORT_OK => qw(check_timezone_iana);
12              
13             our $VERSION = 0.03;
14              
15             sub check_timezone_iana {
16 5     5 1 484489 my ($self, $key) = @_;
17              
18 5 100       23 _check_key($self, $key) && return;
19              
20 3 100       23 if (! DateTime::TimeZone->is_valid_name($self->{$key})) {
21             err "Parameter '".$key."' doesn't contain valid IANA timezone code.",
22 1         422 'Value', $self->{$key},
23             ;
24             }
25              
26 2         563596 return;
27             }
28              
29             sub _check_key {
30 5     5   13 my ($self, $key) = @_;
31              
32 5 100 100     34 if (! exists $self->{$key} || ! defined $self->{$key}) {
33 2         7 return 1;
34             }
35              
36 3         10 return 0;
37             }
38              
39             1;
40              
41             __END__
42              
43             =pod
44              
45             =encoding utf8
46              
47             =head1 NAME
48              
49             Mo::utils::TimeZone - Mo timezone utilities.
50              
51             =head1 SYNOPSIS
52              
53             use Mo::utils::TimeZone qw(check_timezone_iana);
54              
55             check_timezone_iana($self, $key);
56              
57             =head1 DESCRIPTION
58              
59             Mo timezone utilities for checking of data objects.
60              
61             =head1 SUBROUTINES
62              
63             =head2 C<check_timezone_iana>
64              
65             check_timezone_iana($self, $key);
66              
67             Check parameter defined by C<$key> if it's valid IANA timezone code.
68             Value could be undefined.
69              
70             Returns undef.
71              
72             =head1 ERRORS
73              
74             check_timezone_iana():
75             Parameter '%s' doesn't contain valid IANA timezone code.
76             Value: %s
77              
78             =head1 EXAMPLE1
79              
80             =for comment filename=check_timezone_iana_ok.pl
81              
82             use strict;
83             use warnings;
84              
85             use Mo::utils::TimeZone qw(check_timezone_iana);
86              
87             my $self = {
88             'key' => 'Europe/Prague',
89             };
90             check_timezone_iana($self, 'key');
91              
92             # Print out.
93             print "ok\n";
94              
95             # Output:
96             # ok
97              
98             =head1 EXAMPLE2
99              
100             =for comment filename=check_timezone_iana_fail.pl
101              
102             use strict;
103             use warnings;
104              
105             use Error::Pure;
106             use Mo::utils::TimeZone qw(check_timezone_iana);
107              
108             $Error::Pure::TYPE = 'Error';
109              
110             my $self = {
111             'key' => 'BAD',
112             };
113             check_timezone_iana($self, 'key');
114              
115             # Print out.
116             print "ok\n";
117              
118             # Output like:
119             # #Error [...utils.pm:?] Parameter 'key' doesn't contain valid IANA timezone code.
120              
121             =head1 DEPENDENCIES
122              
123             L<DateTime::TimeZone>,
124             L<Error::Pure>,
125             L<Exporter>,
126             L<Readonly>.
127              
128             =head1 SEE ALSO
129              
130             =over
131              
132             =item L<Mo>
133              
134             Micro Objects. Mo is less.
135              
136             =item L<Mo::utils>
137              
138             Mo utilities.
139              
140             =item L<Wikibase::Datatype::Utils>
141              
142             Wikibase datatype utilities.
143              
144             =back
145              
146             =head1 REPOSITORY
147              
148             L<https://github.com/michal-josef-spacek/Mo-utils-TimeZone>
149              
150             =head1 AUTHOR
151              
152             Michal Josef Špaček L<mailto:skim@cpan.org>
153              
154             L<http://skim.cz>
155              
156             =head1 LICENSE AND COPYRIGHT
157              
158             © 2024 Michal Josef Špaček
159              
160             BSD 2-Clause License
161              
162             =head1 VERSION
163              
164             0.03
165              
166             =cut