Module: FFaker::Time

Extended by:
ModuleUtils, Time
Included in:
Time
Defined in:
lib/ffaker/time.rb

Constant Summary collapse

MONTHS =
%w[January February March April May June July August September October November December].freeze
DAYS_OF_WEEK =
%w[Monday Tuesday Wednesday Thursday Friday Saturday Sunday].freeze

Instance Method Summary collapse

Methods included from ModuleUtils

const_missing, k, underscore, unique

Methods included from RandomUtils

#fetch_sample, #rand, #shuffle

Instance Method Details

#between(from, to) ⇒ Object


43
44
45
46
47
# File 'lib/ffaker/time.rb', line 43

def between(from, to)
  from_value = convert_to_time(from)
  to_value = convert_to_time(to)
  ::Time.at(from_value + rand * (to_value.to_f - from_value.to_f))
end

#date(params = {}) ⇒ Object


39
40
41
# File 'lib/ffaker/time.rb', line 39

def date(params = {})
  datetime(params).to_date
end

#datetime(params = {}) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ffaker/time.rb', line 21

def datetime(params = {})
  years_back = params[:year_range] || 5
  latest_year = params [:year_latest] || 0
  year = (rand * years_back).ceil + (::DateTime.now.year - latest_year - years_back)
  month = rand(1..12)
  day = rand(1..Date.new(year, month, -1).day)
  hours = params[:hours] || rand(0..23)
  minutes = params[:minutes] || rand(0..59)
  series = [date = ::DateTime.new(year, month, day, hours, minutes)]
  if params[:series]
    params[:series].each do |some_time_after|
      series << series.last + (rand * some_time_after).ceil
    end
    return series
  end
  date
end

#day_of_week(options = {}) ⇒ Object


17
18
19
# File 'lib/ffaker/time.rb', line 17

def day_of_week(options = {})
  fetch_sample(DAYS_OF_WEEK)[0..(options[:long] ? 10 : 2)]
end

#monthObject


13
14
15
# File 'lib/ffaker/time.rb', line 13

def month
  fetch_sample(MONTHS)
end