created_at in where statement and timezone - mysql

I'm trying to retrieve all the entries of the day in my model:
where('date(created_at) = date(?)', Date.today)
But, because of the timezone (My local time is UTC+2), I don't have the expected result. For example, if an entry has been created one minute after midnight, in the database, it will be stored at 22:01 (so not "Date.today").
Is there a way to do that ?
EDIT:
As the admin of the website, I want to display the number of the entries on my local time "today" (It's only for statistics purpose!). So I want to keep timestamps store in utc, but convert them to my local timezone during this request!

You can set the correct time zone in several ways. One is described in this answer:
https://stackoverflow.com/a/6118837/567126
adding following to application.rb works
config.time_zone = 'Eastern Time (US & Canada)'

where('date(created_at) = ?', Time.zone.now.to_date)

Time in the database should be stored as UTC. This way, when you do a worldwide website, you can serve each user with their own timezone.
It's the UI (views) responsibility to show the time in the user's local timezone.
For your where you should use Time.now.utc
This way, you are searching the DB with the common UTC.

Related

DST identification for msql

I have different users having different timeznoes. Suppose , one user has timezone set as GMT-8:00 (America/Los_Angeles) another has GMT+5:30 (Asia/Kolkata). I have one mysql function which returns current local date time on basis of user.
SELECT CONVERT_TZ(CONCAT(UTC_DATE(),' ',UTC_TIME()),'+00:00','-08:00');
Now this is not considering DST timezone. So how to find whether timezone is currently following DST and get date time on basis of that.

Logging time zone aware events

I know pages and pages have been written on this topics, however, I need to clarify this issue, and be sure I'm doing what is sensible.
The users of my app can be in any part of the world. I log a number of events/actions a user performs on my app. I want to be able to see what events/actions a user made in it's own time zone, or a timezone of my choosing.
At the moment, my app is sending the date and time of the event in UTC in the format "YYYY-MM-DD HH:MM:SS" and also, in a separate field, the time zone offset in minutes (e.g. -120 or +60).
When I get the events, I store them in MySQL in the following way: I use a DATETIME column to store the date and time of the event as is; I use an INT column to store the offset.
When I want to do a select and get back the event in the user's time zone, I just do:
SELECT cdate + INTERVAL tz_offset MINUTES FROM events WHERE uid=123;
I believe this is the best option for me, and offers versatility.
What do you think?
Thank you!

MySQL time zone

I am new to database development and am currently working in MySQL.
I have a column that contains all the time zones for USA. I want to be able to get the current time (only time, no date) as of right now for each time zone in each row.
The TimeZone data looks like: +05:00, -03:00 etc etc etc
This is how I have attempted it. I have 2 tables, one country, one city. TimeZone is found within "city" table. I have tried to use the function sys date() but it returns the complete date and the current time on my system, not based on the time zone. Can anyone help me out? Thanks a lot
select TimeZone, Country, sysdate() as "Current Time"
from city, country
where Country='USA' and city.CountryId= country.CountryId;
You should be able to use the convert_tz function:
SELECT CONVERT_TZ(NOW(),'-07:00', city.TimeZone) AS Time, city.Name
FROM city, country
WHERE country.Name='USA' and city.CountryId= country.CountryId;
Assuming your server is in -07:00 timezone.

Date in IST in SQL Server

My server is hosted in US or so with some other time zone. Is there an option to get the current date in SQL Server in Indian Standard Time.
select getdate()
What should I write to get the current time in India(or some other country as such).
You should use the DATETIMEOFFSET datatype which includes the timezone, and the SWITCHOFFSET method to switch between timezones. Also: to get the current time, use SYSDATETIMEOFFSET() instead of GETDATE()
-- gets current date/time in the current timezone
SELECT
SYSDATETIMEOFFSET()
-- get the current date/time in your preferred timezone +05:30 UTC being Indian Std. Time
SELECT
SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')
According to this link India's time is 9:30 hours ahead from US. So in-order to get the Indian time, you need to add 9.30 hours to US time.
SELECT DATEADD(hh,9.30,getdate())

How to handle the different timezone in mysql?

user login from india at 9:00 am
other user from usa at other time.
how handle this if both the user having same server but login from different timezone
You haven't really told us what your problem is, but the best way to handle multiple timezones is to always store your timestamps in UTC, and convert from local to UTC on insert, and from UTC to local for display.
You could store timezone offset for each user (make the user select one timezone when he creates the account) and do the transformations based on that