Skip to content

Instantly share code, notes, and snippets.

@mmay
Created July 16, 2013 20:18
Show Gist options
  • Save mmay/c881180e8ac73bf504d5 to your computer and use it in GitHub Desktop.
Save mmay/c881180e8ac73bf504d5 to your computer and use it in GitHub Desktop.
# 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