Class: PennMARC::Date

Inherits:
Helper
  • Object
show all
Defined in:
lib/pennmarc/helpers/date.rb

Overview

Parser methods for extracting date info as DateTime objects

Constant Summary

Constants included from Util

Util::TRAILING_PUNCTUATIONS_PATTERNS

Class Method Summary collapse

Methods included from Util

#append_relator, #append_trailing, #datafield_and_linked_alternate, #field_defined?, #field_or_its_linked_alternate?, #join_and_squish, #join_subfields, #linked_alternate, #linked_alternate_not_6_or_8, #no_subfield_value_matches?, #prefixed_subject_and_alternate, #relator, #relator_join_separator, #relator_term_subfield, #remove_paren_value_from_subfield_i, #subfield_defined?, #subfield_in?, #subfield_not_in?, #subfield_undefined?, #subfield_value?, #subfield_value_in?, #subfield_value_not_in?, #subfield_values, #subfield_values_for, #substring_after, #substring_before, #translate_relator, #trim_punctuation, #trim_trailing, #trim_trailing!, #valid_subject_genre_source_code?

Class Method Details

.added(record) ⇒ Time?

Retrieve date added (subfield ‘q’) from enriched marc ‘itm’ field. Enriched maps enriched marc fields and subfields created during Alma publishing. The enriched metadata provided by the Alma API does not include the date created value, so we can’t work with that here.

Parameters:

  • record (MARC::Record)

Returns:

  • (Time, nil)

    The date added, or nil if date found in record is invalid



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pennmarc/helpers/date.rb', line 27

def added(record)
  record.fields(Enriched::Pub::ITEM_TAG).flat_map { |field|
    subfield_values(field, Enriched::Pub::ITEM_DATE_CREATED).filter_map do |date_added|
      # On 2022-05-02, this field value (as exported in enriched publishing
      # job from Alma) began truncating time to day-level granularity. We have
      # no guarantee that this won't switch back in the future, so for the
      # foreseeable future we should support both formats.

      format = date_added.size == 10 ? '%Y-%m-%d' : '%Y-%m-%d %H:%M:%S'

      Time.strptime(date_added, format)
    rescue StandardError => e
      puts 'Error parsing date in date added subfield. ' \
           "mmsid: #{Identifier.mmsid(record)}, value: #{date_added}, error: #{e}"
      nil
    end
  }.max
end

.last_updated(record) ⇒ Time?

Retrieve date last updated from 005 field. Date last updated is a sixteen character String recorded in ISO 8601 format.

Parameters:

  • record (MARC::Record)

Returns:

  • (Time, nil)

    The date last updated, or nil if date found in record is invalid



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pennmarc/helpers/date.rb', line 51

def last_updated(record)
  record.fields('005').filter_map { |field|
    begin
      date_time_string = field.value

      next if date_time_string.blank?

      next if date_time_string.start_with?('0000')

      Time.strptime(date_time_string, '%Y%m%d%H%M%S.%N')
    rescue StandardError => e
      puts 'Error parsing last updated date. ' \
           "mmsid: #{Identifier.mmsid(record)}, value: #{date_time_string}, error: #{e}"
      nil
    end
  }.first
end

.publication(record) ⇒ Time?

Retrieve publication date (Date 1) from 008 field. Publication date is a four-digit year found in position 7-10 and may contain ‘u’ characters to represent partially known dates. We replace any occurrences of ‘u’ with ‘0’ before converting to DateTime object.

Parameters:

  • record (MARC::Record)

Returns:

  • (Time, nil)

    The publication date, or nil if date found in record is invalid



12
13
14
15
16
17
18
19
20
# File 'lib/pennmarc/helpers/date.rb', line 12

def publication(record)
  record.fields('008').filter_map { |field|
    four_digit_year = sanitize_partially_known_date(field.value[7, 4], '0')

    next if four_digit_year.blank?

    Time.new(four_digit_year.to_i)
  }.first
end