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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
#!/usr/bin/ruby1.8
module Custodian
module Util
#
# A class for working with time-spans.
#
class TimeSpan
#
# Convert an hour-string into a sane integer.
#
def TimeSpan.to_hour( desc )
#
# Handle PM times.
#
if ( ( desc.kind_of? String ) &&
( desc =~ /([0-9]+)pm$/i ) )
desc = $1.dup.to_i + 12;
end
#
# Handle AM times.
#
if ( desc.kind_of? String )
desc = desc.sub( /am$/, '' )
desc = desc.to_i
end
#
# Ensure within a valid range
#
raise ArgumentError, "Integer required for time" unless( desc.kind_of? Integer )
raise ArgumentError, "Invalid time: #{desc}" unless( ( desc >= 0 ) && ( desc <= 23 ) )
#
# Return the updated string.
#
desc
end
#
# Given a start/end string convert that
# to a hash of hours which are inside the
# range - inclusively.
#
def TimeSpan.to_hours( p_start, p_end )
p_start = Custodian::Util::TimeSpan::to_hour(p_start)
p_end = Custodian::Util::TimeSpan::to_hour(p_end)
#
# Valid hours, within the span
#
valid = {}
#
# Iterate over the hours. Store in a hash.
#
hour = p_start
while( hour != p_end )
valid[hour] = 1
hour += 1
hour = 0 if ( hour > 23 )
end
valid[p_end]=1
#
# Return the hash
#
valid
end
#
# Given a starting hour, such as 10pm, and an ending hour,
# such as 4am, test whether a time is within that period.
#
def TimeSpan.inside?( p_start, p_end, cur_hour = nil)
#
# Default to the current hour, if not specified.
#
if ( cur_hour.nil? )
cur_hour = Time.now.hour
end
#
# Ensure all values are sane and reasonable.
#
p_start = Custodian::Util::TimeSpan::to_hour(p_start)
p_end = Custodian::Util::TimeSpan::to_hour(p_end)
cur_hour = Custodian::Util::TimeSpan::to_hour(cur_hour)
#
# Get the expanded hours
#
valid =
Custodian::Util::TimeSpan::to_hours( p_start, p_end )
#
# Lookup to see if the specified hour is within the
# hours between the range.
#
( valid[cur_hour] == 1 )
end
end
end
end
|