How to change mysql timezone to UTC from EDT time - mysql

I have installed mysql in my linux server. My server timezone is UTC but mysql timezone in EDT. Now i want to change mysql timezone to UTC.
when i run date in linux command, it shows Fri Nov 6 09:25:01 UTC 2015
when i run select now() in mysql,it shows 2015-11-06 04:25:26.
So how to change EDT to UTC in mysql.
Note : i m using mysql workbench.

Set you MySQL timezone :-
SET GLOBAL time_zone = '+8:00';
SET GLOBAL time_zone = 'Europe/Helsinki';
SET ##global.time_zone='+00:00';
For check timezone :-
SELECT ##session.time_zone;
location of MySQL configuration file :-
/etc/mysql/my.cnf

You can try like this:
SELECT CONVERT_TZ(NOW(), ##session.time_zone, '+00:00')
SQL DEMO and UTC CURRENT TIME
Refer the CONVERT_TZ function.
CONVERT_TZ() converts a datetime value dt from the time zone given by
from_tz to the time zone given by to_tz and returns the resulting
value.

If all you care about is the current time, you don't have to do any conversions or worry about time zones. Just use the appropriate function.
NOW() returns the current local time.
UTC_TIMESTAMP() returns the current UTC time.
See Date and Time Functions in the MySQL documentation.

Related

Bypass MySQL automatic GMT conversion when selecting TIMESTAMP fields

There's lots of stuff on the internet about converting MySQL timestamps, how it works etc. But what I need is the opposite: knowing that MySQL stores every datetime data as UTC in TIMESTAMP fields, all I want is to direclty retrieve the stored UTC value without MySQL messing around the datetime with system/server/connection timezones.
You see, we've built a simple node.js feeder which reads from several third-part MySQL databases (so I can't change their timezone settings) and save the gathered data to a Elasticsearch, as a "denormalization process". As the original data comes from different timezones, I need to store them in UTC, so I can easily coordinate further GETs and aggregations.
I know I can set the connection timezone on the fly and I know I can change every timestamp field fetched in my node application, but since MySQL engine already stores timestamps in UTC, why should I add any other step if I could simply get it directly, without converting functions or costly data processings?
In a nutshell, I'd like to know: is there a way to bypass MySQL automatic GMT conversion?
MySQL provides a UNIX_TIMESTAMP function which returns a raw integer value. And that isn't subject to timezone conversions at all.
SELECT UNIX_TIMESTAMP( timestamp_col ) ...
But that returns a raw integer, not a datetime. The client would need to do the conversion into a "datetime" type object, if that's needed.
Another option would be to use the CONVERT_TZ function to convert to UTC from the session time_zone setting.
SELECT CONVERT_TZ( timestamp_col, ##session.time_zone,'+00:00')
But, that doesn't really "bypass" timezone conversion. One downside of this approach is if the session time_zone is affected by daylight saving time changes, there's ambiguity with a one hour period each year when the clock "falls back" one hour. e.g. Sunday, Nov 1 2015 2AM CDT transition to Sunday Nov 1 2015 1AM CST. (Converting back from UTC, if we get 1:30 AM in the session time_zone, we don't "know" if that's CDT or CST. And the conversion back to UTC doesn't know which it is either.)
Another option (which I think you already mentioned) is changing the session time_zone to UTC. Then you could just return the timestamp_col value as UTC. You could save the current time_zone setting, and set it back when you are done, e.g.
SET #save_session_time_zone := ##session.time_zone ;
SET time_zone = '+00:00' ;
SELECT timestamp_col ...
SET time_zone = #save_session_time_zone ;
But your client Connector might do some not-so-helpful conversions when the time_zone of the MySQL database session doesn't match the time_zone of the client, like the funky shenanigans the JDBC driver (MySQL Connector/J) does. (That concern isn't limited to returning UTC; that's a concern whenever the time_zone of the client doesn't match the time_zone of the database session.)
It looks like there's no way to get the original UTC value from a MySQL field; every single function uses the timezone setting, be that SYSTEM or any other you configure.
The way MySQL forces you to use a date conversion is, at least, very constraining. For example, say you have a MySQL server set to a timezone with GMT -03:00 and GMT/DST -02:00 and you store a datetime like '2016-07-01 10:00:00'. If you select this value after the DST has ended, you'll get '2016-07-01 09:00:00'.
You can't tell what time it is for sure unless you store the GMT offset separately or you previously know what timezone the server was when it was stored.
We used the second approach. We saved the server timezone and used it to calculate the offset and return an ISO datetime, so future calculations can be made easily.
DROP FUNCTION IF EXISTS `iso_datetime`;;
CREATE FUNCTION `iso_datetime` (
p_datetime TIMESTAMP
) RETURNS VARCHAR(25)
READS SQL DATA
BEGIN
DECLARE _timezone VARCHAR(255) DEFAULT NULL;
DECLARE _offset VARCHAR(6) DEFAULT NULL;
SET _timezone = (SELECT timezone FROM network);
SET _offset = (SELECT SUBSTRING(TIMEDIFF(p_datetime,CONVERT_TZ(p_datetime, _timezone,'UTC')), 1,6));
RETURN CONCAT(DATE_FORMAT(p_datetime, '%Y-%m-%dT%H:%i:%S'), _offset);
END;
In order to do so, you have to load timezone info into MySQL, so the server can calculate the tz offset of the date for you.

MySQL CONVERT_TZ() returns null when one of the arguments is "+14:00"

I am trying to convert UTC time into the users local time, it works fine until I try to convert the time to +14:00 timezone, the result is always null, anyone has idea?
Here is my code:
select CONVERT_TZ(now(), '+00:00', '+14:00')
This is a known bug in MySQL:
MySQL does not recognize timezone offset UTC +14:00
You can fix that, by using timezone name instead of offset. UTC +14:00 equals to Pacific/Kiritimati timezone. So if You modify query to:
SELECT CONVERT_TZ(NOW(), '+00:00', 'Pacific/Kiritimati')
or
SELECT CONVERT_TZ(NOW(), 'UTC', 'Pacific/Kiritimati')
then You'll get valid date, not NULL.
BUT there is one condition. Your MySQL engine needs timezones list. If above queries still returns NULL, it means You don't have timezones list loaded into Your MySQL engine.
Probably You'll have to run also this command:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
Plese check this thread for more information, about loading timezones list: convert_tz returns null
Tested on MySQL 5.6.36.

How to check server timezone

I want to know what is the time zone that is currently set in the MySQL server. I do not have administrator rights to the computer I am using so I've tried the method by checking the registry.
I am doing a table with a timestamp column and I noticed the time stamped is different than the one on my computer's time. Is there any reason for this? How do I check what timezone it is on the MySQL server? How do I change it to match my local/computer's time?
You can set the timezone (if you know your offset) for the session by using
set session time_zone = '+00:00';
and to revert to the system default
set session time_zone 'SYSTEM';
In an SQL timestamp column, SQL automatically converts the time to UTC before storing it, using the session's current time offset. It will be the machine's time offset unless you change it (3). Depending on your server's settings (sql.ini), it may or may not always concert back to the expect timezone. This probably explains the time discrepancy.
To get the current timezone offset, try executing
SELECT ##session.time_zone;
To manually override the SQL timezone for the rest of a particular session, execute the following, replacing 00:00 with your desired offset:
SET ##session.time_zone = "+00:00";
Have a look at the system_time_zone system variable.
This may help:
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
You can set the system time zone for MySQL Server at startup with the --timezone=timezone_name option to mysqld_safe. You can also set it by setting the TZ environment variable before you start mysqld. The permissible values for --timezone or TZ are system dependent. Consult your operating system documentation to see what values are acceptable.
You can convert a given timestamp to UTC (or any other TZ you want) with CONVERT_TZ
SELECT CONVERT_TZ(NOW(),##session.time_zone,'GMT');
Note that I use NOW() as simple demonstration, you would put in the timestamp you wanted to convert.
By the same token, you could convert a timestamp in your local TZ to the system
SELECT CONVERT_TZ($timestamp,'Your Time Zone' ##session.time_zone);
To check your shared server
<?php
echo date_default_timezone_get();
?>
To change
<?php
date_default_timezone_set("Africa/Addis_Ababa");
echo date_default_timezone_get();
?>

Different values for Time.now when using activerecord

I have this weird situation:
When I do on rails console Time.now or Time.zone.now I get the same values (suppose they run at the sime time: 2014-06-05 23:38:06 -0300)
But when I use Time.now in a query like: Match.where("datetime = ?", Time.now) it returns the time 3 hours ahead!
.to_sql output:
SELECT `matches`.* FROM `matches` WHERE (datetime = '2014-06-06 02:38:06')
any thoughts on that?
Rails 4
Mysql 5.5
Those are the same times. One has a UTC offset of -3, the other is in UTC time and doesn't have the offset.
The time you are seeing in sql query is UTC time. The reason you have Time.now and Time.zone.now return the same time in rails console is because you have both your system and your rails application in the same time zone. It is Time.now that returns the system time based on the system time zone and Time.zone.now that returns the application's time zone aware time.
There are couple of options that can be set as far as the timezone used in the query is concerned, you can either set it to :utc which is the default or set it to :local. You have utc configured and this is the default. :local sets the timezone to server's time zone. This setting is set in config/application.rb:
config.active_record.default_timezone = :utc
The second portion - rails application's time zone can also be set in config/application.rb:
config.time_zone = 'Brasilia'
With this setup, when you use time zone aware classes e.g. Time.zone.now to retrieve time in your application they will be using this configured timezone. Also a point to note with the setting config.time_zone would be to use timezone aware classes in your application to ensure consistent translation of time zone regardless of environment.

The Generation Time is wrong when pint in phpMyAdmin

I using phpMyAdmin 4.4.14 in Win7+Chrome and MySQL 5.6 in Linux.
My timezone is +8
The date command in Linux returns a correct date and time.
When I issue select now() inside the phpMyAdmin, the date and time is correct.
But, when I print the result, the time value in the Generation Time is wrong.
Look like that the Generation Time does not do a +8 to the hour.
How to fix ?
Cheers,
Alvin SIU
Print view is done via PHP script, so the issue is in PHP, not in MySQL. In order to change this timestamp, you need to open php.ini and to change/add date.timezone variable with desired value:
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Europe/Paris"
All available timezones can be found here:
http://www.php.net/manual/en/timezones.php