Time problems in the development of Rails+mysql

Source: Internet
Author: User
Tags config local time log mysql set time

Time zone in MySQL

Show time zone information

Mysql> Show variables like '%time_zone% '; 
+------------------+--------+ 
| Variable_name    | Value  | 
+------------------+--------+ 
| system_time_zone | CST    | 
| Time_zone        | SYSTEM | 
+------------------+--------+ 
2 rows in Set (0.00 sec)

Set time zone information

+8:00 is China's time zone, East eight area.

mysql> Set time_zone = ' +8:00 ';

Query OK, 0 rows Affected (0.00 sec)

Time zone in Rails

Rails defaults to the UTC time, and then the read is UTC time.

Setting Config.time_zone only guarantees that the time written to the database is local, that is, when the object is created Created_at and Updated_at use the settings locally.

But it may be a UTC time to read it, and it may need to be converted on the interface.

Rails recommends using UTC time, which is unified, and is formatted as local time when the interface is displayed.

Rake Time:zones:all #查看所有时区

Rake Time:zones:local #查看本地时区

Default condition

We use

Rails G Scaffold Post title:string content:string

After building a model, open the "db/migrate/201200_create_posts.rb" file and see the following.

Class Createposts < activerecord::migration 
  def change 
    create_table:p osts do |t| 
      T.string:title 
      t.string:content 
     
      t.timestamps 
    end
  End

T.timestamps will help us generate created_at and updated_at two fields in the database, and these two fields will automatically be assigned by rails without our manual designation.

After you add a post to the interface, open the Log/production.log file and see the following.

INSERT into ' posts ' (' category_id ', ' content ', ' created_at ', ' Picture ', ' published ', ' title ', ' Updated_at ', ' url ') VALUES (1, ' post100 ', ' 2012-11-01 05:13:45 ', NULL, 0, ' post100 ', ' 2012-11-01 05:13:45 ', ' post100 ')

If you don't see this SQL statement, then you add the CONFIG/APPLICAITON.RB

Config.log_level = Debug

The debug level records each SQL in the log for easy debugging. After publishing to the production environment, modify this level of information.

Please note that the value of Created_at is clearly recorded at noon in China, but the time in the INSERT statement is UTC time, eight hours behind, because China is the East eight area +8:00.

The value of inserting a database is naturally this UTC time.

After setting the time zone

To resolve this issue, you can add the following configuration to the Config/application.rb file.

Config.active_record.default_timezone =: Local

Config.time_zone = ' Beijing '

Insert the data again, open the log file, you will find time into Beijing time, insert the database is also Beijing time. After entering Mysql-u root-p, the result of the query is also Beijing time.

Using in the View file

<%= Created_at%>

The result shown is

2012-11-01 13:39:26 +0800

No problem, but more time zone +0800 information.

If you use

<%= post.updated_at.to_s (:d b)%>

The result of the display is

2012-11-01 05:39:26

It's not +0800, but it's time to become UTC again.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.