Class: Faker::Date

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

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

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

Class Method Details

.backward(legacy_days = NOT_GIVEN, days: 365) ⇒ Date

Produce a random date in the past (up to N days).

Examples:

Faker::Date.backward(days: 14) #=> #<Date: 2019-09-12>

Parameters:

  • days (Integer) (defaults to: 365)

    The maximum number of days to go into the past.

Returns:

Available since:

  • 1.0.0


102
103
104
105
106
107
108
109
110
111
# File 'lib/faker/default/date.rb', line 102

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

.between(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, from:, to:) ⇒ Date

Produce a random date between two dates.

Examples:

if used with or without Rails (Active Support)

Faker::Date.between(from: '2014-09-23', to: '2014-09-25') #=> #<Date: 2014-09-24>

if used with Rails (Active Support)

Faker::Date.between(from: 2.days.ago, to: Date.today) #=> #<Date: 2014-09-24>

Parameters:

  • from (Date, String)

    The start of the usable date range.

  • to (Date, String)

    The end of the usable date range.

Returns:

Available since:

  • 1.0.0


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/faker/default/date.rb', line 20

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

.between_except(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, legacy_excepted = NOT_GIVEN, from:, to:, excepted:) ⇒ Date

Produce a random date between two dates.

Examples:

if used with or without Rails (Active Support)

Faker::Date.between_except(from: '2014-09-23', to: '2015-09-25', excepted: '2015-01-24') #=> #<Date: 2014-10-03>

if used with Rails (Active Support)

Faker::Date.between_except(from: 1.year.ago, to: 1.year.from_now, excepted: Date.today) #=> #<Date: 2014-10-03>

Parameters:

  • from (Date, String)

    The start of the usable date range.

  • to (Date, String)

    The end of the usable date range.

  • excepted (Date, String)

    A date to exclude.

Returns:

Raises:

  • (ArgumentError)

Available since:

  • 1.6.2


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/faker/default/date.rb', line 49

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

.birthday(legacy_min_age = NOT_GIVEN, legacy_max_age = NOT_GIVEN, min_age: 18, max_age: 65) ⇒ Date

Produce a random date in the past (up to N days).

Examples:

Faker::Date.birthday(min_age: 18, max_age: 65) #=> #<Date: 1986-03-28>

Parameters:

  • min_age (Integer) (defaults to: 18)

    The minimum age that the birthday would imply.

  • max_age (Integer) (defaults to: 65)

    The maximum age that the birthday would imply.

Returns:

Available since:

  • 1.4.3


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/faker/default/date.rb', line 124

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

.forward(legacy_days = NOT_GIVEN, days: 365) ⇒ Date

Produce a random date in the future (up to N days).

Examples:

Faker::Date.forward(days: 23) #=> #<Date: 2014-10-03>

Parameters:

  • days (Integer) (defaults to: 365)

    The maximum number of days to go into the future.

Returns:

Available since:

  • 1.0.0


81
82
83
84
85
86
87
88
89
90
# File 'lib/faker/default/date.rb', line 81

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

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

Produces a date in the year and/or month specified.

Examples:

Faker::Date.in_date_period #=> #<Date: 2019-09-01>
Faker::Date.in_date_period(year: 2018, month: 2) #=> #<Date: 2018-02-26>
Faker::Date.in_date_period(month: 2) #=> #<Date: 2019-02-26>

Parameters:

  • month (Integer) (defaults to: nil)

    represents the month of the date

  • year (Integer) (defaults to: ::Date.today.year)

    represents the year of the date

Returns:

Available since:

  • 2.13.0


157
158
159
160
161
162
# File 'lib/faker/default/date.rb', line 157

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