Sha256: 99ac1899ddf766891c2ca0f59ca78b4926c6956ecce717b4417a021105626708

Contents?: true

Size: 807 Bytes

Versions: 24

Compression:

Stored size: 807 Bytes

Contents

# frozen_string_literal: true

class PublifyTime
  def self.delta(year = nil, month = nil, day = nil)
    return nil if year.nil? && month.nil? && day.nil?

    year = year.to_i unless year.nil?
    month = month.to_i unless month.nil?
    day = day.to_i unless day.nil?
    return nil if year.zero?

    from = Time.zone.local(year, month, day)
    to = from.end_of_year
    to = from.end_of_month if month.present?
    to = from.end_of_day if day.present?
    from..to
  end

  def self.delta_like(str)
    case str
    when /(\d{4})-(\d{2})-(\d{2})/
      delta(Regexp.last_match(1), Regexp.last_match(2), Regexp.last_match(3))

    when /(\d{4})-(\d{2})/
      delta(Regexp.last_match(1), Regexp.last_match(2))

    when /(\d{4})/
      delta(Regexp.last_match(1))

    else
      str
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
publify_core-10.0.2 lib/publify_time.rb
HornsAndHooves-publify_core-10.5.0 lib/publify_time.rb
HornsAndHooves-publify_core-10.4.0 lib/publify_time.rb
HornsAndHooves-publify_core-10.3.0 lib/publify_time.rb
HornsAndHooves-publify_core-10.2.0 lib/publify_time.rb
publify_core-10.0.1 lib/publify_time.rb
publify_core-10.0.0 lib/publify_time.rb
publify_core-9.2.10 lib/publify_time.rb
HornsAndHooves-publify_core-10.1.1 lib/publify_time.rb
HornsAndHooves-publify_core-10.1.0 lib/publify_time.rb
HornsAndHooves-publify_core-10.0.3 lib/publify_time.rb
HornsAndHooves-publify_core-10.0.2 lib/publify_time.rb
HornsAndHooves-publify_core-10.0.1 lib/publify_time.rb
HornsAndHooves-publify_core-10.0.0 lib/publify_time.rb
publify_core-9.2.9 lib/publify_time.rb
publify_core-9.2.8 lib/publify_time.rb
publify_core-9.2.7 lib/publify_time.rb
publify_core-9.2.6 lib/publify_time.rb
publify_core-9.2.5 lib/publify_time.rb
publify_core-9.2.4 lib/publify_time.rb