class Faker::Date
Public Class Methods
backward(days = 365)
click to toggle source
# File lib/faker/date.rb, line 28 def backward(days = 365) from = ::Date.today - days to = ::Date.today - 1 between(from, to).to_date end
between(from, to)
click to toggle source
# File lib/faker/date.rb, line 4 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)
click to toggle source
# File lib/faker/date.rb, line 11 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, to) break date.to_date if date != excepted end end
birthday(min_age = 18, max_age = 65)
click to toggle source
# File lib/faker/date.rb, line 35 def birthday(min_age = 18, max_age = 65) t = ::Date.today top_bound, bottom_bound = prepare_bounds(t, min_age, max_age) years = handled_leap_years(top_bound, bottom_bound) from = ::Date.new(years[0], t.month, t.day) to = ::Date.new(years[1], t.month, t.day) between(from, to).to_date end
forward(days = 365)
click to toggle source
# File lib/faker/date.rb, line 21 def forward(days = 365) from = ::Date.today + 1 to = ::Date.today + days between(from, to).to_date end
Private Class Methods
customized_bound(bound, increase = false)
click to toggle source
# File lib/faker/date.rb, line 63 def customized_bound(bound, increase = false) if (bound % 4) != 0 bound += 1 if increase bound -= 1 unless increase customized_bound(bound, increase) else bound end end
get_date_object(date)
click to toggle source
# File lib/faker/date.rb, line 73 def get_date_object(date) date = ::Date.parse(date) if date.is_a?(String) date = date.to_date if date.respond_to?(:to_date) date end
handled_leap_years(top_bound, bottom_bound)
click to toggle source
# File lib/faker/date.rb, line 52 def handled_leap_years(top_bound, bottom_bound) if (top_bound % 4) != 0 || (bottom_bound % 4) != 0 [ customized_bound(top_bound), customized_bound(bottom_bound, true) ] else [top_bound, bottom_bound] end end
prepare_bounds(t, min_age, max_age)
click to toggle source
# File lib/faker/date.rb, line 48 def prepare_bounds(t, min_age, max_age) [t.year - min_age, t.year - max_age] end