# Time zone and UTC offset

**URL:** <https://www.ruby-forum.com/t/time-zone-and-utc-offset/152510>\
**Category:** Rails\
**Created:** [November 4, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/time-zone-and-utc-offset/152510 "2008-11-04T18:32:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrdamien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mrdamien](https://www.ruby-forum.com/u/mrdamien)\
**Post date:** [November 4, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/time-zone-and-utc-offset/152510/1 "2008-11-04T18:32:09Z")

</div>

Hi all,

I am trying to change a date from a time zone to UTC.

First I set the time zone and then get its offset from UTC.

Then I am trying to change the time zone of my date by substracting the  
offset but it failed.

* * *

Time.zone = @time\_zone # ‘Paris’  
@offset = Time.zone.utc\_offset() # @offset = 3600

# @date = now

@date = @date - @offset

# @date should be now - 1 but its somewhere in 1998 …

* * *

Reading the pick axe Time documentation, when you substract something  
from a date, it should be in seconds, which is the case. But with my  
code, it failed.

What can I do to fix that ?

---

<div class="post-metadata">

**Author:** ![mrdamien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mrdamien](https://www.ruby-forum.com/u/mrdamien)\
**Post date:** [November 4, 2008, 7:02pm UTC](https://www.ruby-forum.com/t/time-zone-and-utc-offset/152510/2 "2008-11-04T19:02:31Z")

</div>

On Nov 4, 5:32 pm, MR Damien [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Time.zone = @time\_zone # ‘Paris’  
> code, it failed.

Are you sure your date is an instance of Time and not an instance of  
Date? (for Date - 1 means -1 day so -3600 would be about 10 years ie  
somewhere in 1998).

Fred

---

<div class="post-metadata">

**Author:** ![mrdamien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mrdamien](https://www.ruby-forum.com/u/mrdamien)\
**Post date:** [November 5, 2008, 10:09am UTC](https://www.ruby-forum.com/t/time-zone-and-utc-offset/152510/3 "2008-11-05T10:09:18Z")

</div>

Frederick C. wrote:

> On Nov 4, 5:32ï¿½pm, MR Damien [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Time.zone = @time\_zone # ‘Paris’  
> > code, it failed.
> 
> Are you sure your date is an instance of Time and not an instance of  
> Date? (for Date - 1 means -1 day so -3600 would be about 10 years ie  
> somewhere in 1998).
> 
> Fred

Hi,

I am using DateTime.

@date = DateTime.strptime(params[:start\_date], “%B %d, %Y %I:%M %p”)

It seems that - 1 substract a day as you said. I will now search how to  
convert a DateTime to Time.

Thanks

---

<div class="post-metadata">

**Author:** ![mrdamien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mrdamien](https://www.ruby-forum.com/u/mrdamien)\
**Post date:** [November 5, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/time-zone-and-utc-offset/152510/4 "2008-11-05T15:22:22Z")

</div>

On 5 Nov 2008, at 09:09, MR Damien wrote:

> > 
> 
> convert a DateTime to Time.  
> You can also use rails’ date/time calculation things, for example

t += 5.seconds

does the right thing whether t is a Time or a DateTime

Fred

---

<div class="post-metadata">

**Author:** ![mrdamien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mrdamien](https://www.ruby-forum.com/u/mrdamien)\
**Post date:** [November 6, 2008, 3:32pm UTC](https://www.ruby-forum.com/t/time-zone-and-utc-offset/152510/5 "2008-11-06T15:32:56Z")

</div>

Frederick C. wrote:

> On 5 Nov 2008, at 09:09, MR Damien wrote:
> 
> > > 
> > 
> > convert a DateTime to Time.  
> > You can also use rails’ date/time calculation things, for example
> 
> t += 5.seconds
> 
> does the right thing whether t is a Time or a DateTime
> 
> Fred

Hey,

Thanks for that trick.
