JDBC give MySQL datetime in UTC - mysql

My Java:
Date parsedDate = dateFormat.parse(timestamp);
Timestamp dbTimestamp = new Timestamp(parsedDate.getTime());
insertTimestampPreparedStatement.setTimestamp(3, dbTimestamp);
PhpMyAdmin/MySQL always displays this in my local time, even when I do
insertTimestampPreparedStatement.setTimestamp(3, dbTimestamp, Calendar.instance(TimeZone.getTimeZone("UTC"));
I'm not sure if MySQL stores them aware of timezone, and the only way I checked this was updating a timezone row to NOW() directly in phpmyadmin, and it then showed me a time in GMT. So either there is a bug in phpmyadmin/mysql or my code is not sending over timestamps in the correct timezone.
How do I get this to work in UTC?

A JDBC driver must use the local timezone when retrieving or storing timestamps, unless a Calendar with the specific timezone is provided.
Unfortunately, the JDBC spec isn't always clear on how drivers should behave, and it looks like MySQL does it wrong. For MySQL you can force the timezone it uses by specifying one or more options in the connection string serverTimeZone and related properties. See http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html for a full list.

Related

Node.js App Datetime stored in Mysql is ahead of UTC time

Columns definition :
`local_punched_at` timestamp NULL DEFAULT NULL,
`punched_at` datetime DEFAULT NULL,
We have an application that is used in multiple timezones, so we are storing UTC time in the punched_at column and the user local time in the local_punched_at column.
When we persist these dates in MySQL the local_punched_at is stored as the same value as punched_at(even we passed the user's local time which is always lower than UTC) or sometimes it is stored value as 1 hour ahead of punched_at value.
For example : For user in America/Chicago Timezone, we send local_punched_at as "2021-11-03 08:52:14" and punched_at : "2021-11-03 13:52:14" , but the DB stored it as
local_punched_at : "2021-11-03 14:52:14" and punched_at : "2021-11-03 13:52:14"
The problem is why the local_punched_at date is changed during saving in MySQL.
MySQL server timezone is UTC.
Node app is running on AWS EC2 and DB is Aurora MySQL.
Did anyone face such an issue?
The TIMESTAMP and DATETIME data types handle time zones differently in MySQL. You are running into that problem.
TIMESTAMP values, when stored, are always translated from the current timezone setting to UTC. And, when retrieved and sent to a client, they are always translated back to the current timezone setting. Functions like NOW() and CURDATE() also obey the current timezone setting.
You can set the timezone, for each connection, with SET time_zone='America/Chicago';. And you can retrieve the current setting with SELECT ##time_zone;
On the other hand, the DATETIME data type attempts no translation. It stores whatever you give it verbatim.
Many global applications ask users to set their preferred time zones, with values like 'America/Chicago' and 'Asia/Kolkata' and so forth. Then, when the application connects to the database on behalf of a user it does the SET time_zone operation. It's a cool feature, for timestamps that lie within the range of UNIX timestamps.
The global application stores timezone-sensitive data (like the scheduled time for a phone call) in TIMESTAMP columns. This allows each user to see times in their local time zone, even when daylight-time changeover days are different between jurisdictions. Like now: England has switched to standard time, but US has not yet. It's based on the Internet Assigned Numbers Authority's wonderful zoneinfo database.
Your confusion here? It's possible your database connection's time_zone setting defaults to 'America/New_York' or '-04:00'. It seems likely your server is located in the US-East region (in the suburbs of DC) of your cloud provider. The time_zone setting does not have to be the same as the server's clock.
If you want to always speak UTC to TIMESTAMP data, use SET time_zone='UTC'; immediately when you open each database connection.

How to configure Code Workbook's timezone for CURRENT_TIMESTAMP?

How can I configure Code Workbooks's timezone such that calling CURRENT_TIMESTAMP in SQL returns my local time instead of the default UTC?
Are there any reasons this would be advised against?
Timestamps in Spark have no concept of timezones since they represent the number of microseconds since Unix epoch. I'm not aware of any Code Workbook setting to change this, but Spark likely uses the system clock of whatever host it executes the function on to determine what the current timestamp should be, and it wouldn't be possible to fiddle with those settings.
Sounds like what you're looking for is some function like from_utc_timestamp, which takes your timestamp in UTC and shifts it to your timezone. Note that your timestamp would still be timezone-agnostic, but if you were to print the string representation of your timestamp, it would now look like the wall-clock date/time in your local timezone.

Mysql data insert issue in timestamp type

Using DBeaver we are trying to execute the following query.
UPDATE listing SET ScheduledTime='2019-01-09 15:14:51.0', Status='SCHEDULED' where ID=108
after successful execution, we can see the ScheduledTime column as '2019-01-09 20:44:51' in DB. Why there is a time mismatch and how we can solve it? Assistance in this matter is greatly appreciated.
You use different timezone setting when you store and when you view the data. As mysql documentation on timestamp says:
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. The time zone can be set on a per-connection basis.
Mysql documentation describes how to view and set timezone here

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

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);