/usr/share/perl5/HTML/Microformats/Datatype/DateTime.pm is in libhtml-microformats-perl 0.105-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | =head1 NAME
HTML::Microformats::Datatype::DateTime - dates and datetimes
=head1 SYNOPSIS
my $duration = HTML::Microformats::Datatype::DateTime->now;
print "$duration\n";
=cut
package HTML::Microformats::Datatype::DateTime;
use base qw(DateTime HTML::Microformats::Datatype);
use DateTime;
use DateTime::Format::Natural;
use DateTime::Format::Strptime;
use HTML::Microformats::Datatype::DateTime::Parser;
use HTML::Microformats::Datatype::String qw();
use HTTP::Date;
use Object::AUTHORITY;
BEGIN {
$HTML::Microformats::Datatype::DateTime::AUTHORITY = 'cpan:TOBYINK';
$HTML::Microformats::Datatype::DateTime::VERSION = '0.105';
}
=head1 DESCRIPTION
This class inherits from DateTime, so supports all of DateTime's methods.
=head2 Constructors
This class inherits from DateTime, so DateTime's standard constructors (C<new>,
C<now>, etc) should work. Also:
=over 4
=item C<< $dt = HTML::Microformats::Datatype::DateTime-E<gt>parse($string, $elem, $context, [$tz], [$base]) >>
Creates a new HTML::Microformats::Datatype::DateTime object.
$tz is the timezone to use (if it can't be figured out) and $base is a base datetime to use for
relative datetimes, whatever that means.
=back
=cut
sub parse
{
my $class = shift;
my $string = shift;
my $elem = shift||undef;
my $page = shift||undef;
my $tz = shift||undef;
my $base = shift||undef;
return $class->_microformat_datetime($string, $tz, $base);
}
sub _microformat_datetime
{
my $class = shift;
my $dt = $class->_microformat_datetime_helper(@_);
return undef unless $dt;
# Super-dangerous: reblessing an already-blessed object.
bless $dt, 'HTML::Microformats::Datatype::DateTime';
}
sub _microformat_datetime_helper
# Very tolerant DateTime parsing. Microformats are supposed to always use W3CDTF,
# but we'll be lenient and do our best to parse other ISO 8601 formats, and if
# that fails, even try to parse natural language.
{
my $class = shift;
my $string = shift;
my $tz = shift || 'UTC';
my $base = shift || undef;
my $f;
my $dt;
$string = $string->{string} if HTML::Microformats::Datatype::String::isms($string);
my $parser = __PACKAGE__ . '::Parser';
if ($base)
{ $f = $parser->new(base_datetime => $base); }
else
{ $f = $parser->new; }
eval {
my $isostring = $string;
$isostring =~ s/([\+\-]\d\d)([014][50])$/$1\:$2/;
$dt = $f->parse_datetime($isostring);
$dt->{resolution} = $f->{resolution};
if ($dt->{resolution} eq 'end')
{
$dt = $dt->add( days => 1 );
$dt->{resolution} = 'day';
}
};
unless ($dt)
{
eval {
my $time = str2time($string);
$dt = DateTime->from_epoch( epoch => $time );
$dt->{resolution} = 'second' unless ($dt->{resolution});
};
}
unless ($dt)
{
$f = DateTime::Format::Natural->new(
lang => 'en', # Should read this from source input
prefer_future => 1,
daytime => {
morning => 9,
afternoon => 13,
evening => 20
},
time_zone => "$tz"
);
$dt = $f->parse_datetime($string);
$dt->{resolution} = 'second' unless $dt->{resolution};
return undef unless $f->success;
}
return undef unless $dt;
my %pattern = (
year => '%Y',
month => '%Y-%m',
week => '%F',
day => '%F',
hour => '%FT%H:%M',
minute => '%FT%H:%M',
second => '%FT%H:%M:%S',
nanosecond => '%FT%T.%9N'
);
my %tz_pattern = (
year => '%Y',
month => '%Y-%m',
week => '%F',
day => '%F',
hour => '%FT%H:%M%z',
minute => '%FT%H:%M%z',
second => '%FT%H:%M:%S%z',
nanosecond => '%FT%T.%9N%z'
);
if ($dt->year >= 100000)
{
foreach my $x (keys %pattern)
{
$pattern{$x} = '+'.$pattern{$x};
$tz_pattern{$x} = '+'.$tz_pattern{$x};
}
}
elsif ($dt->year >= 10000)
{
foreach my $x (keys %pattern)
{
$pattern{$x} = '+0'.$pattern{$x};
$tz_pattern{$x} = '+0'.$tz_pattern{$x};
}
}
if ($dt->{tz}->{name} eq 'floating')
{
$dt->set_formatter(DateTime::Format::Strptime->new(
pattern => ( $pattern{$dt->{resolution}} )
));
}
else
{
$dt->set_formatter(DateTime::Format::Strptime->new(
pattern => ( $tz_pattern{$dt->{resolution}} )
));
}
return $dt;
}
=head2 Public Methods
=over 4
=item C<< $d-E<gt>to_string >>
Returns a literal string.
=cut
sub to_string
{
my $self = shift;
my $type = $self->datatype;
if ($type eq 'http://www.w3.org/2001/XMLSchema#gYear')
{
return $self->strftime('%Y');
}
elsif ($type eq 'http://www.w3.org/2001/XMLSchema#gYearMonth')
{
return $self->strftime('%Y-%m');
}
elsif ($type eq 'http://www.w3.org/2001/XMLSchema#date')
{
return $self->strftime('%Y-%m-%d');
}
else
{
return "$self";
}
}
=item C<< $d-E<gt>datatype >>
Returns an the RDF datatype URI representing the data type of this literal.
=back
=cut
sub datatype
{
my $self = shift;
if($self->{datatype})
{ return $self->{datatype}; }
elsif ($self->{resolution} eq 'year')
{ return 'http://www.w3.org/2001/XMLSchema#gYear'; }
elsif ($self->{resolution} eq 'month')
{ return 'http://www.w3.org/2001/XMLSchema#gYearMonth'; }
elsif ($self->{resolution} eq 'day')
{ return 'http://www.w3.org/2001/XMLSchema#date'; }
return 'http://www.w3.org/2001/XMLSchema#dateTime';
}
sub TO_JSON
{
return "$_[0]";
}
1;
__END__
=head1 BUGS
Please report any bugs to L<http://rt.cpan.org/>.
=head1 SEE ALSO
L<HTML::Microformats>,
L<HTML::Microformats::Datatype>,
L<DateTime>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
Copyright 2008-2012 Toby Inkster
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
=cut
|