Question

     I am using MySQL database for my Application. I am using Java (with Spring) for my web development and Apache Tomcat for Application Server. In my application there are multiple users with different Timezons. Now suppose I am calculating total login time for users.

     I am providing SQL Fiddle link here for necessary DDL and DML queries. In that I have also written select query for count total login time for every user. In that users with userIds 3 and 4 are not still logged-Out, so I am using NOW() for counting total logged in time. But they are from different timezones. And I am storing all times in MYSQL with GMT-0. So will it give me wrong logged time for userId 3 and 4 or not? If wrong, what is solution for it?

Was it helpful?

Solution

So will it give me wrong logged time for userId 3 and 4 or not? If wrong, what is solution for it?

If you actually record the value with GMT+0 won't give you the wrong answer. It doesn't matter what the local time is for the user - all you're interested in is how much time has elapsed between login and logout.

Indeed, using the user's local time zone would make this much harder - you'd end up having to take account of daylight saving time. So for example, suppose you were logging my interactions on October 26th 2014. I might have a local login time of 1:45am and a local logout time of 1:05am, which sounds messed up - until you realize that the login time was when daylight saving time was in effect. If you just record the UTC time (GMT+0) you'd record 00:45 and 01:05, leaving the obvious difference of 20 minutes.

Basically, when you are trying to record points in time you might want to record the time zone of the user so you can tell them what they saw at the time... but you should record the date/time itself in UTC for easy manipulation (e.g. translation into another time zone). You should very rarely record the local date/time; it's useful for recurring events (e.g. "2pm local time every week) but for individual points in time it's just a bad idea.

Note that recording the UTC time is not the same as recording the local time on the server; you should try to make sure that the server's time zone is completely irrelevant to the application's behaviour... aside from anything else, that means nothing bad will happen if you have multiple servers in different time zones. Given that NOW() uses the system local time zone, that could give the wrong answer - it would be a good idea to try to use a function which uses UTC (e.g. use UTC_TIMESTAMP), or set the "current time zone" in MySQL to UTC. (You claim to already be storing the dates in GMT-0, but it's not clear how you expect that to happen if you're using NOW(), unless you are setting the time zone to UTC.)

OTHER TIPS

I am pretty sure you are doing it correctly. im sure it will give the time of the server that is hosting MYSQL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top