问题
I have a database storing records in UTC timestamp. I want to fetch them in terms of local time standard(IST).
By referring to some referral i tried like this.
select date(convert_tz(sa.created_at,'+00:00','+05:30')) as date,count(*) as hits from session_acts sa, sessions s where sa.session_id = s.id and s.created_at between convert_tz('2015-03-12T11:33:00+00:00','+00:00','-05:30') and convert_tz('2015-03-13T11:33:00+00:00','+00:00','-05:30') group by date;
But it will resulting in
+------------+------+
| date | hits |
+------------+------+
| 2015-03-12 | 94 |
| 2015-03-13 | 34 |
+------------+------+
I want to display only hits that are requested on 13th. Where i am going wrong.?
回答1:
IST is 5.30 hours ahead of UTC, so when 13th starts in IST i.e. 2015-03-13 : 00:00:00 its 2015-03-12 18:30:00 in UTC
mysql> select convert_tz('2015-03-13T00:00:00+00:00','+00:00','-05:30') ;
+-----------------------------------------------------------+
| convert_tz('2015-03-13T00:00:00+00:00','+00:00','-05:30') |
+-----------------------------------------------------------+
| 2015-03-12 18:30:00 |
+-----------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
And when 13 ends in IST i.e. 2015-03-13 : 23:59:59 its 2015-03-13 18:29:59 in UTC
mysql> select convert_tz('2015-03-13T23:59:59+00:00','+00:00','-05:30') ;
+-----------------------------------------------------------+
| convert_tz('2015-03-13T23:59:59+00:00','+00:00','-05:30') |
+-----------------------------------------------------------+
| 2015-03-13 18:29:59 |
+-----------------------------------------------------------+
So yo get the data in IST for 13th you will need to search data within this range of dates.
So the condition would be as below -
s.created_at
between convert_tz('2015-03-13T00:00:00+00:00','+00:00','-05:30')
and convert_tz('2015-03-13T23:59:59+00:00','+00:00','-05:30');
and since you are doing conversion at the time of select so it will return all 13th data.
来源:https://stackoverflow.com/questions/29163349/mysql-converting-from-utc-to-ist