Python CGI - 根据用户时区更改数据
我有一个打印日期时间值的 Python CGI 程序。时间戳全部格式化为服务器时间,这与某些用户的本地时间相差很大。如何根据所有用户的时区自动设置其格式?
以下是格式化字符串之一:
2011-09-25 02:04:54
该字符串是使用 datetime.datetime 对象创建的。
-Sunjay03
I have a Python CGI program which prints datetime values. The timestamps are all formatted to the server's time which is way off from the local time of some of the users. How can I format it automatically for all users based on their time zone?
Here is one of the formatted string:
2011-09-25 02:04:54
That string was created by using a datetime.datetime object.
-Sunjay03
您必须有办法找到用户的时区。实现此目的的一种方法是让他们将其设置在首选项页面中,并将其存储在数据库中,并在呈现页面时查找它。另一种方法是让浏览器在请求中包含其当前时间,以便您可以调整时间。
如果您知道用户与服务器时间的偏移量,则可以使用 timedelta 方法来调整日期时间对象。
You will have to have a way to find the timezone of the user. One way to do this is by having them set it in a preference page, and storing it in the database, and looking it up when you render the page. Another way is to have the browser include its current time in the request, so that you can adjust your times.
One you know the user's offset from your server time, you can use
timedelta
methods to adjust the datetime object.