-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
time.rb
54 lines (43 loc) · 1.32 KB
/
time.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true
module Faker
class Time < Faker::Date
TIME_RANGES = {
all: (0..23),
day: (9..17),
night: (18..23),
morning: (6..11),
afternoon: (12..17),
evening: (17..21),
midnight: (0..4)
}.freeze
class << self
def between(from, to, period = :all, format = nil)
time = period == :between ? rand(from..to) : date_with_random_time(super(from, to), period)
time_with_format(time, format)
end
def forward(days = 365, period = :all, format = nil)
time_with_format(date_with_random_time(super(days), period), format)
end
def backward(days = 365, period = :all, format = nil)
time_with_format(date_with_random_time(super(days), period), format)
end
private
def date_with_random_time(date, period)
::Time.local(date.year, date.month, date.day, hours(period), minutes, seconds)
end
def time_with_format(time, format)
format.nil? ? time : I18n.l(DateTime.parse(time.to_s), format: format)
end
def hours(period)
raise ArgumentError, 'invalid period' unless TIME_RANGES.key? period
sample(TIME_RANGES[period].to_a)
end
def minutes
seconds
end
def seconds
sample((0..59).to_a)
end
end
end
end