Module: Puree::Date

Defined in:
lib/puree/date.rb

Overview

Date utilities

Class Method Summary collapse

Class Method Details

.iso(data) ⇒ String

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



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
37
38
# File 'lib/puree/date.rb', line 11

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 date component
    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 date component
    if day.length < 2
      day.insert(0, '0')
    end
    iso_date << '-' + day
  end
  iso_date
end