-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtime.rb
104 lines (81 loc) · 1.91 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true
module Biz
class Time
MINUTE_SECONDS = 60
HOUR_MINUTES = 60
DAY_HOURS = 24
WEEK_DAYS = 7
HOUR_SECONDS = HOUR_MINUTES * MINUTE_SECONDS
DAY_SECONDS = DAY_HOURS * HOUR_SECONDS
DAY_MINUTES = DAY_HOURS * HOUR_MINUTES
WEEK_MINUTES = WEEK_DAYS * DAY_MINUTES
BIG_BANG = ::Time.new(-100_000_000).freeze
HEAT_DEATH = ::Time.new(100_000_000).freeze
private_constant :MINUTE_SECONDS,
:HOUR_MINUTES,
:DAY_HOURS,
:WEEK_DAYS,
:HOUR_SECONDS,
:DAY_SECONDS,
:DAY_MINUTES,
:WEEK_MINUTES,
:BIG_BANG,
:HEAT_DEATH
def self.minute_seconds
MINUTE_SECONDS
end
def self.hour_minutes
HOUR_MINUTES
end
def self.day_hours
DAY_HOURS
end
def self.week_days
WEEK_DAYS
end
def self.hour_seconds
HOUR_SECONDS
end
def self.day_seconds
DAY_SECONDS
end
def self.day_minutes
DAY_MINUTES
end
def self.week_minutes
WEEK_MINUTES
end
def self.big_bang
BIG_BANG
end
def self.heat_death
HEAT_DEATH
end
def initialize(time_zone)
@time_zone = time_zone
end
def local(time)
time_zone.utc_to_local(time.utc)
end
def on_date(date, day_time)
time_zone.local_to_utc(
::Time.new(
date.year,
date.month,
date.mday,
day_time.hour,
day_time.minute,
day_time.second
),
true
)
rescue TZInfo::PeriodNotFound
on_date(Date.for_dst(date, day_time), day_time.for_dst)
end
def during_week(week, week_time)
on_date(week.start_date + week_time.wday, week_time.day_time)
end
private
attr_reader :time_zone
end
end