File Coverage

blib/lib/Plucene/Document/DateSerializer.pm
Criterion Covered Total %
statement 29 29 100.0
branch 4 4 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 43 43 100.0


line stmt bran cond sub pod time code
1             package Plucene::Document::DateSerializer;
2              
3 2     2   1871 use strict;
  2         4  
  2         75  
4 2     2   8 use warnings;
  2         5  
  2         48  
5              
6 2     2   2483 use Time::Piece;
  2         42070  
  2         14  
7 2     2   183 use base 'Exporter';
  2         5  
  2         1273  
8              
9             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
26             function so that the L 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 object into
36             a string in a format expected by both Plucene and Lucene.
37              
38             =cut
39              
40 10     10 1 1412 sub freeze_date { _to_base_36(shift->epoch * 1000); }
41              
42             sub _to_base_36 {
43 10     10   273 my $number = shift;
44 10         21 my $string = "";
45 10         33 while ($number) {
46 80         105 my $quot = $number % 36;
47 80 100       193 $string = ($quot < 10 ? $quot : chr($quot + 87)) . $string;
48 80         184 $number = int($number / 36);
49             }
50 10         57 $string = "0$string" while length($string) < 9;
51 10         55 $string;
52             }
53              
54             sub _from_base_36 {
55 1     1   2 my $string = shift;
56 1         3 my $exponent = 0;
57 1         2 my $number;
58 1         6 for (reverse split //, $string) {
59 9 100       37 $number += ($_ =~ /\d/ ? $_ : (ord($_) - 87)) * (36**$exponent++);
60             }
61 1         8 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 operation.
72              
73             =cut
74              
75             sub thaw_date {
76 1     1 1 1281 my $self = shift;
77 1         6 return Time::Piece->new(_from_base_36($self) / 1000);
78             }
79              
80             1;