Sha256: cc1d5a41da71dd32eed051991968e71144d697bef7ff4b15a8d175b7a84f92c8

Contents?: true

Size: 1.15 KB

Versions: 12

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: false

module Origami
  class Date < LiteralString
    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

    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
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-initiatives-0.27.9 lib/gem_overrides/origami/date.rb
decidim-initiatives-0.27.8 lib/gem_overrides/origami/date.rb
decidim-initiatives-0.27.7 lib/gem_overrides/origami/date.rb
decidim-initiatives-0.27.6 lib/gem_overrides/origami/date.rb
decidim-initiatives-0.27.5 lib/gem_overrides/origami/date.rb
decidim-initiatives-0.27.4 lib/gem_overrides/origami/date.rb
decidim-initiatives-0.27.3 lib/gem_overrides/origami/date.rb
decidim-initiatives-0.27.2 lib/gem_overrides/origami/date.rb
decidim-initiatives-0.27.1 lib/gem_overrides/origami/date.rb
decidim-initiatives-0.27.0 lib/gem_overrides/origami/date.rb
decidim-initiatives-0.27.0.rc2 lib/gem_overrides/origami/date.rb
decidim-initiatives-0.27.0.rc1 lib/gem_overrides/origami/date.rb