File Coverage

blib/lib/Date/Gregorian/Simple.pm
Criterion Covered Total %
statement 40 51 78.4
branch 4 16 25.0
condition 5 15 33.3
subroutine 13 15 86.6
pod 4 8 50.0
total 66 105 62.8


line stmt bran cond sub pod time code
1             package Date::Gregorian::Simple;
2              
3             $Date::Gregorian::Simple::VERSION = '0.17';
4             $Date::Gregorian::Simple::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Date::Gregorian::Simple - Represents Gregorian date.
9              
10             =head1 VERSION
11              
12             Version 0.17
13              
14             =cut
15              
16 2     2   111634 use 5.006;
  2         15  
17 2     2   1018 use Data::Dumper;
  2         11185  
  2         102  
18 2     2   711 use Time::localtime;
  2         7641  
  2         89  
19 2     2   803 use Date::Exception::InvalidDay;
  2         108563  
  2         71  
20              
21 2     2   16 use Moo;
  2         4  
  2         8  
22 2     2   609 use namespace::autoclean;
  2         5  
  2         10  
23              
24 2     2   118 use overload q{""} => 'as_string', fallback => 1;
  2         5  
  2         10  
25              
26             =head1 DESCRIPTION
27              
28             Represents the Gregorian date.
29              
30             =cut
31              
32             our $GREGORIAN_MONTHS = [
33             undef,
34             qw(January February March April May June
35             July August September October November December)
36             ];
37              
38             our $GREGORIAN_DAYS = [
39             qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
40             ];
41              
42             our $GREGORIAN_MONTH_DAYS = [
43             qw(31 28 31 30 31 30
44             31 31 30 31 30 31)
45             ];
46              
47             has days => (is => 'ro', default => sub { $GREGORIAN_DAYS });
48             has months => (is => 'ro', default => sub { $GREGORIAN_MONTHS });
49             has year => (is => 'rw', predicate => 1);
50             has month => (is => 'rw', predicate => 1);
51             has day => (is => 'rw', predicate => 1);
52              
53             with 'Date::Utils';
54              
55             sub BUILD {
56 2     2 0 12 my ($self) = @_;
57              
58 2 50 33     36 if ($self->has_year && $self->has_month && $self->has_day) {
      33        
59 2         14 $self->validate_year($self->year);
60 2         43 $self->validate_month($self->month);
61 2 50       44 if ($self->is_leap_year($self->year)) {
62 2         17 my $day = $self->day;
63 2         15 my @caller = caller(0);
64 2 50       6 @caller = caller(2) if $caller[3] eq '(eval)';
65              
66 2 0 33     32 Date::Exception::InvalidDay->throw({
    50 33        
      33        
67             method => __PACKAGE__."::validate_day",
68             message => sprintf("ERROR: Invalid day [%s].", defined($day)?($day):('')),
69             filename => $caller[1],
70             line_number => $caller[2] })
71             unless (defined($day) && ($day =~ /^\d+$/) && ($day >= 1) && ($day <= 29));
72             }
73             else {
74 0         0 $self->validate_day($self->day);
75             }
76             }
77             else {
78 0         0 my $today = localtime;
79 0         0 $self->year($today->year + 1900);
80 0         0 $self->month($today->mon + 1);
81 0         0 $self->day($today->mday);
82             }
83             }
84              
85             =head1 SYNOPSIS
86              
87             use strict; use warnings;
88             use Date::Gregorian::Simple;
89              
90             # prints today's Gregorian date
91             print Date::Gregorian::Simple->new, "\n";
92              
93             my $date = Date::Gregorian::Simple->new({ year => 2016, month => 1, day => 1 });
94              
95             # prints the given Gregorian date
96             print $date->as_string, "\n";
97              
98             # prints the equivalent Julian date
99             print $date->to_julian, "\n";
100              
101             # prints day of the week index (0 for Sunday, 1 for Monday and so on).
102             print $date->day_of_week, "\n";
103              
104             # prints the Gregorian date equivalent of the Julian date (2456955.5).
105             print $date->from_julian(2456955.5), "\n";
106              
107             =head1 METHODS
108              
109             =head2 to_julian()
110              
111             Returns Julian date equivalent of the Gregorian date.
112              
113             =cut
114              
115             sub to_julian {
116 2     2 1 723 my ($self) = @_;
117              
118 2         13 return $self->gregorian_to_julian($self->year, $self->month, $self->day);
119             }
120              
121             =head2 from_julian($julian_date)
122              
123             Returns Gregorian date as an object of type L equivalent
124             of the given Julian date C<$julian_date>.
125              
126             =cut
127              
128             sub from_julian {
129 1     1 1 8684 my ($self, $julian_date) = @_;
130              
131 1         5 my ($year, $month, $day) = $self->julian_to_gregorian($julian_date);
132              
133 1         97 return Date::Gregorian::Simple->new({
134             year => $year,
135             month => $month,
136             day => $day });
137             }
138              
139             =head2 day_of_week()
140              
141             Returns day of the week, starting 0 for Sunday, 1 for Monday and so on.
142              
143             +-------+-------------------------------------------------------------------+
144             | Index | English Name |
145             +-------+-------------------------------------------------------------------+
146             | 0 | Sunday |
147             | 1 | Monday |
148             | 2 | Tuesday |
149             | 3 | Wednesday |
150             | 4 | Thursday |
151             | 5 | Friday |
152             | 6 | Saturday |
153             +-------+-------------------------------------------------------------------+
154              
155             =cut
156              
157             sub day_of_week {
158 1     1 1 507 my ($self) = @_;
159              
160 1         3 return $self->jwday($self->to_julian);
161             }
162              
163             =head2 is_leap_year($year)
164              
165             Returns 0 or 1 if the given Gregorian year C<$year> is a leap year or not.
166              
167             =cut
168              
169             sub is_leap_year {
170 2     2 1 6 my ($self, $year) = @_;
171              
172 2         8 return $self->is_gregorian_leap_year($year);
173             }
174              
175             sub days_in_year {
176 0     0 0 0 my ($self, $year) = @_;
177              
178 0 0       0 ($self->is_leap_year($year))
179             ?
180             (return 366)
181             :
182             (return 365);
183             }
184              
185             sub days_in_month_year {
186 0     0 0 0 my ($self, $month, $year) = @_;
187              
188 0 0       0 if ($self->is_leap_year($year)) {
189 0 0       0 return 29 if ($month == 2);
190             }
191              
192 0         0 return $GREGORIAN_MONTH_DAYS->[$month-1];
193             }
194              
195             sub as_string {
196 1     1 0 7 my ($self) = @_;
197              
198 1         5 return sprintf("%d, %s %d", $self->day, $self->get_month_name, $self->year);
199             }
200              
201             =head1 AUTHOR
202              
203             Mohammad S Anwar, C<< >>
204              
205             =head1 REPOSITORY
206              
207             L
208              
209             =head1 SEE ALSO
210              
211             =over 4
212              
213             =item L
214              
215             =item L
216              
217             =item L
218              
219             =item L
220              
221             =item L
222              
223             =item L
224              
225             =back
226              
227             =head1 BUGS
228              
229             Please report any bugs / feature requests to C,
230             or through the web interface at L.
231             I will be notified, and then you'll automatically be notified of progress on your
232             bug as I make changes.
233              
234             =head1 SUPPORT
235              
236             You can find documentation for this module with the perldoc command.
237              
238             perldoc Date::Gregorian::Simple
239              
240             You can also look for information at:
241              
242             =over 4
243              
244             =item * RT: CPAN's request tracker
245              
246             L
247              
248             =item * AnnoCPAN: Annotated CPAN documentation
249              
250             L
251              
252             =item * CPAN Ratings
253              
254             L
255              
256             =item * Search CPAN
257              
258             L
259              
260             =back
261              
262             =head1 LICENSE AND COPYRIGHT
263              
264             Copyright (C) 2016 - 2017 Mohammad S Anwar.
265              
266             This program is free software; you can redistribute it and / or modify it under
267             the terms of the the Artistic License (2.0). You may obtain a copy of the full
268             license at:
269              
270             L
271              
272             Any use, modification, and distribution of the Standard or Modified Versions is
273             governed by this Artistic License.By using, modifying or distributing the Package,
274             you accept this license. Do not use, modify, or distribute the Package, if you do
275             not accept this license.
276              
277             If your Modified Version has been derived from a Modified Version made by someone
278             other than you,you are nevertheless required to ensure that your Modified Version
279             complies with the requirements of this license.
280              
281             This license does not grant you the right to use any trademark, service mark,
282             tradename, or logo of the Copyright Holder.
283              
284             This license includes the non-exclusive, worldwide, free-of-charge patent license
285             to make, have made, use, offer to sell, sell, import and otherwise transfer the
286             Package with respect to any patent claims licensable by the Copyright Holder that
287             are necessarily infringed by the Package. If you institute patent litigation
288             (including a cross-claim or counterclaim) against any party alleging that the
289             Package constitutes direct or contributory patent infringement,then this Artistic
290             License to you shall terminate on the date that such litigation is filed.
291              
292             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
293             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
294             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
295             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
296             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
297             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
298             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
299              
300             =cut
301              
302             1; # End of Date::Gregorian::Simple