Skip to content

Instantly share code, notes, and snippets.

@swaathi
Last active September 26, 2016 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swaathi/4d20c8989172351268173c4f16421208 to your computer and use it in GitHub Desktop.
Save swaathi/4d20c8989172351268173c4f16421208 to your computer and use it in GitHub Desktop.
Offset your time by timezone difference
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