Changing CURRENT_TIMESTAMP value based on timezone in MySQL - mysql

I want to migrate our mysql server from shared hosting to local server.
Current server is in MST time zone and the values for the CURRENT_TIMESTAMP in databsse is stored as -7:00 GMT.
Now I want to move complete application on dedicated server in India. Also want to convert the date values stored in -7:00 GMT to +5:30 GMT.
I can accomplish this task of updating the dates by writing script to update the time, however I would like to know if is there any way I can do this from database itself (at time of import or while exporting itself)
mysql version 5.0.96-log. I am not getting option export timestamp in UTC.

When using mysqldump, set the flag: --tz-utc to force all timestamps to be exported as UTC. ( http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_tz-utc ). Note here --tz-utc is enabled by default. So you should have to do nothing: but test first :)
If just working with timestamps on the server you don't have to do anything to convert them, from the documentation on TIMESTAMP post MySQL 4.1 ( http://dev.mysql.com/doc/refman/4.1/en/timestamp.html ):
"values still are stored in UTC, but are converted from the current
time zone for storage, and converted back to the current time zone for
retrieval. As long as the time zone setting remains constant, you
get back the same value you store. If you store a TIMESTAMP value, and
then change the time zone and retrieve the value, the retrieved value
is different from the value you stored."
This is easy to test:
Save a timestamp to your table
Change the server's timezone
Retrieve it: the return value should reflect the new timezone.
So another option is you could just have both the servers set to the same timezone while doing the export / import, than set them back to the correct timezone(s) after it is complete, but note with MySQLDump this should not be necessary.

General syntax
SELECT DATE_ADD(<column_name>, INTERVAL HOUR);
for changing to UTC to MST
SELECT DATE_ADD(NOW(), INTERVAL 7 HOUR);

Related

Stop changing dates automatically in MySQL on tz change

I am using DATETIME field to store my dates in MySQL. Without any timezone specified directly (there is just a datetime column)
But when timezone was changed on server MySQL updated all datetime columns according to new timezone.
Actually switching to EDT was the reason.
I don't need to recalculate my dates automatically - just want to store specific dates in it.
So even if tz changed manually to UTC from EST date should be same (from characters POV) if it was 2016-01-01 18:55 it should be same in any new tz..
I did not run any scripts\queries to update dates.
So it was performed either by MySQL itself or by server.
Need advice what I need to check to find and disable such feature.
Make sure you are using DATETIME and not TIMESTAMP
[From the MySQL documentation][1]:
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)
You should also review this post (Duplicate):
Will changing a MySQL timezone change values of DateTime fields in a database?
I am sorry for the mess I brought
Just extended my query to grab some old dates - and it looks unchanged
So error defenetly in my code..
Anyway - thanks for your help

MySQL timezone discrepancy on Update tale

I'm working on a shared hosting so I don't have access to mysql configuration files.
The default timezone on that system is 'America/New_York' but I need to use UTC -5:00 for my databases.
Everytime I perform a query the timezone is set to UTC -5:00 like this:
SET time_zone='-5:00';
If I want the current time, SELECT NOW() returns the correct time and date, but when updating a table mysql uses the SYSTEM time and not the set timezone
UPDATE administradores SET ultimo_acceso=NOW() WHERE id=1
Why does those values are different? Shouldn't be the same time in both queries since I'm overriding the timezone?
I also tried with INSERT statement and that works fine.
If the ultimo_acceso field is of type TIMESTAMP, then the value is actually being stored as UTC and then converted back to the current time zone when you select the value back out. So you need to set the time zone again in the select statement.
If you are using a DATETIME data type, then the value you set should be persisted without conversion and you will get back exactly what you store - regardless of the timezone setting at time of select.
See the MySQL docs on this subject.
Note that DATETIME, DATE, and TIME fields will always return the exact time you placed into them, regardless of the value of the time_zone variable.
TIMESTAMP fields will automatically convert their values to the timezone specified by the time_zone variable.

Set MySQL database timezone to GMT

