In my MySQL database the updated_at field is being stored as UTC. Last week a record was entered at 7pm EDT and it's updated_at value is "2012-08-01 23:00:00". I'm trying to convert local time for a web client to UTC that can be compared against the updated_at field in the database.
在我的MySQL数据库中,updated_at字段存储为UTC。上周美国东部时间晚上7点输入了一条记录,其update_at值为“2012-08-01 23:00:00”。我正在尝试将Web客户端的本地时间转换为UTC,可以与数据库中的updated_at字段进行比较。
As an example, I'd like to convert '08/01/2012 07:00 pm' to '2012-08-01 23:00:00' (accounting for me being in EDT) but I'm missing the time zone aspect to the conversion. '7:00 pm' is local time and could be from any time zone. My current code:
作为一个例子,我想将'08 / 01/2012 07:00 pm'转换为'2012-08-01 23:00:00'(占我在EDT),但我错过了时区转换的方面。 '晚上7点'是当地时间,可能来自任何时区。我目前的代码:
ruby-1.9.2-head :015 > from_date = DateTime.strptime('08/01/2012 07:00 pm', '%m/%d/%Y %H:%M %p')
=> Wed, 01 Aug 2012 19:00:00 +0000
ruby-1.9.2-head :016 > from_date = Time.zone.parse(from_date.to_s).utc
=> 2012-08-01 19:00:00 UTC
Any thoughts?
有什么想法吗?
Edit: Also, I could use "%Z" in my strptime call but that's assuming I have the timezone in the value, which I currently do not. I guess I could use Javascript to send that along with the date/time value.
编辑:另外,我可以在我的strptime调用中使用“%Z”但是假设我在值中有时区,我目前没有。我想我可以使用Javascript将其与日期/时间值一起发送。
2 个解决方案
#1
22
local to utc
当地的utc
created_at.utc
created_at.utc
utc to local
对当地人说
created_at.localtime
created_at.localtime
#2
0
First I would check your time zone setting.
首先,我会检查您的时区设置。
In your environment.rb (Rails 2) or application.rb (Rails 3) file, you can set the default timezone with: config.time_zone = 'Eastern Daylight Time (EDT)'
在environment.rb(Rails 2)或application.rb(Rails 3)文件中,您可以设置默认时区:config.time_zone ='Eastern Daylight Time(EDT)'
Secondly I would look at this post for information and guidance and tips to meet your needs:
http://databasically.com/2010/10/22/what-time-is-it-or-handling-timezones-in-rails/
其次,我会查看这篇文章,以获取满足您需求的信息和指导以及提示:http://databasically.com/2010/10/22/what-time-is-it-or-handling-timezones-in-rails/
#1
22
local to utc
当地的utc
created_at.utc
created_at.utc
utc to local
对当地人说
created_at.localtime
created_at.localtime
#2
0
First I would check your time zone setting.
首先,我会检查您的时区设置。
In your environment.rb (Rails 2) or application.rb (Rails 3) file, you can set the default timezone with: config.time_zone = 'Eastern Daylight Time (EDT)'
在environment.rb(Rails 2)或application.rb(Rails 3)文件中,您可以设置默认时区:config.time_zone ='Eastern Daylight Time(EDT)'
Secondly I would look at this post for information and guidance and tips to meet your needs:
http://databasically.com/2010/10/22/what-time-is-it-or-handling-timezones-in-rails/
其次,我会查看这篇文章,以获取满足您需求的信息和指导以及提示:http://databasically.com/2010/10/22/what-time-is-it-or-handling-timezones-in-rails/