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