Python time zone settings and getting local time zone methods

Source: Internet
Author: User
Tags datetime function prototype time zones local time types of functions win32 in python

Processing of the Python time zone

Discover that Python does not have a simple way of dealing with time zones and does not understand why Python does not provide a time zone module to handle time zone problems. Fortunately we have a third party Pytz module that can help us solve the time zone problem.

Pytz Simple Tutorial

Pytz query for a time zone

You can find all of the country's time zones based on the country code.

>>> Import Pytz
>>> pytz.country_timezones (' CN ')
[' Asia/shanghai ', ' asia/harbin ', ' asia/chongqing ', ' Asia/urumqi ', ' Asia/kashgar ']

Pytz Create Time Zone objects

Depending on the time zone information obtained above, you can create the specified time zone object. For example, create a Shanghai time zone object:


TZ = Pytz.timezone (' Asia/shanghai ')

Get time in a time zone

You can then specify the date time for the specified time zone by specifying the time zone above when you create it:

>>> Import datetime
>>> Datetime.datetime.now (TZ)

Second, get the local time zone n methods

The first method:
When it comes to using Python to manipulate timezone, the first thought must be win32timezone ...
I tried to get a win32timezone with the results as follows:

>>> from win32timezone Import *
>>> Now (Datetime.datetime, 4, 9, 623000, Tzinfo=timezoneinfo (U ' Standard time ', True)
>>> Print Now () 2011-04-29 17:09:27.154000+08:00
>>> Print Type (now ()) <type ' Datetime.datetime ' >
>>> now ()-Tzinfo TimeZoneInfo (U ' Standard time ', True)
>>> print Now (). Tzinfo Unknown
>>> Print Type (now (). Tzinfo) <class ' Win32timezone. TimeZoneInfo ' >

The results of printing can be seen: using functions, you can get results, but if you use print, it is different from using only functions, even printing win32timezone.now (). Tzinfo's result is unknow. After I saw the types of functions, I found that they were a class type (I guess), not str, so the results printed were different.
Therefore, the use of Win32timezone method does not seem to work, because the program to write a bit longer, it is impossible to use the idle interactive ...

Note: If the "no module named ' Win32timezone" is prompted when writing the program, there is no import module

Workaround: From win32timezone import *

The second method:
Then I went to see TimeZone's function prototype, address:%python_home%/lib/site-packages/win32/lib/win32timezone.py
I found a function that wrote this:
def _get_time_zone_key (Subkey=none):
"Return to the registry key that stores time zone details"
Key = _regkeydict.open (_winreg. HKEY_LOCAL_MACHINE, Timezoneinfo.tzregkey)
If subkey:
Key = Key.subkey (subkey)
Return key

So, I've found that the way to get time zone key in Python is to get it from the registry because it uses ' _winreg ', so if the call Win32timezone is invalid, then I get it directly from the registry.
TimeZone's location is: [Hkey_current_user/software/microsoft/windows nt/currentversion/time Zones]
KeyName is: timezonekeyname
So I can get this:

Import _winreg key = _winreg. Openkey (_winreg. HKEY_CURRENT_USER, R ' software/microsoft/windows nt/currentversion/time Zones ')
Value, type = _winreg. QueryValueEx (Key, ' timezonekeyname ')
Print value
#----Result:----#
>>> Standard Time


So it worked in this way ...

Nonsense, actually use the registry to get the key value of the method is very powerful, all the information related to Win32, can be obtained from the registry, such as the user name and so on ...

The third method:
Today, I found a very simple way to look at this, example:

>>> Import Time
>>> str = time.strftime ('%Z ', Time.localtime ())
>>> Print str
Standard time

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.