---
title: "rails mysql 日期格式问题"
author: "暮雨风吹"
date: "2014-03-23T19:11:00Z"
tags: []
slug: "rails-mysql"
draft: false
lastmod: "2014-04-15T14:02:32Z"
id: 2046300
url: "https://dreamskyforjava.iteye.com/blog/2046300"
---

标签：[rails](http://blog.51cto.com/tag-rails.html) [mysql](http://blog.51cto.com/tag-mysql.html) [time\_zone](http://blog.51cto.com/tag-time_zone.html)

原创作品，允许转载，转载时请务必以超链接形式标明文章 [原始出处](http://virusswb.blog.51cto.com/115214/1046723) 、作者信息和本声明。否则将追究法律责任。[http://virusswb.blog.51cto.com/115214/1046723](http://virusswb.blog.51cto.com/115214/1046723)

**MySQL中的时区**

**显示时区信息**

```

mysql>showvariableslike'%time_zone%';
+------------------+--------+
|Variable_name|Value|
+------------------+--------+
|system_time_zone|CST|
|time_zone|SYSTEM|
+------------------+--------+
2rowsinset(0.00sec)
```

**设置时区信息**

+8:00是中国所在的时区，东八区。

```

mysql>settime_zone='+8:00';
QueryOK,0rowsaffected(0.00sec)
```

**Rails中的时区**

rails默认就是写入utc时间，然后读取也是utc时间。

设置config.time\_zone只能保证写入数据库的时间是local，就是保证创建对象的时候created\_at和updated\_at使用设置的本地时间。

但是读出来的时候还是有可能是utc时间，有可能需要在界面上转换的。

rails推荐使用utc时间，这样就统一了，只是在界面显示的时候格式化为本地时间。

```

raketime:zones:all#查看所有时区
raketime:zones:local#查看本地时区
```

**默认情况**

我们使用

```
railsg scaffold posttitle:stringcontent:string
```

生成一个model之后，打开【db/migrate/201200\_create\_posts.rb】文件看到下面的内容。

```

classCreatePosts<ActiveRecord::Migration
defchange
create_table:postsdo|t|
t.string:title
t.string:content

t.timestamps
end
end
end
```

t.timestamps会帮我们在数据库中生成created\_at和updated\_at两个字段，这两个字段rails会自动赋值不用我们手动指定。

在界面上添加post之后，打开log/production.log文件，会看到下面的内容。

```
INSERTINTO`posts`(`category_id`,`content`,`created_at`,`picture`,`published`,`title`,`updated_at`,`url`)VALUES(1,'post100','2012-11-0105:13:45',NULL,0,'post100','2012-11-0105:13:45','post100')
```

如果没有看到这条sql语句，那么就是在config/applicaiton.rb中添加

```
config.log_level=debug
```

debug的级别会在日志中记录每个sql，方便调试。在发布到生产环境之后，修改这个级别信息。

请注意created\_at的值，明明是中国时间中午插入的记录，但是insert语句中的时间却是utc时间，落后八个小时，因为中国是东八区+8:00。

插入数据库的值自然也就是这个utc时间。

**设置时区之后**

解决这个问题可以在config/application.rb文件中添加下面的配置。

```

config.active_record.default_timezone=:local
config.time_zone='Beijing'
```

再次插入数据，打开log文件，就会发现时间变成了北京时间，插入数据库的也是北京时间。进入mysql -u root -p之后，查询的结果也是北京时间。

在view文件中使用

```
<%=created_at%>
```

显示的结果是

```
2012-11-0113:39:26+0800
```

是没有问题的，但是多了时区+0800信息。

要是使用

```
<%=post.updated_at.to_s(:db)%>
```

显示的结果就是

```
2012-11-0105:39:26
```

没有了+0800，但是时间又变成了utc时间了。

使用

```
<%=post.updated_at.localtime.to_s(:db)%>
```

就变成

```
2012-11-0113:39:26
```

这下没有时区+0800信息，时间也是本地时间了。**就是先转换为本地时间，然后在进行格式化。**

```

created_at.utc        #转换为utc时间
created_at.localtime  #转换为local时间
```

还有就是在rails console中有一点特别。

```

1.9.3-p286:013>p=Post.last
PostLoad(0.3ms)SELECT`posts`.*FROM`posts`ORDERBY`posts`.`id`DESCLIMIT1
=>#<Postid:67,title:"发郭德纲的法国队",content:"asdfasdf",created_at:"2012-11-0105:39:26",updated_at:"2012-11-0105:39:26",url:"df",category_id:1,published:false,picture:nil>
1.9.3-p286:014>p.created_at
=>Thu,01Nov201213:39:26CST+08:00
```

大家注意到了吗，在p=Post.last之后查询的结果显示created\_at的时间是utc时间，但是等你敲入p.created\_at之后，显示的值就变成了本地时间。

**总结**

时区以及日期的格式化是每个程序员的必修课，就像字符串的各种处理一样重要，而且使用频率很高。

rails默认就是写入utc时间，然后读取也是utc时间。

设置config.time\_zone只能保证写入数据库的时间是local，就是保证创建对象的时候created\_at和updated\_at使用设置的本地时间。

但是读出来的时候还是有可能是utc时间，有可能需要在界面上转换的。

rails推荐使用utc时间，这样就统一了，只是在界面显示的时候格式化为本地时间。

对于任何应用来说，遇到时区问题，都应该考虑语言本身和存储本身，甚至是操作系统本身的时区设置和一些默认值，这样才能最终较好的解决时区问题。

**参考文献**

1.[Convert local time to UTC in Rails](http://stackoverflow.com/questions/11833435/convert-local-time-to-utc-in-rails)

2.[Rails 3 default datetime format without UTC](http://stackoverflow.com/questions/3694352/rails-3-default-datetime-format-without-utc)

3.[rails 有关时区的设置的问题](http://kkito.cn/index.php/blog/getArticle/304/rails_%E6%9C%89%E5%85%B3%E6%97%B6%E5%8C%BA%E7%9A%84%E8%AE%BE%E7%BD%AE%E7%9A%84%E9%97%AE%E9%A2%98_zz)

4.[Date Time Format in RUBY](http://madhanror.blogspot.com/2010/12/date-time-format-in-ruby.html)

5.[rails time format](http://bbs.chinaunix.net/thread-3593247-1-1.html)

6.[完美解决rails中国时区时间设置](http://hi.baidu.com/you5a_com/item/541fcfcc13f19dd1ee183b45)

7.[Time Zones in Rails 2.1](http://railscasts.com/episodes/106-time-zones-in-rails-2-1)

8.[MySQL时区设置](http://blog.sina.com.cn/s/blog_6183b4210100fuel.html)

9.[Rails内置的时间格式化](http://www.360doc.com/content/09/0317/18/4131_2836807.shtml)

本文出自 “[突破中的IT结构师](http://virusswb.blog.51cto.com)” 博客，请务必保留此出处[http://virusswb.blog.51cto.com/115214/1046723](http://virusswb.blog.51cto.com/115214/1046723)