-
-
Save addisaden/4cd682f5415059eac0ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TimeDuration | |
attr_reader :duration | |
def initialize(duration, plain=false) | |
if plain | |
@duration = duration | |
else | |
init_set_duration(duration) | |
end | |
end | |
def +(time_stamp) | |
if time_stamp.is_a? TimeDuration | |
TimeDuration.new(time_stamp.duration + @duration, true) | |
else | |
time_stamp + @duration | |
end | |
end | |
private | |
def init_set_duration(duration) | |
result = duration.match(/^(\d+)\s+(\w+)$/) | |
durations = { | |
"day" => 60*60*24, | |
"days" => 60*60*24, | |
"min" => 60, | |
"minute" => 60, | |
"minutes" => 60, | |
"hour" => 60*60, | |
"hours" => 60*60, | |
"week" => 60*60*24*7, | |
"weeks" => 60*60*24*7, | |
"year" => 60*60*24*365, | |
"years" => 60*60*24*365 | |
} | |
@duration = result[1].to_i * durations[result[2]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
five_minutes = TimeDuration.new("5 min")
five_minutes + Time.now
Time.now
=> 2013-09-13 02:50:06 +0200
five_minutes + Time.now
=> 2013-09-13 02:55:13 +0200