ruby on Rails 转换为用户时区
我拥有所需的所有时区信息,并且它根据 此railscast。一切都非常适合所有正常目的,例如在表格中显示时间,但是我想知道如何将以下内容转换为 current_users
保存的时区。
我发现每小时的呼叫总数:
Calls.count(:group => DATE_PART('hour', start_time),
:conditions => ["start_time BETWEEN ? AND ?", start, finish]
此查询提取开始时间和结束时间之间一小时内进行的所有呼叫。唯一的问题是它会拉回数据库中存储为 UTC 的时间。我用 Time.now.utc_offset 编写了自己的小转换方法,但意识到这行不通。将用户保存的 time_zone
与从数据库中获取的时间进行比较并添加 utc_offset
的好方法是什么。
time_zone
作为字符串存储在数据库东部时间(美国和加拿大)中。
解决这个问题的最佳方法是什么,我可以更改以将用户的偏移量保存在数据库中,但在执行此操作之前,我宁愿看看是否有更简单的方法。
编辑
更清楚地说,查询返回小时数和计数的哈希值。例如:
Hour Count
"5" 20
"6" 15
"7" 35
... ...
小时以 UTC 偏移小时的形式返回,我需要将该数字转换为用户时区,例如,如果是东部时间(美国和加拿大),则循环遍历哈希并将 -4 添加到那个小时。
编辑2
感谢安迪,我能够非常简单地解决这个问题。您要做的第一件事是将 Time 对象设置为当前用户的时区。所以:
Time.zone = @current_user.time_zone
=> "Eastern Time (US & Canada)
然后将当前用户的偏移量添加到散列内的“小时”中,
hour + Time.now.in_time_zone.utc_offset / 3600
这很不言自明,但这会获取您之前设置的区域的时间,并找到以秒为单位的 UTC 偏移量,这就是我们除以 3600 的原因。
I have all the time zone information that is needed and it saves all of the information correctly according to this railscast. Everything works great for all the normal purposes like displaying time's in a table, however I am wondering how I can convert the following to the current_users
saved time zone.
I have a finds the total number of calls hourly:
Calls.count(:group => DATE_PART('hour', start_time),
:conditions => ["start_time BETWEEN ? AND ?", start, finish]
This query pulls in all all the calls that were made in an hour between the start and finish time. The only thing is that it's pulling back the time in the database which is stored as UTC. I wrote my own little conversion method with Time.now.utc_offset
but realized that wouldn't work. What would be a good way to compare the users saved time_zone
with what is coming down from the database and add the utc_offset
.
The time_zone
is stored as a string in the database Eastern Time (US & Canada).
What is the best way of figuring this out, I can change to save the user's offset in the database but before I go do that I would rather see if there is an easier way.
Edit
To be more clear, the query returns a hash of hours and the count. So for example:
Hour Count
"5" 20
"6" 15
"7" 35
... ...
The hour is coming back as a UTC offset hour, I need to convert that digit to the users time zone, so for example, if it's Eastern Time (US and Canada) you loop through the hash and add -4 to the hour.
Edit 2
Thanks to Andy I was able to figure this out pretty simply. The first thing you want to do is set the Time object to the current users Time zone. So:
Time.zone = @current_user.time_zone
=> "Eastern Time (US & Canada)
Then Add the current user's offset to the "Hour" inside the hash
hour + Time.now.in_time_zone.utc_offset / 3600
Pretty self explanatory, but that gets the time of the zone you set earlier, and finds the UTC offset in seconds which is why we divide by 3600.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有帮助吗?
在我之前发帖的人删除了他的答案(这比我的答案好得多)。如果他带回答案,我会再次删除我的答案。
Does this help?
Whoever posted before me deleted his answer (it was much better than my answer). If he brings his answer back I'll take mine down again.