MySql returns wrong time from table data - mysql

I have data in a table, and one of the columns is DATETIME.
select time from tbdt where unix_timestamp(time) > unix_timestamp(now()) order by time asc limit 1
now NodeJS prints wrong time in console.log() like,
actual datetime is 2018-12-16 15:00:00 in db table..
but mysql returns 2018-12-16T09:30:00.000Z
which is 5 hours 30 minutes difference and my time zone is +5:30 (IST)
I don't exactly know where it goes wrong, either in MySql or in Node Js

Need to use convert_tz function in MySql.
SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+5:30');
It has the following signature:
CONVERT_TZ(dt,from_tz,to_tz)
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_convert-tz

You should check the server time on which your MySQL is running as it will pick the time from the server on which it is hosted.
This is being done by the MySQL, not by NodeJS and you can verify the same by directly running your above query into the database by console or SQL developer tool.

Related

Is PHPmyadmin current date based on my computer's localdate?

I'm doing some testing for my system in selecting data between two dates.
so I tried changing my computer's localdate to like year 2020 and run my system, so I'm expecting my CURRENT_DATE is May 10, 2020.
and I wont be getting any rows from my query because all of my data is year 2018
But after I use my query of cur_date() its still selecting those 2018 rows.
so I thought maybe my Phpmyadmin has its own cur_date().
I'm doing this test for my system will be use for the next couple of years. so I want to try and test my queries if today is already 2025 or something.
I thought maybe my Phpmyadmin has its own cur_date().
PhpMyAdmin has nothing to do with it. Your local computer also has nothing to do with it.
When you put CURDATE() in a query, that's part of the query. It's just text, like the SELECT part or the FROM part.
That means it's evaluated by the MySQL server. Just like the data of your rows is retrieved from the server, not from PhpMyAdmin or your local computer.
So the date returned will be that of the MySQL server.
so I want to try and test my queries if today is already 2025 or something.
The way to do this is to take out the expression CURDATE(), and replace it with the "fake" date you wish to use instead.
Something like:
SELECT * FROM `TheTable` WHERE `TheDate` > '2025-01-01';

Django querying against external database with different timezone

I have a Django API application running on postgres with TIME_ZONE='America/New_York' and USE_TZ = True.
For a daily report, I need to query another database, MySQL, and compare records from the postgres DB to check for some updates. They should contain the same number of results. The MySQL DB's timezone is UTC however. How can I perform SELECT queries against the MySQL DB to have it match the same date range on my postgres DB?
Example:
These two queries should return the same number of results
# Django/Postgres with TIME_ZONE='America/New_York'`
MyObject.objects.filter(created_on__date=date(2016, 9, 17))
# External MySQL Databse in UTC
sql.execute('SELECT * from MY_TABLE where created_on BETWEEN "2016-09-17" AND "2016-09-18"')
What you need is to convert the date in MySQL to match America/New_York time zone.
The function to achieve that would be CONVERT_TZ() and since you need named time zone first you need to set up time zone tables.
If you are uncertain whether named time zones are available issue below query - if it returns zero the table is empty so using named time zones is unavailable and you need to populate them (I've mentioned a link to documentation above).
SELECT COUNT(*) FROM mysql.time_zone_name;
mysql_tzinfo_to_sql is used to populate time zones tables.
If your time zone is not a moving one then you can go with less safe, hardcored approach (not recommended) by explicitly typing the time difference like so:
mysql> SELECT CONVERT_TZ('2016-09-17 10:00:00','+00:00','+06:00');
-> '2016-09-17 16:00:00' -- Result

TIME STAMP off by 5 Hours

There is a table that stores my users date access, the time stored in this Table is 5 hrs ahead.
i.e. there is a user that access at 4:02 am on my live report
but when i run the query in SQL shows he enter at 9:02 am
How can i compesate for that on my Query?
Your database is running on UTC (CDT+5). If you need to adjust to local time, you can use the CONVERT_TZ() function.
More info: https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

convert_tz returns null even after loading time zone tables

I have a MySQL database (hosted on Ubuntu) that has a table with a time zone. This date is in the UTC time zone. I have an application that is using this data but needs to convert the time from UTC to PST to display the data in a useful format.
This can probably be changed on the application level, but I don't have the time to do that currently, so what I want to do is use convert_tz to get the correct time zone until I have time to change the application to convert the time zone.
However, whenever I do something like
SELECT id,
category,
convert_tz(create_datetime, 'UTC', 'PST') as create_datetime
FROM table
I get a result like
1, category, NULL
I know that I need to load the time zone tables, so I did, running this command:
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
After that I restarted the mysql service. However, convert_tz still returns NULL. Can someone give me a hand?
Thanks!
Make sure that your create_datetime colum is of type TIMESTAMP.
Please refer to the official MySQL Reference Manual:
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_convert-tz
Example:
SELECT CONVERT_TZ('2004-01-01 12:00:00', 'GMT', 'MET');
When I had this problem it was because I used the wrong time zone name. I incorrectly inferred from the time zone used in the example in the MySQL documentation, MET, that 3-letter US time zones such as PST would work. That's not the case: US time zones are named 'PST8PDT' and the like. So whereas
select convert_tz('2016-10-16 06:23:00', 'UTC', 'PST');
returns NULL,
select convert_tz('2016-10-16 06:23:00', 'UTC', 'PST8PDT');
returns the converted time.
You can see the time zone names in your MySQL instance with
select Name from mysql.time_zone_name;

How do I change the mysql server date using SQL

I need to change the server date via sql.
Preferably through my application (CL is also good).
How do I change the mysql server date using SQL?
Why:
We test a scenario that spans several weeks. To test it we need to advance the server date by that time.
Do you want to change the server date or the server timezone?
If it is the server date, you have to do it at the OS level.
If it is the time zone, you can change it globally for entire database instance or for your session/connection only.
The SET TIMESTAMP statement affects the value returned by NOW() but not by SYSDATE()