[LRUG] How does Rails decide how to parse ambiguous dates?
Paul Robinson
paul at 32moves.com
Tue Jul 26 06:48:18 PDT 2011
On 26 Jul 2011, at 14:20, John Winters wrote:
> You make reference to "helpers" - can you provide any pointers to those? I keep find myself going round and round in the documentation in the area of Date/Time/DateTime, with one bit referring to another bit, which refers you to a third bit and then back to the start, with none of them providing the hard information.
Oh Lord, this is actually a bit of a mess for complex cases, but if your use case is simple, it's dead easy.
You might want to get familiar with i18n:
http://guides.rubyonrails.org/i18n.html
You need to think about how to manage the user's locale (I normally initially guess it based on their IP address) and then expose it in the URL (so that the user can change it), and then you can use the "l" helper:
http://guides.rubyonrails.org/i18n.html#adding-date-time-formats
If you set the locale to one that the Rails locale code recognises:
https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
Then everything should "just work" even on assignment.
I'm working on a tool at the moment where we're working out flight times between continents, and this can get a little nasty if you're not careful when doing addition/substraction work because of timezone and people's own timezones changing. Even using TZInfo I've found it's best to sometimes just store stuff in straight UTC and then do some conversion work.
If you store everything in UTC and then store a user's timezone in a format like "Europe/London", all you have to do is a to_local on the object (assuming you set the Time.zone correctly), and then use the locale for it to display correctly.
Hope that makes sense,
Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20110726/37ce3225/attachment-0003.html>
More information about the Chat
mailing list