| 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
| |||||||||||
| 46 | 80 | my $quot = $number % 36; | |||||||||
| 47 | 80 | ▶ | $string = ($quot < 10 ? $quot : chr($quot + 87)) . $string; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 80 | $number = int($number / 36); | |||||||||
| 49 | } | ||||||||||
| 50 | 10 | ▶ | $string = "0$string" while length($string) < 9; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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; | |||||||||