File Coverage

blib/lib/Date/QuarterOfYear.pm
Criterion Covered Total %
statement 31 35 88.5
branch 14 18 77.7
condition 6 15 40.0
subroutine 8 8 100.0
pod 0 1 0.0
total 59 77 76.6


line stmt bran cond sub pod time code
1             package Date::QuarterOfYear;
2             $Date::QuarterOfYear::VERSION = '0.03';
3 3     3   41365 use 5.006;
  3         10  
4 3     3   15 use strict;
  3         5  
  3         64  
5 3     3   22 use warnings;
  3         6  
  3         97  
6 3     3   15 use Scalar::Util qw/ reftype /;
  3         4  
  3         297  
7 3     3   15 use Carp;
  3         4  
  3         208  
8 3     3   2143 use parent 'Exporter';
  3         904  
  3         17  
9              
10             our @EXPORT_OK = qw/ quarter_of_year /;
11              
12             sub quarter_of_year
13             {
14 12     12 0 5922 my $date = _dwim_date(@_);
15 12         41 my $quarter_number = 1 + int(($date->{month}-1) / 3);
16              
17             return wantarray
18             ? ($date->{year}, $quarter_number)
19 12 100       72 : sprintf('%4d-Q%d', $date->{year}, $quarter_number)
20             ;
21             }
22              
23             sub _dwim_date
24             {
25 12 100   12   48 if (@_ == 1) {
    100          
    50          
26 8         15 my $param = shift;
27              
28 8 100 66     86 if (reftype($param) && reftype($param) eq 'HASH') {
    50          
    100          
29             return $param if exists($param->{year})
30             && exists($param->{month})
31 2 50 33     22 && exists($param->{day});
      33        
32 0         0 croak "you must specify year, month and day\n";
33             }
34             elsif (reftype($param)) {
35 0         0 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         27 return { year => $1, month => $2, day => $3 };
39             }
40              
41 2         45 my @tm = gmtime($param);
42 2         14 return { year => $tm[5] + 1900, month => $tm[4]+1, day => $tm[3] };
43              
44             }
45             elsif (@_ == 3) {
46 2         5 my ($year, $month, $day) = @_;
47 2         8 return { year => $year, month => $month, day => $day };
48             }
49             elsif (@_ == 6) {
50 2         7 my $hashref = { @_ };
51              
52             return $hashref if exists($hashref->{year})
53             && exists($hashref->{month})
54 2 50 33     24 && exists($hashref->{day});
      33        
55 0           croak "you must specify year, month and day\n";
56             }
57             else {
58 0           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, C,
81             which takes a date and returns what quarter that date is in.
82             The input date can be specified in various ways, and the result
83             will either be returned as a string of the form 'YYYY-QN' (eg '2014-Q2'),
84             or as a list C<($year, $quarter)>.
85              
86             $time = time();
87             $qstring = quarter_of_year($time);
88             ($year, $quarter) = quarter_of_year($time);
89              
90             This is a very simple module, and there are other modules that can
91             calculate the quarter for you. But I have similar code in multiple places
92             where I don't want to load L just for this.
93              
94             =head1 SEE ALSO
95              
96             L has several features related to quarters:
97             given a C instance, the C method returns a
98             number between 1 and 4.
99             The C method returns a number between 1 and the number
100             of days in the quarter.
101             The C method returns a locale-specific name for the quarter.
102              
103             L provides a C function that will generate
104             the quarter number (1..4).
105              
106             L also provides a C method that returns the
107             quarter number for a given date.
108              
109             =head1 REPOSITORY
110              
111             L
112              
113             =head1 AUTHOR
114              
115             Neil Bowers Eneilb@cpan.orgE
116              
117             =head1 LICENSE AND COPYRIGHT
118              
119             This software is copyright (c) 2014 by Neil Bowers .
120              
121             This is free software; you can redistribute it and/or modify it under
122             the same terms as the Perl 5 programming language system itself.
123