Module: Locomotive::Liquid::Filters::Date

Defined in:
lib/locomotive/liquid/filters/date.rb

Instance Method Summary collapse

Instance Method Details

#distance_of_time_in_words(input, from_time = Time.zone.now, include_seconds = false) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/locomotive/liquid/filters/date.rb', line 32

def distance_of_time_in_words(input, from_time = Time.zone.now, include_seconds = false)
  # make sure we deals with instances of Time
  input     = to_time(input)
  from_time = to_time(from_time)

  ::ActionController::Base.helpers.distance_of_time_in_words(input, from_time, { include_seconds: include_seconds })
end

#localized_date(input, *args) ⇒ Object Also known as: format_date



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/locomotive/liquid/filters/date.rb', line 40

def localized_date(input, *args)
  return '' if input.blank?

  format, locale = args

  locale ||= I18n.locale
  format ||= I18n.t('date.formats.default', locale: locale)

  if input.is_a?(String)
    begin
      fragments = ::Date._strptime(input, format)
      input = ::Date.new(fragments[:year], fragments[:mon], fragments[:mday])
    rescue
      input = Time.parse(input)
    end
  end

  return input.to_s unless input.respond_to?(:strftime)

  input = input.in_time_zone(@context.registers[:site].timezone) if input.respond_to?(:in_time_zone)

  I18n.l input, format: format, locale: locale
end

#parse_date(input, format) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/locomotive/liquid/filters/date.rb', line 19

def parse_date(input, format)
  return '' if input.blank?

  format  ||= I18n.t('date.formats.default')
  date    = ::Date._strptime(input, format)

  if date
    ::Date.new(date[:year], date[:mon], date[:mday])
  else
    ::Date.parse(value) rescue ''
  end
end

#parse_date_time(input, format = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/locomotive/liquid/filters/date.rb', line 6

def parse_date_time(input, format = nil)
  return '' if input.blank?

  format    ||= I18n.t('time.formats.default')
  date_time = ::DateTime._strptime(input, format)

  if date_time
    ::Time.zone.local(date_time[:year], date_time[:mon], date_time[:mday], date_time[:hour], date_time[:min], date_time[:sec] || 0)
  else
    ::Time.zone.parse(input) rescue ''
  end
end