forked from elixirs/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.ex
75 lines (60 loc) · 1.79 KB
/
date.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
defmodule Faker.Date do
import Faker.Util, only: [pick: 1]
@moduledoc """
Functions for generating dates
"""
@doc """
Returns a random date of birth for a person with an age specified by a number or range
"""
@spec date_of_birth(integer | Range.t()) :: Date.t()
def date_of_birth(age_or_range \\ 18..99)
def date_of_birth(age) when is_integer(age) do
{date, _time} = :calendar.local_time()
today = Date.from_erl!(date)
earliest_year = today.year - (age + 1)
{:ok, earliest_date} = Date.new(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)
date_range = Date.range(earliest_date, latest_date)
pick(date_range)
end
def date_of_birth(age_range) do
age_range
|> pick()
|> date_of_birth()
end
@doc """
Returns a random date in the past up to N days, today not included
"""
@spec backward(integer) :: Date.t()
def backward(days) do
forward(-days)
end
@doc """
Returns a random date in the future up to N days, today not included
"""
@spec forward(integer) :: Date.t()
def forward(days) do
days
|> Faker.DateTime.forward()
|> DateTime.to_date()
end
@doc """
Returns a random date between two dates
## Examples
iex> Faker.Date.between(~D[2010-12-10], ~D[2016-12-25])
~D[2013-06-07]
iex> Faker.Date.between(~D[2000-12-20], ~D[2000-12-25])
~D[2000-12-20]
iex> Faker.Date.between(~D[2000-02-02], ~D[2016-02-05])
~D[2014-10-23]
iex> Faker.Date.between(~D[2010-12-20], ~D[2010-12-25])
~D[2010-12-21]
"""
@spec between(Date.t(), Date.t()) :: Date.t()
def between(from, to) do
from
|> Faker.DateTime.between(to)
|> DateTime.to_date()
end
end