Sha256: c0fcbae6c2ba1a2da980a90fea47d705b08de9736e1bc8681e0920f6192c6d65

Contents?: true

Size: 774 Bytes

Versions: 3

Compression:

Stored size: 774 Bytes

Contents

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

3 entries across 3 versions & 1 rubygems

Version Path
publify_core-9.1.0 lib/publify_time.rb
publify_core-9.0.1 lib/publify_time.rb
publify_core-9.0.0 lib/publify_time.rb