Class: Faker::Date

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/date.rb

Direct Known Subclasses

Time

Constant Summary

Constants inherited from Base

Base::Letters, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, sample, shuffle, translate, unique, with_locale

Class Method Details

.backward(days = 365) ⇒ Object


28
29
30
31
32
33
# 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) ⇒ Object


4
5
6
7
8
9
# 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) ⇒ Object

Raises:

  • (ArgumentError)

11
12
13
14
15
16
17
18
19
# 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) ⇒ Object


35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object


21
22
23
24
25
26
# 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