如何在Rails中存储时区信息

编程入门 行业动态 更新时间:2024-10-28 16:28:42
本文介绍了如何在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中存储时区信息

本文发布于:2023-10-19 11:19:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507335.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时区   如何在   信息   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!