# Problems with new Time.zone updates in edge. Please help.

**URL:** <https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184>\
**Category:** rubyonrails-core\
**Created:** [March 12, 2008, 5:08am UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184 "2008-03-12T05:08:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![seth\_b](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/seth_b/32/2862_2.png) [@seth\_b](https://discuss.rubyonrails.org/u/seth_b)\
**Post date:** [March 12, 2008, 5:08am UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/1 "2008-03-12T05:08:33Z")

</div>

Yo Core...on Rick's prompting last night I dropped tztime plugins and updated to edge.

One of my tests is failing regarding Time.zone now, and I believe it's a bug with the new code. The time that comes back from the result seems proper, but the zone is not set. Help?

# Editing time entries doesn't return proper time as saved.

---

<div class="post-metadata">

**Author:** ![seth\_b](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/seth_b/32/2862_2.png) [@seth\_b](https://discuss.rubyonrails.org/u/seth_b)\
**Post date:** [March 12, 2008, 6:22am UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/2 "2008-03-12T06:22:17Z")

</div>

Also, I believe this is a bug with the code. If zone is set, Time.now should return the proper time in the right zone.

> > Time.zone = "Eastern Time (US & Canada)"

=\> "Eastern Time (US & Canada)"

> > Time.now

=\> Tue Mar 11 23:21:15 PDT 2008

---

<div class="post-metadata">

**Author:** ![Michael\_Koziarski1](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@Michael\_Koziarski1](https://discuss.rubyonrails.org/u/Michael_Koziarski1)\
**Post date:** [March 12, 2008, 8:17am UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/3 "2008-03-12T08:17:40Z")

</div>

> Also, I believe this is a bug with the code. If zone is set, Time.now should return the proper time in the right zone.
> 
> \>\> Time.zone = "Eastern Time (US & Canada)" =\> "Eastern Time (US & Canada)" \>\> Time.now =\> Tue Mar 11 23:21:15 PDT 2008

Messing with Time.now is pretty dangerous, we had discussed it earlier but there's a large number of non-rails-app cases where we could cause strange results. The most common example being the Timestamps used in log messages.

---

<div class="post-metadata">

**Author:** ![seth\_b](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/seth_b/32/2862_2.png) [@seth\_b](https://discuss.rubyonrails.org/u/seth_b)\
**Post date:** [March 12, 2008, 8:57am UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/4 "2008-03-12T08:57:51Z")

</div>

That's how the old TzTime plugin works, you set the zone, call TzTime.now. I'd expect this to act in the same manner.

Barring that, I notice that there's some methods in zones.rb that allow you to get time in another zone. Specifically in\_current\_time\_zone. Is this the recommended method now?

> > Time.zone = "Eastern Time (US & Canada)"

=\> "Eastern Time (US & Canada)"

> > Time.zone

=\> #\<TimeZone:0x23d1000 @utc\_offset=-18000, @name="Eastern Time (US & Canada)"\>

> > Time.in\_current\_time\_zone

NoMethodError: undefined method `in\_current\_time\_zone' for Time:Class &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):3

> > Time.now.in\_current\_time\_zone

=\> Wed, 12 Mar 2008 04:56:48 EDT -04:00

Also - any idea on my first message in the thread regarding the failing test?

---

<div class="post-metadata">

**Author:** ![Geoff\_B](https://avatars.discourse-cdn.com/v4/letter/g/bbe5ce/32.png) [@Geoff\_B](https://discuss.rubyonrails.org/u/Geoff_B)\
**Post date:** [March 12, 2008, 2:25pm UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/5 "2008-03-12T14:25:17Z")

</div>

> That's how the old TzTime plugin works, you set the zone, call TzTime.now. I'd expect this to act in the same manner.

You just need to replace calls to TzTime.now with Time.zone.now.

> Barring that, I notice that there's some methods in zones.rb that allow you to get time in another zone. Specifically in\_current\_time\_zone. Is this the recommended method now?

You can call #in\_current\_time\_zone on any time instance, and it will return the simultaneous time in Time.zone. So, Time.now.in\_current\_time\_zone would return the same result as Time.zone.now.

> Also - any idea on my first message in the thread regarding the failing test?

Looks like you haven't set config.time\_zone in environment.rb, which sets

ActiveRecord::Base.time\_zone\_aware\_attributes = true

...without that, you'll just get times back in ActiveRecord::Base.default\_timezone.

---

<div class="post-metadata">

**Author:** ![seth\_b](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/seth_b/32/2862_2.png) [@seth\_b](https://discuss.rubyonrails.org/u/seth_b)\
**Post date:** [March 12, 2008, 8:12pm UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/6 "2008-03-12T20:12:11Z")

</div>

Geoff thanks for the pointers.

Pardon my ignorance, but what exactly do I set config.time\_zone to? What are the values it \_can\_ be set to?

The article... [http://weblog.techno-weenie.net/2008/2/6/timezone-awareness-in-rails](http://weblog.techno-weenie.net/2008/2/6/timezone-awareness-in-rails) ...has no mention of that anywhere.

All I have set in environment.rb is: # Make Active Record use UTC-base instead of local time config.active\_record.default\_timezone = :utc

---

<div class="post-metadata">

**Author:** ![seth\_b](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/seth_b/32/2862_2.png) [@seth\_b](https://discuss.rubyonrails.org/u/seth_b)\
**Post date:** [March 12, 2008, 8:44pm UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/7 "2008-03-12T20:44:46Z")

</div>

PS: I tried setting config.time\_zone = 'UTC' and :utc, and my test still fails.

---

<div class="post-metadata">

**Author:** ![seth\_b](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/seth_b/32/2862_2.png) [@seth\_b](https://discuss.rubyonrails.org/u/seth_b)\
**Post date:** [March 12, 2008, 8:59pm UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/8 "2008-03-12T20:59:08Z")

</div>

Just for clarification - here's the failing test, and it's message...

# Editing time entries doesn't return proper time as saved. &nbsp;&nbsp;# (PS, My computer is in Pacific time...) &nbsp;&nbsp;def test\_bug\_time\_entry\_save &nbsp;&nbsp;&nbsp;&nbsp;zones = [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Eastern Time (US & Canada)", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Pacific Time (US & Canada)" &nbsp;&nbsp;&nbsp;&nbsp;] &nbsp;&nbsp;&nbsp;&nbsp;zones.each do |z| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Time.zone = z &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert !Time.zone.nil? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time = Time.zone.now &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;te = TimeEntry.new( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:account\_id =\> accounts(:paid).id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:line\_item\_id =\> line\_items(:not\_on\_estimate).id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:person\_id =\> people(:paying\_admin).id, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:created\_on =\> time &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert te.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;te.reload &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal te.created\_on, time, "Time not equal for zone: #{z}" &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

1) Failure: test\_bug\_time\_entry\_save(TimeEntryTest) [./test/unit/time\_entry\_test.rb:124:in `test_bug_time_entry_save' ./test/unit/time_entry_test.rb:112:in `test\_bug\_time\_entry\_save' /Work/cashboard/site/trunk/vendor/rails/activerecord/lib/../../ activesupport/lib/active\_support/testing/setup\_and\_teardown.rb:59:in `run']: Time not equal for zone: Eastern Time (US & Canada). [no difference--suspect ==]

---

<div class="post-metadata">

**Author:** ![Geoff\_B](https://avatars.discourse-cdn.com/v4/letter/g/bbe5ce/32.png) [@Geoff\_B](https://discuss.rubyonrails.org/u/Geoff_B)\
**Post date:** [March 12, 2008, 9:00pm UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/9 "2008-03-12T21:00:35Z")

</div>

Ah, right, and the rdoc doesn't yet have anything about setting the config option either. You set time zone config in the Rails initializer block in environment.rb, like so:

Rails::Initializer.run do |config| &nbsp;&nbsp;... &nbsp;&nbsp;config.time\_zone = "Pacific Time (US & Canada)" &nbsp;&nbsp;... end

You can set it to the name of any time zone defined by the ActiveSupport TimeZone class -- see lines 267-314 of: [http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active\_support/values/time\_zone.rb](http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/values/time_zone.rb) . This zone is used as the default value for Time.zone.

You can remove config.active\_record.default\_timezone = :utc from your Rails initializer block, since config.time\_zone takes care of that for you.

---

<div class="post-metadata">

**Author:** ![Geoff\_B](https://avatars.discourse-cdn.com/v4/letter/g/bbe5ce/32.png) [@Geoff\_B](https://discuss.rubyonrails.org/u/Geoff_B)\
**Post date:** [March 12, 2008, 9:55pm UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/10 "2008-03-12T21:55:29Z")

</div>

Is the problem here that you're trying to set a column named :created\_on, which AR will overwrite with its own timestamp value?

If so, then the timestamp will be a few milliseconds later than Time.zone.now, and they won't be equal.

---

<div class="post-metadata">

**Author:** ![Geoff\_B](https://avatars.discourse-cdn.com/v4/letter/g/bbe5ce/32.png) [@Geoff\_B](https://discuss.rubyonrails.org/u/Geoff_B)\
**Post date:** [March 12, 2008, 10:05pm UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/11 "2008-03-12T22:05:46Z")

</div>

Ok, ignore the previous about the :created\_on comment, you can overwrite that...

But I think the problem is, Time.zone.now will have a milliseconds value, which is trimmed by #to\_s(:db). So, the attribute value and Time.zone.now won't be equal.

---

<div class="post-metadata">

**Author:** ![seth\_b](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/seth_b/32/2862_2.png) [@seth\_b](https://discuss.rubyonrails.org/u/seth_b)\
**Post date:** [March 13, 2008, 12:38am UTC](https://discuss.rubyonrails.org/t/problems-with-new-time-zone-updates-in-edge-please-help/26184/12 "2008-03-13T00:38:52Z")

</div>

Geoff, thanks for the help - this line seems to work fine:

assert\_equal te.created\_on.to\_s(:db), time.to\_s(:db)

Seems accurate enough for my tests as well.
