File Coverage

blib/lib/Date/Utils/Bahai.pm
Criterion Covered Total %
statement 45 45 100.0
branch 7 12 58.3
condition 7 21 33.3
subroutine 12 12 100.0
pod 7 7 100.0
total 78 97 80.4


line stmt bran cond sub pod time code
1             package Date::Utils::Bahai;
2              
3             $Date::Utils::Bahai::VERSION = '0.02';
4              
5             =head1 NAME
6              
7             Date::Utils::Bahai - Bahai date specific routines as Moo Role.
8              
9             =head1 VERSION
10              
11             Version 0.02
12              
13             =cut
14              
15 2     2   18387 use 5.006;
  2         6  
  2         68  
16 2     2   1058 use Data::Dumper;
  2         9388  
  2         105  
17 2     2   778 use POSIX qw/floor/;
  2         7632  
  2         13  
18              
19 2     2   1782 use Moo::Role;
  2         13159  
  2         13  
20 2     2   1094 use namespace::clean;
  2         4677269  
  2         11  
21              
22             our $BAHAI_MONTHS = [
23             '',
24             'Baha', 'Jalal', 'Jamal', 'Azamat', 'Nur', 'Rahmat',
25             'Kalimat', 'Kamal', 'Asma', 'Izzat', 'Mashiyyat', 'Ilm',
26             'Qudrat', 'Qawl', 'Masail', 'Sharaf', 'Sultan', 'Mulk',
27             'Ala'
28             ];
29              
30             our $BAHAI_CYCLES = [
31             '',
32             'Alif', 'Ba', 'Ab', 'Dal', 'Bab', 'Vav',
33             'Abad', 'Jad', 'Baha', 'Hubb', 'Bahhaj', 'Javab',
34             'Ahad', 'Vahhab', 'Vidad', 'Badi', 'Bahi', 'Abha',
35             'Vahid'
36             ];
37              
38             our $BAHAI_DAYS = [
39             ' Jamal ',
40             ' Kamal ',
41             ' Fidal ',
42             ' Idal ',
43             ' Istijlal ',
44             ' Istiqlal ',
45             ' Jalal '
46             ];
47              
48             has bahai_epoch => (is => 'ro', default => sub { 2394646.5 });
49             has bahai_days => (is => 'ro', default => sub { $BAHAI_DAYS });
50             has bahai_months => (is => 'ro', default => sub { $BAHAI_MONTHS });
51             has bahai_cycles => (is => 'ro', default => sub { $BAHAI_CYCLES });
52              
53             with 'Date::Utils';
54              
55             =head1 DESCRIPTION
56              
57             Bahai date specific routines as Moo Role.
58              
59             =head1 METHODS
60              
61             =head2 bahai_to_gregorian($major, $cycle, $year, $month, $day)
62              
63             Returns Gregorian date as list (year, month, day) equivalent of the given bahai
64             date.
65              
66             =cut
67              
68             sub bahai_to_gregorian {
69 1     1 1 239 my ($self, $major, $cycle, $year, $month, $day) = @_;
70              
71 1         3 return $self->julian_to_gregorian($self->bahai_to_julian($major, $cycle, $year, $month, $day));
72             }
73              
74             =head2 gregorian_to_bahai($year, $month, $day)
75              
76             Returns Bahai date component as list (majaor, cycle, year, month, day) equivalent
77             of the given gregorian date.
78              
79             =cut
80              
81             sub gregorian_to_bahai {
82 1     1 1 289 my ($self, $year, $month, $day) = @_;
83              
84 1         4 return $self->julian_to_bahai($self->gregorian_to_julian($year, $month, $day));
85             }
86              
87             =head2 bahai_to_julian($major, $cycle, $year, $month, $day)
88              
89             Returns julian date of the given bahai date.
90              
91             =cut
92              
93             sub bahai_to_julian {
94 8     8 1 231 my ($self, $major, $cycle, $year, $month, $day) = @_;
95              
96 8         17 my ($g_year) = $self->julian_to_gregorian($self->bahai_epoch);
97 8         221 my $gy = (361 * ($major - 1)) +
98             (19 * ($cycle - 1)) +
99             ($year - 1) + $g_year;
100              
101 8 50       10 return $self->gregorian_to_julian($gy, 3, 20)
    100          
102             +
103             (19 * ($month - 1))
104             +
105             (($month != 20) ? 0 : ($self->is_gregorian_leap_year($gy + 1) ? -14 : -15))
106             +
107             $day;
108             }
109              
110             =head2 julian_to_bahai($julian_date)
111              
112             Returns Bahai date component as list (majaor, cycle, year, month, day) equivalent
113             of the given Julian date C<$julian_date>.
114              
115             =cut
116              
117             sub julian_to_bahai {
118 2     2 1 246 my ($self, $julian_date) = @_;
119              
120 2         5 $julian_date = floor($julian_date) + 0.5;
121 2         4 my $gregorian_year = ($self->julian_to_gregorian($julian_date))[0];
122 2         54 my $start_year = ($self->julian_to_gregorian($self->bahai_epoch))[0];
123              
124 2         47 my $j1 = $self->gregorian_to_julian($gregorian_year, 1, 1);
125 2         9 my $j2 = $self->gregorian_to_julian($gregorian_year, 3, 20);
126              
127 2 50 33     23 my $bahai_year = $gregorian_year - ($start_year + ((($j1 <= $julian_date) && ($julian_date <= $j2)) ? 1 : 0));
128 2         5 my ($major, $cycle, $year) = $self->get_major_cycle_year($bahai_year);
129              
130 2         4 my $days = $julian_date - $self->bahai_to_julian($major, $cycle, $year, 1, 1);
131 2         15 my $bld = $self->bahai_to_julian($major, $cycle, $year, 20, 1);
132 2 50       34 my $month = ($julian_date >= $bld) ? 20 : (floor($days / 19) + 1);
133 2         4 my $day = ($julian_date + 1) - $self->bahai_to_julian($major, $cycle, $year, $month, 1);
134              
135 2         17 return ($major, $cycle, $year, $month, $day);
136             }
137              
138             =head2 get_major_cycle_year($bahai_year)
139              
140             Returns the attribute as list major, cycle & year as in Kull-i-Shay) of the given
141             Bahai year C<$bahai_year>.
142              
143             =cut
144              
145             sub get_major_cycle_year {
146 3     3 1 2 my ($self, $bahai_year) = @_;
147              
148 3         7 my $major = floor($bahai_year / 361) + 1;
149 3         6 my $cycle = floor(($bahai_year % 361) / 19) + 1;
150 3         1 my $year = ($bahai_year % 19) + 1;
151              
152 3         5 return ($major, $cycle, $year);
153             }
154              
155             =head2 validate_month($month)
156              
157             Dies if the given C<$month> is not a valid Bahai month.
158              
159             =cut
160              
161             sub validate_month {
162 1     1 1 374 my ($self, $month) = @_;
163              
164 1 50 33     25 die("ERROR: Invalid month [$month].\n")
      33        
      33        
165             unless (defined($month) && ($month =~ /^\d{1,2}$/) && ($month >= 1) && ($month <= 19));
166             }
167              
168             =head2 validate_day($day)
169              
170             Dies if the given C<$day> is not a valid Bahai day.
171              
172             =cut
173              
174             sub validate_day {
175 1     1 1 219 my ($self, $day) = @_;
176              
177 1 50 33     17 die ("ERROR: Invalid day [$day].\n")
      33        
      33        
178             unless (defined($day) && ($day =~ /^\d{1,2}$/) && ($day >= 1) && ($day <= 19));
179             }
180              
181             =head1 AUTHOR
182              
183             Mohammad S Anwar, C<< >>
184              
185             =head1 REPOSITORY
186              
187             L
188              
189             =head1 ACKNOWLEDGEMENTS
190              
191             Entire logic is based on the L written by John Walker.
192              
193             =head1 BUGS
194              
195             Please report any bugs / feature requests to C
196             , or through the web interface at L.
197             I will be notified, and then you'll automatically be notified of progress on your
198             bug as I make changes.
199              
200             =head1 SUPPORT
201              
202             You can find documentation for this module with the perldoc command.
203              
204             perldoc Date::Utils::Bahai
205              
206             You can also look for information at:
207              
208             =over 4
209              
210             =item * RT: CPAN's request tracker
211              
212             L
213              
214             =item * AnnoCPAN: Annotated CPAN documentation
215              
216             L
217              
218             =item * CPAN Ratings
219              
220             L
221              
222             =item * Search CPAN
223              
224             L
225              
226             =back
227              
228             =head1 LICENSE AND COPYRIGHT
229              
230             Copyright (C) 2015 Mohammad S Anwar.
231              
232             This program is free software; you can redistribute it and / or modify it under
233             the terms of the the Artistic License (2.0). You may obtain a copy of the full
234             license at:
235              
236             L
237              
238             Any use, modification, and distribution of the Standard or Modified Versions is
239             governed by this Artistic License.By using, modifying or distributing the Package,
240             you accept this license. Do not use, modify, or distribute the Package, if you do
241             not accept this license.
242              
243             If your Modified Version has been derived from a Modified Version made by someone
244             other than you,you are nevertheless required to ensure that your Modified Version
245             complies with the requirements of this license.
246              
247             This license does not grant you the right to use any trademark, service mark,
248             tradename, or logo of the Copyright Holder.
249              
250             This license includes the non-exclusive, worldwide, free-of-charge patent license
251             to make, have made, use, offer to sell, sell, import and otherwise transfer the
252             Package with respect to any patent claims licensable by the Copyright Holder that
253             are necessarily infringed by the Package. If you institute patent litigation
254             (including a cross-claim or counterclaim) against any party alleging that the
255             Package constitutes direct or contributory patent infringement,then this Artistic
256             License to you shall terminate on the date that such litigation is filed.
257              
258             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
259             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
260             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
261             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
262             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
263             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
264             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265              
266             =cut
267              
268             1; # End of Date::Utils::Bahai