如何在用户的时区显示时间 [英] How to display the time in user's timezone

查看:52
本文介绍了如何在用户的时区显示时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 rails 3.0.5 并且我已经 create_at 和 updated_at 存储在 UTC 中.现在我想在用户的时区中显示 created_at 时间.我相信可以从浏览器中选择用户的时区,然后将时间转换为用户的时区.

I am using rails 3.0.5 and I have created_at and updated_at stored in UTC. Now I want to display the created_at time in users' timezone. I believe it is possible to pick user's timezone from the browser and then convert time to user's timezone.

我确信 rails 会有一个 gem/plugin 来处理这样的事情.有事吗?

I am sure rails will have a gem/plugin to take care of something like this. Is there something?

推荐答案

Rails 默认将每个日期转换为 UTC,然后再将值存储到数据库中.这意味着,无论服务器时区如何,您的数据库中始终都有 UTC 日期.

Rails by default converts every date into UTC before storing the value into the database. This means that, regardless the server timezone, you always have UTC dates in your database.

为了将日期转换为用户的时区,您基本上有两种可能性:

In order to convert the dates into your user's timezone you have basically two possibilities:

  1. 服务器端方法
  2. 客户端方法

服务器端方法

如果您的站点允许注册,您可以将用户时区存储为用户首选项.在用户表中,存储用户时区.然后创建一个自定义帮助程序,您可以使用 in_time_zone 方法将任何日期/时间格式化为正确的时区.

Server-side approach

If your site allows registration, you can store the user timezone as user preference. In the user table, store the user timezone. Then create a custom helper you can use to format any date/time into the proper timezone using the in_time_zone method.

> t = Time.current
# => Mon, 23 Dec 2013 18:25:55 UTC +00:00 
> t.zone
# => "UTC" 
> t.in_time_zone("CET")
# => Mon, 23 Dec 2013 19:25:55 CET +01:00 

你的助手可能看起来像

def format_time(time, timezone)
  time.in_time_zone(timezone)
end

我通常也喜欢输出标准格式,使用 I18n.l 帮手

I normally also like to output a standard format, using the I18n.l helper

def format_time(time, timezone)
  I18n.l time.to_time.in_time_zone(timezone), format: :long
end

客户端方法

如果您的网站没有注册,或者您不想询问用户的时区,或者您只想使用用户系统时区,那么您可以使用 JavaScript.

Client-side approach

If your site has no registration or you don't want to ask your users for their timezone or you simply want to use the user system timezone, then you can use JavaScript.

我的建议是创建一个自定义助手,每次都以适当的方式打印出来,以便您可以创建一个通用的 JavaScript 函数来转换值.

My suggestion is to create a custom helper that will print out every time in a proper way so that you can create a generic JavaScript function to convert the values.

def format_time(time, timezone)
  time = time.to_time
  content_tag(:span, I18n.l(time, format: :long), data: { timezone: timezone, time: time.iso8601 })
end

现在,创建一个在 DOM 加载时执行的 JavaScript 函数,它将选择所有具有 data-time 属性的 HTML 标记.循环它们并使用给定时区中的适当时间更新 span 标记内的值.

Now, create a JavaScript function that is performed on DOM load and that will select all the HTML tags with data-time attribute. Loop them and update the value inside the span tag with the proper time in the given timezone.

一个简单的 jQuery 示例是

A simple jQuery example would be

$(function() {
    $("span[data-time]").each(function() {
        // get the value from data-time and format according to data-timezone
        // write the content back into the span tag
    });
});

我不会在这里发布完整的代码,因为通过简单的搜索就可以使用大量的 JavaScript 时间格式化程序.以下是一些可能的解决方案

I'm not posting the full code here, since there are plenty of JavaScript time formatters available with a simple search. Here's a few possible solutions

这篇关于如何在用户的时区显示时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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