I need to change the timezone of a single database is this possible?
I know we can change the timezone within our WHM (we are using a dedicated server from hostgator), however a large number of legacy software running on the server has a lot of +6 hours coding in it (i.e. server timezone is CST, we wanted the time in GMT so previous developers altered date/times manually within the code - bad!).
I am now working on a new software and would like to have it all in GMT, I know I can use date_default_timezone_set('GMT') however that will not solve MySQL inserts where the datetime column is set to CURRENT_TIMESTAMP as it will insert # CST timezone.
No, it's not possible to change the timezone for a single database within a MySQL instance.
We can retrieve the server and client time_zone settings with a query, like this:
SELECT ##global.time_zone, ##session.time_zone;
We can also change the client timezone for a session, or change the timezone for the entire MySQL instance.
But we need to be keenly aware of the implication that this change will have on existing client connections, and the how DATETIME and TIMESTAMP values already stored in the instance will be interpreted.
To have the server time_zone set at MySQL instance startup, we can modify the /etc/my.cnf file (or wherever the mysql instance initialization parameters are read from), under the [mysqld] section:
[mysqld]
default-time-zone='+00:00'
-- or --
It is also possible (less desirable) to add the --default_time_zone='+00:00' option to mysqld_safe
NOTE: Changing the timezone setting on the MySQL server does NOT change the values stored in existing DATETIME or TIMESTAMP columns, BUT since it does effectively change the context in which those stored values are interpreted, it will look like all of the values ARE shifted. (Where 08:00 was taken to mean 8AM CST, with the time_zone of the server changed from CST to GMT, that same '08:00' will now be taken to be 8AM GMT, which would effectively be 2AM CST.
Also keep in mind that TIMESTAMP columns are always stored in UTC, while DATETIME columns do not have a timezone.
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
Each client session can change the timezone setting for their own session:
SET time_zone='-06:00';
But none of this really "solves" the timezone conversion problem, it just moves the conversion problem around.
There's nothing inherently "bad" with the application layer handling timezone conversions; sometimes, that's the best place to handle. It just has to be done correctly and consistently.
(What's odd about the setup you describe is that the app is storing DATETIME values as if the MySQL server time_zone is set to GMT, but the MySQL server time_zone is set to something else.)
If you can't change your current time zone you can change the result.
date 2015-01-05 12:06:16
Select date + Interval 2 Hour 'date' From ExampleTable
date 2015-01-05 14:06:16
You can modify the the default value instead of entering current_timestamp make it insert current_timestamp added to hours offset of your timezone. I just did it this way when didn't found any solution, had to invent my own.

GMT time in Database

Is there is any possibility in database, What ever time i am given which should store as GMT time stamp. If it so, Please share me.
For PostgreSQL, your table should have a column of type timestamp with time zone (timestamptz for short) like this:
CREATE TABLE tbl (id serial, ts timestamptz);
If what you want to store (let's call it my_ts) is ...
a local timestamp without time zone, i.e. it agrees with the current setting of timezone in your client:
Insert as is, everything is saved as UTC automatically internally:
INSERT INTO tbl (ts) VALUES my_ts'
a timestamp with time zone:
Insert as is, everything is saved as UTC automatically internally:
INSERT INTO tbl (ts) VALUES my_ts;
a timestamp without time zone but from another time zone:
Say, you get timestamp without time zone values from the Olympic Games in London, but your client thinks you are at a different time zone. Transform the timestamp with the AT TIME ZONE construct:
INSERT INTO tbl (ts) VALUES my_ts AT TIME ZONE 'Europe/London';
If you use the time zone name (instead of a time zone abbreviation, BST in this case), DST (daylight saving time) rules are applied automatically. Details in this related question.
More about timestamp handling in PostgreSQL in this related answer.
Now, if you want to display tbl.ts as UTC (~ GMT) timestamp without time zone, regardless of your current time zone, retrieve the values like this:
SELECT ts AT TIME ZONE 'UTC' FROM tbl;
Note that AT TIME ZONE has a different effect when applied to timestamptz than with timestamp! Read the manual carefully.
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
MySQL converts TIMESTAMP values from the current time zone to UTC for
storage, and back from UTC to the current time zone for retrieval.
(This does not occur for other types such as DATETIME.) By default,
the current time zone for each connection is the server's time.
Times in timestamp with time zone (and the old-style timestamp, but you shouldn't generally use that) are always stored in UTC ("GMT") in PostgreSQL. They're converted to and from UTC for display. If you want them to be treated as UTC for input and output:
SET timezone = 'UTC'
in each session, or set the param at the database or user level. Your app must then send all timestamps qualified with a time zone if they're in a TZ other than UTC; you've told the system that "local time" is UTC.
The AT TIME ZONE construct can be useful if you want to explicitly convert timestamps to a time zone other than the default, eg:
regress=# SELECT TIMESTAMP '2012-01-01 00:00:00 UTC' AT TIME ZONE '+08:00';
timezone
------------------------
2012-01-01 16:00:00+08
(1 row)
The keyword you are looking for is UTC instead of GMT ("For most common purposes, UTC is synonymous with GMT, but GMT is no longer precisely defined by the scientific community.", wikipedia).
For MySQL, you can use the CONVERT_TZ() function (please note the warning in the manual about named time zones, it can break your queries if it's not properly set up).
Just a word of caution: there is no 100% reliable way to work with timezone conversion between MySQL and your application (see my blog post about mysql time zone conversions to learn in detail about why this is). It's better to pass your datetime strings to MySQL in UTC directly (e.g. by doing the conversion in the application itself). You almost always end up storing datetimes in a DATETIME field using UTC formatted date strings if you need reliability.

MySQL and international dates

Say I have multiple servers in multiple locations and I want to use MySQL's datetime type for the field date and I always want to have the field date have the UTC timestamp so I would execute a UTC_TIMESTAMP() when I add it to the database. Now say I want to have MySQL output the UNIX TIMESTAMP for it.
When I do this on Server A I get the string "2009-06-17 12:00:00" doing the UNIX_TIMESTAMP(STRING) on it gives me the number 1245240000 back. Which is 2009-06-17 12:00:00 in UTC time. Now I do the same thing on Server B.
I get the same string back since its the UTC string but when executing UNIX_TIMESTAMP(STRING) again I get back the wrong number back 1245232800 which is the UTC +2 time. How do I get around this? Should I do the convertion from string to timestamp on the PHP side?
G'day,
I'll ask the obvious here, did you check the date and time on both machines?
Edit: ... and the MySQL timezone was the same on both machines?
Update: Ok. The problem is in the fact that the timestamp string being passed into UNIX_TIMESTAMP is interpreted to be a value in the current timezone which is then converted back to UTC so, because you're in MEZ, two hours is subtracted to return it back to UTC so 7200 is subtracted from your timestamp when it is converted back to a Unix timestring.
Hence, the variation you see when using UNIX_TIMESTAMP() to convert is back to a Unix Epoch timestring.
BTW Shouldn't you be using a TIMESTAMP type for storing off your UTC_TIMESTAMPs instead of DATETIME type?
Update: Decoupling presentation time from stored time is definitely the way to go. You can then reuse the same data all around the world and only have to convert to and from local time when you are presenting the data to a user.
If you don't do this then you are going to have to store off the timezone when the timestamp was made and then go into all sorts of complicated permutations of having to work out if
the local timezone was in daylight saving time when it was stored,
what the difference is between the timezone at the time that the data was stored and the timezone where the data is to be presented.
Leaving it all storeed as UTC gets rid of that.
Most users won't be that happy if they have to work out the local time themselves based on the UTC time returned so systems usually convert to current local time for the user.
This is of course if the user wants the data expressed in local time which is usually the case. The only widely used system I can think of, off the top of my head, that stores and presents its data in UTC is system for air traffic control and flight plan management which are always kept in UTC (or ZULU time to be more precise).
HTH
cheers,
Have you tried doing this?
Execute this instructions together.
SET time_zone = 'UTC';
SELECT FROM_UNIXTIME(0), UNIX_TIMESTAMP('2009-06-17 12:00:00');
// 1970-01-01 00:00:00 1245240000
They only affect the client session, not the server configuration.