使用python,什么是自动确定用户当前时区的最准确的方法 [英] Using python, what is the most accurate way to auto determine a users current timezone

查看:175
本文介绍了使用python,什么是自动确定用户当前时区的最准确的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经证实dateutils.tz.tzlocal()在heroku上不起作用,即使这样做,是不是只能从计算机的操作系统上获得tz,而不是必须的用户呢?



存储用户时区不足,有什么方法可以确定请求来自哪里? (我用烧瓶)

Twitter的确有一个设置来调整你的时区,但我想知道他们是如何确定什么样的默认应该是什么样的,用户没有登录。

解决方案

您可以使用Javascript并在Cookie中设置客户端的时区。您甚至可以使用AJAX请求,然后将偏移量发送到服务器并保存在客户端的会话中。

  var offset = new 。日期()使用getTimezoneOffset(); 




描述

时区偏移量是UTC和本地时间之间的差值,以分钟为单位。请注意,>这意味着如果本地时区在UTC之后,则偏移量为正,如果超前则为负。例如,如果您的时区是UTC + 10(澳大利亚东部标准时间),则会返回-600。夏令时可以防止这个值在给定的语言环境中保持不变。

Mozilla Javascript Reference:getTimezoneOffset < a>


I have verified that dateutils.tz.tzlocal() does not work on heroku and even if it did, wouldn't it just get the tz from the OS of the computer its on, not necessarly the users?

Short of storing a users timezone, is there any way to determine where a request is coming from? (I'm using flask)

Twitter does have a setting to adjust your timezone but I'm wondering how they determine what the default should be and how that would work when a user is not logged in.

解决方案

You could use Javascript and set the client's time zone in a cookie. You could even use an AJAX request and then send the offset to the server and save in the client's session.

var offset = new Date().getTimezoneOffset();

Description

The time-zone offset is the difference, in minutes, between UTC and local time. Note that>this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight savings time prevents this value from being a constant even for a given locale

Mozilla Javascript Reference: getTimezoneOffset

这篇关于使用python,什么是自动确定用户当前时区的最准确的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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