如何让我的ASP.net Web应用程序知道它在用户所在位置的时间?

时间:2022-06-11 20:13:42

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.

我正在开发一个跟踪组织安排的Web应用程序。它可以使用100多种不同的官方位置。用户登录时,用户的物理位置将保存在会话中。

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).

我知道将应用程序设计为内部使用UTC可能是有意义的,但该程序多年来一直使用本地时间。但是,到目前为止,我看不出有人做过任何需要使用当前时间的事情(日期除外)。

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.

我可以在我的数据库中看到每个位置都有一个应该与时区对应的“区域设置ID”,但我知道世界各地的“夏令时”和“夏令时”的考虑方式有所不同。

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?

什么是考虑所有这些事情的有效方式,以便当用户在100个位置之一登录时,我可以告诉用户点击按钮时在用户的物理位置的时间?

2 个解决方案

#1


1  

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.

我所提到的“本地ID”是可以传递到各种.net时区函数的相同字符串,例如TimeZoneInfo.ConvertTimeBySystemTimeZoneId。此功能似乎足以获得用户所在位置的本地时间。

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.

这是一个很好的观点,你们中的一些人在评论中提到,关于用户欺骗系统的动机如何发挥作用,但我相信我的解决方案无论如何都有效。

#2


0  

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.

您将无法保证它是正确的,但您可以使用javascript(通过调用new Date())检索浏览器认为它的日期和时间,然后使用AJAX将该值发布到服务器 - 从那里,做你需要做的事。

There's a very good chance this date will be correct from the user's perspective.

从用户的角度来看,这个日期很有可能是正确的。

#1


1  

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.

我所提到的“本地ID”是可以传递到各种.net时区函数的相同字符串,例如TimeZoneInfo.ConvertTimeBySystemTimeZoneId。此功能似乎足以获得用户所在位置的本地时间。

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.

这是一个很好的观点,你们中的一些人在评论中提到,关于用户欺骗系统的动机如何发挥作用,但我相信我的解决方案无论如何都有效。

#2


0  

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.

您将无法保证它是正确的,但您可以使用javascript(通过调用new Date())检索浏览器认为它的日期和时间,然后使用AJAX将该值发布到服务器 - 从那里,做你需要做的事。

There's a very good chance this date will be correct from the user's perspective.

从用户的角度来看,这个日期很有可能是正确的。