class Faker::Time
def backward(legacy_days = NOT_GIVEN, legacy_period = NOT_GIVEN, legacy_format = NOT_GIVEN, days: 365, period: :all, format: nil)
-
(Time)
-
Parameters:
-
format
(Symbol
) -- The name of a DateTime format to use. -
period
(Symbol
) -- The time of day, if any. See {TIME_RANGES}. -
days
(Integer
) -- The maximum number of days to go into the past.
def backward(legacy_days = NOT_GIVEN, legacy_period = NOT_GIVEN, legacy_format = NOT_GIVEN, days: 365, period: :all, format: nil) warn_for_deprecated_arguments do |keywords| keywords << :days if legacy_days != NOT_GIVEN keywords << :period if legacy_period != NOT_GIVEN keywords << :format if legacy_format != NOT_GIVEN end time_with_format(date_with_random_time(Faker::Date.backward(days: days), period), format) end
def between(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, legacy_format = NOT_GIVEN, from:, to:, format: nil)
-
(Time)
-
Parameters:
-
format
(Symbol
) -- The name of a DateTime format to use. -
to
(Time, Date, DateTime
) -- The end of the usable time range. -
from
(Time, Date, DateTime
) -- The start of the usable time range.
def between(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, legacy_format = NOT_GIVEN, from:, to:, format: nil) warn_for_deprecated_arguments do |keywords| keywords << :from if legacy_from != NOT_GIVEN keywords << :to if legacy_to != NOT_GIVEN keywords << :format if legacy_format != NOT_GIVEN end from = get_time_object(from) to = get_time_object(to) time = Faker::Base.rand_in_range(from, to) time_with_format(time, format) end
def between_dates(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, legacy_period = NOT_GIVEN, legacy_format = NOT_GIVEN, from:, to:, period: :all, format: nil)
-
(Time)
-
Parameters:
-
format
(Symbol
) -- The name of a DateTime format to use. -
period
(Symbol
) -- The time of day, if any. See {TIME_RANGES}. -
to
(Date
) -- The end of the usable time range. -
from
(Date
) -- The start of the usable time range.
def between_dates(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, legacy_period = NOT_GIVEN, legacy_format = NOT_GIVEN, from:, to:, period: :all, format: nil) warn_for_deprecated_arguments do |keywords| keywords << :from if legacy_from != NOT_GIVEN keywords << :to if legacy_to != NOT_GIVEN keywords << :period if legacy_period != NOT_GIVEN keywords << :format if legacy_format != NOT_GIVEN end date = Faker::Date.between(from: from, to: to) time = date_with_random_time(date, period) time_with_format(time, format) end
def date_with_random_time(date, period)
def date_with_random_time(date, period) ::Time.local(date.year, date.month, date.day, hours(period), minutes, seconds) end
def forward(legacy_days = NOT_GIVEN, legacy_period = NOT_GIVEN, legacy_format = NOT_GIVEN, days: 365, period: :all, format: nil)
-
(Time)
-
Parameters:
-
format
(Symbol
) -- The name of a DateTime format to use. -
period
(Symbol
) -- The time of day, if any. See {TIME_RANGES}. -
days
(Integer
) -- The maximum number of days to go into the future.
def forward(legacy_days = NOT_GIVEN, legacy_period = NOT_GIVEN, legacy_format = NOT_GIVEN, days: 365, period: :all, format: nil) warn_for_deprecated_arguments do |keywords| keywords << :days if legacy_days != NOT_GIVEN keywords << :period if legacy_period != NOT_GIVEN keywords << :format if legacy_format != NOT_GIVEN end time_with_format(date_with_random_time(Faker::Date.forward(days: days), period), format) end
def get_time_object(time)
def get_time_object(time) time = ::Time.parse(time) if time.is_a? String time = time.to_time if time.respond_to?(:to_time) time end
def hours(period)
def hours(period) raise ArgumentError, 'invalid period' unless TIME_RANGES.key? period sample(TIME_RANGES[period].to_a) end
def minutes
def minutes seconds end
def seconds
def seconds sample((0..59).to_a) end
def time_with_format(time, format)
def time_with_format(time, format) format.nil? ? time : I18n.localize(time, format: format) end