Google App Engine -- Python time zone conversion

Source: Internet
Author: User

After playing Gae for a while, I suddenly found that in the Python application of gae, the obtained time is UTC time, But I clearly remember that the obtained time was local time !!

An application should have a time reference point. Most applications should use local time as the reference point.

 

In python2.5:

>>> Import datetime
>>> Datetime. datetime. Now ()
Datetime. datetime (2009, 5, 26, 20, 18, 2, 593000)
>>> Datetime. datetime. Now (8)
Traceback (most recent call last ):
File "<interactive input>", line 1, in <module>
Typeerror: tzinfo argument must be none or of a tzinfo subclass, not type 'int'

 

It can be seen that the passed TZ parameter must be a subclass of datetime. tzinfo or none. However, datetime. tzinfo is an abstract class and cannot be instantiated.

 

Python third-party packages are easy to handle:

>>> Import pytz
>>> Import datetime
>>> Datetime. datetime. Now ()
Datetime. datetime (2009, 5, 26, 20, 23, 12,109 000)
>>> Datetime. datetime. Now (pytz. UTC)
Datetime. datetime (2009, 5, 26, 12, 23, 23,500 000, tzinfo = <UTC>)
>>> Datetime. datetime. Now (pytz. timezone (pytz. country_timezones ('cn') [2])
Datetime. datetime (2009, 5, 26, 20, 24, 9, 296000, tzinfo = <dsttzinfo 'Asia/chongqing' CST + 8:00:00 STD>)
>>> Pytz. country_timezones ('cn ')
['Asia/Shanghai', 'Asia/harbin', 'Asia/Chongqing ', 'Asia/Urumqi', 'Asia/kashgar']
>>>

 

It is mainly the TZ parameter construction of datetime. datetime. Now (Tz.

 

To reduce the volume of a third-party package, compress the package and add it to SYS. Path.

 

For example:

The pytzpy2.5-2006.zip contains the pytz folder

Pytz_path = r 'd:/applit/Python/pytzpy2.5-2006.zip'

Import sys

SYS. insert (0, pytz_path)

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.