Class: Wikidata::Property::Time

Inherits:
Base
  • Object
show all
Defined in:
lib/wikidata/property/time.rb

Constant Summary collapse

DATE_PRECISION =
{
  0 => { key: nil, value: 1_000_000_000 * 365 * 24 * 3600 },
  1 => { key: nil, value: 100_000_000 * 365 * 24 * 3600 },
  2 => { key: nil, value: 10_000_000 * 365 * 24 * 3600 },
  3 => { key: nil, value: 1_000_000 * 365 * 24 * 3600 },
  4 => { key: nil, value: 100_000 * 365 * 24 * 3600 },
  5 => { key: nil, value: 10_000 * 365 * 24 * 3600 },
  6 => { key: nil, value: 1000 * 365 * 24 * 3600 },
  7 => { key: :century, value: 100 * 365 * 24 * 3600 },
  8 => { key: :decade, value: 10 * 365 * 24 * 3600 },
  9 => { key: :year, value: 365 * 24 * 3600 },
  10 => { key: :month, value: 30 * 24 * 3600 },
  11 => { key: :day, value: 24 * 3600 },
  12 => { key: nil, value: 3600 },
  13 => { key: nil, value: 60 },
  14 => { key: nil, value: 1 }
}.freeze
DAYS_IN_MONTH =
{
  1 => 31,
  2 => 28,
  3 => 31,
  4 => 30,
  5 => 31,
  6 => 30,
  7 => 31,
  8 => 31,
  9 => 30,
  10 => 31,
  11 => 30,
  12 => 31
}

Instance Attribute Summary

Attributes inherited from Base

#property

Instance Method Summary collapse

Methods inherited from Base

#initialize, #value

Constructor Details

This class inherits a constructor from Wikidata::Property::Base

Instance Method Details

#afterObject



61
62
63
# File 'lib/wikidata/property/time.rb', line 61

def after
  value.after
end

#beforeObject



65
66
67
# File 'lib/wikidata/property/time.rb', line 65

def before
  value.before
end

#dateObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wikidata/property/time.rb', line 37

def date
  return @_date if @_date

  d = ::Hash[%i[year month day hour min sec].zip(
    value.time.scan(/(-?\d+)-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/).first.map(&:to_i)
  )]
  %i[month day].each do |k|
    d[k] = (d[k] == 0 ? 1 : d[k])
  end
  @_date ||= DateTime.new(*d.values)
end

#precisionObject



53
54
55
# File 'lib/wikidata/property/time.rb', line 53

def precision
  DATE_PRECISION[value.precision.to_i][:value]
end

#precision_keyObject



57
58
59
# File 'lib/wikidata/property/time.rb', line 57

def precision_key
  DATE_PRECISION[value.precision.to_i][:key]
end

#rangeObject



69
70
71
72
73
74
75
76
77
# File 'lib/wikidata/property/time.rb', line 69

def range
  return @_range if @_range

  if before.to_i == 0 && after.to_i == 0 && precision_key
    send(:"#{precision_key}_range")
  else
    generic_range
  end
end

#timestampObject



49
50
51
# File 'lib/wikidata/property/time.rb', line 49

def timestamp
  date.to_time.utc.to_i
end