module DateCommon
Constants
- BLACK_FRIDAY_2019
- BLACK_FRIDAY_2020
- BLACK_FRIDAY_2021
- BLACK_FRIDAY_2022
- BLACK_FRIDAY_2023
- THANKSGIVING_2019
Todo - this bites me in the ass in 2024
- THANKSGIVING_2020
- THANKSGIVING_2021
- THANKSGIVING_2022
- THANKSGIVING_2023
- VERSION
Public Class Methods
between?(current_date, date_start, date_end)
click to toggle source
TESTED
# File lib/date_common.rb, line 74 def self.between?(current_date, date_start, date_end) return true if current_date >= date_start && current_date <= date_end end
day_of_week(date, abbrev = false)
click to toggle source
TESTED
# File lib/date_common.rb, line 163 def self.day_of_week(date, abbrev = false) wday = case date.wday when 0 'Sunday' when 1 'Monday' when 2 'Tuesday' when 3 'Wednesday' when 4 'Thursday' when 5 'Friday' when 6 'Saturday' end return wday[0..2] if abbrev return wday end
extract_date_string(text)
click to toggle source
# File lib/date_common.rb, line 197 def self.extract_date_string(text) #raise "foo" patterns = [] patterns << / (o?n?|l?a?s?t?) ?(monday) ?/i patterns << / (o?n?|l?a?s?t?) ?(tuesday) ?/i patterns << / (o?n?|l?a?s?t?) ?(wednesday) ?/i patterns << / (o?n?|l?a?s?t?) ?(thursday) ?/i patterns << / (o?n?|l?a?s?t?) ?(friday) ?/i patterns << / (o?n?|l?a?s?t?) ?(saturday) ?/i patterns << / (o?n?|l?a?s?t?) ?(sunday) ?/i patterns.each do |pattern| if text =~ pattern return $2.downcase end end patterns = [] patterns << / (yesterday) ?/i patterns << / (today) ?/i patterns.each do |pattern| if text =~ pattern return $1.downcase end end patterns = [] patterns << / on ([0-9]+) ([0-9]+) ?/ patterns << /[0-9]+\/[0-9]+/ patterns << /[0-9]+-[0-9]+/ patterns << /[0-9]+ [0-9]+/ patterns.each do |pattern| if text =~ pattern current_year = Date.today.year return Date.new(current_year, $1.to_i, $2.to_i).to_s.downcase end end # patterns.each do |pattern| # if text =~ pattern # open_parentheses_count = pattern.to_s.count("(") # close_parentheses_count = pattern.to_s.count(")") # if open_parentheses_count || close_parentheses_count # total_parentheses_count = open_parentheses_count + close_parentheses_count # else # total_parentheses_count = 0 # end # # return the last match_group # match_group1 = $1 # match_group2 = $2 # return match_group2 if total_parentheses_count >= 4 # return match_group1 # end # end end
first_date_of_month(date)
click to toggle source
TESTED
# File lib/date_common.rb, line 79 def self.first_date_of_month(date) Date.new(date.year, date.month, 1) end
get_start_and_end_dates_for_period(period=:yesterday)
click to toggle source
NOTE: only works under rails
# File lib/date_common.rb, line 135 def self.get_start_and_end_dates_for_period(period=:yesterday) case period when :yesterday date_start = Date.yesterday date_end = Date.yesterday when :last_week, :past_week date_start = 8.days.ago.to_date.to_date date_end = Date.yesterday when :last_month, :past_month date_start = 31.days.ago.to_date date_end = Date.yesterday when :last_quarter, :past_quarter date_start = 91.days.ago.to_date date_end = Date.yesterday when :last_year, :past_year date_start = 366.days.ago.to_date date_end = Date.yesterday when :year date_start = (Date.yesterday - 365.days).to_date date_end = Date.yesterday else date_start = period date_end = period end return date_start, date_end end
holiday?(date)
click to toggle source
DateCommon.holiday?(Date.current)
# File lib/date_common.rb, line 21 def self.holiday?(date) date = date.to_date if date.class.is_a?(DateTime) date = date.parse if date.class.is_a?(String) this_year = Date.today.year return true if date == Date.new(this_year, 12, 25) return true if date == Date.new(this_year, 10, 31) return true if date == Date.new(this_year, 12, 31) return true if date == Date.new(this_year, 1, 1) return true if date == Date.new(this_year, 7, 4) return true if date == Date.new(this_year, 2, 14) return true if date == THANKSGIVING_2019 return true if date == BLACK_FRIDAY_2019 # return true if date == THANKSGIVING_2019 + 2.day # return true if date == THANKSGIVING_2019 + 3.day return true if date == THANKSGIVING_2020 return true if date == BLACK_FRIDAY_2020 # return true if date == THANKSGIVING_2020 + 2.day # return true if date == THANKSGIVING_2020 + 3.day return true if date == THANKSGIVING_2021 return true if date == BLACK_FRIDAY_2021 # return true if date == THANKSGIVING_2021 + 2.day # return true if date == THANKSGIVING_2021 + 3.day return true if date == THANKSGIVING_2022 return true if date == BLACK_FRIDAY_2022 # return true if date == THANKSGIVING_2022 + 2.day # return true if date == THANKSGIVING_2022 + 3.day return true if date == THANKSGIVING_2023 return true if date == BLACK_FRIDAY_2023 # return true if date == THANKSGIVING_2023 + 2.day # return true if date == THANKSGIVING_2023 + 3.day return false end
last_date_of_month(date)
click to toggle source
TESTED
# File lib/date_common.rb, line 93 def self.last_date_of_month(date) # fucking god awful approach but it works; refactor # the 31st begin date_end = Date.new(date.year, date.month, 31) rescue date_end = nil end #byebug return date_end if date_end # the 30th begin date_end = Date.new(date.year, date.month, 30) rescue date_end = nil end #byebug return date_end if date_end # the 29th begin date_end = Date.new(date.year, date.month, 29) rescue date_end = nil end return date_end if date_end # the 28th begin date_end = Date.new(date.year, date.month, 28) rescue date_end = nil end return date_end if date_end end
parse_date(text)
click to toggle source
day before yesterday yesterday on thursday last monday on 9 4
# File lib/date_common.rb, line 190 def self.parse_date(text) date_text = extract_date_string(text) puts date_text date = recognize_date(date_text) return date end
recognize_date(text)
click to toggle source
# File lib/date_common.rb, line 254 def self.recognize_date(text) # yesterday return Date.today if text == "today" # yesterday return Date.today - 1 if text == "yesterday" # 2020-9-4 if text =~ /[0-9]+-[0-9]+-[0-9]+/ parts = text.split("-") return Date.new(parts[0].to_i, parts[1].to_i, parts[2].to_i) end if text =~ /[0-9]+\/[0-9]+\/[0-9]+/ parts = text.split("/") return Date.new(parts[0].to_i, parts[1].to_i, parts[2].to_i) end if text =~ /[0-9]+-[0-9]+-[0-9]+/ parts = text.split("-") return Date.new(parts[0].to_i, parts[1].to_i, parts[2].to_i) end if text =~ /([0-9]+)\/([0-9]+)/ #parts = text.split("-") year = Date.today.year return Date.new(year, $1.to_i, $2.to_i) end if text =~ /([0-9]+)-([0-9]+)/ #parts = text.split("-") year = Date.today.year return Date.new(year, $1.to_i, $2.to_i) end # monday / tuesday / wednesday. if text =~ /.+day/ current_date = Date.today day_range = 7 possible_dates = [] possible_dates << current_date - 1 possible_dates << current_date - 2 possible_dates << current_date - 3 possible_dates << current_date - 4 possible_dates << current_date - 5 possible_dates << current_date - 6 possible_dates << current_date - 7 possible_dates.each do |date| return date if date.monday? && text == 'monday' return date if date.tuesday? && text == 'tuesday' return date if date.wednesday? && text == 'wednesday' return date if date.thursday? && text == 'thursday' return date if date.friday? && text == 'friday' return date if date.saturday? && text == 'saturday' return date if date.sunday? && text == 'sunday' end end end
swap_lower_and_upper(date1, date2)
click to toggle source
TESTED
# File lib/date_common.rb, line 84 def self.swap_lower_and_upper(date1, date2) if date1 > date2 return date2, date1 elsif date2 > date1 return date1, date2 end end
week_end(current_date)
click to toggle source
NOTE: only works under rails
# File lib/date_common.rb, line 68 def self.week_end(current_date) this_week_start = DateCommon.week_start(current_date) this_week_start + 6.days end
week_start(current_date)
click to toggle source
NOTE: only works under rails
# File lib/date_common.rb, line 63 def self.week_start(current_date) current_date.monday end