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.
Related
In my DB there is a date-time (slot_start) field. Unfortunately the entries are expressed in two different time zones.
For example: 2021-02-08T08:00:00+01:00 and 2021-02-08T07:00:00+00:00
Now I'm trying to list all entries in Central European Time (CET).
I have tried the following:
SELECT customer_nicename,
service_name,
CONVERT_TZ(slot_start,'+00:00','+00:00') AS Slot_Start,
status
FROM reservations
WHERE status LIKE 'confirmed'
But one of the two time zones are expressed shifted by an hour.
I dont get any further - can somebody help me out?
Thanks a lot!
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.
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!
Is there any query to insert country name and its time zone in GMT, for a mysql database. I want to add all the country name and its time zone difference with GMT or UTC
try to google the list but you may have to actually write extraction code
On top of that, large countries like USA, Canada & Russia have multiple time zones
Its all up to you how you want to design the database and code according to that.
This is the list Fedora is using...
http://unicode.org/repos/cldr/trunk/common/supplemental/windowsZones.xml
start from here
Here is another list
http://www.timegenie.com/world.time
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())