TIME STAMP off by 5 Hours - mysql

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

Related

MySql returns wrong time from table data

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.

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

MySQL timestamp using the server's time....but it's not my time

I'm based in a certain timezone but my server is one hour ahead. When I save new records with a timestamp column, it displays the time as one hour ahead.
How will this affect me in the long run? How will I need to alter my queries to return my time instead of the server's time? Or should I not worry about this at all? I'm new to web dev...
After connecting to database execute query:
SET time_zone = 'Europe/London';
List of timezones: http://php.net/manual/en/timezones.php
Or change it via WordPress settings:
http://help.coschedule.com/hc/en-us/articles/214455448-How-To-Change-Your-WordPress-Timezone

How to handle UTC dates in a report

I have a report with two parameters - StartDate and EndDate - which run a stored procedure that returns data between these two dates.
The dates in the database are stored in UTC. Currently in the UK we are on BST (UTC+1).
To show the times in the correct time zone, in the report I am using System.TimeZone.CurrentTimeZone.ToLocalTime.
The end result is that the times as displayed on the report look correct, but the user still has to enter the StartDate and EndDate parameters in UTC. This confuses the user greatly as they expect to be able to enter a date in the current time zone, but the times in the database are UTC.
How can I allow the date parameters to be entered in the correct time zone?
Unfortunately, we can't modify the schema of the database so I need to find a way to fix this on the reporting side
In SSRS, if the date stored in database is UTC, it has to be UTC when select from a query in parameter setting. It is not supported to change the field which is from a query in parameter. For your requirement, you can either format the date into local time in query. Or you can select UTC date in parameter, and render it as local time

Confusion regarding FROM_UNIXTIME

I am running MYSQL 4.1 database that stores call center data for our offices that operate in Europe.
My MYSQL database sits on a Windows 2003 server that's has its time zone set to Central European Time, which automatically adjusts to day light savings.
I want to able to produce a report that shows the log date and time in the correct time zone to our customers in Europe.
My database stores the log date / time of the call as a unix time stamp. So therefore the dates stored as UTC. I found MYSQL function that can easily adjust the log date time to a time zone of your choice.
It's called CONVERT_TZ. (More info:http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_convert-tz)
Example: SELECT CONVERT_TZ('2004-01-01 12:00:00','UTC','CET');
I tried to apply this to call that was logged on 01-06-2013 22:12
CONVERT_TZ(FROM_UNIXTIME(o.logdatex), 'UTC','CET') 'CET_L_DATETIME'
(logdatex is the unxtimestamp for when the call was logged)
To my confusion the statement returned - 2013-06-02 00:12:56.
I wanted to see what was returned when I just selected FROM_UNIXTIME(o.logdatex)
It returned the correct time! 2013-06-01 22:12:56 (This call was logged from our Amsterdam office)
My question is, does the FROM_UNIXTIME function automatically adjust the time from UTC according to what the time zone MYSQL server is set to? I cannot find any documentation that says it does.
My assumption this is because when you do FROM_UNIXTIME(o.logdatex) you convert from UTC to timezone of your server.
And now you date is not in UTC timezone. So in CONVERT_TZ(FROM_UNIXTIME(o.logdatex), 'UTC','CET') you don't convert from 'UTC' but from timezone of your server so you get a strange result for you.
Try to select FROM_UNIXTIME(o.logdatex) as a separate field in your query and compare results.