Module: OrcidClient::Date

Included in:
Notification, Work
Defined in:
lib/orcid_client/date.rb

Instance Method Summary collapse

Instance Method Details

#get_date_parts(iso8601_time) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/orcid_client/date.rb', line 3

def get_date_parts(iso8601_time)
  return { "date_parts" => [[]] } if iso8601_time.nil?

  year = iso8601_time[0..3].to_i
  month = iso8601_time[5..6].to_i
  day = iso8601_time[8..9].to_i
  { 'date-parts' => [[year, month, day].reject { |part| part == 0 }] }
end

#get_date_parts_from_parts(year = nil, month = nil, day = nil) ⇒ Object



21
22
23
# File 'lib/orcid_client/date.rb', line 21

def get_date_parts_from_parts(year = nil, month = nil, day = nil)
  { 'date-parts' => [[year.to_i, month.to_i, day.to_i].reject { |part| part == 0 }] }
end

#get_iso8601_from_epoch(epoch) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/orcid_client/date.rb', line 52

def get_iso8601_from_epoch(epoch)
  return nil if epoch.blank?

  # handle milliseconds
  epoch = epoch.to_i
  epoch = epoch / 1000 if epoch > 9999999999
  Time.at(epoch).utc.iso8601
end

#get_iso8601_from_time(time) ⇒ Object



46
47
48
49
50
# File 'lib/orcid_client/date.rb', line 46

def get_iso8601_from_time(time)
  return nil if time.blank?

  Time.zone.parse(time.to_s).utc.iso8601
end

#get_parts_from_date_parts(date_parts) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/orcid_client/date.rb', line 25

def get_parts_from_date_parts(date_parts)
  parts = date_parts.fetch('date-parts', []).first
  return { "date_parts" => [[]] } unless parts.present?

  { 'year' => parts[0],
    'month' => parts[1],
    'day' => parts[2] }.compact
end

#get_year_month(iso8601_time) ⇒ Object



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

def get_year_month(iso8601_time)
  return [nil, nil] if iso8601_time.nil?

  year = iso8601_time[0..3].to_i
  month = iso8601_time[5..6].to_i

  [year, month]
end

#get_year_month_day(iso8601_time) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/orcid_client/date.rb', line 34

def get_year_month_day(iso8601_time)
  return [] if iso8601_time.nil?

  year = iso8601_time[0..3]
  month = iso8601_time[5..6]
  day = iso8601_time[8..9]

  { 'year' => year,
    'month' => month,
    'day' => day }.compact
end