File Coverage

blib/lib/Data/OFN/Common/TimeMoment.pm
Criterion Covered Total %
statement 39 39 100.0
branch 20 20 100.0
condition 24 24 100.0
subroutine 6 6 100.0
pod 0 1 0.0
total 89 90 98.8


line stmt bran cond sub pod time code
1             package Data::OFN::Common::TimeMoment;
2              
3 6     6   170328 use strict;
  6         14  
  6         266  
4 6     6   36 use warnings;
  6         19  
  6         423  
5              
6 6     6   3375 use Error::Pure qw(err);
  6         107511  
  6         167  
7 6     6   3791 use Mo qw(build is);
  6         4554  
  6         47  
8 6     6   19626 use Mo::utils 0.08 qw(check_bool check_isa);
  6         20328  
  6         164  
9              
10             our $VERSION = 0.03;
11              
12             has date => (
13             is => 'ro',
14             );
15              
16             has date_and_time => (
17             is => 'ro',
18             );
19              
20             has flag_unspecified => (
21             is => 'ro',
22             );
23              
24             sub BUILD {
25 19     19 0 4361262 my $self = shift;
26              
27             # Check date.
28 19         97 check_isa($self, 'date', 'DateTime');
29 19 100       500 if (defined $self->{'date'}) {
30 9 100       37 if ($self->date->hour != '0') {
31 1         18 err "Parameter 'date' must have a hour value of zero.";
32             }
33 8 100       158 if ($self->date->minute != '0') {
34 1         15 err "Parameter 'date' must have a minute value of zero.";
35             }
36 7 100       92 if ($self->date->second != '0') {
37 1         14 err "Parameter 'date' must have a second value of zero.";
38             }
39             }
40              
41             # Check date_and_time.
42 16         112 check_isa($self, 'date_and_time', 'DateTime');
43 16 100 100     301 if (defined $self->date_and_time
      100        
      100        
44             && $self->date_and_time->hour == 0
45             && $self->date_and_time->minute == 0
46             && $self->date_and_time->second == 0) {
47              
48 1         48 err "Parameter 'date_and_time' should be a 'date' parameter.";
49             }
50              
51 15 100 100     274 if (defined $self->date && defined $self->date_and_time) {
52 1         19 err "Parameters 'date' and 'date_and_time' could not be defined together.";
53             }
54              
55             # Check flag_unspecified.
56 14 100       182 if (! defined $self->{'flag_unspecified'}) {
57 8         21 $self->{'flag_unspecified'} = 0;
58             }
59 14         52 check_bool($self, 'flag_unspecified');
60              
61 14 100 100     287 if (defined $self->date && $self->{'flag_unspecified'}) {
62 1         26 err "Parmaeter 'date' and 'flag_unspecified' could not be defined together.";
63             }
64 13 100 100     111 if (defined $self->date_and_time && $self->{'flag_unspecified'}) {
65 1         20 err "Parmaeter 'date_and_time' and 'flag_unspecified' could not be defined together.";
66             }
67 12 100 100     125 if (! $self->flag_unspecified
      100        
68             && ! defined $self->date
69             && ! defined $self->date_and_time) {
70              
71 2         47 err "Parameter 'flag_unspecified' disabled needs to be with 'date' or 'date_and_time' parameters.";
72             }
73              
74 10         187 return;
75             }
76              
77             1;
78              
79             __END__
80              
81             =pod
82              
83             =encoding utf8
84              
85             =head1 NAME
86              
87             Data::OFN::Common::TimeMoment - OFN common data object for time moment.
88              
89             =head1 SYNOPSIS
90              
91             use Data::OFN::Common::TimeMoment;
92              
93             my $obj = Data::OFN::Common::TimeMoment->new(%params);
94             my $date = $obj->date;
95             my $date_and_time = $obj->date_and_time;
96             my $flag_unspecified = $obj->flag_unspecified;
97              
98             =head1 DESCRIPTION
99              
100             Immutable data object for OFN (Otevřené formální normy) representation of
101             time moment in the Czech Republic.
102              
103             This object is actual with L<2020-07-01|https://ofn.gov.cz/z%C3%A1kladn%C3%AD-datov%C3%A9-typy/2020-07-01/#%C4%8Dasov%C3%BD-okam%C5%BEik>
104             version of OFN basic data types standard.
105              
106             =head1 METHODS
107              
108             =head2 C<new>
109              
110             my $obj = Data::OFN::Common::TimeMoment->new(%params);
111              
112             Constructor.
113              
114             =over 8
115              
116             =item * C<date>
117              
118             Date object defined by DataTime.
119              
120             It's optional.
121              
122             Default value is undef.
123              
124             =item * C<date_and_time>
125              
126             Date and time object defined by DataTime.
127              
128             It's optional.
129              
130             Default value is undef.
131              
132             =item * C<flag_unspecified>
133              
134             Flag for definition that date isn't defined.
135              
136             It's required.
137              
138             Default value is 0.
139              
140             =back
141              
142             Returns instance of object.
143              
144             =head2 C<date>
145              
146             my $date = $obj->date;
147              
148             Get date.
149              
150             Returns L<DateTime> instance.
151              
152             =head2 C<date_and_time>
153              
154             my $date_and_time = $obj->date_and_time;
155              
156             Get date and time
157              
158             Returns L<DateTime> instance.
159              
160             =head2 C<flag_unspecified>
161              
162             my $flag_unspecified = $obj->flag_unspecified;
163              
164             Get flag for unspecified date.
165              
166             Returns bool value (0/1).
167              
168             =head1 ERRORS
169              
170             new():
171             From Mo::utils::check_bool():
172             Parameter 'flag_unspecified' must be a bool (0/1).
173             Value: %s
174             From Mo::utils::check_isa():
175             Parameter 'date' must be a 'DataTime' object.
176             Value: %s
177             Reference: %s
178             Parameter 'date_and_time' must be a 'DataTime' object.
179             Value: %s
180             Reference: %s
181             Parmaeter 'date' and 'flag_unspecified' could not be defined together.
182             Parameter 'date' must have a hour value of zero.
183             Parameter 'date' must have a minute value of zero.
184             Parameter 'date' must have a second value of zero.
185             Parmaeter 'date_and_time' and 'flag_unspecified' could not be defined together.
186             Parameter 'date_and_time' should be a 'date' parameter.
187             Parameter 'flag_unspecified' disabled needs to be with 'date' or 'date_and_time' parameters.
188             Parameters 'date' and 'date_and_time' could not be defined together.
189              
190             =head1 EXAMPLE1
191              
192             =for comment filename=time_moment_date.pl
193              
194             use strict;
195             use warnings;
196              
197             use Data::OFN::Common::TimeMoment;
198             use DateTime;
199              
200             my $obj = Data::OFN::Common::TimeMoment->new(
201             'date' => DateTime->new(
202             'day' => 8,
203             'month' => 7,
204             'year' => 2025,
205             ),
206             );
207              
208             print 'Date: '.$obj->date."\n";
209              
210             # Output:
211             # Date: 2025-07-08T00:00:00
212              
213             =head1 EXAMPLE2
214              
215             =for comment filename=time_moment_date_and_time.pl
216              
217             use strict;
218             use warnings;
219              
220             use Data::OFN::Common::TimeMoment;
221             use DateTime;
222              
223             my $obj = Data::OFN::Common::TimeMoment->new(
224             'date_and_time' => DateTime->new(
225             'day' => 8,
226             'month' => 7,
227             'year' => 2025,
228             'hour' => 12,
229             'minute' => 10,
230             ),
231             );
232              
233             print 'Date and time: '.$obj->date_and_time."\n";
234              
235             # Output:
236             # Date and time: 2025-07-08T12:10:00
237              
238             =head1 DEPENDENCIES
239              
240             L<Error::Pure>
241             L<Mo>,
242             L<Mo::utils>.
243              
244             =head1 REPOSITORY
245              
246             L<https://github.com/michal-josef-spacek/Data-OFN-Common>
247              
248             =head1 AUTHOR
249              
250             Michal Josef Špaček L<mailto:skim@cpan.org>
251              
252             L<http://skim.cz>
253              
254             =head1 LICENSE AND COPYRIGHT
255              
256             © 2023-2025 Michal Josef Špaček
257              
258             BSD 2-Clause License
259              
260             =head1 VERSION
261              
262             0.03
263              
264             =cut