class Datacite::Mapping::DateValue
Represents a DataCite “date” value, which can be a year, date (year-month-day or just year-month), or ISO8601 datetime.
@!attribute [r] year
@return [Integer] The year.
@!attribute [r] month
@return [Integer, nil] The month. Can be `nil` if no month was specified.
@!attribute [r] day
@return [Integer, nil] The day. Can be `nil` if no day was specified.
@!attribute [r] hour
@return [Integer, nil] The hour. Can be `nil` if no hour was specified.
@!attribute [r] minute
@return [Integer, nil] The minutes. Can be `nil` if no minutes were specified.
@!attribute [r] sec
@return [Integer, nil] The seconds. Can be `nil` if no seconds were specified.
@!attribute [r] nsec
@return [Integer, nil] The nanoseconds. Can be `nil` if no nanoseconds were specified.
Attributes
date[R]
day[R]
hour[R]
iso_value[R]
minute[R]
month[R]
nsec[R]
sec[R]
year[R]
zone[R]
Public Class Methods
new(val)
click to toggle source
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)
# File lib/datacite/mapping/date_value.rb, line 34 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 raise ArgumentError, "Unable to parse date value '#{val}'" unless @year end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/datacite/mapping/date_value.rb, line 51 def <=>(other) return nil unless other.instance_of?(self.class) %i[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()
click to toggle source
# File lib/datacite/mapping/date_value.rb, line 61 def hash [year, month, day, hour, minute, sec, nsec, zone].hash end
to_s()
click to toggle source
# File lib/datacite/mapping/date_value.rb, line 65 def to_s @str_value = begin str_value = iso_value if nsec && nsec != 0 frac_str = (nsec / 1e9).to_s.sub('0', '') str_value.sub!(/(T[0-9]+:[0-9]+:[0-9]+)/, "\\1#{frac_str}") unless str_value.include?(frac_str) end str_value end end
Private Instance Methods
to_date(val)
click to toggle source
# File lib/datacite/mapping/date_value.rb, line 108 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
to_datetime(val)
click to toggle source
# File lib/datacite/mapping/date_value.rb, line 100 def to_datetime(val) return val if val.is_a?(DateTime) DateTime.parse(val.to_s) rescue ArgumentError nil end
to_day(val)
click to toggle source
# File lib/datacite/mapping/date_value.rb, line 93 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
to_month(val)
click to toggle source
# File lib/datacite/mapping/date_value.rb, line 86 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
to_year(val)
click to toggle source
# File lib/datacite/mapping/date_value.rb, line 78 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