I'm working on a Rails application that's kind of like a blog. Users create Entries. I'm trying to work out how to handle time storage and display. I've read this writeup about Rails timezone support.
It's great but it doesn't cover what my app needs to do. It only looks at cases where you want to convert stored time to the current logged in user's time zone. In contrast, the effect I want is...
A user creates an entry in California at 10:00 a.m.
A couple years later he moves to New York and then at some point looks at his old entry. The "created" date should say "10:00 a.m." He doesn't care about time zones. He just wants to know what time of day he felt like it was when he wrote the entry.
If he then edits the Entry in New York the displayed "modified" date is, again, his subjective time of day when he made the edit. (Let's assume he went to "preferences" and changed his time zone setting when he moved.)
Also, for the sake of thoroughness, the app should be able to report the "real" absolute time when an Entry was created or updated.
(Note -- my imaginary user is a guy, but for women it should work roughly the same way.)
The way I'm thinking of implementing it is...
Have the attributes
User#time_zone
,Entry#created_at_utc
, andEntry#updated_at_utc
in addition to the standardcreated_at
andupdated_at
.The user selects their time zone from a menu when they sign up. (They can change it later if they want.)
The app uses
User#time_zone
to storecreated_at
andupdated_at
in the user's subjective local time. If it's 10:00 a.m. for them, the app writes "10:00 a.m." to the DB.The app also saves the current UTC time in the aforementioned
_utc
fields to deal with the last requirement above.
Is that a good way to do it? Is there a better way?