rails - 将DateTime转换为UTC,然后保存到服务器 [英] rails - convert DateTime to UTC before saving to server

查看:141
本文介绍了rails - 将DateTime转换为UTC,然后保存到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何,但是我的控制台和我的服务器有两个不同的时区为DateTime.now。如果我在我的控制台中运行DateTime.now,则返回以下内容:

I don't know how, but my console and my server have two different timezones for DateTime.now. If I run DateTime.now in my console, the following is returned:

Wed, 04 Dec 2013 14:27:23 -0500 

但是,我的任务模型中有以下内容:

However, I have the following in my Task model:

def self.already_expired
    where("time_frame < ?", DateTime.now)
end

上面的代码的目标是在time_Frame是过去的日期的任务上消除一些任务方法。这里是我所说的方法:

The goal of the code above is to fire off some task methods on tasks where 'time_Frame' is a past date. Here is where i call the method:

task :change_it => :environment do
  puts "yo yo you yo you"
  @tasks = Task.already_expired
  @tasks.each do |task|
        puts "Kalabar" + task.inspect + "now time is:" + DateTime.now.to_s
  end
end

服务器日志显示:

Task Load (1.2ms)  SELECT "tasks".* FROM "tasks" WHERE (time_frame < '2013-12-04 17:25:37')
    Kalabar#<Task id: 3, title: "task 1 - past", content: "Should show this one", 
        created_at: "2013-12-04 17:05:53", updated_at: "2013-12-04 17:05:53", 
        schedule_id: 2, amount: nil, time_frame: "2013-12-02 08:15:00">
        now time is:2013-12-04T12:25:37-05:00
    Kalabar#<Task id: 5, title: "another task - past", 
        content: "should show this one", created_at: "2013-12-04 17:11:23", 
        updated_at: "2013-12-04 17:11:23", schedule_id: 3, amount: nil, 
        time_frame: "2013-12-03 02:07:00">now time is:2013-12-04T12:25:37-05:00
    Kalabar#<Task id: 6, title: "another task - future", 
        content: "should not show at first", created_at: "2013-12-04 17:11:23",
        updated_at: "2013-12-04 17:25:09", schedule_id: 3, amount: nil, 
        time_frame: "2013-12-04 12:27:00">now time is:2013-12-04T12:25:37-05:00

所以,将time_frame与2013-12-04 12:25:37进行比较,这是UTC 。但是,当我打印DateTime.now.to_s时,它会打印2012-12-04 08:15:00。

So, it is comparing time_frame to '2013-12-04 12:25:37 which is UTC. However, when i have it print DateTime.now.to_s, it prints '2012-12-04 08:15:00'.

在进入数据库之前,我想将所有内容转换为UTC。我也不明白为什么它有两个不同的时区DateTime.now这是我的问题

I want to convert everything to UTC before it goes into the database. I also don't understand why it has two different timezones for DateTime.now Here are my questions


  1. 为什么有两个不同的时区为DateTime .now

  2. 在保存到数据库之前,如何将时区转换为UTC?

更新:

我将以下内容放在我的application.rb文件:

I put the following in my application.rb file:

config.time_zone = 'Eastern Time (US & Canada)'

现在,用户在东部时间输入日期时间,并将其存储为UTC时间。所以现在,time_frame正好与DateTime.now进行了比较。问题是如果我把time_frame放在东部时间。我不太确定发生了什么。它存储在UTC,但在东部时间打印。这是危险/正确吗?我添加了一个'puts time_Frame'到任务代码块,如下所示:

And now the user enters in a datetime in Eastern Time, and it gets stored as a UTC time. So now time_frame is being compared to DateTime.now correctly. The issue is that if i puts time_frame, it puts it in Eastern time. I'm not really sure whats going on. Its storing it in UTC but printing it in Eastern time. Is this dangerous / correct? I added a 'puts time_Frame' to the task block of code like this:

task :change_it => :environment do
  @tasks = Task.already_expired
  @tasks.each do |task|
        puts "Kalabar" + task.inspect + "now time is:" + DateTime.now.to_s
      puts "time_frame is:" + task.time_frame.to_s
  end
end

并输出:

Kalabar#<Task id: 3, title: "task 1 - past", content: "Should show this one", created_at: "2013-12-04 17:05:53", updated_at: "2013-12-04 17:05:53", schedule_id: 2, amount: nil, time_frame: "2013-12-02 08:15:00">now time is:2013-12-04T15:27:22-05:00
time_frame is:2013-12-02 03:15:00 -0500
Kalabar#<Task id: 7, title: "Task 1 - past by days", content: "Should always show", created_at: "2013-12-04 20:20:40", updated_at: "2013-12-04 20:20:40", schedule_id: 4, amount: nil, time_frame: "2013-12-02 09:02:00">now time is:2013-12-04T15:27:22-05:00
time_frame is:2013-12-02 04:02:00 -0500
Kalabar#<Task id: 8, title: "Task 2 - past by minutes", content: "should always show", created_at: "2013-12-04 20:20:40", updated_at: "2013-12-04 20:20:40", schedule_id: 4, amount: nil, time_frame: "2013-12-04 20:15:00">now time is:2013-12-04T15:27:22-05:00
time_frame is:2013-12-04 15:15:00 -0500

所以,因为你可以看到,在数据库中它是UTC,但当它放在它在东部。为什么这样做/是危险的吗?

So, as you can see, in the database it is UTC, but when it puts it it is in Eastern. Why is it doing this / is it dangerous?

推荐答案

这是activerecord的默认行为,所有的时间都将被保存到数据库以UTC格式。你不需要转换它。无论何时初始化当前时间,它将返回一个时间对象,意味着不考虑时区,它将指向特定的时间。无论您在不同时区的时间如何,您都可以将其转换为UTC。所以,我不认为找到当前时间应该是有任何问题的,因为在相应的utc时间内,如何保存时间。

It is the default behaviour of activerecord that all your time will get saved into database in UTC format. You don't need to convert it. Whenever you are initializing current time it will return you a time object that means irrespective of timezone it will point to a particular time. Whatever time you are getting in different time zone all are pointing to same time if you will convert it into UTC. So, I don't think there should be any problem with finding current time, because any how the time will get stored in respective utc time.

时区问题只有当您尝试使用字符串(或通过传递DateTime属性)初始化时间对象时才会发生。使用系统时区解析时间对象,而不是使用application.rb中设置的时区。所以在这种情况下,最好使用ActiveSupport时区对象来解析时间。

这篇关于rails - 将DateTime转换为UTC,然后保存到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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