Last active
September 26, 2016 04:26
-
-
Save swaathi/4d20c8989172351268173c4f16421208 to your computer and use it in GitHub Desktop.
Offset your time by timezone difference
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 Time | |
# Adds an offset to your time | |
# So you can go from | |
# Fri, 22 Jul 2016 07:56:38 UTC +00:00 | |
# To, | |
# Fri, 22 Jul 2016 13:26:38 UTC +00:00 | |
# | |
# Example, | |
# t = Time.now.utc | |
# => Fri, 22 Jul 2016 07:56:38 UTC +00:00 | |
# t.offset("+05:30") | |
# => Fri, 22 Jul 2016 13:26:38 UTC +00:00 | |
# | |
# Offsets must be of the format, | |
# (+|-)(0-1)(1-12):(0-5)(0-9) | |
def offset(off) | |
if match = off.match(/^(?:Z|([+-])((?:2[0-3]|[01][0-9])):([0-5][0-9]))$/) | |
set, hour, minute = match.captures | |
return self.send(set, hour.to_i.hour).send(set, minute.to_i.minute) | |
else | |
return self | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment