Module: DateCommon
- Defined in:
- lib/date_common.rb,
lib/date_common/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- THANKSGIVING_2019 =
Todo - this bites me in the ass in 2024
Date.new(2019, 11, 28)
- BLACK_FRIDAY_2019 =
Date.new(2019, 11, 29)
- THANKSGIVING_2020 =
Date.new(2020, 11, 26)
- BLACK_FRIDAY_2020 =
Date.new(2020, 11, 27)
- THANKSGIVING_2021 =
Date.new(2021, 11, 25)
- BLACK_FRIDAY_2021 =
Date.new(2021, 11, 26)
- THANKSGIVING_2022 =
Date.new(2022, 11, 24)
- BLACK_FRIDAY_2022 =
Date.new(2022, 11, 25)
- THANKSGIVING_2023 =
Date.new(2023, 11, 23)
- BLACK_FRIDAY_2023 =
Date.new(2023, 11, 24)
- VERSION =
"0.12"
Class Method Summary collapse
-
.between?(current_date, date_start, date_end) ⇒ Boolean
TESTED.
-
.day_of_week(date, abbrev = false) ⇒ Object
TESTED.
- .extract_date_string(text) ⇒ Object
-
.first_date_of_month(date) ⇒ Object
TESTED.
-
.get_start_and_end_dates_for_period(period = :yesterday) ⇒ Object
NOTE: only works under rails.
-
.holiday?(date) ⇒ Boolean
DateCommon.holiday?(Date.current).
-
.last_date_of_month(date) ⇒ Object
TESTED.
-
.parse_date(text) ⇒ Object
day before yesterday yesterday on thursday last monday on 9 4.
- .recognize_date(text) ⇒ Object
-
.swap_lower_and_upper(date1, date2) ⇒ Object
TESTED.
-
.week_end(current_date) ⇒ Object
NOTE: only works under rails.
-
.week_start(current_date) ⇒ Object
NOTE: only works under rails.
Class Method Details
.between?(current_date, date_start, date_end) ⇒ Boolean
TESTED
74 75 76 |
# 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) ⇒ Object
TESTED
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# 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) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# 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) ⇒ Object
TESTED
79 80 81 |
# 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) ⇒ Object
NOTE: only works under rails
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# 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) ⇒ Boolean
DateCommon.holiday?(Date.current)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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) ⇒ Object
TESTED
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# 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) ⇒ Object
day before yesterday yesterday on thursday last monday on 9 4
190 191 192 193 194 195 |
# 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) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# 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) ⇒ Object
TESTED
84 85 86 87 88 89 90 |
# 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) ⇒ Object
NOTE: only works under rails
68 69 70 71 |
# 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) ⇒ Object
NOTE: only works under rails
63 64 65 |
# File 'lib/date_common.rb', line 63 def self.week_start(current_date) current_date.monday end |