8 use base 'MARC::Field';
13 my $class = ref($proto) || $proto;
15 my $last_enum = undef;
17 $self->{_mfhdc_ENUMS} = {};
18 $self->{_mfhdc_CHRONS} = {};
19 $self->{_mfhdc_PATTERN} = {};
20 $self->{_mfhdc_COPY} = undef;
21 $self->{_mfhdc_UNIT} = undef;
22 $self->{_mfhdc_COMPRESSIBLE} = 1; # until proven otherwise
24 foreach my $subfield ($self->subfields) {
25 my ($key, $val) = @$subfield;
28 } elsif ($key =~ /[a-h]/) {
29 # Enumeration Captions
30 $self->{_mfhdc_ENUMS}->{$key} = {CAPTION => $val,
38 } elsif ($key =~ /[i-m]/) {
40 $self->{_mfhdc_CHRONS}->{$key} = $val;
41 } elsif ($key eq 'u') {
42 # Bib units per next higher enumeration level
43 carp('$u specified for top-level enumeration')
44 unless defined($last_enum);
45 $self->{_mfhdc_ENUMS}->{$last_enum}->{COUNT} = $val;
46 } elsif ($key eq 'v') {
47 carp '$v specified for top-level enumeration'
48 unless defined($last_enum);
49 $self->{_mfhdc_ENUMS}->{$last_enum}->{RESTART} = ($val eq 'r');
50 } elsif ($key =~ /[npwz]/) {
51 # Publication Pattern info ('o' == type of unit, 'q'..'t' undefined)
52 $self->{_mfhdc_PATTERN}->{$key} = $val;
53 } elsif ($key =~ /x/) {
54 # Calendar change can have multiple comma-separated values
55 $self->{_mfhdc_PATTERN}->{x} = [split /,/, $val];
56 } elsif ($key eq 'y') {
57 # Publication pattern: 'y' is repeatable
58 $self->{_mfhdc_PATTERN}->{y} = [] if (!defined $self->{_mfhdc_PATTERN}->{y});
59 push @{$self->{_mfhdc_PATTERN}->{y}}, $val;
60 } elsif ($key eq 'o') {
62 $self->{_mfhdc_UNIT} = $val;
63 } elsif ($key eq 't') {
64 $self->{_mfhdc_COPY} = $val;
66 carp "Unknown caption subfield '$key'";
70 # subsequent levels of enumeration (primary and alternate)
71 # If an enumeration level doesn't document the number
72 # of "issues" per "volume", or whether numbering of issues
73 # restarts, then we can't compress.
74 foreach my $key ('b', 'c', 'd', 'e', 'f', 'h') {
75 if (exists $self->{_mfhdc_ENUMS}->{$key}) {
76 my $pattern = $self->{_mfhdc_ENUMS}->{$key};
77 if (!$pattern->{RESTART} || !$pattern->{COUNT}
78 || ($pattern->{COUNT} eq 'var')
79 || ($pattern->{COUNT} eq 'und')) {
80 $self->{_mfhdc_COMPRESSIBLE} = 0;
86 # If there's a $x subfield and a $j, then it's compressible
87 if (exists $self->{_mfhdc_PATTERN}->{x} && exists $self->{_mfhdc_CHRONS}->{'j'}) {
88 $self->{_mfhdc_COMPRESSIBLE} = 1;
91 bless ($self, $class);
93 if (exists $self->{_mfhdc_PATTERN}->{y}) {
94 $self->decode_pattern;
102 my $pattern = $self->{_mfhdc_PATTERN}->{y};
110 return $self->{_mfhdc_COMPRESSIBLE};
117 if (exists $self->{_mfhdc_CHRONS}->{$key}) {
118 return $self->{_mfhdc_CHRONS}->{$key};
128 if (exists $self->{_mfhdc_ENUMS}->{$key}) {
129 return $self->{_mfhdc_ENUMS}->{$key};
130 } elsif (exists $self->{_mfhdc_CHRONS}->{$key}) {
131 return $self->{_mfhdc_CHRONS}->{$key};
140 my $val = $self->capfield($key);
143 return $val->{CAPTION};
149 sub calendar_change {
152 return $self->{_mfhdc_PATTERN}->{x};
155 # If items are identified by chronology only, with no separate
156 # enumeration (eg, a newspaper issue), then the chronology is
157 # recorded in the enumeration subfields $a - $f. We can tell
158 # that this is the case if there are $a - $f subfields and no
159 # chronology subfields ($i-$k), and none of the $a-$f subfields
160 # have associated $u or $v subfields, but there are $w and $y
163 sub enumeration_is_chronology {
166 # There is always a '$a' subfield in well-formed fields.
167 return 0 if exists $self->{_mfhdc_CHRONS}->{i};
169 foreach my $key ('a' .. 'f') {
172 last if !exists $self->{_mfhdc_ENUMS}->{$key};
174 $enum = $self->{_mfhdc_ENUMS}->{$key};
175 return 0 if defined $enum->{COUNT} || defined $enum->{RESTART};
178 return (exists $self->{_mfhdc_PATTERN}->{w} && exists $self->{_mfhdc_PATTERN}->{y});
191 my $daypat = '(mo|tu|we|th|fr|sa|su)';
192 my $weekpat = '(99|98|97|00|01|02|03|04|05)';
194 my $monthpat = '(01|02|03|04|05|06|07|08|09|10|11|12)';
195 my $seasonpat = '(21|22|23|24)';
197 # Initialize $weeknopat to be '(01|02|03|...|51|52|53)'
199 foreach my $weekno (1..52) {
200 $weeknopat .= sprintf('%02d|', $weekno);
207 # Translate daynames into day of week for DateTime
208 # also used to check if dayname is valid.
210 if (exists $daynames{$pat}) {
212 # figure out day of week for date and compare
213 my $dt = DateTime->new(year => $date[0],
216 return ($dt->day_of_week == $daynames{$pat});
217 } elsif (length($pat) == 2) {
219 return $pat == $date[3];
220 } elsif (length($pat) == 4) {
223 $mon = substr($pat, 0, 2);
224 $day = substr($pat, 2, 2);
226 return (($mon == $date[1]) && ($day == $date[2]));
228 carp "Invalid day pattern '$pat'";
233 # Calcuate date of "n"th last "dayname" of month: second last Tuesday
234 sub last_week_of_month {
238 my $end_dt = DateTime->last_day_of_month(year => $dt->year,
239 month => $dt->month);
241 $day = $daynames{$day};
242 while ($end_dt->day_of_week != $day) {
243 $end_dt->subtract(days => 1);
246 # 99: last week of month, 98: second last, etc.
247 for (my $i = 99 - $week; $i > 0; $i--) {
248 $end_dt->subtract(weeks => 1);
262 return (($dt->month == $month)
263 && (($dt->week_of_month == $weekno)
264 || ($dt->week_of_month == last_day_of_month($dt, $weekno, 'th')->week_of_month)));
268 if ($daynames{$day} != $dt->day_of_week) {
269 # if it's the wrong day of the week, rest doesn't matter
273 if (!defined $month) {
275 return (($dt->weekday_of_month == $weekno)
276 || ($dt->weekday_of_month == last_day_of_month($dt, $weekno, $day)->weekday_of_month));
280 if ($month != $dt->month) {
281 # If it's the wrong month, then we're done
285 # It's the right day of the week
286 # It's the right month
288 if ($weekno == $dt->weekday_of_month) {
289 # If this matches, then we're counting from the beginning
290 # of the month and it matches and we're done.
294 # only case left is that the week number is counting from
295 # the end of the month: eg, second last wednesday
296 return (last_week_of_month($weekno, $day)->weekday_of_month == $dt->weekday_of_month);
302 my $dt = DateTime->new(year => $date[0],
306 if ($pat =~ m/^$weekpat$daypat$/) {
307 # WWdd: 03we = Third Wednesday
308 return check_date($dt, undef, $1, $2);
309 } elsif ($pat =~ m/^$monthpat$weekpat$daypat$/) {
310 # MMWWdd: 0599tu Last Tuesday in May XXX WRITE ME
311 return check_date($dt, $1, $2, $3);
312 } elsif ($pat =~ m/^$monthpat$weekpat$/) {
313 # MMWW: 1204: Fourth week in December XXX WRITE ME
314 return check_date($dt, $1, $2, undef);
316 carp "invalid week pattern '$pat'";
325 return ($pat eq $date[1]);
332 return ($pat eq $date[1]);
345 'm' => \&match_month,
346 's' => \&match_season,
349 sub regularity_match {
354 foreach my $regularity ($self->{_mfhdc_PATTERN}->{y}) {
355 next unless $regularity =~ m/^$pubcode/;
357 my $chroncode= substr($regularity, 1, 1);
358 my @pats = split(/,/, substr($regularity, 2));
361 foreach my $pat (@pats) {
362 if ($dispatch{$chroncode}->($pat, @date)) {
375 return $self->regularity_match('o', @date);
382 return $self->regularity_match('p', @date);
389 return $self->regularity_match('c', @date);