Skip to content

Commit

Permalink
fix: date_of_birth invalid date due to leap year (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
igas authored Feb 29, 2024
1 parent e01b757 commit 60efcf9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format.
## Unreleased

### Added

- `Faker.Fruits.En` - add english fruits [[@KevinDaSilvaS](https://github.com/KevinDaSilvaS)]

### Changed

- `Faker.Commerce.PtBr` - add more products in product_name_product [[@igorgbr](https://github.com/igorgbr)]
- `Faker.Fruits.PtBr` - fix typo in nectarina [[@KevinDaSilvaS](https://github.com/KevinDaSilvaS)]
- `Faker.Internet.image_url/0` - switched unresponsive placeholder.it with picsum.photos [[@almirsarajcic](https://github.com/almirsarajcic)]
Expand All @@ -22,6 +24,8 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format.
### Removed

### Fixed

- `Faker.date_of_birth` - fix invalid dates due to leap year [[igas](https://github.com/igas)]
- `Faker.Currency.code/0` remove duplicates/replace old currencies [[@yassinrais](https://github.com/yassinrais)]
- `Faker.Adress.PtBr` - fix model documentation [[@laraujo7](https://github.com/laraujo7)]
- `Faker.Address.En/0` corrected formatting for US and Britian [[@atavistock](https://github.com/atavistock)]
Expand All @@ -31,6 +35,7 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format.
## 0.17.0

### Added

- `Faker.Internet.StatusCode` [[@emmetreza](https://github.com/emmetreza)]
- CI workflow using GitHub Actions [[@anthonator](https://github.com/anthonator)]
- `Faker.Cat.PtBr` [[@f-francine](https://github.com/f-francine)]
Expand All @@ -39,6 +44,7 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format.
- `Faker.Commerce.PtBr` [[@f-francine](https://github.com/f-francine)]

### Changed

- `Faker.Vehicles` add makes and models that are multi-word, refactor existing fns [[jersearls](https://github.com/jersearls)]
- `Faker.Avatar` switch to `https` to prevent redirect [[igas](https://github.com/igas)]
- Updated build badge for GitHub Actions [[@anthonator](https://github.com/anthonator)]
Expand Down
11 changes: 9 additions & 2 deletions lib/faker/date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ defmodule Faker.Date do

earliest_year = today.year - (age + 1)

{:ok, earliest_date} = Date.new(earliest_year, today.month, today.day)
{:ok, earliest_date} = safe_date(earliest_year, today.month, today.day)
earliest_date = Date.add(earliest_date, 1)

{:ok, latest_date} = Date.new(earliest_year + 1, today.month, today.day)
{:ok, latest_date} = safe_date(earliest_year + 1, today.month, today.day)

date_range = Date.range(earliest_date, latest_date)

Expand Down Expand Up @@ -72,4 +72,11 @@ defmodule Faker.Date do
|> Faker.DateTime.between(to)
|> DateTime.to_date()
end

defp safe_date(year, month, day) do
case Date.new(year, month, day) do
{:ok, date} -> {:ok, date}
{:error, :invalid_date} -> safe_date(year, month, day - 1)
end
end
end

0 comments on commit 60efcf9

Please sign in to comment.