0

I'm using Rails 3.2.8. When I generate a scaffold with a time field or datetime field. The HTML form field gets pre-populated with the current date/time in UTC. Great that it's pre-populated with the current date/time, but because it's in UTC, we have to change the time back 7 hours every time (which will sometimes require setting the day back 1 also, possibly month and year too). And then it seems the UTC time gets stored in the database, so I'll have issues displaying/editing it as well if I recorded it in our local time.

I looked at the Ruby documentation for the form helpers to deal with local time better, but I don't see anything relevant.

What's the best way to deal with editing and displaying dates and times in Rails?

UPDATE: To clarify, I like the idea that UTC time is stored in the database. I just want the time_select and datetime_select form helpers to edit the times in the user's local timezone. And, of course, I want to easily display the time in the user's local timezone.

at.
  • 50,922
  • 104
  • 292
  • 461
  • How about setting the default timezone in `config/application.rb` instead? That way, such issues won't arrive at first place. `config.time_zone = "foo_place"` – kiddorails Oct 01 '12 at 19:09
  • The time zone might be different for different users. – at. Oct 01 '12 at 19:11
  • @at. did you find an answer? i'm having the same issue. The user-specific timezone is set in the application controller, so I can display date just fine. I've got a column that is type 'time' in the migration. I'm using a time_select helper in the view. However, the time_select seems to ignore thee user-timezone preference. It just displays in the zone as it's stored in the DB not the user-timezone. Also, when the form is saved, it's interpreted as UTC not the user-timezone. – eggie5 Aug 07 '14 at 04:12

2 Answers2

0

You have to set the time zone to display on a per-user basis somewhere in a before/around_filter. This has to be done like:

Time.zone = "Kyiv"

An example can be found in its API: http://api.rubyonrails.org/classes/Time.html#method-c-zone-3D.

Run the rake time:zones:all to see all of them.

All the conversions has to be handled for you behind the scene.

jdoe
  • 15,665
  • 2
  • 46
  • 48
  • That definitely helped, but I'm more confused now about how Rails deals with date/times and timezones. So I did a `Time.zone="Pacific Time (US & Canada)"` in the `new` method of a scaffold. Worked brilliantly for my `datetime` field. Was able to edit/create/display it in local time and it saved it in the database in UTC time. I just can't set the timezone for the update/create methods of the controller. However, my `time` fields completely disregard the `Time.zone` setting. It's **always** UTC time for everything. I guess I can't use `time` fields and `time_select` for zone-sensitive things? – at. Oct 01 '12 at 19:41
  • I miswrote the above. I have to put `Time.zone="Pacific Time (US & Canada)"` at the beginning of all methods in the scaffold (or of course use a filter). But the issue still remains for `time` fields and `time_select` form helpers, they disregard the `Time.zone`. – at. Oct 01 '12 at 19:52
  • @at. *So I did a `Time.zone="Pacific Time (US & Canada)"` in the new method of a scaffold* - but you have to do it in a filter, to ensure it's called for every action. – jdoe Oct 01 '12 at 20:06
  • you could just put that `Time.zone` code at the top of every method as I described. I also mentioned "(or of course use a filter)". – at. Oct 01 '12 at 20:31
  • @at. I'm little confused: you're sometimes saying about `time`, about `datetime` and `date`. What exactly type of field do you have? – jdoe Oct 01 '12 at 20:55
  • I have multiple fields. Some are `datetime` and some are `time`. The `datetime`s now work great thanks to your help, but the `time` fields don't deal with timezones well at all. – at. Oct 01 '12 at 21:00
  • 1
    I ended up simply migrating my time fields to datetime fields. Annoying, but couldn't find any better solution. – at. Oct 03 '12 at 19:25
0

This has helped me in the past. You can do things like:

Time.now.in_time_zone("Central Time (US & Canada)")

See Rails: convert UTC DateTime to another time zone

Community
  • 1
  • 1
Timothy Hunkele
  • 877
  • 7
  • 15
  • 1
    That's great for displaying the right time. But creating/editing the time with the `datetime_select` and `time_select` form helpers is another story. – at. Oct 01 '12 at 19:41