Module: Puree::Date

Defined in:
lib/puree/date.rb

Class Method Summary collapse

Class Method Details

.iso(data) ⇒ String

Converts a date with three components (year, month, day) to ISO8601 format

Parameters:

  • data (Hash)

Returns:

  • (String)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puree/date.rb', line 9

def self.iso(data)
  iso_date = ''
  year =  data['year']
  month = data['month']
  day = data['day']
  if !year.empty?
    iso_date << year
  else
    iso_date
  end
  if !month.empty?
    # Add leading zero to convert to ISO 8601
    if month.length < 2
      month.insert(0, '0')
    end
    iso_date << '-' + month
  else
    iso_date
  end
  if !day.empty?
    # Add leading zero to convert to ISO 8601
    if day.length < 2
      day.insert(0, '0')
    end
    iso_date << '-' + day
  end
  iso_date
end