Class: PublifyTime

Inherits:
Object
  • Object
show all
Defined in:
lib/publify_time.rb

Class Method Summary collapse

Class Method Details

.delta(year = nil, month = nil, day = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/publify_time.rb', line 4

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

.delta_like(str) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/publify_time.rb', line 19

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