class Faker::Date

def backward(legacy_days = NOT_GIVEN, days: 365)

Returns:
  • (Date) -

Parameters:
  • days (Integer) -- The maximum number of days to go into the past.
def backward(legacy_days = NOT_GIVEN, days: 365)
  warn_for_deprecated_arguments do |keywords|
    keywords << :days if legacy_days != NOT_GIVEN
  end
  from = ::Date.today - days
  to   = ::Date.today - 1
  between(from: from, to: to).to_date
end

def between(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, from:, to:)

Returns:
  • (Date) -

Parameters:
  • to (Date) -- The end of the usable date range.
  • from (Date) -- The start of the usable date range.
def between(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, from:, to:)
  warn_for_deprecated_arguments do |keywords|
    keywords << :from if legacy_from != NOT_GIVEN
    keywords << :to if legacy_to != NOT_GIVEN
  end
  from = get_date_object(from)
  to   = get_date_object(to)
  Faker::Base.rand_in_range(from, to)
end

def between_except(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, legacy_excepted = NOT_GIVEN, from:, to:, excepted:)

Returns:
  • (Date) -

Parameters:
  • excepted (Date) -- A date to exclude.
  • to (Date) -- The end of the usable date range.
  • from (Date) -- The start of the usable date range.
def between_except(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, legacy_excepted = NOT_GIVEN, from:, to:, excepted:)
  warn_for_deprecated_arguments do |keywords|
    keywords << :from if legacy_from != NOT_GIVEN
  end
  warn_for_deprecated_arguments do |keywords|
    keywords << :to if legacy_to != NOT_GIVEN
  end
  warn_for_deprecated_arguments do |keywords|
    keywords << :excepted if legacy_excepted != NOT_GIVEN
  end
  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

def birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18, max_age: 65)

Returns:
  • (Date) -

Parameters:
  • max_age (Integer) -- The maximum age that the birthday would imply.
  • min_age (Integer) -- The minimum age that the birthday would imply.
def birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18, max_age: 65)
  warn_for_deprecated_arguments do |keywords|
    keywords << :min_age if legacy_min_age != NOT_GIVEN
  end
  warn_for_deprecated_arguments do |keywords|
    keywords << :max_age if legacy_max_age != NOT_GIVEN
  end
  t = ::Date.today
  from = birthday_date(t, max_age)
  to   = birthday_date(t, min_age)
  between(from: from, to: to).to_date
end

def birthday_date(date, age)

def birthday_date(date, age)
  year = date.year - age
  day =
    if date.day == 29 && date.month == 2 && !::Date.leap?(year)
      28
    else
      date.day
    end
  ::Date.new(year, date.month, day)
end

def forward(legacy_days = NOT_GIVEN, days: 365)

Returns:
  • (Date) -

Parameters:
  • days (Integer) -- The maximum number of days to go into the future.
def forward(legacy_days = NOT_GIVEN, days: 365)
  warn_for_deprecated_arguments do |keywords|
    keywords << :days if legacy_days != NOT_GIVEN
  end
  from = ::Date.today + 1
  to   = ::Date.today + days
  between(from: from, to: to).to_date
end

def get_date_object(date)

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

def in_date_period(month: nil, year: ::Date.today.year)

Returns:
  • (Date) -

Parameters:
  • year (Integer) -- represents the year of the date
  • month (Integer) -- represents the month of the date
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