Forum: Ruby converting Unix time to Ruby time

Posted by Fearless Fool (fearless_fool)
on 2013-01-18 15:28
I've been passed an integer:

   1325376000

... which is the number of seconds since midnight, Jan 1, 1970 IN A
SPECIFIC TIME ZONE (in this case, Pacific).  Assuming I know the time
zone (I actually don't -- see PS below), how to I convert this into a
Ruby time object?  I could fudge by adding the number of seconds in the
time zone offset, but that seems suspect.

- ff

P.S.: Even if I know that 1325376000 is given in Pacific Time, I don't
know a-priori if that's UTC-0800 or UTC-0700 because of daylight savings
time.  If there's an easy way to take that into account, that would be
nifty.
Posted by Xavier Noria (fxn)
on 2013-01-18 16:04
(Received via mailing list)
Just curious.

A Unix epoch is UTC by definition, do you know what generates those
time zone dependent integers?
Posted by Fearless Fool (fearless_fool)
on 2013-01-18 17:01
Xavier Noria wrote in post #1092798:
> Just curious.
>
> A Unix epoch is UTC by definition, do you know what generates those
> time zone dependent integers?

Yes -- it's part of the GreenButton energy reporting standard.  If 
you're feeling masochistic, see:
   http://www.openespi.org/
   http://www.naesb.org/ESPI_Standards.asp

I dug down into the xls code and it appears that they're ignoring 
timezones altogether.  Since a billing interval of "midnight to 
midnight" is always in the context of the local time, this is probably a 
reasonable approach.

Now that I think about it, I could just adopt that technique as well, in 
which case I can always coerce to UTC:

  t = Time.at(1325376000).to_datetime.new_offset("00:00")

Perhaps there's a simpler way?
Posted by Siep Korteling (steenslag)
on 2013-01-19 00:42
>
>   t = Time.at(1325376000).to_datetime.new_offset("00:00")
>
> Perhaps there's a simpler way?

Not much simpler:

DateTime.strptime(1325376000.to_s,'%s')
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.