12 my $class = ref($proto) || $proto;
15 my $last_enum = undef;
17 $self->{CAPTION} = $caption;
20 $self->{PATTERN} = {};
21 $self->{COPY} = undef;
22 $self->{UNIT} = undef;
23 $self->{COMPRESSIBLE} = 1; # until proven otherwise
25 foreach my $subfield ($caption->subfields) {
26 my ($key, $val) = @$subfield;
29 } elsif ($key =~ /[a-h]/) {
30 # Enumeration Captions
31 $self->{ENUMS}->{$key} = {CAPTION => $val,
39 } elsif ($key =~ /[i-m]/) {
41 $self->{CHRONS}->{$key} = $val;
42 } elsif ($key eq 'u') {
43 # Bib units per next higher enumeration level
44 carp('$u specified for top-level enumeration')
45 unless defined($last_enum);
46 $self->{ENUMS}->{$last_enum}->{COUNT} = $val;
47 } elsif ($key eq 'v') {
48 carp '$v specified for top-level enumeration'
49 unless defined($last_enum);
50 $self->{ENUMS}->{$last_enum}->{RESTART} = ($val eq 'r');
51 } elsif ($key =~ /[npwxz]/) {
52 # Publication Pattern ('o' == type of unit, 'q'..'t' undefined)
53 $self->{PATTERN}->{$key} = $val;
54 } elsif ($key eq 'y') {
55 # Publication pattern: 'y' is repeatable
56 $self->{PATTERN}->{y} = [] if (!defined $self->{PATTERN}->{y});
57 push @{$self->{PATTERN}->{y}}, $val;
58 } elsif ($key eq 'o') {
61 } elsif ($key eq 't') {
64 carp "Unknown caption subfield '$key'";
68 # subsequent levels of enumeration (primary and alternate)
69 # If an enumeration level doesn't document the number
70 # of "issues" per "volume", or whether numbering of issues
71 # restarts, then we can't compress.
72 foreach my $key ('b', 'c', 'd', 'e', 'f', 'h') {
73 if (exists $self->{ENUMS}->{$key}) {
74 my $pattern = $self->{ENUMS}->{$key};
75 if (!$pattern->{RESTART} || !$pattern->{COUNT}
76 || ($pattern->{COUNT} eq 'var')
77 || ($pattern->{COUNT} eq 'und')) {
78 $self->{COMPRESSIBLE} = 0;
84 # If there's a $x subfield and a $j, then it's compressible
85 if (exists $self->{PATTERN}->{x} && exists $self->{CHRONS}->{'j'}) {
86 $self->{COMPRESSIBLE} = 1;
89 bless ($self, $class);
91 if (exists $self->{PATTERN}->{y}) {
92 $self->decode_pattern;
100 my $pattern = $self->{PATTERN}->{y};
108 return $self->{COMPRESSIBLE};
117 if (exists $self->{ENUMS}->{$key}) {
118 return $self->{ENUMS}->{$key}->{CAPTION};
119 } elsif (exists $self->{CHRONS}->{$key}) {
120 return $self->{CHRONS}->{$key};
125 return $self->{CAPTION};
129 # If items are identified by chronology only, with no separate
130 # enumeration (eg, a newspaper issue), then the chronology is
131 # recorded in the enumeration subfields $a - $f. We can tell
132 # that this is the case if there are $a - $f subfields and no
133 # chronology subfields ($i-$k), and none of the $a-$f subfields
134 # have associated $u or $v subfields, but there are $w and $y
137 sub enumeration_is_chronology {
140 # There is always a '$a' subfield in well-formed fields.
141 return 0 if exists $self->{CHRONS}->{i};
143 foreach my $key ('a' .. 'f') {
146 last if !exists $self->{ENUMS}->{$key};
148 $enum = $self->{ENUMS}->{$key};
149 return 0 if defined $enum->{COUNT} || defined $enum->{RESTART};
152 return (exists $self->{PATTERN}->{w} && exists $self->{PATTERN}->{y});
165 my $daypat = '(mo|tu|we|th|fr|sa|su)';
166 my $weekpat = '(99|98|97|00|01|02|03|04|05)';
168 my $monthpat = '(01|02|03|04|05|06|07|08|09|10|11|12)';
169 my $seasonpat = '(21|22|23|24)';
171 # Initialize $weeknopat to be '(01|02|03|...|51|52|53)'
173 foreach my $weekno (1..52) {
174 $weeknopat .= sprintf("%02d|", $weekno);
181 # Translate daynames into day of week for DateTime
182 # also used to check if dayname is valid.
184 if (exists $daynames{$pat}) {
186 # figure out day of week for date and compare
187 my $dt = DateTime->new(year => $date[0],
190 return ($dt->day_of_week == $daynames{$pat});
191 } elsif (length($pat) == 2) {
193 return $pat == $date[3];
194 } elsif (length($pat) == 4) {
197 $mon = substr($pat, 0, 2);
198 $day = substr($pat, 2, 2);
200 return (($mon == $date[1]) && ($day == $date[2]));
202 carp "Invalid day pattern '$pat'";
207 # Calcuate date of "n"th last "dayname" of month: second last Tuesday
208 sub last_week_of_month {
212 my $end_dt = DateTime->last_day_of_month(year => $dt->year,
213 month => $dt->month);
215 $day = $daynames{$day};
216 while ($end_dt->day_of_week != $day) {
217 $end_dt->subtract(days => 1);
220 # 99: last week of month, 98: second last, etc.
221 for (my $i = 99 - $week; $i > 0; $i--) {
222 $end_dt->subtract(weeks => 1);
236 return (($dt->month == $month)
237 && (($dt->week_of_month == $weekno)
238 || ($dt->week_of_month == last_day_of_month($dt, $weekno, 'th')->week_of_month)));
242 if ($daynames{$day} != $dt->day_of_week) {
243 # if it's the wrong day of the week, rest doesn't matter
247 if (!defined $month) {
249 return (($dt->weekday_of_month == $weekno)
250 || ($dt->weekday_of_month == last_day_of_month($dt, $weekno, $day)->weekday_of_month));
254 if ($month != $dt->month) {
255 # If it's the wrong month, then we're done
259 # It's the right day of the week
260 # It's the right month
262 if ($weekno == $dt->weekday_of_month) {
263 # If this matches, then we're counting from the beginning
264 # of the month and it matches and we're done.
268 # only case left is that the week number is counting from
269 # the end of the month: eg, second last wednesday
270 return (last_week_of_month($weekno, $day)->weekday_of_month == $dt->weekday_of_month);
276 my $dt = DateTime->new(year => $date[0],
280 if ($pat =~ m/^$weekpat$daypat$/) {
281 # WWdd: 03we = Third Wednesday
282 return check_date($dt, undef, $1, $2);
283 } elsif ($pat =~ m/^$monthpat$weekpat$daypat$/) {
284 # MMWWdd: 0599tu Last Tuesday in May XXX WRITE ME
285 return check_date($dt, $1, $2, $3);
286 } elsif ($pat =~ m/^$monthpat$weekpat$/) {
287 # MMWW: 1204: Fourth week in December XXX WRITE ME
288 return check_date($dt, $1, $2, undef);
290 carp "invalid week pattern '$pat'";
299 return ($pat eq $date[1]);
306 return ($pat eq $date[1]);
319 'm' => \&match_month,
320 's' => \&match_season,
323 sub regularity_match {
328 foreach my $regularity ($self->{PATTERN}->{y}) {
329 next unless $regularity =~ m/^$pubcode/;
331 my $chroncode= substr($regularity, 1, 1);
332 my @pats = split(/,/, substr($regularity, 2));
335 foreach my $pat (@pats) {
336 if ($dispatch{$chroncode}->($pat, @date)) {
349 return $self->regularity_match('o', @date);
356 return $self->regularity_match('p', @date);
363 return $self->regularity_match('c', @date);