How to get the local time from mysql database - mysql

i'm new to mysql. i'm working on ruby on rails project. i'm using mysql database which store time in utc format. But i want localtime on india.

Keep the time in the DB in utc. You can always convert the time to the local time.
Look more into Rails Timezones, there is lots of information on it and it's well supported.
Essentially, you will set the Time.zone value based on the user in an around_filter or based on an environment setting, and Rails will take care of displaying dates in the correct time zone.
There are lots of other details, but that should get you started.

Related

how to change automatically UTC time to current local time in my web application.

In my web application like eCommerce site. Every projects have time limit.I will display time left details in each and every projects.i am using UTC time.how to convert current localtime for every users.For example, USA have 4 or 5 different time zone.i am using php codeigniter and mysql for my web application
If I was you, I would keep all times on your server side UTC and only convert to local time in the client via JavaScript. However, dealing with client time is tricky, since you can't really know their wall time.
I have seen 3 approaches:
1) Use var offset = new Date().getTimezoneOffset();
Probably the best way of getting the client system timezone in an automatic fashion.
2) Try to figure out the timezone by the client IP.
If you need to do the time rendering on your server after all, this might me the only automatic way you can do it. It is very error prone though, because your clients might be using proxies, VPNs etc. Also, the geoIP databases might not be accurate enough.
3) Let the user set the timezone.
This is playing it safe. the user can decide. You can also kepp the setting in a cookie or such.
The momentjs timezone library might help you with all three approaches.

How to Convert from .Net TimeZone to MySQL TimeZone

I am trying to find out how to take the TimeZone or TimeZoneInfo object in .NET and use it to convert all my dates in my MySQL database from UTC to the Local Users Correct Time Zone. I am building a Desktop App. I'm planing on using the built in Convert_TZ function, but the Information that i get back from those objects does not line up with the built in method.
I found this article (http://codeofmatt.com/2014/04/07/working-with-time-zone-names-in-net/) which puts the Time Zone Info object into a better format for display but it still does not match up.
I am very new to using the TimeZone object. If there is a better way to get the current Time Zone and is easier to use then I will try that.
MySQL uses IANA time zones. You can read about MySQL's time zone support here. You can read about the different types of time zones in the timezone tag wiki.
.NET on Windows works with Microsoft Windows time zones. This is done via the TimeZoneInfo object. The older TimeZone object is also in the .NET Framework, but should not be used anymore.
To work with IANA time zones instead of Windows time zones within .NET, you can use the Noda Time library. Using Noda Time, you can get the system's local IANA time zone like this:
string tz = DateTimeZoneProviders.Tzdb.GetSystemDefault().Id;
Note that .NET on Linux or macOS already uses IANA time zones with the TimeZoneInfo object, so Noda Time is optional.
If you already are using Windows time zones, you can convert them to IANA time zones, using the methods described here.
Also note that in a previous version of this answer, I advised against using MySQL's built-in time zone support on Windows because they had not been keeping the time zone data current. This was reported to the MySQL team here, and has been fixed. If you are running MySQL on Windows, you will find the latest time zone data files here.

Mysql saving user's and server's current time difference

I have a web server which will be serving people from around the world.
The time is a really important matter in my application, since a lot of queries are taking the time in consideration.
I need a way to store user's and server's time differences.
For example:
User login to server and send its current time.
Server's save the current time difference in mySql server
Every every time the server needs to use user's time in a query he knows how to calculate it since
it knows the difference..
What i really need is the server's ability to calculate user's time according to server's own time.
I've read about using UTC time but i didn't really understand how to implement it...
Let's say you are in the US and i am in the UK. If we both check the UTC time at the same time (with whatever code/method) we should see the same result. Check out the different ways of getting UTC timestamps in milliseconds. Then, in theory, if you sync everything by UTC time (including the server) you don't really need to be aware of timezone offsets because these offsets are only relevant if you consider the server time is based on its local timezone.
In MySQL you can get a unix timestamp with UNIX_TIMESTAMP(). As far as i know it has granularity to the second, not millisecond (so you will get the number of seconds since 1970). You can also check out UTC_TIME() and UTC_TIMESTAMP().

sql server database timezone different from system timezone

Could anyone help me with a sugesstion, how can I implement following stuff:
I have a system A where my sql server is installed.
But I want to have different timezone for my database in sql server not the system A timezone.
SQL Server has no concept of a time zone. It inherits the system time from Windows, and uses that in real time. For example, if you install SQL Server while the system is in one time zone, and you change the server to a different time zone, the next time you call GETDATE() it will reflect the new time zone, not the one that was in use at the time SQL Server was installed.
In this scenario I think you should just always store UTC data (e.g. using GETUTCDATE() instead of GETDATE(). Much easier to convert it later to your desired (or any!) time zone, e.g. .NET has all kinds of built-in functionality to handle this for you.
I would recommend to use this (EST in this example)
SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time'
This return precise time date with offset for any selected time zone.
This approach will not depend on server time zone.

Mysql timestamp: output UTC time instead of local server time?

I'm creating software where I have to present time based on user's local time. The users can come from all over the world.
From what I've been reading Mysql stores timestamps in UTC and then convert it to the server timezone.
Is there anyway I can output the timestamp in the original UTC time instead of having it being automatically convert to server's local time?
I would like to do this without having to mess around with mysql's timezone settings since my software will be used in shared hosts and I don't know If I have control over those settings.
See
https://stackoverflow.com/a/12290486/110933
SELECT UNIX_TIMESTAMP(<your_datetime_col>)...