Stop changing dates automatically in MySQL on tz change - mysql

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

Related

Mysql date ok but signaled as incorrect

I am trying to insert the date below into my TIMESTAMP column but it's unexpectedly signaled as incorrect format by mysql :
INSERT INTO `assets`( `updated_at`) VALUES ('2022-03-27 02:12:31.217573')
If i add or substract one day or one hour then the date is executed without error.
I going crazy ^^
Thx for your help
mysql version : 5.7.36
timestamp columns are weird. They effectively store a utc time, but on input and output convert to the timezone of the connection. Presumably you are using a timezone that has daylight savings where the second after 2022-03-27 01:59:59 is 2022-03-27 03:00:00, so your entered time is invalid.
If you intend to be storing times that are already in UTC (which you should!) you need to set your timezone appropriately. See https://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html. But be aware that then all previously entered times will appear to change.
I much prefer to use datetime columns which will store and show exactly the data entered, though they have their own quirks.

MySQL Date/Time Functions and Timezone Issue

I'm working on an application that uses created_at and updated_at columns in MySQL tables. The server/system timezone is set to EDT.
Upon INSERT, I'm using the MySQL CURRENT_TIMESTAMP function for the created_at value. MySQL automatically updates the updated_at value whenever he record is updated (ON UPDATE CURRENT_TIMESTAMP). My problem is that the created_at value is entered as UTC and the updated_at value is entered as EDT.
Can someone please help with a fix and explanation of this issue?
We can't help you with a fix because you've not shown any code. Mysql date, time and date time values have no concept of a timezone. Timestamp values use UTC. It is up to you to write your code to deal with these constraints. If your data will always be constrained to a single timezone then just cast any timestamp values to that timezone:
SELECT CONVERT_TZ(created_at, 'GMT', 'EDT'), updated_at
FROM your_table
This gets very messy when you start to use more than one timezone - a better solution is to always store times in UTC and change the representation at the front end (but this can be a bit of a pain if you run on brain damaged operating systems or don't control the server config)

Changing CURRENT_TIMESTAMP value based on timezone in 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);

How does mySQL determine the time zone for the TIMESTAMP field?

According to mysqltutorial
The values of the MySQL TIMESTAMP columns depend on connection’s time
zone. When insert values for MySQL TIMESTAMP columns, they are
converted to Universal Coordinated Time (UTC) from connection’s time
zone. When you select the value, the server converts it back from UTC
to the connection’s time zone so you have the same value that you
inserted.
Where does it get this info. from, the time the user sets his OS to or from some other method?
Javascript also has a way to do timezones but is more involved: onlineaspect
To specify connection-specific time you need to perform
SET time_zone = TZ;
Where TZ can be either numerical offset like +11:00 or name of timezone Asia/Vladivostok (for the latter you need to import timezones description. Ask your DBA to do so)
the example there is wrong, i just tested it. it returns the previously inserted timestamp, regardless of the timezone. mysql will always insert the time you specify, without modifying it. if you need timezone adjustment, you have to tell mysql explicit to do so; otherwise all times are assumed to be "local server time".

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.