Module: Jekyll::TimeSince

Defined in:
lib/jekyll_time_since.rb

Overview

Instance Method Summary collapse

Instance Method Details

#days_since(string) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/jekyll_time_since.rb', line 46

def days_since(string)
  string += '-01-01' unless string.include?('-')
  date1 = Date.parse(string)

  date2 = Time.new.to_date
  (date2 - date1).to_i
end

#hours_since(string) ⇒ Object



54
55
56
# File 'lib/jekyll_time_since.rb', line 54

def hours_since(string)
  minutes_since(string) / 60
end

#minutes_since(string) ⇒ Object



58
59
60
# File 'lib/jekyll_time_since.rb', line 58

def minutes_since(string)
  seconds_since(string) / 60
end

#months_since(string) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/jekyll_time_since.rb', line 31

def months_since(string)
  string += '-01-01' unless string.include?('-')
  date1 = Date.parse(string)

  date2 = Time.new.to_date
  months = ((date2 - date1).to_f / 365 * 12).round
  months -= 1 if date2.day > date1.day
  months
end

#seconds_since(string) ⇒ Object



62
63
64
65
# File 'lib/jekyll_time_since.rb', line 62

def seconds_since(string)
  string += '-01-01' unless string.include?('-')
  (Time.new - Time.parse(string)).to_i
end

#weeks_since(string) ⇒ Object



41
42
43
44
# File 'lib/jekyll_time_since.rb', line 41

def weeks_since(string)
  result = days_since(string)
  result.to_i / 7
end

#years_since(string) ⇒ integer

Filters a string containing a year, date or ISO_8601 datetime.

Returns:

  • (integer)

    the number of years since the input.



22
23
24
25
26
27
28
29
# File 'lib/jekyll_time_since.rb', line 22

def years_since(string)
  now = DateTime.now
  string += '-01-01' unless string.include?('-')
  date = Time.parse(string)
  years = now.year - date.year
  years -= 1 if date.month > now.month || (date.month >= now.month && date.day > now.day)
  years
end