Class: Datacite::Mapping::DateValue

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/datacite/mapping/date_value.rb

Overview

Represents a DataCite "date" value, which can be a year, date (year-month-day or just year-month), or ISO8601 datetime.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ DateValue

Parameters:

  • val (DateTime, Date, Integer, String)

    The value, as a DateTime, Date, or Integer, or as a String in any W3C DateTime format



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/datacite/mapping/date_value.rb', line 40

def initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  datetime = to_datetime(val)
  @date = datetime ? datetime.to_date : to_date(val)
  @year = to_year(val)
  @month = to_month(val)
  @day = to_day(val)
  iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
  if datetime && iso_value.include?('T')
    @hour = datetime.hour
    @minute = datetime.minute
    @sec = datetime.sec
    @nsec = datetime.to_time.nsec
    @zone = datetime.zone
  end
  fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
end

Instance Attribute Details

#date (readonly)

Returns the value of attribute date.



33
34
35
# File 'lib/datacite/mapping/date_value.rb', line 33

def date
  @date
end

#dayInteger? (readonly)

Returns The day. Can be nil if no day was specified.

Returns:

  • (Integer, nil)

    The day. Can be nil if no day was specified.



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
# File 'lib/datacite/mapping/date_value.rb', line 23

class DateValue
  include Comparable

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec
  attr_reader :date
  attr_reader :zone

  # Creates a new {DateValue}.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
    datetime = to_datetime(val)
    @date = datetime ? datetime.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if datetime && iso_value.include?('T')
      @hour = datetime.hour
      @minute = datetime.minute
      @sec = datetime.sec
      @nsec = datetime.to_time.nsec
      @zone = datetime.zone
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
  end

  def <=>(other)
    return nil unless other.class == self.class
    [:year, :month, :day, :hour, :minute, :sec, :nsec].each do |v|
      order = send(v) <=> other.send(v)
      return order if order.nonzero?
    end
    0
  end

  def hash
    [year, month, day, hour, minute, sec, nsec, zone].hash
  end

  def to_s
    'DateTime(' + [:year, :month, :day, :hour, :minute, :sec, :nsec, :zone].map do |field|
      value = send(field)
      value = format('%02d', value) if value && value.is_a?(Integer)
      "#{field}: #{value}" if value
    end.compact.join(', ') + ')'
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError
    nil
  end

end

#hourInteger? (readonly)

Returns The hour. Can be nil if no hour was specified.

Returns:

  • (Integer, nil)

    The hour. Can be nil if no hour was specified.



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
# File 'lib/datacite/mapping/date_value.rb', line 23

class DateValue
  include Comparable

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec
  attr_reader :date
  attr_reader :zone

  # Creates a new {DateValue}.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
    datetime = to_datetime(val)
    @date = datetime ? datetime.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if datetime && iso_value.include?('T')
      @hour = datetime.hour
      @minute = datetime.minute
      @sec = datetime.sec
      @nsec = datetime.to_time.nsec
      @zone = datetime.zone
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
  end

  def <=>(other)
    return nil unless other.class == self.class
    [:year, :month, :day, :hour, :minute, :sec, :nsec].each do |v|
      order = send(v) <=> other.send(v)
      return order if order.nonzero?
    end
    0
  end

  def hash
    [year, month, day, hour, minute, sec, nsec, zone].hash
  end

  def to_s
    'DateTime(' + [:year, :month, :day, :hour, :minute, :sec, :nsec, :zone].map do |field|
      value = send(field)
      value = format('%02d', value) if value && value.is_a?(Integer)
      "#{field}: #{value}" if value
    end.compact.join(', ') + ')'
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError
    nil
  end

end

#minuteInteger? (readonly)

Returns The minutes. Can be nil if no minutes were specified.

Returns:

  • (Integer, nil)

    The minutes. Can be nil if no minutes were specified.



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
# File 'lib/datacite/mapping/date_value.rb', line 23

