简体   繁体   English

Django是否会自动检测最终用户的时区?

[英]Does Django automatically detect the end user's timezone?

I am building an application in Django which allows the end-user to retrieve information which is sensitive to the time of day (12 am to 12 am) on a given day. 我正在Django中构建一个应用程序,该应用程序允许最终用户检索在一天中对一天中的时间(上午12点至上午12点)敏感的信息。 I store this information in my database as an integer representing the seconds since midnight in 30-minute increments. 我将此信息作为整数存储在数据库中,该整数表示自午夜以来以30分钟为增量的秒数。 I was looking at Django's timezone documentation: https://docs.djangoproject.com/en/2.0/topics/i18n/timezones/ and found myself confused on whether or Django automatically uses the end-users time, or if I must collect this information and account for it in my views. 我正在查看Django的时区文档: https : //docs.djangoproject.com/en/2.0/topics/i18n/timezones/ ,发现自己对Django是否自动使用最终用户时间或是否必须收集该时间感到困惑信息并在我看来对其进行解释。

Any information would be helpful! 任何信息都有帮助! Thanks. 谢谢。

Django doesn't detect end user's timezone. Django无法检测最终用户的时区。 But what it does is pretty clever. 但是它的确很聪明。

Consider this: 考虑一下:

Client               |  Server              | UTC
time      9:00 am    |  time      12:00 pm  | time   7:00 am
timezone  UTC+2:00   |  timezone  UTC+5:00  |

As you can see, the server and client are in different timezones and therefore have different local time. 如您所见,服务器和客户端位于不同的时区,因此具有不同的本地时间。

Now, let's say the client makes a request to create a new object in your database. 现在,假设客户端发出了在数据库中创建新对象的请求。 This object has a date field. 该对象具有日期字段。 So what Django does is, it saves the object's date under UTC. 因此Django要做的是,将对象的日期保存在UTC下。

How does that work? 这是如何运作的?

Django will first take the server's local time, that is 12:00 am. Django首先将采用服务器的本地时间,即12:00 am。 Then it will convert it to UTC time by subtracting 5 hours which becomes 7:00 am. 然后,它将减去5小时(即7:00 am)将其转换为UTC时间。 And this is the time that Django will use to save the object. 这是Django用于保存对象的时间。


Now, let's say later, the client wants to see at what time did he create that object. 现在,让我们稍后再说,客户希望看到他何时创建该对象。

The object's creation time is 7:00 am UTC . 对象的创建时间为7:00 am UTC To display the object's creation time to your client, you can either use the app linked by Jason in another answer. 要将对象的创建时间显示给客户端,您可以在另一个答案中使用由Jason链接的应用程序。 But I don't find timezone lookup using IP very reliable as the user could be using a proxy. 但是我发现使用IP进行时区查找不是很可靠,因为用户可能正在使用代理。

OR, you can render the time in UTC and use some JavaScript to convert it to Client's local time. 或者,您可以使用UTC呈现时间,并使用一些JavaScript将其转换为客户的本地时间。 Since, JS runs on browser, it will accurately pick client's timezone from their computer. 由于JS在浏览器上运行,因此它将从他们的计算机中准确选择客户端的时区。

Finally, when you convert 7:00 am UTC to the client's timezone, by adding 2 hours (as it is 2 hours ahead of UTC), the client will see the object's creation time as 9:00 am which is what they expected. 最后,当您将7:00 am UTC转换为客户端的时区时,通过增加2小时(因为它比UTC提前2小时),客户端将看到对象的创建时间为9:00 am ,这是他们期望的时间。

Not only, that, if there's another client on the other side of the Earth in UTC-5:00 timezone, you can display the object's time to them by subtracting 5 hours which would give 2:00 am . 不仅如此,如果地球另一边的UTC-5:00时区还有另一个客户端,您可以通过减去5小时(即2:00 am向他们显示对象的时间。 So, according to that user, it was added at 2:00 am. 因此,根据该用户的说法,它是在凌晨2:00添加的。

No. There are some packages where you can detect timezone based on IP address. 否。有些软件包可以在其中基于IP地址检测时区。 You can look at https://github.com/adamcharnock/django-tz-detect 您可以查看https://github.com/adamcharnock/django-tz-detect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM