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

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

Instance Method Summary collapse

Instance Method Details

#adjust_date(input, adjustment, unit) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/locomotive/steam/liquid/filters/date.rb', line 165

def adjust_date(input, adjustment, unit)
  return '' if input.blank?

  adjustment = adjustment.to_i
  unit = unit.to_sym

  if (adjustment.respond_to? unit)
    input + adjustment.send(unit)
  else
    input
  end
end

#beginning_of_day(input) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/locomotive/steam/liquid/filters/date.rb', line 153

def beginning_of_day(input)
  return '' if input.blank?

  if input.respond_to? :beginning_of_day
    return input.beginning_of_day
  end

  if input.is_a?(String)
    Time.zone.parse(input).beginning_of_day rescue ''
  end
end

#beginning_of_month(input) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/locomotive/steam/liquid/filters/date.rb', line 105

def beginning_of_month(input)
  return '' if input.blank?

  if input.respond_to? :beginning_of_month
    return input.beginning_of_month
  end

  if input.is_a?(String)
    Time.zone.parse(input).beginning_of_month rescue ''
  end
end

#beginning_of_week(input) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/locomotive/steam/liquid/filters/date.rb', line 129

def beginning_of_week(input)
  return '' if input.blank?

  if input.respond_to? :beginning_of_week
    return input.beginning_of_week
  end

  if input.is_a?(String)
    Time.zone.parse(input).beginning_of_week rescue ''
  end
end

#beginning_of_year(input) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/locomotive/steam/liquid/filters/date.rb', line 81

def beginning_of_year(input)
  return '' if input.blank?

  if input.respond_to? :beginning_of_year
    return input.beginning_of_year
  end

  if input.is_a?(String)
    Time.zone.parse(input).beginning_of_year rescue ''
  end
end

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



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/locomotive/steam/liquid/filters/date.rb', line 33

def distance_of_time_in_words(input, from_time = Time.zone.now, include_seconds = false)
  return '' if input.blank?

  # make sure we deal with instances of Time
  to_time   = convert_to_time!(input)
  from_time = convert_to_time!(from_time)

  ::I18n.with_options(scope: :'datetime.distance_in_words') do |locale|
    _distance_of_time_in_words(locale, from_time, to_time, include_seconds)
  end
end

#end_of_day(input) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/locomotive/steam/liquid/filters/date.rb', line 141

def end_of_day(input)
  return '' if input.blank?

  if input.respond_to? :end_of_day
    return input.end_of_day
  end

  if input.is_a?(String)
    Time.zone.parse(input).end_of_day rescue ''
  end
end

#end_of_month(input) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/locomotive/steam/liquid/filters/date.rb', line 93

def end_of_month(input)
  return '' if input.blank?

  if input.respond_to? :end_of_month
    return input.end_of_month
  end

  if input.is_a?(String)
    Time.zone.parse(input).end_of_month rescue ''
  end
end

#end_of_week(input) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/locomotive/steam/liquid/filters/date.rb', line 117

def end_of_week(input)
  return '' if input.blank?

  if input.respond_to? :end_of_week
    return input.end_of_week
  end

  if input.is_a?(String)
    Time.zone.parse(input).end_of_week rescue ''
  end
end

#end_of_year(input) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/locomotive/steam/liquid/filters/date.rb', line 69

def end_of_year(input)
  return '' if input.blank?

  if input.respond_to? :end_of_year
    return input.end_of_year
  end

  if input.is_a?(String)
    Time.zone.parse(input).end_of_year rescue ''
  end
end

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/locomotive/steam/liquid/filters/date.rb', line 45

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 = nil) ⇒ Object



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

def parse_date(input, format = nil)
  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



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

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