class DateValue
  include Comparable

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec
  attr_reader :date
  attr_reader :zone

  # Creates a new {DateValue}.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
    datetime = to_datetime(val)
    @date = datetime ? datetime.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if datetime && iso_value.include?('T')
      @hour = datetime.hour
      @minute = datetime.minute
      @sec = datetime.sec
      @nsec = datetime.to_time.nsec
      @zone = datetime.zone
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
  end

  def <=>(other)
    return nil unless other.class == self.class
    [:year, :month, :day, :hour, :minute, :sec, :nsec].each do |v|
      order = send(v) <=> other.send(v)
      return order if order.nonzero?
    end
    0
  end

  def hash
    [year, month, day, hour, minute, sec, nsec, zone].hash
  end

  def to_s
    'DateTime(' + [:year, :month, :day, :hour, :minute, :sec, :nsec, :zone].map do |field|
      value = send(field)
      value = format('%02d', value) if value && value.is_a?(Integer)
      "#{field}: #{value}" if value
    end.compact.join(', ') + ')'
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError
    nil
  end

end

#monthInteger? (readonly)

Returns The month. Can be nil if no month was specified.

Returns:

  • (Integer, nil)

    The month. Can be nil if no month was specified.



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
# File 'lib/datacite/mapping/date_value.rb', line 23

class DateValue
  include Comparable

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec
  attr_reader :date
  attr_reader :zone

  # Creates a new {DateValue}.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
    datetime = to_datetime(val)
    @date = datetime ? datetime.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if datetime && iso_value.include?('T')
      @hour = datetime.hour
      @minute = datetime.minute
      @sec = datetime.sec
      @nsec = datetime.to_time.nsec
      @zone = datetime.zone
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
  end

  def <=>(other)
    return nil unless other.class == self.class
    [:year, :month, :day, :hour, :minute, :sec, :nsec].each do |v|
      order = send(v) <=> other.send(v)
      return order if order.nonzero?
    end
    0
  end

  def hash
    [year, month, day, hour, minute, sec, nsec, zone].hash
  end

  def to_s
    'DateTime(' + [:year, :month, :day, :hour, :minute, :sec, :nsec, :zone].map do |field|
      value = send(field)
      value = format('%02d', value) if value && value.is_a?(Integer)
      "#{field}: #{value}" if value
    end.compact.join(', ') + ')'
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError
    nil
  end

end

#nsecInteger? (readonly)

Returns The nanoseconds. Can be nil if no nanoseconds were specified.

Returns:

  • (Integer, nil)

    The nanoseconds. Can be nil if no nanoseconds were specified.



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
# File 'lib/datacite/mapping/date_value.rb', line 23

class DateValue
  include Comparable

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec
  attr_reader :date
  attr_reader :zone

  # Creates a new {DateValue}.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
    datetime = to_datetime(val)
    @date = datetime ? datetime.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if datetime && iso_value.include?('T')
      @hour = datetime.hour
      @minute = datetime.minute
      @sec = datetime.sec
      @nsec = datetime.to_time.nsec
      @zone = datetime.zone
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
  end

  def <=>(other)
    return nil unless other.class == self.class
    [:year, :month, :day, :hour, :minute, :sec, :nsec].each do |v|
      order = send(v) <=> other.send(v)
      return order if order.nonzero?
    end
    0
  end

  def hash
    [year, month, day, hour, minute, sec, nsec, zone].hash
  end

  def to_s
    'DateTime(' + [:year, :month, :day, :hour, :minute, :sec, :nsec, :zone].map do |field|
      value = send(field)
      value = format('%02d', value) if value && value.is_a?(Integer)
      "#{field}: #{value}" if value
    end.compact.join(', ') + ')'
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError
    nil
  end

end

#secInteger? (readonly)

Returns The seconds. Can be nil if no seconds were specified.

Returns:

  • (Integer, nil)

    The seconds. Can be nil if no seconds were specified.



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
# File 'lib/datacite/mapping/date_value.rb', line 23

class DateValue
  include Comparable

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec
  attr_reader :date
  attr_reader :zone

  # Creates a new {DateValue}.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
    datetime = to_datetime(val)
    @date = datetime ? datetime.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if datetime && iso_value.include?('T')
      @hour = datetime.hour
      @minute = datetime.minute
      @sec = datetime.sec
      @nsec = datetime.to_time.nsec
      @zone = datetime.zone
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
  end

  def <=>(other)
    return nil unless other.class == self.class
    [:year, :month, :day, :hour, :minute, :sec, :nsec].each do |v|
      order = send(v) <=> other.send(v)
      return order if order.nonzero?
    end
    0
  end

  def hash
    [year, month, day, hour, minute, sec, nsec, zone].hash
  end

  def to_s
    'DateTime(' + [:year, :month, :day, :hour, :minute, :sec, :nsec, :zone].map do |field|
      value = send(field)
      value = format('%02d', value) if value && value.is_a?(Integer)
      "#{field}: #{value}" if value
    end.compact.join(', ') + ')'
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError
    nil
  end

