Class: Faker::Time

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

Constant Summary collapse

TIME_RANGES =
{
  all: (0..23),
  day: (9..17),
  night: (18..23),
  morning: (6..11),
  afternoon: (12..17),
  evening: (17..21),
  midnight: (0..4)
}.freeze

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, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.backward(days: 365, period: :all, format: nil) ⇒ Time

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

Examples:

Faker::Time.backward(days: 14, period: :evening)
  #=> "2014-09-17 19:56:33 -0700"
Faker::Time.backward(days: 5, period: :morning, format: :short)
  #=> "14 Oct 07:44"

Parameters:

  • days (Integer) (defaults to: 365)

    The maximum number of days to go into the past.

  • period (Symbol) (defaults to: :all)

    The time of day, if any. See TIME_RANGES.

  • format (Symbol) (defaults to: nil)

    The name of a DateTime format to use.

Returns:

Available since:

  • 1.5.0



114
115
116
# File 'lib/faker/default/time.rb', line 114

def backward(days: 365, period: :all, format: nil)
  time_with_format(date_with_random_time(Faker::Date.backward(days: days), period), format)
end

.between(from:, to:, format: nil) ⇒ Time

Produce a random time between two times.

Examples:

# Random Stringified time between two times, formatted to the specified I18n format
# (Examples are from a Rails console with rails-i18n 5.1.1 defaults loaded)
I18n.locale = 'en-US'
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :default) #=> "Tue, 16 Oct 2018 10:48:27 AM -05:00"
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :short) #=> "15 Oct 10:48 AM"
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :long) #=> "October 15, 2018 10:48 AM"

I18n.locale = 'ja'
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :default) #=> "2018/10/15 10:48:27"
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :short) #=> "18/10/15 10:48"
Faker::Time.between(from: DateTime.now - 1, to: DateTime.now, format: :long) #=> "2018年10月16日(火) 10時48分27秒 -0500"

Parameters:

  • from (Time, Date, DateTime)

    The start of the usable time range.

  • to (Time, Date, DateTime)

    The end of the usable time range.

  • format (Symbol) (defaults to: nil)

    The name of a DateTime format to use.

Returns:

Available since:

  • 1.5.0



38
39
40
41
42
43
44
# File 'lib/faker/default/time.rb', line 38

def between(from:, to:, format: nil)
  from = get_time_object(from)
  to = get_time_object(to)

  time = Faker::Base.rand_in_range(from, to)
  time_with_format(time, format)
end

.between_dates(from:, to:, period: :all, format: nil) ⇒ Time

Produce a random time between two dates.

Examples:

Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :all)
  #=> "2014-09-19 07:03:30 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :day)
  #=> "2014-09-18 16:28:13 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :night)
  #=> "2014-09-20 19:39:38 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :morning)
  #=> "2014-09-19 08:07:52 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :afternoon)
  #=> "2014-09-18 12:10:34 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :evening)
  #=> "2014-09-19 20:21:03 -0700"
Faker::Time.between_dates(from: Date.today - 1, to: Date.today, period: :midnight)
  #=> "2014-09-20 00:40:14 -0700"
Faker::Time.between_dates(from: Date.today - 5, to: Date.today + 5, period: :afternoon, format: :default)
  #=> "Fri, 19 Oct 2018 15:17:46 -0500"

Parameters:

  • from (Date)

    The start of the usable time range.

  • to (Date)

    The end of the usable time range.

  • period (Symbol) (defaults to: :all)

    The time of day, if any. See TIME_RANGES.

  • format (Symbol) (defaults to: nil)

    The name of a DateTime format to use.

Returns:

Available since:

  • 1.0.0



74
75
76
77
78
# File 'lib/faker/default/time.rb', line 74

def between_dates(from:, to:, period: :all, format: nil)
  date = Faker::Date.between(from: from, to: to)
  time = date_with_random_time(date, period)
  time_with_format(time, format)
end

.forward(days: 365, period: :all, format: nil) ⇒ Time

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

Examples:

Faker::Time.forward(days: 23, period: :morning)
  # => "2014-09-26 06:54:47 -0700"
Faker::Time.forward(days: 5,  period: :evening, format: :long)
  #=> "October 21, 2018 20:47"

Parameters:

  • days (Integer) (defaults to: 365)

    The maximum number of days to go into the future.

  • period (Symbol) (defaults to: :all)

    The time of day, if any. See TIME_RANGES.

  • format (Symbol) (defaults to: nil)

    The name of a DateTime format to use.

Returns:

Available since:

  • 1.5.0



95
96
97
# File 'lib/faker/default/time.rb', line 95

def forward(days: 365, period: :all, format: nil)
  time_with_format(date_with_random_time(Faker::Date.forward(days: days), period), format)
end