在GAE上,我如何根据正确的客户端TimeZone显示日期? [英] On GAE, how may I show a date according to right client TimeZone?

查看:106
本文介绍了在GAE上,我如何根据正确的客户端TimeZone显示日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Google App Engine应用程序中,我在自己的模型中存储了自动更新的日期/时间,如下所示:

  class MyModel(db.Model):
date = db.DateTimeProperty(auto_now_add = True)



<但是,该日期/时间是服务器上的当地时间,根据它的时区。



所以,当我想在我的网页上显示它时,如何根据客户时区格式化它?

另外,我怎样才能知道在哪个时区google服务器?
对于你的问题的第二部分:



Python时间()会返回UTC,而不管服务器在哪个时区。timezone()和tzname()将分别给出服务器上本地时间的偏移量和时区名称以及DST时区的元组值。截至本文发布时,GAE使用Python 2.5.x;请参阅Python时间函数的文档此处



对于问题的第一部分:

您可以使用服务器上的代码格式化日期,或者与客户端上的代码。


  • 如果您在服务器上进行格式化,您可以


    • 使用请求者IP地址的时区

    • 要求用户注册并为您提供时区,存储它并使用存储的值

    • 在GET或POST上使用(hidden?)字段,以便客户所需的时区成为请求的一部分


  • 如果您在客户端进行格式化,则需要编写几行JavaScript代码。该过程类似于使用Date(utctime)从UTC生成日期,然后将其填入文档中。默认情况下,JavaScript日期显示为本地时间,无论它们是如何初始化的 - 真棒!




我建议在客户端进行格式设置,因为什么是没有JavaScript的网页?这是2009年! 马塞洛有一些例子。


On my Google App Engine application, i'm storing an auto-updated date/time in my model like that :

class MyModel(db.Model):
   date = db.DateTimeProperty(auto_now_add=True)

But, that date/time is the local time on server, according to it's time zone.

So, when I would like to display it on my web page, how may I format it according the client time zone ?

Also, how may I know on which time zone that google server is ?

解决方案

With respect to the second part of your question:

Python time() returns UTC regardless of what time zone the server is in. timezone() and tzname() will give you, respectively, the offset to local time on the server and the name of the timezone and the DST timezone as a tuple. GAE uses Python 2.5.x as of the time of this posting; see the documentation for Python time functions here.

For the first part of the question:

You can either format the date with code on the server, or with code on the client.

  • If you format on the server, you can

    • Use the timezone of the requester's IP address
    • Require a user to register and give you a timezone, store it, and use the stored value
    • Use a (hidden?) field on the GET or POST so the client's desired timezone is part of the request
  • If you format on the client, you'll need to write a few lines of JavaScript. The procedure is something like "make a date from UTC using Date(utctime), then stuff it into the document." By default, JavaScript dates display as local time regardless of how they were initialized - awesome!

I recommend formatting on the client, because what's a webpage like without a bit of JavaScript? It's 2009! Marcelo has some examples.

这篇关于在GAE上,我如何根据正确的客户端TimeZone显示日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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