Module: Bolognese::DateUtils

Included in:
Metadata
Defined in:
lib/bolognese/date_utils.rb

Instance Method Summary collapse

Instance Method Details

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



35
36
37
# File 'lib/bolognese/date_utils.rb', line 35

def get_date_from_parts(year, month = nil, day = nil)
  [year.to_s.rjust(4, '0'), month.to_s.rjust(2, '0'), day.to_s.rjust(2, '0')].reject { |part| part == "00" }.join("-")
end

#get_date_parts(iso8601_time) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/bolognese/date_utils.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, month = nil, day = nil) ⇒ Object



31
32
33
# File 'lib/bolognese/date_utils.rb', line 31

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

#get_datetime_from_iso8601(iso8601_time) ⇒ Object

parsing of incomplete iso8601 timestamps such as 2015-04 is broken in standard library return nil if invalid iso8601 timestamp



42
43
44
45
46
# File 'lib/bolognese/date_utils.rb', line 42

def get_datetime_from_iso8601(iso8601_time)
  ISO8601::DateTime.new(iso8601_time).to_time.utc
rescue
  nil
end

#get_year_month(iso8601_time) ⇒ Object



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

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

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

  [year.to_i, month.to_i].reject { |part| part == 0 }
end

#get_year_month_day(iso8601_time) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/bolognese/date_utils.rb', line 21

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.to_i, month.to_i, day.to_i].reject { |part| part == 0 }
end