开发者

Custom time in MYSQL on update

I have a table with some rows and within them there is a definition that goes like this:

  `metric_update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

So what i actually want it to do is to automatically insert a timestamp when inserting data into that table. And it 开发者_开发问答does. But what I need is to write a GMT based time into that field (current server time is like GMT+2).

Is there a way to say to MYSQL to do such thing?


If your server time and timezone settings are configured correctly, then internally all times stored in TIMESTAMP columns are converted to GMT (since that's what Unix timestamp mandates). They're converted back to your session timezone when you retrieve this data. If you want it presented in GMT timezone, you need to do conversion while retrieving data not while inserting.

See the console dump below for example. You can run these commands yourself to check.

mysql> use test;
Database changed
mysql> -- let's create a table we'll be working with
mysql> CREATE TABLE tsTable (
    -> ID INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    -> ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    -> );
Query OK, 0 rows affected (0.08 sec)

mysql> -- let's check current time as well as timezone settings
mysql> SELECT CURTIME(),@@global.time_zone, @@session.time_zone;
+-----------+--------------------+---------------------+
| CURTIME() | @@global.time_zone | @@session.time_zone |
+-----------+--------------------+---------------------+
| 16:25:51  | SYSTEM             | +02:00              |
+-----------+--------------------+---------------------+
1 row in set (0.05 sec)

mysql> -- inserting empty row to table to trigger auto timestamp
mysql> INSERT INTO tsTable VALUES (null,null);
Query OK, 1 row affected (0.00 sec)

mysql> -- looks like the time in my local timezone is stored in table
mysql> SELECT * FROM tsTable;
+----+---------------------+
| ID | ts                  |
+----+---------------------+
|  1 | 2011-07-28 16:26:25 |
+----+---------------------+
1 row in set (0.00 sec)

mysql> -- switching to GMT
mysql> SET SESSION time_zone = '+0:00';
Query OK, 0 rows affected (0.00 sec)

mysql> -- check current time and timezone settings again
mysql> SELECT CURTIME(),@@global.time_zone, @@session.time_zone;
+-----------+--------------------+---------------------+
| CURTIME() | @@global.time_zone | @@session.time_zone |
+-----------+--------------------+---------------------+
| 14:27:53  | SYSTEM             | +00:00              |
+-----------+--------------------+---------------------+
1 row in set (0.00 sec)

mysql> -- note: CURTIME() returns time two hours 'earlier' than before
mysql> -- let's see what's stored in the table again
mysql> SELECT * FROM tsTable;
+----+---------------------+
| ID | ts                  |
+----+---------------------+
|  1 | 2011-07-28 14:26:25 |
+----+---------------------+
1 row in set (0.00 sec)

mysql> -- TIMESTAMP is two hours 'earlier' than before too! Magick!


INSERT INTO table_name(column1, metric_update_time) VALUES('dummy', CONVERT_TZ(CURRENT_TIMESTAMP,'+02:00','+03:00');

This will convert the inserted timestamp from GMT+2 to GMT+3.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