Sterling Rose Design Blog

Rendering datetime in a user's timezone

0 Comments
Tags: Rails date and time

We recently had a client who wanted to account for a user’s time zone when they entered dates for various fields. For example, if a user were to input a new bill, the bill entry date should default to the current date. Time.now is what we had been using, but that would only give the right date if you were on the correct side of the magic UTC dividing line.

The solution to this problem involved 1) storing a user’s time zone in a string field in the User model, and 2) adjusting Time.now for that time zone.

For part 1, we used Rails’ built-in time_zone_select method:

<%= form.label :time_zone, "Time Zone" %>
<%= time_zone_select :time_zone, @user.time_zone, nil, :default => 
  "Pacific Time (US & Canada)" %>


For part 2, the solution was to add a method to the User model to get the user’s current date:
Read the whole post...

Does the world really need another Rails blog?

2 Comments
Tags: announcements

After a few weeks of coding it, I’ve finally kicked off my first purely “professional” weblog. I’ve been coding in Ruby on Rails for a little over a year now, and decided it was about time for me to start sharing some of the things I’ve learned and discovered and figured out.

This is not a news/announcements aggregation site. There are plenty of those out there, doing it much better than I have the time for.

So what are you likely to see here? Code snippets, tutorials (probably starting with a series on how to build a blog just like this one), obscure API documentation, mentions of plugins I’ve found useful or cool, etc. I plan to focus on some of the most commonly-used web functionality, with focus on doing things “the Rails Way” as much as possible.


Copyright 2007-2010, Sterling Rose Design. All rights reserved.