Django是否会自动检测最终用户的时区? [英] Does Django automatically detect the end user's timezone?

查看:58
本文介绍了Django是否会自动检测最终用户的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django中构建一个应用程序,该应用程序允许最终用户检索在一天中对一天中的特定时间(上午12点至上午12点)敏感的信息。我将此信息作为整数存储在数据库中,该整数代表自午夜以来以30分钟为增量的秒数。我正在查看Django的时区文档: https://docs.djangoproject.com/ zh_cn / 2.0 / topics / i18n / timezones / ,发现自己对Django是否自动使用最终用户的时间感到困惑,或者我是否必须收集此信息并在我的意见中作出解释。



任何信息都将有所帮助!谢谢。

解决方案

Django无法检测到最终用户的时区。



考虑一下:

 客户|服务器| UTC 
时间9:00 am |时间12:00 pm |时间7:00 am
时区UTC + 2:00 |时区UTC + 5:00 |

如您所见,服务器和客户端位于不同的时区,因此具有不同的本地时间。 / p>

现在,假设客户端发出了在数据库中创建新对象的请求。该对象具有日期字段。因此Django要做的是,将对象的日期保存在UTC下。



这是怎么工作的?



Django首先会占用服务器的本地时间,即上午12:00。 。然后,它将减去5小时(即7:00)将其转换为UTC时间。这是Django用于保存对象的时间。






现在,让我们稍后再说,客户端想要看看他什么时候创建那个对象的。



对象的创建时间为世界标准时间 7:00 AM 。要将对象的创建时间显示给客户,您可以在另一个答案中使用由Jason链接的应用程序。但是我发现使用IP进行时区查找不是很可靠,因为用户可能正在使用代理。



或者,您可以在UTC中渲染时间并使用一些JavaScript来将其转换为客户的当地时间。由于JS是在浏览器上运行的,因此它会从他们的计算机上准确选择客户端的时区。



最后,当您转换UTC上午 到客户端的时区,通过增加2小时(因为它比UTC早2小时),客户端将看到对象的创建时间为 9:00 am

不仅如此,如果地球另一端的 UTC-5:00中还有另一个客户时区,您可以通过减去5小时来向他们显示对象的时间,这将使 2:00 am 。因此,根据该用户的说法,它是在凌晨2:00添加的。


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. I store this information in my database as an integer representing the seconds since midnight in 30-minute increments. 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.

Any information would be helpful! Thanks.

解决方案

Django doesn't detect end user's timezone. 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.

How does that work?

Django will first take the server's local time, that is 12:00 am. Then it will convert it to UTC time by subtracting 5 hours which becomes 7:00 am. And this is the time that Django will use to save the object.


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. To display the object's creation time to your client, you can either use the app linked by Jason in another answer. But I don't find timezone lookup using IP very reliable as the user could be using a proxy.

OR, you can render the time in UTC and use some JavaScript to convert it to Client's local time. Since, JS runs on browser, it will accurately pick client's timezone from their computer.

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.

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. So, according to that user, it was added at 2:00 am.

这篇关于Django是否会自动检测最终用户的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