1

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?

2 Answers 2

2

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!
1
  • This is just great info. I think Windows version treats time zones differently, doesn't it? Jul 28, 2011 at 15:10
0
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.

2
  • Ok i know that but can I define it like: 'metric_update_time timestamp NOT NULL DEFAULT CONVERT_TZ(CURRENT_TIMESTAMP,'+02:00','+03:00')',
    – Igor Hrcek
    Jul 28, 2011 at 12:26
  • @Igor Hrcek: Nope, you can't do that.
    – Shef
    Jul 28, 2011 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.