Class: Origami::Date

Inherits:
LiteralString
  • Object
show all
Defined in:
decidim-initiatives/lib/gem_overrides/origami/date.rb

Class Method Summary collapse

Class Method Details

.nowObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'decidim-initiatives/lib/gem_overrides/origami/date.rb', line 30

def self.now
  now = Time.now.utc

  date =
    {
      year: now.strftime("%Y").to_i,
      month: now.strftime("%m").to_i,
      day: now.strftime("%d").to_i,
      hour: now.strftime("%H").to_i,
      min: now.strftime("%M").to_i,
      sec: now.strftime("%S").to_i,
      utc_offset: now.utc_offset
    }

  Origami::Date.new(**date)
end

.parse(str) ⇒ Object

Raises:

  • (InvalidDateError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'decidim-initiatives/lib/gem_overrides/origami/date.rb', line 5

def self.parse(str)
  #:nodoc:
  raise InvalidDateError, "Not a valid Date string" unless str =~ REGEXP_TOKEN

  date =
    {
      year: $~['year'].to_i
    }

  date[:month] = $~['month'].to_i if $~['month']
  date[:day] = $~['day'].to_i if $~['day']
  date[:hour] = $~['hour'].to_i if $~['hour']
  date[:min] = $~['min'].to_i if $~['min']
  date[:sec] = $~['sec'].to_i if $~['sec']

  if %w[+ -].include?($~['ut'])
    utc_offset = $~['ut_hour_off'].to_i * 3600 + $~['ut_min_off'].to_i * 60
    utc_offset = -utc_offset if $~['ut'] == '-'

    date[:utc_offset] = utc_offset
  end

  Origami::Date.new(**date)
end