9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/tinypass/utils.rb', line 9
def parse_loose_period_in_msecs(period)
period = period.to_s
return period.to_i if period.to_i.to_s == period
if matches = /(\d+)\s*(\w+)/.match(period)
number = matches[1].to_i
string = matches[2]
return number if string.start_with?("ms")
return number * 1000 if string.start_with?("s")
return number * 1000 * 60 if string.start_with?("mi")
return number * 1000 * 60 * 60 if string.start_with?("h")
return number * 1000 * 60 * 60 * 24 if string.start_with?("d")
return number * 1000 * 60 * 60 * 24 * 7 if string.start_with?("w")
return number * 1000 * 60 * 60 * 24 * 30 if string.start_with?("mo")
return number * 1000 * 60 * 60 * 24 * 365 if string.start_with?("y")
end
raise ArgumentError.new("Cannot parse the specified period: #{ period }")
end
|