forked from hewmanitarian/loopdo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeblock.rb
72 lines (58 loc) · 2.33 KB
/
timeblock.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
class TimeBlock
attr_reader :month, :day, :year, :startTime, :endTime, :dayOfWeek, :isWeekly
def initialize(startTime, isWeekly, length) (
@month = startTime.strftime("%m")
@day = startTime.strftime("%d")
@year = startTime.strftime("%Y")
@startTime = startTime
@isWeekly = isWeekly
@endTime = startTime + ((length.to_f)/24)/60
@dayOfWeek = startTime.strftime("%A")
)
end
def contains(timeblock2)
#return true if the timeblock contains timeblock2
starts_after = (timeblock2.startTime >= @startTime)
ends_before = (timeblock2.endTime <= @endTime)
return starts_after && ends_before
end
def contains_time(timeblock2)
#return true if the timeblock contains timeblock2 (regardless of date)
startTime1 = Time.parse(@startTime.strftime("%H:%M:%S %z"))
startTime2 = Time.parse(timeblock2.startTime.strftime("%H:%M:%S %z"))
endTime1 = Time.parse(@endTime.strftime("%H:%M:%S %z"))
endTime2 = Time.parse(timeblock2.endTime.strftime("%H:%M:%S %z"))
starts_after = (startTime2 >= startTime1)
ends_before = (endTime2 <= endTime1)
return starts_after && ends_before
end
def overlaps(timeblock2)
#returns true if timeblocks overlap
check1 = (timeblock2.startTime >= @startTime &&
timeblock2.startTime <= @endTime)
check2 = (@startTime >= timeblock2.startTime &&
@startTime <= timeblock2.endTime)
return check1 || check2
end
def overlaps_time(timeblock2)
#returns true if timeblocks overlap (regardless of date)
startTime1 = Time.parse(@startTime.strftime("%H:%M:%S %z"))
startTime2 = Time.parse(timeblock2.startTime.strftime("%H:%M:%S %z"))
endTime1 = Time.parse(@endTime.strftime("%H:%M:%S %z"))
endTime2 = Time.parse(timeblock2.endTime.strftime("%H:%M:%S %z"))
check1 = (startTime2 >= startTime1 &&
startTime2 <= endTime1)
check2 = (startTime1 >= startTime2 &&
startTime1 <= endTime2)
return check1 || check2
end
def calculate_endtime(startTime, length)
return startTime + ((length.to_f)/24)/60
end
def printDetails
puts self.getDetails
end
def getDetails
"Date: #{Green}#{@startTime.month}/#{@startTime.day}/#{@startTime.year}#{Reset} | Start: #{Green}#{@startTime.strftime("%T")}#{Reset} | Stop: #{Green}#{@endTime.strftime("%T")}#{Reset} | Weekly #{Green}#{@isWeekly}#{Reset}"
end
end