Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Faker::Date.in_date_period #1755

Merged
merged 10 commits into from
Oct 10, 2019
12 changes: 12 additions & 0 deletions doc/default/date.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ Faker::Date.backward(days: 14) #=> "Fri, 19 Sep 2014"
# Random birthday date (maximum age between 18 and 65)
# Keyword arguments: min_age, max_age
Faker::Date.birthday(min_age: 18, max_age: 65) #=> "Mar, 28 Mar 1986"

# Random date in current year
Faker::Date.in_date_period #=> #<Date: 2019-09-01>

# Random date for range of year 2018 and month 2
# Keyword arguments: year, month
Faker::Date.in_date_period(year: 2018, month: 2) #=> #<Date: 2018-02-26>

# Random date for range of current year and month 2
# Keyword arguments: month
Faker::Date.in_date_period(month: 2) #=> #<Date: 2019-02-26>
AmrAdelKhalil marked this conversation as resolved.
Show resolved Hide resolved

```
24 changes: 24 additions & 0 deletions lib/faker/default/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ def birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18
between(from: from, to: to).to_date
end

##
# Produces a date in the year and/or month specified.
#
# @param month [Integer] represents the month of the date
# @param year [Integer] represents the year of the date
# @return [Date]
#
# @example
# Faker::Date.in_date_period #=> #<Date: 2019-09-01>
#
# @example
# Faker::Date.in_date_period(year: 2018, month: 2) #=> #<Date: 2018-02-26>
#
# @example
# Faker::Date.in_date_period(month: 2) #=> #<Date: 2019-02-26>
#
# @faker.version next
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

private

def birthday_date(date, age)
Expand Down
36 changes: 36 additions & 0 deletions test/faker/default/test_faker_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,40 @@ def test_default_birthday
assert birthday < birthdate_max, "Expect < \"#{birthdate_max}\", but got #{birthday}"
end
end

def test_default_in_date_period
current_year = Date.today.year
10.times do
date = @tester.in_date_period
assert date.year == current_year
end
end

def test_in_date_period_with_year
year = 2015
10.times do
date = @tester.in_date_period(year: year)
assert date.year == year
end
end

def test_in_date_period_with_month
month = 2
current_year = Date.today.year
10.times do
date = @tester.in_date_period(month: month)
assert date.month == month
assert date.year == current_year
end
end

def test_in_date_period_date
year = 2008
month = 3
10.times do
date = @tester.in_date_period(year: year, month: month)
assert date.month == month
assert date.year == year
end
end
end