📄 dateserializer.pm
字号:
package Plucene::Document::DateSerializer;use strict;use warnings;use Time::Piece;use base 'Exporter';our @EXPORT = qw(freeze_date);=head1 NAMEPlucene::Document::DateSerializer - Utility functions for dealing with dates=head1 SYNOPSIS use Plucene::Document::DateSerializer my $field = Plucene::Document::Field->Text( date => freeze_date(Time::Piece $t) ); $doc->add($field);=head1 DESCRIPTIONDates and times in Plucene should be serialized using the C<freeze_date> function so that the L<Plucene::Search::DateFilter> can filter on themduring future searches.=head1 SUBROUTINES=head2 freeze_date my $string = freeze_date(Time::Piece $t)This routine, exported by default, turns a C<Time::Piece> object intoa string in a format expected by both Plucene and Lucene.=cutsub freeze_date { _to_base_36(shift->epoch * 1000); }sub _to_base_36 { my $number = shift; my $string = ""; while ($number) { my $quot = $number % 36; $string = ($quot < 10 ? $quot : chr($quot + 87)) . $string; $number = int($number / 36); } $string = "0$string" while length($string) < 9; $string;}sub _from_base_36 { my $string = shift; my $exponent = 0; my $number; for (reverse split //, $string) { $number += ($_ =~ /\d/ ? $_ : (ord($_) - 87)) * (36**$exponent++); } return $number;}# Java uses milliseconds, but Perl doesn't have 'em=head2 thaw_date my Time::Piece $t = Plucene::Document::DateSerializer::thaw_date($string)This routine is not exported, and is not used by the Plucene core. It isuseful for debugging dates, and simply reverses the C<freeze_date> operation.=cutsub thaw_date { my $self = shift; return Time::Piece->new(_from_base_36($self) / 1000);}1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -