Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UTC offset in Ruby

Tags:

datetime

ruby

I'm trying to get the UTC offset for a datetime. The datetime is saved as:

Fri, 31 May 2013 15:19:08 EDT -04:00

What I'd like to get is "-04:00". When I do object.utc_offset, I get -14400. What function should I use?

like image 944
scientiffic Avatar asked Jun 01 '13 00:06

scientiffic


2 Answers

You are looking for strftime:

Time.now.strftime("%:z")

That will give you the offset such as "-04:00".

like image 103
vgoff Avatar answered Nov 19 '22 08:11

vgoff


Its in seconds. Divide by 3600 for hours, or 60 for minutes.

But if you want the formatted offset string, use vgoff's answer.

like image 33
Matt Johnson-Pint Avatar answered Nov 19 '22 08:11

Matt Johnson-Pint