I want to change timezone in mysql from EDT to GMT ,
SELECT ##system_time_zone;
+--------------------+
| ##system_time_zone |
+--------------------+
| EDT |
+--------------------+
1 row in set (0.00 sec)
on researching google I am hitting its not possible ! I am pretty sure it should be.
For the current session, just:
SET ##session.time_zone='+00:00'; -- UTC
Or:
SET time_zone = '+00:00';
To make it permanent, you can change mysqld conf file (requires a server restart):
default_time_zone='+00:00'
Demo on DB Fiddle:
SELECT ##global.time_zone, ##session.time_zone;
##global.time_zone | ##session.time_zone
:----------------- | :------------------
SYSTEM | SYSTEM
SET ##session.time_zone='+00:00';
SELECT ##global.time_zone, ##session.time_zone;
##global.time_zone | ##session.time_zone
:----------------- | :------------------
SYSTEM | +00:00
Related
I have a table with the following columns (simplified): openingtime | timezone
This tables holds opening times. The datetime is in the timezone my server is in, timezone is the timezone of the location the opening time is for. I would like to query the datetime column ending up with the local time.
Example data:
2022-07-08 11:00:00 | Europe/London
My server is in Europe/Paris so the expected output would be 2022-07-08 10:00:00
The following only gives my NULL as results:
SELECT CONVERT_TZ(openingtime, 'Europe/Paris', timezone)
FROM openingtimes
To use CONVERT_TZ() you need to install the time-zone tables otherwise MySQL returns NULL.
To load time zone:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
After time zone loaded:
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.25 |
+-----------+
1 row in set (0.00 sec)
mysql> SELECT CONVERT_TZ('2022-07-08 11:00:00', 'Europe/London','GMT') ;
+----------------------------------------------------------+
| CONVERT_TZ('2022-07-08 11:00:00', 'Europe/London','GMT') |
+----------------------------------------------------------+
| 2022-07-08 10:00:00 |
+----------------------------------------------------------+
1 row in set (0.00 sec)
I verified that my global timezone is set to "+05:30" but the system_time_zone is showing UTC. I want to set system_time_zone to IST or "+05:30". I searched over the internet but couldn't find the solution.
mysql> SELECT ##global.time_zone;
+--------------------+
| ##global.time_zone |
+--------------------+
| +05:30 |
+--------------------+
1 row in set (0.00 sec)
mysql> SELECT ##system_time_zone;
+--------------------+
| ##system_time_zone |
+--------------------+
| UTC |
+--------------------+
1 row in set (0.00 sec)
I just want to set system_time_zone to IST. Can someone please help me?
The system timezone is not related to the database but to the OS. Check the documentation of your OS or share with us which OS you use.
Anyway, I would recommend always using UTC for the OS, the database and in your app. Then you can format the date/time in the right timezone at display time, based on the user's preference.
I am trying to set the time zone in MySQL so that it matches the Ubuntu 20.04 time zone. When I check the error.log for MySQL I see a different set of data for the time zone.
Here is what I have from the system, MySQL 8.0.23 and the error.log:
sudo nano /etc/mysql/my.cnf
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[mysqld]
default-time-zone = "SYSTEM"
Console commands:
date
Sat 01 Jan 2022 10:01:29 PM EST
var/log/mysql/error.log
2022-01-02T02:31:15.751387Z 0 [System] [MY-010931] [Server]
mysql
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2022-01-01 22:04:43 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT ##global.time_zone;
+--------------------+
| ##global.time_zone |
+--------------------+
| SYSTEM |
+--------------------+
1 row in set (0.00 sec)
After further research I found what I was looking for in the post at stackoverflow.com/questions/35123049/… this change did the trick:
[mysqld]
log_timestamps = SYSTEM
I'm currently investigating the behaviour of MySQL dates and times with the clients and servers running in different time zones. I am writing test scripts in order to test what is going on when I run with the client and the server in certain time zones and as part of that work I need to be able to control the session time zone. I am having problems setting the time zone to the same one that my system is currently defaulting to, so that it behaves exactly the same.
I would like to set the mysql SESSION time_zone variable set so that dates are interpreted in the same way as when the time_zone variable is left as the default SYSTEM value. When I manually set the time_zone to Europe/London I expect UNIX_TIMESTAMP to continue to behave in the same way as when the the time_zone is set to SYSTEM, because my mysql system_time_zone is set to being in London (either GMT Summer time, or GMT Standard time). Instead I am finding that the UNIX_TIMESTAMP function returns different values depending on whether the time_zone is set to 'SYSTEM' or 'Europe/London', as you can see from the following:-
With date set to 25th August in the UK summer time and MySQL server rebooted so that it uses system_time_zone = 'GMT Summer Time' you can see that the UNIX_TIMESTAMP function returns different values when time_zone is SYSTEM (i.e. GMT Summer Time) and when it is set to Europe/London:-
$ date; mysql --protocol=TCP -uroot -proot mysql -e "show variables LIKE 'system_time_zone'; SET SESSION time_zone = 'SYSTEM'; SELECT ##SESSION.time_zone; select NOW(); select UNIX_TIMESTAMP('1970-01-01 12:00:00');SET SESSION time_zone = 'Europe/London'; SELECT ##SESSION.time_zone; select NOW(); select UNIX_TIMESTAMP('1970-01-01 12:00:00');";
25 Aug 2015 09:12:20
+------------------+-----------------+
| Variable_name | Value |
+------------------+-----------------+
| system_time_zone | GMT Summer Time |
+------------------+-----------------+
+---------------------+
| ##SESSION.time_zone |
+---------------------+
| SYSTEM |
+---------------------+
+---------------------+
| NOW() |
+---------------------+
| 2015-08-25 09:12:21 |
+---------------------+
+---------------------------------------+
| UNIX_TIMESTAMP('1970-01-01 12:00:00') |
+---------------------------------------+
| 43200 |
+---------------------------------------+
+---------------------+
| ##SESSION.time_zone |
+---------------------+
| Europe/London |
+---------------------+
+---------------------+
| NOW() |
+---------------------+
| 2015-08-25 09:12:21 |
+---------------------+
+---------------------------------------+
| UNIX_TIMESTAMP('1970-01-01 12:00:00') |
+---------------------------------------+
| 39600 |
+---------------------------------------+
MyPc
$
With date set to 24th December in the UK winter time and mysql server rebooted so that it uses system_time_zone = 'GMT Standard Time' I still get the same results, and so the whether system_time_zone is in Summer or Standard time doesn't seem to make a difference:-
$ date; mysql --protocol=TCP -uroot -proot mysql -e "show variables LIKE 'system_time_zone'; SET SESSION time_zone = 'SYSTEM'; SELECT ##SESSION.time_zone; select NOW(); select UNIX_TIMESTAMP('1970-01-01 12:00:00');SET SESSION time_zone = 'Europe/London'; SELECT ##SESSION.time_zone; select NOW(); select UNIX_TIMESTAMP('1970-01-01 12:00:00');";
25 Dec 2015 09:14:13
+------------------+-------------------+
| Variable_name | Value |
+------------------+-------------------+
| system_time_zone | GMT Standard Time |
+------------------+-------------------+
+---------------------+
| ##SESSION.time_zone |
+---------------------+
| SYSTEM |
+---------------------+
+---------------------+
| NOW() |
+---------------------+
| 2015-12-25 09:14:14 |
+---------------------+
+---------------------------------------+
| UNIX_TIMESTAMP('1970-01-01 12:00:00') |
+---------------------------------------+
| 43200 |
+---------------------------------------+
+---------------------+
| ##SESSION.time_zone |
+---------------------+
| Europe/London |
+---------------------+
+---------------------+
| NOW() |
+---------------------+
| 2015-12-25 09:14:14 |
+---------------------+
+---------------------------------------+
| UNIX_TIMESTAMP('1970-01-01 12:00:00') |
+---------------------------------------+
| 39600 |
+---------------------------------------+
MyPc
$
My Windows time zone is set to Europe/London as you can see from the cygwin TZ variable:-
MyPc
$ echo $TZ
Europe/London
MyPc
$
My questions are:-
Why are the UNIX_TIMESTAMPs returned different when it seems the time zones should be seen as the same?
What should I do to set my time zone so that UNIX_TIMESTAMP (and everything else) behaves the same as the when time_zone is left set to the default SYSTEM value?
Update:
I think I may have answered my own question. I have found out that GMT and UTC are NOT the same before 31st Oct 1971 as you can see from:-
http://www.timeanddate.com/time/change/uk/london?year=1970
http://www.timeanddate.com/time/change/uk/london?year=1971
(try the above links for 1972 and 1973 too)
This is because BST didn't exist in the summer of 1971 and so in that summer GMT was an hour ahead of UTC and then on 31st Oct 1971 the clocks went back for the first time and GMT became in sync with UTC.
So if I do the same test as I did above, but use 1973 as the year then I get the same values for the Unix timestamps (as expected). This must mean that when I leave the time zone as SYSTEM MySQL is interpretting the date as a UTC date and so giving 12 * 3600 = 43200 but when I set the time_zone to Europe/London it thinks the time means UK GMT in the winter of 1970 which is an hour behind UTC and so midday Jan 1st 1970 is 11:00 UTC = 11 * 3600 = 39600, as you can see from the following:-
$ date; mysql --protocol=TCP -uroot -proot mysql -e "show variables LIKE 'system_time_zone'; SET SESSION time_zone = 'SYSTEM'; SELECT ##SESSION.time_zone; select NOW(); select UNIX_TIMESTAMP('1973-01-01 12:00:00');SET SESSION time_zone = 'Europe/London'; SELECT ##SESSION.time_zone; select NOW(); select UNIX_TIMESTAMP('1973-01-01 12:00:00');";
25 Aug 2015 12:33:36
+------------------+-----------------+
| Variable_name | Value |
+------------------+-----------------+
| system_time_zone | GMT Summer Time |
+------------------+-----------------+
+---------------------+
| ##SESSION.time_zone |
+---------------------+
| SYSTEM |
+---------------------+
+---------------------+
| NOW() |
+---------------------+
| 2015-08-25 12:33:37 |
+---------------------+
+---------------------------------------+
| UNIX_TIMESTAMP('1973-01-01 12:00:00') |
+---------------------------------------+
| 94737600 |
+---------------------------------------+
+---------------------+
| ##SESSION.time_zone |
+---------------------+
| Europe/London |
+---------------------+
+---------------------+
| NOW() |
+---------------------+
| 2015-08-25 12:33:37 |
+---------------------+
+---------------------------------------+
| UNIX_TIMESTAMP('1973-01-01 12:00:00') |
+---------------------------------------+
| 94737600 |
+---------------------------------------+
MyPc
$
To avoid this problem I will do all must tests from now on using dates after 1973, and just pity anyone who does have to deal with dates pre 1973 in MySQL. I wouldn't be surprised if this is a MySQL bug because it seems system time zone of GMT should map exactly to the SESSION timezone of Europe/London, but doesn't.
In my server, I have the following from the command line:
]$ date
Fri Sep 16 13:47:02 JST 2011
Which is correct.
in mysql, I have the following:
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2011-09-16 04:50:21 |
+---------------------+
1 row in set (0.00 sec)
mysql> SELECT ##global.time_zone, ##session.time_zone;
+--------------------+---------------------+
| ##global.time_zone | ##session.time_zone |
+--------------------+---------------------+
| SYSTEM | SYSTEM |
+--------------------+---------------------+
1 row in set (0.00 sec)
So, it is set to the system time, but then why is not showing the same time?
You just need to restart mysqld after altering timezone of System..
The Global time zone of MySQL takes timezone of System. When you change any such attribute of system, you just need a restart of Mysqld.
That's it.
Your queries are returning the values in UTC but from the command line output your system is in JST. Since the variables are returning SYSTEM I would suggest that perhaps your configuration has identified system as UTC. Check out your my.cnf as suggested here How do I make MySQL's NOW() and CURDATE() functions use UTC?.