Module: Metricdeck::Helpers::DateHelpers
- Defined in:
- lib/metricdeck/helpers/date_helpers.rb
Instance Method Summary collapse
- #calculate_previous_period(start_date, end_date, comparison_period = :previous) ⇒ Object
- #generate_comparison_text(comparison_period) ⇒ Object
- #parse_date_with_formats(date_str) ⇒ Object
Instance Method Details
#calculate_previous_period(start_date, end_date, comparison_period = :previous) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/metricdeck/helpers/date_helpers.rb', line 25 def calculate_previous_period(start_date, end_date, comparison_period = :previous) period_length = (end_date - start_date).to_i + 1 case comparison_period.to_sym when :month, :last_month prev_start_date = start_date.prev_month prev_end_date = [end_date.prev_month, prev_start_date.end_of_month].min when :year, :last_year prev_start_date = start_date.prev_year prev_end_date = end_date.prev_year else prev_start_date = start_date - period_length.days prev_end_date = end_date - period_length.days end [prev_start_date, prev_end_date] end |
#generate_comparison_text(comparison_period) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/metricdeck/helpers/date_helpers.rb', line 43 def generate_comparison_text(comparison_period) period = comparison_period.to_sym if comparison_period.respond_to?(:to_sym) comparison_key = case period when :month, :last_month :last_month when :year, :last_year :last_year else :previous_period end I18n.t("metricdeck.comparisons.#{comparison_key}") end |
#parse_date_with_formats(date_str) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/metricdeck/helpers/date_helpers.rb', line 6 def parse_date_with_formats(date_str) return nil if date_str.blank? return date_str if date_str.is_a?(Date) formats = ['%d-%m-%Y', '%Y-%m-%d', '%m/%d/%Y', '%d/%m/%Y'] formats.each do |format| return Date.strptime(date_str.to_s, format) rescue Date::Error next end begin Date.parse(date_str.to_s) rescue Date::Error nil end end |