# Time.new.utc Question

**URL:** <https://www.ruby-forum.com/t/time-new-utc-question/175764>\
**Category:** Ruby\
**Created:** [September 19, 2009, 5:05am UTC](https://www.ruby-forum.com/t/time-new-utc-question/175764 "2009-09-19T05:05:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dbf](https://www.ruby-forum.com/u/dbf)\
**Post date:** [September 19, 2009, 5:05am UTC](https://www.ruby-forum.com/t/time-new-utc-question/175764/1 "2009-09-19T05:05:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dbf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dbf](https://www.ruby-forum.com/u/dbf)\
**Post date:** [September 19, 2009, 8:23am UTC](https://www.ruby-forum.com/t/time-new-utc-question/175764/2 "2009-09-19T08:23:33Z")

</div>

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
