mardi 8 septembre 2015

How to test database record with different time zone?

Now I am developing a rails project. My database is MySQL.

I check my MySQL's timezone:

mysql> SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM             | SYSTEM              |
+--------------------+---------------------+

And the SYSTEM timezone is:

mysql> SELECT EXTRACT(HOUR FROM (TIMEDIFF(NOW(), UTC_TIMESTAMP))) AS `timezone`;
+----------+
| timezone |
+----------+
|        9 |
+----------+ 

Also, my linux system's timezone is:

$ date +'%:z %Z'
+09:00 JST

Now, when I insert a record to database, the created_at value will be utc time. For example:

# Now is 14:25:59
Product.create(name: 'Best')
# select created_at from products;
# 5:25:59

So if I test the feature with rspec in my spec source, I should get that data from database and plus 9 to equal the current system time. Is it good?

Why the data been saved as a utc time with active_record? If use this configuration, is there a way to test the database record in a right way?

Aucun commentaire:

Enregistrer un commentaire