Class: Faker::Date
Constant Summary
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(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.
Methods inherited from Base
bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale
Class Method Details
.backward(days: 365) ⇒ Date
Produce a random date in the past (up to N days).
80 81 82 83 84 85 |
# File 'lib/faker/default/date.rb', line 80 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.
20 21 22 23 24 25 |
# File 'lib/faker/default/date.rb', line 20 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.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/faker/default/date.rb', line 42 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).
98 99 100 101 102 103 104 105 |
# File 'lib/faker/default/date.rb', line 98 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(days: 365) ⇒ Date
Produce a random date in the future (up to N days).
63 64 65 66 67 68 |
# File 'lib/faker/default/date.rb', line 63 def forward(days: 365) from = ::Date.today + 1 to = ::Date.today + days between(from: from, 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.
124 125 126 127 128 129 |
# File 'lib/faker/default/date.rb', line 124 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 |