Created
July 16, 2013 20:18
-
-
Save mmay/c881180e8ac73bf504d5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Adds minutes to the time string | |
# @param time [String] of format "[H]H:MM {AM|PM}" | |
# @param minutes [Integer] the number of minutes to add to the time | |
def add_minutes(time, minutes) | |
hours, min, am_pm = time.match(/(\d+):(\d+)\s(\w\w)/).captures | |
hours = hours.to_i | |
min = min.to_i + minutes | |
if hours == 11 && min > 59 | |
am_pm = am_pm == "PM" ? "AM" : "PM" | |
end | |
while min > 59 | |
min = min - 60 | |
hours = hours + 1 | |
end | |
if hours > 12 | |
hours = hours - 12 | |
end | |
min = "%02d" % min # zero pad minutes | |
"#{hours}:#{min} #{am_pm}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment