app/helpers/account/dates_helper.rb in bullet_train-1.2.27 vs app/helpers/account/dates_helper.rb in bullet_train-1.3.0
- old
+ new
@@ -1,40 +1,20 @@
module Account::DatesHelper
- # e.g. October 11, 2018
- def display_date(timestamp, custom_date_format = nil)
- return nil unless timestamp
- if custom_date_format
- local_time(timestamp).strftime(custom_date_format)
- elsif local_time(timestamp).year == local_time(Time.now).year
- local_time(timestamp).strftime("%B %-d")
- else
- local_time(timestamp).strftime("%B %-d, %Y")
- end
+ def display_date(timestamp, format: :default, date_format: nil)
+ format = date_format if date_format
+ localize(local_time(timestamp).to_date, format: format) if timestamp
end
- # e.g. October 11, 2018 at 4:22 PM
- # e.g. Yesterday at 2:12 PM
- # e.g. April 24 at 7:39 AM
- def display_date_and_time(timestamp, custom_date_format = nil, custom_time_format = nil)
- return nil unless timestamp
-
- # today?
- if local_time(timestamp).to_date == local_time(Time.now).to_date
- "Today at #{display_time(timestamp, custom_time_format)}"
- # yesterday?
- elsif (local_time(timestamp).to_date) == (local_time(Time.now).to_date - 1.day)
- "Yesterday at #{display_time(timestamp, custom_time_format)}"
- else
- "#{display_date(timestamp, custom_date_format)} at #{display_time(timestamp, custom_time_format)}"
- end
+ def display_time(timestamp, format: :default, time_format: nil)
+ format = time_format if time_format
+ localize(local_time(timestamp).to_time, format: format) if timestamp
end
- # e.g. 4:22 PM
- def display_time(timestamp, custom_time_format = nil)
- local_time(timestamp).strftime(custom_time_format || "%l:%M %p")
+ def display_date_and_time(timestamp, format: :default, date_format: nil, time_format: nil)
+ format = "#{date_format} #{time_format}" if date_format && time_format
+ localize(local_time(timestamp).to_datetime, format: format) if timestamp
end
- def local_time(time)
- return time if current_user.time_zone.nil?
- time.in_time_zone(current_user.time_zone)
+ def local_time(timestamp)
+ timestamp&.in_time_zone(current_user.time_zone)
end
end