File Coverage

blib/lib/BACnet/DataTypes/Date.pm
Criterion Covered Total %
statement 51 53 96.2
branch 5 6 83.3
condition n/a
subroutine 13 13 100.0
pod 0 7 0.0
total 69 79 87.3


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package BACnet::DataTypes::Date;
4              
5 27     27   159 use warnings;
  27         47  
  27         1538  
6 27     27   130 use strict;
  27         44  
  27         735  
7              
8 27     27   169 use BACnet::DataTypes::Utils;
  27         44  
  27         855  
9              
10 27     27   106 use parent 'BACnet::DataTypes::DataType';
  27         64  
  27         212  
11              
12 27     27   2063 use constant { YEAR_OFFSET => 1900 };
  27         54  
  27         1732  
13 27     27   139 use constant { LENGTH => 4 };
  27         39  
  27         17663  
14              
15             # this did not check if the date make sense or not, because someone may want to use some weird calendar
16              
17             sub construct {
18 10     10 0 309306 my ( $class, $year, $month, $day, $day_of_the_week, $modified_tag ) = @_;
19              
20             # $month (1-12), $day, $day_of_the_week (1-7)
21              
22 10         59 my $self = {
23             data => '',
24             year => $year,
25             month => $month,
26             day => $day,
27             day_of_the_week => $day_of_the_week,
28             };
29              
30 10         72 $self->{data} .= BACnet::DataTypes::Utils::_construct_head(
31             BACnet::DataTypes::Utils::DATE_TAG,
32             $modified_tag, LENGTH );
33              
34 10 100       30 if ( defined $year ) {
35             $self->{data} .=
36 9         55 BACnet::DataTypes::Utils::_encode_int_octet_undef( $year - YEAR_OFFSET,
37             0xFF );
38             }
39             else {
40             $self->{data} .=
41 1         8 BACnet::DataTypes::Utils::_encode_int_octet_undef( $year, 0xFF );
42             }
43              
44 10         31 my @date_parts = ( $month, $day, $day_of_the_week );
45              
46 10         24 for my $date_part (@date_parts) {
47             $self->{data} .=
48 30         65 BACnet::DataTypes::Utils::_encode_int_octet_undef( $date_part, 0xFF );
49             }
50              
51 10         45 return bless $self, $class;
52             }
53              
54             sub parse {
55 10     10 0 27 my ( $class, $data_in ) = @_;
56              
57 10         33 my $data = substr $data_in, 1;
58 10         35 my $self = bless { data => $data_in, }, $class;
59              
60 10         50 my $headache = BACnet::DataTypes::Utils::_correct_head(
61             data_in => $data_in,
62             expected_tag => BACnet::DataTypes::Utils::DATE_TAG,
63             expected_length => LENGTH,
64             );
65              
66 10 50       29 if ( $headache ne "" ) {
67 0         0 $self->{error} = "Date: $headache";
68 0         0 return $self;
69             }
70              
71 10         27 my $head_len = BACnet::DataTypes::Utils::_get_head_length($data_in);
72              
73             $self->{year} =
74 10         64 BACnet::DataTypes::Utils::_decode_int_octet_undef(
75             substr( $data_in, $head_len, 1 ), 0xFF );
76 10 100       48 if ( defined $self->{year} ) {
77             $self->{year} =
78 9         46 BACnet::DataTypes::Utils::_decode_int_octet_undef(
79             substr( $data_in, $head_len, 1 ), 0xFF ) + YEAR_OFFSET;
80             }
81              
82             $self->{month} =
83 10         42 BACnet::DataTypes::Utils::_decode_int_octet_undef(
84             substr( $data_in, $head_len + 1, 1 ), 0xFF );
85             $self->{day} =
86 10         34 BACnet::DataTypes::Utils::_decode_int_octet_undef(
87             substr( $data_in, $head_len + 2, 1 ), 0xFF );
88             $self->{day_of_the_week} =
89 10         36 BACnet::DataTypes::Utils::_decode_int_octet_undef(
90             substr( $data_in, $head_len + 3, 1 ), 0xFF );
91              
92 10         39 return $self;
93             }
94              
95             sub val {
96 20     20 0 5484 my ($self) = @_;
97              
98             return return [
99             $self->{year}, $self->{month},
100             $self->{day}, $self->{day_of_the_week}
101 20         97 ];
102             }
103              
104             sub year {
105 10     10 0 5350 my ($self) = @_;
106              
107 10         53 return $self->{year};
108             }
109              
110             sub month {
111 10     10 0 36 my ($self) = @_;
112              
113 10         54 return $self->{month};
114             }
115              
116             sub day {
117 10     10 0 34 my ($self) = @_;
118              
119 10         50 return $self->{day};
120             }
121              
122             sub day_of_the_week {
123 10     10 0 34 my ($self) = @_;
124              
125 10         43 return $self->{day_of_the_week};
126             }
127              
128             1;