I am trying to convert CET date to UTC Format in MySql. Whats wrong with this?
SELECT
CONVERT_TZ('2018-01-26T06:15:00+01:00', ##session.time_zone, '+00:00');
Expected:
2018-01-26T05:15:00Z
Actual:
2018-01-26T06:15:00Z
I guess it is not taking the +01:00 component
MySQL's date format doesn't have a timezone. It is interpreting your '2018-01-26T06:15:00+01:00' as '2018-01-26 06:15:00'.
The 1-st parameter is the time, the 2-nd parameter is the time zone for the 1-st and the 3-rd is the result time zone.
SELECT
CONVERT_TZ('2018-01-26T06:15:00', '+01:00', '+00:00');
Related
I have a SQL query:
update party set left_time=(next_dose-dose)/power,takeoff=from_unixtime(unix_timestamp()+left_time);
How can I modify it without using unix time to get the dates further than 2038-01-19?
If you want just the UTC time that is left_time seconds from now, just do:
utc_timestamp() + interval left_time second
But that's not what from_unixtime does; from_unixtime will produce a time in the session's timezone. If that is what you need, you could naively do
current_timestamp() + interval left_time second
but that will not produce correct results if there is a daylight savings transition upcoming, so you have to do:
convert_tz(utc_timestamp() + interval left_time second, '+00:00', ##SESSION.time_zone)
(An example of why you should always just store UTC times and only convert them for display.) If takeoff is a timestamp type instead of a datetime, you have to do this, since it automatically converts to/from the session timezone whenever you read/update it, though it actually stores a utc time.
Let's say today is 30-NOV-2016 at 00:00:00 in Europe (GMT+1) and a frontend JavaScript application JSON.stringify(new Date(2016, 11-1, 30)) send a JSON (stringified) date to an Oracle backend.
This date would arrive as the string "2016-11-29T23:00:00.000Z" and now I would like to convert this string to a proper Oracle DATE in the (again) local timezone (GMT+1).
I expected
SELECT CAST(TO_TIMESTAMP_TZ('2016-11-29T23:00:00.000Z', 'FXYYYY-MM-DD"T"HH24:MI:SS.FXFF3"Z"') AT LOCAL AS DATE) FROM DUAL;
to do the trick, but this actually returns me the UTC date 29.11.2016 23:00:00 and not the correct local date 30.11.2016 00:00:00.
This should be quite straightforward but I cannot seem to figure out, what I'm doing wrong?
When you use to_timestamp_tz() but don't actually specify the time zone in the conversion it defaults to your system time zone, which presumably isn't UTC. The timestamp with time zone you are generating is therefore already in your local system time zone, so at local isn't doing anything.
You can convert to a plain timestamp instead, and declare the time zone as UTC with the from_tz() function; then still use the AT LOCAL expression to change it to your local time zone:
alter session set time_zone = 'Europe/Vienna';
SELECT CAST(
FROM_TZ(TO_TIMESTAMP('2016-11-29T23:00:00.000Z', 'FXYYYY-MM-DD"T"HH24:MI:SS.FXFF3"Z"'),
'UTC') AT LOCAL AS DATE) FROM DUAL;
CAST(FROM_TZ(TO_TIM
-------------------
2016-11-30 00:00:00
Breaking that down a bit:
TO_TIMESTAMP('2016-11-29T23:00:00.000Z', 'FXYYYY-MM-DD"T"HH24:MI:SS.FXFF3"Z"') converts your string value to a plain TIMESTAMP value, with no time zone information.
FROM_TZ(..., 'UTC') converts that plain timestamp to a time stamp with time zone with the time zone part as UTC - no adjustment is done to any of the date/time elements, it just states those represent a UTC value.
... AT LOCAL converts to your session time zone (which might not be the same as your system time zone).
CAST(... AS DATE) converts the value in your local time zone to a date; again no adjustment is done to the element values, but you lose the fractional seconds and time zone information.
You could also stick with to_timestamp_tz() but include the UTC code:
SELECT CAST(
TO_TIMESTAMP_TZ('2016-11-29T23:00:00.000Z' || 'UTC',
'FXYYYY-MM-DD"T"HH24:MI:SS.FXFF3"Z"TZR')
AT LOCAL AS DATE) FROM DUAL;
or replace the Z with UTC:
SELECT CAST(
TO_TIMESTAMP_TZ(REPLACE('2016-11-29T23:00:00.000Z', 'Z', 'UTC'),
'FXYYYY-MM-DD"T"HH24:MI:SS.FXFF3TZR')
AT LOCAL AS DATE) FROM DUAL;
All of these assume - correctly, I believe - that the JSON string will always be UTC and the Z can be assumed to be there and to mean that (as it should do, of course).
Is there a function in MySQL like in Postgres where I can convert the current timestamp to another timezone?
In Postgres, I do this like these:
SELECT now() AT TIME ZONE 'PST'
and it converts it on its own. How do I do this in MySQL? is there anyway?
Thanks
Use the CONVERT_TZ() for this.
Syntax:
CONVERT_TZ(dt,from_tz,to_tz)
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.
Try this:
SELECT CONVERT_TZ(now(),##session.time_zone,'-08:00');
Here is the reference.
now() -the current time
##session.time_zone -gives the current time zone(local)
-08:00 -required format in PST.
I actually solved! HAH!
Anyway, I used this:
SELECT CONVERT_TZ(NOW(),SUBSTRING(TIMEDIFF(NOW(), UTC_TIMESTAMP), 1, 6),'-05:00') AS est_timezone
Basically the query interprets as:
SELECT CONVERT_TZ(NOW(),'-08:00','-05:00') AS est_timezone
I finally got my current timezone which is -08:00 and will convert it to -05:00. This way, I can now proceed with my previous query to making it a VIEW.
I ran this statement in mysql and it returns null on every row:
SELECT STR_TO_DATE('Fri, 22 Jun 2012 03:35:24 +0000', '%a, %e %b %Y %H:%i:%s %%%%%%%%%%') FROm t2;
I don't know the right specifier for the +0000. What should it be?
The +0000 is a time zone offset.
mySQL doesn't understand the concept of time zones, and apparently neither does STR_TO_DATE: the list of date/time format placeholders doesn't mention time zones at all.
If you expect dates from varying time zones, this doesn't seem solvable with the help of mySQL only. You would have to preprocess the date elsewhere to turn it into one that is always UTC (or your local time zone, whatever applies), or if it already is always UTC, ManseUK's suggestion (adding +0000 to your format string so mySQL ignores it) should work.
When I insert data into my MySQL database, a lot of the time I need to store the current datetime. I always want to store my datetimes as UTC time.
How do I store the current UTC time? Is there a built-in function or do I have to apply an offset to NOW()?
In MySQL you can use UNIX_TIMESTAMP() which can provide all kinds of Date and Time Values and specific to your request you can use UTC_TIMESTAMP, UTC_TIMESTAMP() which returns the current UTC date and time as a value in YYYY-MM-DD HH:MM:SS or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context.
Example:
mysql> SELECT UTC_TIMESTAMP(), UTC_TIMESTAMP() + 0;
-> '2003-08-14 18:08:04', 20030814180804.000000
More Info is described here about MySQL Date/Time Functions:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp
You can update the timezone property in your my.cnf file:
[mysqld_safe]
timezone = UTC