Time.new.utc Question

Can anyone explain the following?

local = Time.new; utc = Time.new.utc
=> Sat Sep 19 02:42:40 UTC 2009

local
=> Fri Sep 18 20:42:40 -0600 2009

utc
=> Sat Sep 19 02:42:40 UTC 2009

local.strftime(’%s’).to_i
=> 1253328160

utc.strftime(’%s’).to_i
=> 1253353360

(local.strftime(’%s’).to_i - utc.strftime(’%s’).to_i)/60/60
=> -7

Why is the time printed 6 hours apart but in unix time it is 7 hours
apart? Is it a DST issue?

Thanks,

Dan

On Sep 18, 2009, at 23:04, Dan F. wrote:

Why is the time printed 6 hours apart but in unix time it is 7 hours
apart? Is it a DST issue?

To me, this seems like a bug in strftime. The only difference between
‘local’ and ‘utc’ should be in the default time zone shown.
Internally they’re both the same time. local.to_i and utc.to_i should
be the same number.

It seems to me that, from the description, Time#to_i and
Time#strftime("%s").to_i should be the same number, but they’re
apparently not.

My guess about what’s happening is that internally Ruby creates a
struct_tm object by using Time#to_a, which would be different for the
two different variables since it uses the display values for the
hours, minutes, seconds, etc. It then uses those fields in a call to
the underlying C strftime() call. Since the only time-zone related
field in a struct_tm is tm_isdst, the time-zone related info is mostly
lost.

I’d recommend that if you want the epoch time, just use Time#to_i and
not Time#strftime("%s").to_i

Ben