Class: Faker::Date
Constant Summary collapse
- DAYS_OF_WEEK =
%i[sunday monday tuesday wednesday thursday friday saturday].freeze
Constants inherited from Base
Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters
Class Method Summary collapse
-
.backward(days: 365) ⇒ Date
Produce a random date in the past (up to N days).
-
.between(from:, to:) ⇒ Date
Produce a random date between two dates.
-
.between_except(from:, to:, excepted:) ⇒ Date
Produce a random date between two dates.
-
.birthday(min_age: 18, max_age: 65) ⇒ Date
Produce a random date in the past (up to N days).
-
.forward(from: ::Date.today, days: 365) ⇒ Date
Produce a random date in the future (up to N days).
-
.in_date_period(month: nil, year: ::Date.today.year) ⇒ Date
Produces a date in the year and/or month specified.
-
.on_day_of_week_between(day:, from:, to:) ⇒ Date
Produce a random date at given day(s) of the week between two dates.
Methods inherited from Base
bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale
Class Method Details
.backward(days: 365) ⇒ Date
Produce a random date in the past (up to N days).
90 91 92 93 94 95 |
# File 'lib/faker/default/date.rb', line 90 def backward(days: 365) from = ::Date.today - days to = ::Date.today - 1 between(from: from, to: to).to_date end |
.between(from:, to:) ⇒ Date
Produce a random date between two dates.
22 23 24 25 26 27 |
# File 'lib/faker/default/date.rb', line 22 def between(from:, to:) from = get_date_object(from) to = get_date_object(to) Faker::Base.rand_in_range(from, to) end |
.between_except(from:, to:, excepted:) ⇒ Date
Produce a random date between two dates.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/faker/default/date.rb', line 44 def between_except(from:, to:, excepted:) raise ArgumentError, 'From date, to date and excepted date must not be the same' if from == to && to == excepted excepted = get_date_object(excepted) loop do date = between(from: from, to: to) break date.to_date if date != excepted end end |
.birthday(min_age: 18, max_age: 65) ⇒ Date
Produce a random date in the past (up to N days).
108 109 110 111 112 113 114 115 |
# File 'lib/faker/default/date.rb', line 108 def birthday(min_age: 18, max_age: 65) t = ::Date.today from = birthday_date(t, max_age) to = birthday_date(t, min_age) between(from: from, to: to).to_date end |
.forward(from: ::Date.today, days: 365) ⇒ Date
Produce a random date in the future (up to N days).
72 73 74 75 76 77 78 |
# File 'lib/faker/default/date.rb', line 72 def forward(from: ::Date.today, days: 365) start_date = get_date_object(from) since = start_date + 1 to = start_date + days between(from: since, to: to).to_date end |
.in_date_period(month: nil, year: ::Date.today.year) ⇒ Date
Produces a date in the year and/or month specified.
134 135 136 137 138 139 |
# File 'lib/faker/default/date.rb', line 134 def in_date_period(month: nil, year: ::Date.today.year) from = ::Date.new(year, month || 1, 1) to = ::Date.new(year, month || 12, ::Date.civil(year, month || 12, -1).day) between(from: from, to: to).to_date end |
.on_day_of_week_between(day:, from:, to:) ⇒ Date
Produce a random date at given day(s) of the week between two dates.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/faker/default/date.rb', line 156 def on_day_of_week_between(day:, from:, to:) days = [day].flatten raise ArgumentError, 'Day of week cannot be empty' if days.empty? # Convert given days of the week to numbers used by `Date#wday` method numeric_weekdays = days.map do |d| DAYS_OF_WEEK.index(d.to_sym.downcase) || raise(ArgumentError, "#{d} is not a valid day of the week") end from = get_date_object(from) to = get_date_object(to) date = Faker::Base.rand_in_range(from, to) # If the initial date is not on one of the wanted days of the week... unless numeric_weekdays.include? date.wday # ...pick a date nearby that is on one of the wanted days of the week instead date += sample(numeric_weekdays) - date.wday # Move date 1 week earlier or later if the adjusted date is now outside the date range date += 7 if date < from date -= 7 if date > to if date > to || date < from raise ArgumentError, "There is no #{DAYS_OF_WEEK[date.wday].capitalize} between #{from} and #{to}. Increase the from/to date range or choose a different day of the week." end end date end |