Plucene/Document/DateSerializer.pm

filter: Statementstmt 100% 31 / 31 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 100% 49 / 49 CC 6 SCAR 8.7
CC
6
Cov
100%
_to_base_36
4.0
_from_base_36
3.0
BEGIN
1.0
CRAP 2.4
« Plucene/Index/TermInfosReader.pm ↑ Summary Plucene/Search/PhrasePositions.pm »
1 package Plucene::Document::DateSerializer;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our @EXPORT = qw(freeze_date);
10  
11 =head1 NAME
12  
13 Plucene::Document::DateSerializer - Utility functions for dealing with dates
14  
15 =head1 SYNOPSIS
16  
17 use Plucene::Document::DateSerializer
18 my $field = Plucene::Document::Field->Text(
19 date => freeze_date(Time::Piece $t)
20 );
21 $doc->add($field);
22  
23 =head1 DESCRIPTION
24  
25 Dates and times in Plucene should be serialized using the C<freeze_date>
26 function so that the L<Plucene::Search::DateFilter> can filter on them
27 during future searches.
28  
29 =head1 SUBROUTINES
30  
31 =head2 freeze_date
32  
33 my $string = freeze_date(Time::Piece $t)
34  
35 This routine, exported by default, turns a C<Time::Piece> object into
36 a string in a format expected by both Plucene and Lucene.
37  
38 =cut
39  
40 10 ▶sub freeze_date { _to_base_36(shift->epoch * 1000); }
Subroutine freeze_date: called CC=1 SCAR=0.0
Pod documented
41  
42 10 ▶sub _to_base_36 {
Subroutine _to_base_36: called CC=4 SCAR=13.9
43 10 my $number = shift;
44 10 my $string = "";
45 10 ▶ while ($number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number) 80 10 90
46 80 my $quot = $number % 36;
47 80 ▶ $string = ($quot < 10 ? $quot : chr($quot + 87)) . $string;
Branch: 1 branch
BranchTrueFalseTotal
$quot < 10 ? : 20 60 80
48 80 $number = int($number / 36);
49 }
50 10 ▶ $string = "0$string" while length($string) < 9;
Branch: 1 branch
BranchTrueFalseTotal
length $string < 9 and do { $string = "0$string" } 10 10 20
51 10 $string;
52 }
53  
54 1 ▶sub _from_base_36 {
Subroutine _from_base_36: called CC=3 SCAR=11.0
55 1 my $string = shift;
56 1 my $exponent = 0;
57 1 my $number;
58 1 for (reverse split //, $string) {
59 9 ▶ $number += ($_ =~ /\d/ ? $_ : (ord($_) - 87)) * (36**$exponent++);
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /\d/ ? : 4 5 9
60 }
61 1 return $number;
62 }
63  
64 # Java uses milliseconds, but Perl doesn't have 'em
65  
66 =head2 thaw_date
67  
68 my Time::Piece $t = Plucene::Document::DateSerializer::thaw_date($string)
69  
70 This routine is not exported, and is not used by the Plucene core. It is
71 useful for debugging dates, and simply reverses the C<freeze_date> operation.
72  
73 =cut
74  
75 1 ▶sub thaw_date {
Subroutine thaw_date: called CC=1 SCAR=0.0
Pod documented
76 1 my $self = shift;
77 1 return Time::Piece->new(_from_base_36($self) / 1000);
78 }
79  
80 2 1;
« Plucene/Index/TermInfosReader.pm ↑ Summary Plucene/Search/PhrasePositions.pm »