end

#yearInteger (readonly)

Returns The year.

Returns:

  • (Integer)

    The year.



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
# File 'lib/datacite/mapping/date_value.rb', line 23

class DateValue
  include Comparable

  attr_reader :year
  attr_reader :month
  attr_reader :day
  attr_reader :hour
  attr_reader :minute
  attr_reader :sec
  attr_reader :nsec
  attr_reader :date
  attr_reader :zone

  # Creates a new {DateValue}.
  #
  # @param val [DateTime, Date, Integer, String] The value, as a `DateTime`, `Date`, or `Integer`,
  #   or as a `String` in any [W3C DateTime format](http://www.w3.org/TR/NOTE-datetime)
  def initialize(val) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
    datetime = to_datetime(val)
    @date = datetime ? datetime.to_date : to_date(val)
    @year = to_year(val)
    @month = to_month(val)
    @day = to_day(val)
    iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
    if datetime && iso_value.include?('T')
      @hour = datetime.hour
      @minute = datetime.minute
      @sec = datetime.sec
      @nsec = datetime.to_time.nsec
      @zone = datetime.zone
    end
    fail ArgumentError, "Unable to parse date value '#{val}'" unless @year
  end

  def <=>(other)
    return nil unless other.class == self.class
    [:year, :month, :day, :hour, :minute, :sec, :nsec].each do |v|
      order = send(v) <=> other.send(v)
      return order if order.nonzero?
    end
    0
  end

  def hash
    [year, month, day, hour, minute, sec, nsec, zone].hash
  end

  def to_s
    'DateTime(' + [:year, :month, :day, :hour, :minute, :sec, :nsec, :zone].map do |field|
      value = send(field)
      value = format('%02d', value) if value && value.is_a?(Integer)
      "#{field}: #{value}" if value
    end.compact.join(', ') + ')'
  end

  private

  def to_year(val)
    return val if val.is_a?(Integer)
    return val.year if val.respond_to?(:year)
    matchdata = val.to_s.match(/^[0-9]+/)
    matchdata[0].to_i if matchdata
  end

  def to_month(val)
    return val.month if val.respond_to?(:month)
    matchdata = val.to_s.match(/^[0-9]+-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_day(val)
    return val.day if val.respond_to?(:day)
    matchdata = val.to_s.match(/^[0-9]+-[0-9]{2}-([0-9]{2})(?![0-9])/)
    matchdata[1].to_i if matchdata
  end

  def to_datetime(val)
    return val if val.is_a?(DateTime)
    DateTime.parse(val.to_s)
  rescue ArgumentError
    nil
  end

  def to_date(val)
    return val if val.is_a?(::Date)
    return ::Date.parse(val.iso8601) if val.respond_to?(:iso8601)
    ::Date.parse(val.to_s)
  rescue ArgumentError
    nil
  end

end

#zone (readonly)

Returns the value of attribute zone.



34
35
36
# File 'lib/datacite/mapping/date_value.rb', line 34

def zone
  @zone
end

Instance Method Details

#<=>(other)



57
58
59
60
61
62
63
64
# File 'lib/datacite/mapping/date_value.rb', line 57

def <=>(other)
  return nil unless other.class == self.class
  [:year, :month, :day, :hour, :minute, :sec, :nsec].each do |v|
    order = send(v) <=> other.send(v)
    return order if order.nonzero?
  end
  0
end

#hash



66
67
68
# File 'lib/datacite/mapping/date_value.rb', line 66

def hash
  [year, month, day, hour, minute, sec, nsec, zone].hash
end

#to_s



70
71
72
73
74
75
76
# File 'lib/datacite/mapping/date_value.rb', line 70

def to_s
  'DateTime(' + [:year, :month, :day, :hour, :minute, :sec, :nsec, :zone].map do |field|
    value = send(field)
    value = format('%02d', value) if value && value.is_a?(Integer)
    "#{field}: #{value}" if value
  end.compact.join(', ') + ')'
end