File Coverage

blib/lib/Date/QuarterOfYear.pm
Criterion Covered Total %
statement 35 35 100.0
branch 18 18 100.0
condition 15 15 100.0
subroutine 8 8 100.0
pod 1 1 100.0
total 77 77 100.0


line stmt bran cond sub pod time code
1             package Date::QuarterOfYear;
2             $Date::QuarterOfYear::VERSION = '0.04';
3 4     4   213643 use 5.006;
  4         40  
4 4     4   22 use strict;
  4         8  
  4         107  
5 4     4   22 use warnings;
  4         6  
  4         149  
6 4     4   23 use Scalar::Util qw/ reftype /;
  4         20  
  4         200  
7 4     4   23 use Carp;
  4         8  
  4         263  
8 4     4   1942 use parent 'Exporter';
  4         1243  
  4         23  
9              
10             our @EXPORT_OK = qw/ quarter_of_year /;
11              
12             sub quarter_of_year
13             {
14 20     20 1 11059 my $date = _dwim_date(@_);
15 12         51 my $quarter_number = 1 + int(($date->{month}-1) / 3);
16              
17             return wantarray
18             ? ($date->{year}, $quarter_number)
19 12 100       69 : sprintf('%4d-Q%d', $date->{year}, $quarter_number)
20             ;
21             }
22              
23             sub _dwim_date
24             {
25 20 100   20   80 if (@_ == 1) {
    100          
    100          
26 12         23 my $param = shift;
27              
28 12 100 100     143 if (reftype($param) && reftype($param) eq 'HASH') {
    100          
    100          
29             return $param if exists($param->{year})
30             && exists($param->{month})
31 5 100 100     53 && exists($param->{day});
      100        
32 3         274 croak "you must specify year, month and day\n";
33             }
34             elsif (reftype($param)) {
35 1         186 croak "you can't pass a reference of type ".reftype($param);
36             }
37             elsif ($param =~ /^([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])$/) {
38 4         25 return { year => $1, month => $2, day => $3 };
39             }
40              
41 2         43 my @tm = gmtime($param);
42 2         16 return { year => $tm[5] + 1900, month => $tm[4]+1, day => $tm[3] };
43              
44             }
45             elsif (@_ == 3) {
46 2         6 my ($year, $month, $day) = @_;
47 2         10 return { year => $year, month => $month, day => $day };
48             }
49             elsif (@_ == 6) {
50 5         16 my $hashref = { @_ };
51              
52             return $hashref if exists($hashref->{year})
53             && exists($hashref->{month})
54 5 100 100     40 && exists($hashref->{day});
      100        
55 3         265 croak "you must specify year, month and day\n";
56             }
57             else {
58 1         86 croak "invalid arguments\n";
59             }
60             }
61              
62             1;
63              
64             =head1 NAME
65              
66             Date::QuarterOfYear - calculate what quarter a given date is in
67              
68             =head1 SYNOPSIS
69              
70             use Date::QuarterOfYear qw/ quarter_of_year /;
71              
72             $q = quarter_of_year('2013-02-17'); # '2013-Q1'
73             $q = quarter_of_year($epoch); # '2013-Q1'
74             $q = quarter_of_year({ year => 2012, month => 8, day => 9 });
75              
76             ($year, $quarter) = quarter_of_year($epoch);
77              
78             =head1 DESCRIPTION
79              
80             Date::QuarterOfYear provides a single function,
81             but even so you must explicitly ask for it,
82             as per the SYNOPSIS.
83              
84             =head2 quarter_of_year()
85              
86             C takes a date and returns what quarter that date is in.
87             The input date can be specified in various ways, and the result
88             will either be returned as a string of the form 'YYYY-QN' (eg '2014-Q2'),
89             or as a list C<($year, $quarter)>.
90              
91             $time = time();
92             $qstring = quarter_of_year($time);
93             ($year, $quarter) = quarter_of_year($time);
94              
95             This is a very simple module, and there are other modules that can
96             calculate the quarter for you. But I have similar code in multiple places
97             where I don't want to load L just for this.
98              
99             =head1 SEE ALSO
100              
101             L has several features related to quarters:
102             given a C instance, the C method returns a
103             number between 1 and 4.
104             The C method returns a number between 1 and the number
105             of days in the quarter.
106             The C method returns a locale-specific name for the quarter.
107              
108             L provides a C function that will generate
109             the quarter number (1..4).
110              
111             L also provides a C method that returns the
112             quarter number for a given date.
113              
114             =head1 REPOSITORY
115              
116             L
117              
118             =head1 AUTHOR
119              
120             Neil Bowers Eneilb@cpan.orgE
121              
122             =head1 LICENSE AND COPYRIGHT
123              
124             This software is copyright (c) 2014 by Neil Bowers .
125              
126             This is free software; you can redistribute it and/or modify it under
127             the same terms as the Perl 5 programming language system itself.
128