如何在Rails中存储时区信息 [英] How to store timezone info in rails

查看:80
本文介绍了如何在Rails中存储时区信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的rails应用程序中,用户可以创建问题并将其发布,任何国家的任何人都可以对此做出回应. 我们正在为此设计数据库结构.因此,计划使用一些js来获取用户时区,并回答转换该时间并将其存储在单独的列(tz_created_at)中. 因此in中创建的日期将以utc格式存储,并在另一列中说tz_created_at,datetime将存储为用户的时区转换时间. (即)在created_at列中 irb(main):013:0> DateTime.now.utc => Sun,2014年3月30日18:54:46 +0000

In my rails application,users can create questions and publish that, anybody from any country can response for that. We are designing database structure for that. so planning to get user timezone using some js and while answering converting that time and to store in a separate column(tz_created_at). so in created at the date will be stored in utc format, and in another column say tz_created_at the datetime will be stored as user's timezone converted time. (ie) in created_at column i have irb(main):013:0> DateTime.now.utc => Sun, 30 Mar 2014 18:54:46 +0000

在tz_created_at列中,我有 irb(main):015:0> DateTime.now + 6.hours(从用户所在的时区开始)=> 2014年3月31日,星期一06:58:31 +0600

And in tz_created_at column i have irb(main):015:0> DateTime.now + 6.hours(from user's timezone) => Mon, 31 Mar 2014 06:58:31 +0600

我们正在使用黑子solr来显示一段时间内的一些统计信息.

we are using sunspot solr to show some statistics in response over the time period.

因此,在查询特定问题的答案时,我将在tz_created_at列中进行搜索.这种方法正确吗?如果我错了,请纠正我

so while querying for responses for a particular question i will search in tz_created_at column. is this approach is correct . please correct me if i am wrong

推荐答案

您可能不想将日期时间存储为UTC,然后将用户的时区存储在另一列中,而不是这样做.这样一来,您随时可以在不影响实际时间的情况下更改时区(例如,用户移动国家/地区).当您向用户显示时间时,您可以轻松地将时间转换为他们的区域以显示它.

Instead of doing that, you probably want to store the datetime as UTC, then store the user's timezone in another column. By doing that you can always change the timezone if you need to without affecting the true time (e.g. the user moves country). When you display a time to the user you can easily convert the time to their zone to display it.

如果您是railscasts的订户,那里有一个视频 ,但如果您不是订阅者,则有年龄较大的人免费观看,可让您大致了解最佳做法.

If you're a subscriber of railscasts there's a video on it there, but if you're not a subscriber there's an older one that's free to watch that will give you a general idea of best practices.

这篇关于如何在Rails中存储时区信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