首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby:将UTC时区更改为UTC+或UTC - zone

Ruby:将UTC时区更改为UTC+或UTC - zone
EN

Stack Overflow用户
提问于 2014-07-31 06:39:59
回答 2查看 3K关注 0票数 1

我希望将utc时间转换为“utc+1”或“utc+something”时区,并将其转换回utc时区。

这就是我想做的事。我要求用户选择UTC时区ex: utc+4 .and,我正在使用Time.now.utc .Now获得当前的UTC时间,我想将这个utc时间转换为'utc+4‘。

在将该时间显示为“utc+4”之后,我希望将该时间等效的utc时区转换回来。

这是怎么做到的?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-31 09:43:42

如果您正在使用Rails,并且希望更改每个请求的时区,并在完成请求后将其重新设置。您可以使用Time.use_zone为用户设置时区(document:区域)

下面是我在Rails 4.1中测试的内容。

首先,建议为config.time_zone (in config/application.rb)设置sane默认值,例如,我将其设置为“孟买”(UTC+5.30)。(要列出时区,可以使用命令bundle exec rake time:zones:all)

代码语言:javascript
复制
module MyApp
  class Application < Rails::Application
     config.time_zone = 'Mumbai'
  end
end

在您的项目中,运行rails g model User name:string time_zone:stringbundle exec rake db:migrate

然后,通过rails控制台创建一些测试用户,运行rails c

代码语言:javascript
复制
Loading development environment (Rails 4.1.4)
irb(main):001:0> first_user = User.create!(name: 'zdk', time_zone: 'Bangkok')
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "users" ("created_at", "name", "time_zone", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", "2014-07-31 09:21:13.750710"], ["name", "zdk"], ["time_zone", "Bangkok"], ["updated_at", "2014-07-31 09:21:13.750710"]]
   (0.6ms)  commit transaction
=> #<User id: 1, name: "zdk", time_zone: "Bangkok", created_at: "2014-07-31 09:21:13", updated_at: "2014-07-31 09:21:13">
irb(main):002:0> second_user = User.create!(name: 'joe', time_zone: 'London')
   (0.1ms)  begin transaction
  SQL (0.8ms)  INSERT INTO "users" ("created_at", "name", "time_zone", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", "2014-07-31 09:21:31.299606"], ["name", "joe"], ["time_zone", "London"], ["updated_at", "2014-07-31 09:21:31.299606"]]
   (1.9ms)  commit transaction
=> #<User id: 2, name: "joe", time_zone: "London", created_at: "2014-07-31 09:21:31", updated_at: "2014-07-31 09:21:31">
irb(main):003:0>

尝试查询我们刚才创建的内容,您可以看到它使用了在Application (config.time_zone)中设置的时区。产出:

代码语言:javascript
复制
irb(main):003:0> first_user.created_at
=> Thu, 31 Jul 2014 14:51:13 IST +05:30
irb(main):005:0> second_user.created_at
=> Thu, 31 Jul 2014 14:51:31 IST +05:30

以及如何使用Time.zone处理每个请求的基本时区。转到您的ApplicationController (应用程序/控制器/应用程序_Controller.rb文件)。创建一个设置由around_filter调用的时区的方法(更详细信息:http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails )。我还创建了hello操作,将从根url路由。就像这样:

代码语言:javascript
复制
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  around_filter :set_time_zone

  def set_time_zone
   if true #if user loggin ?
     @user = User.first #Change to User.last to see the result.
     Time.use_zone(@user.time_zone) { yield }
   else
     yield
   end
  end

  def hello
    render plain: "Hello, user: #{@user.name}. Created: #{@user.created_at}"
  end

end

将应用程序uri路由到控制器方法,方法是编辑配置路由( config /routeres.rb),如下所示

代码语言:javascript
复制
Rails.application.routes.draw do
  root 'application#hello'
end

如果你设置正确的话。您应该有这种格式的输出。

对于第一个用户:Hello, user: zdk. Created: <Date> <Time> +0700

对于第二个用户:Hello, user: joe. Created: <Date> <time> +0100

总之,流程类似于:

代码语言:javascript
复制
               +------------+
               |   APP      |
               +------------+


           +           Use the Time.zone value instead if it's set
           |                      ^
       WRITE                      |
           v                      |  READ
                                  +
 Convert to UTC        Convert from UTC to config.time_zone
           +                      ^
           |                      |
       WRITE                      |  READ
           |                      |
           v                      +
       +--------------------------------+
       |                                |
       |           DB                   |
       |                                |
       |          UTC                   |
       +--------------------------------+

希望这能有所帮助。

票数 4
EN

Stack Overflow用户

发布于 2014-07-31 07:31:18

您可以使用ActiveSupport::TimeWithZone

例子:

代码语言:javascript
复制
#Define a TimeZone
Time.zone = 'Eastern Time (US & Canada)' # => 'Eastern Time (US & Canada)'
#Define a utc time
t = Time.utc(2007, 2, 10, 20, 30, 45)    # => '2007-02-10 20:30:45 UTC
#same time with gmt -5
t.in_time_zone                           # => Sat, 10 Feb 2007 15:30:45 EST -5:00 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25052312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档