Reputation: 32390
I'm working on a web application that keeps track of scheduling for my organization. There are over 100 different official locations that it can be used from. When a user logs in, the user's physical location is saved in the session.
There is a button on the application that the user can click on. I have a requirement that when the user clicks the button, the application must record what time it is at the user's physical location.
I'm aware that it may have made sense to design the application to instead use UTC internally, but the program has been using local time for years. However, thus far, I can't see that anyone ever did anything that required use of the current time (other than the date).
I can see in my database that each location has a "locale ID" that should correspond to a time-zone, but I know that there are differences in how "daylight savings time" and "summer time" are taken into account around the world.
What's an efficient way of taking into account all these things so that when the user logs in at one of 100s of locations, I can tell what time it is at the user's physical location when he clicks a button?
Upvotes: 1
Views: 131
Reputation: 32390
I think I have this figured out. I was able to verify this solution with at least one location I've tested so far.
The "Local IDs" that I referred to are the same strings that you can pass into various .net time zone functions such as TimeZoneInfo.ConvertTimeBySystemTimeZoneId
. This function seems to be sufficient to get the local time at the user's location.
It's a good point that some of you brought up in the comments, about how a user's motivation to cheat the system might come into play, but I believe that my solution works regardless.
Upvotes: 1
Reputation: 40393
You won't be able to guarantee that it's right, but you can retrieve the date and time the browser thinks it is by using javascript (by calling new Date()
), then post that value using AJAX to the server - from there, do whatever you need to do.
There's a very good chance this date will be correct from the user's perspective.
Upvotes: 0