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
|
package Biber::Date::Format;
use v5.24;
use strict;
use Carp;
use DateTime;
use DateTime::TimeZone;
use DateTime::Format::Builder;
use DateTime::Calendar::Julian;
use Unicode::UCD qw(num);
use Biber::Constants;
=encoding utf-8
=head1 NAME
Biber::Date::Format - Biber::Date::Format objects
=head2 Description
Implements ISO8601-2 Extended Format and also allows detection of
missing month/year.
=cut
# Needed as a reset of class information between parses as this isn't reset
# by a new parse_datetime
sub init {
my $self = shift;
delete $self->{missing};
delete $self->{approximate};
delete $self->{uncertain};
delete $self->{yeardivision};
delete $self->{julian};
# map of Unicode numeric script dateparts to arabic as DateTime needs arabic
delete $self->{scriptmap};
return $self;
}
sub set_julian {
my $self = shift;
$self->{julian} = 1;
}
sub julian {
my $self = shift;
return $self->{julian};
}
sub missing {
my $self = shift;
my $part = shift;
return $self->{missing}{$part};
}
sub approximate {
my $self = shift;
return $self->{approximate};
}
sub uncertain {
my $self = shift;
return $self->{uncertain};
}
sub yeardivision {
my $self = shift;
return $self->{yeardivision};
}
sub resolvescript {
my ($self, $dp) = @_;
return $self->{scriptmap}{atos}{$dp} // $dp;
}
DateTime::Format::Builder->create_class(
parsers => {
parse_datetime => [
[ preprocess => \&_pre ],
{# ISO8601-1 4.2
# Ignore milliseconds, if present
#[-]YYYY-MM-DDThh:mm:ss[.mmm] 1985-04-12T10:15:30.003
length => [ qw( 19 20 23 24) ],
regex => qr/^ (-?\d{4}) - (\d\d) - (\d\d)
T (\d\d) : (\d\d) : (\d\d) (?:\.\d\d\d)? $/x,
params => [ qw( year month day hour minute second ) ],
},
{# ISO8601-1 4.1
#[-]YYYY-MM-DD 1985-04-12
length => [ qw( 10 11 ) ],
regex => qr/^ (-?\d{4}) - (\d\d) - (\d\d) $/x,
params => [ qw( year month day ) ],
postprocess => \&_missing_time
},
{# ISO8601-1 4.1
#[-]YYYY-MM 1985-04
length => [ qw( 7 8 ) ],
regex => qr/^ (-?\d{4}) - (\d\d) $/x,
params => [ qw( year month ) ],
postprocess => [ \&_missing_day,
\&_missing_time ]
},
{# ISO8601-1 4.1
#[-]YYYY 1985
length => [ qw( 4 5 ) ],
regex => qr/^ (-?\d{4}) $/x,
params => [ qw( year ) ],
postprocess => [ \&_missing_month,
\&_missing_day,
\&_missing_time ]
},
{# ISO8601-2 4.5.1
#Y[-]YYYYY... Y17000000002
regex => qr/^ Y(-?\d{5,}) $/x,
params => [ qw( year ) ],
postprocess => [ \&_missing_month,
\&_missing_day,
\&_missing_time ]
},
],
}
);
# Parse out timezones and missing/meta information
sub _pre {
my %p = @_;
delete $p{self}{missing};
delete $p{self}{approximate};
delete $p{self}{uncertain};
delete $p{self}{yeardivision};
# Convert and save information on non-arabic numerics
foreach my $num ($p{input} =~ m/\d+/g) {
my $lnum = length($num);
my $rnum = num($num);
my $anum = sprintf("%0${lnum}d", $rnum); # num() strips leading zeros - pad them back
unless ($num eq $anum) {
$p{self}{scriptmap}{atos}{$anum} = $num; # Save padded ...
$p{self}{scriptmap}{atos}{$rnum} = $num; # ... and non-padded versions
$p{self}{scriptmap}{stoa}{$num} = $anum;
}
}
if (defined($p{self}{scriptmap})) {
$p{input} =~ s/(\d+)/$p{self}{scriptmap}{stoa}{$1}/xge;
}
# ISO 8601-2:2016 4.2.1 (uncertain)
if ($p{input} =~ s/^\s*(.+?)\s*\?\s*$/$1/i) {
$p{self}{uncertain} = 1;
}
# ISO 8601-2:2016 4.2.1 (approximate)
if ($p{input} =~ s/^\s*(.+?)\s*\~\s*$/$1/i) {
$p{self}{approximate} = 1;
}
# ISO 8601-2:2016 4.2.1 (uncertain+approximate)
if ($p{input} =~ s/^\s*(.+?)\s*\%\s*$/$1/i) {
$p{self}{uncertain} = 1;
$p{self}{approximate} = 1;
}
# ISO8601-1 4.2.2 (time zone)
if ($p{input} =~ s/Z$//) {
$p{parsed}{time_zone} = 'UTC';
}
elsif ($p{input} =~ s/([+-]\d\d:\d\d)$//) {
$p{parsed}{time_zone} = $1;
}
# ISO8601-2:2016 4.8 (yeardivisions)
if ($p{input} =~ s/^(-?\d{4})-([23]\d|4[01])$/$1/) {
$p{self}{yeardivision} = $Biber::Constants::YEARDIVISIONS{$2};
}
return $p{input};
}
sub _missing_month {
my %p = @_;
$p{self}{missing}{month} = 1;
return 1;
}
sub _missing_day {
my %p = @_;
$p{self}{missing}{day} = 1;
return 1;
}
sub _missing_time {
my %p = @_;
$p{self}{missing}{time} = 1;
return 1;
}
1;
__END__
=head1 AUTHORS
Philip Kime C<< <philip at kime.org.uk> >>
=head1 BUGS
Please report any bugs or feature requests on our Github tracker at
L<https://github.com/plk/biber/issues>.
=head1 COPYRIGHT & LICENSE
Copyright 2012-2025 Philip Kime, all rights reserved.
This module is free software. You can redistribute it and/or
modify it under the terms of the Artistic License 2.0.
This program is distributed in the hope that it will be useful,
but without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.
=cut
|