Search code examples
timezonecountrypytz

why pytz.country_timezones('cn') in centos system have different result?


Two computer install centos 6.5, kernel is 3.10.44, have different result. one result is [u'Asia/Shanghai', u'Asia/Urumqi'], and the other is ['Asia/Shanghai', 'Asia/Harbin', 'Asia/Chongqing', 'Asia/Urumqi', 'Asia/Kashgar'].

Is there any config that make the first result same as the second result?

I have following python code:

def get_date():
    date = datetime.utcnow()
    from_zone = pytz.timezone("UTC")
    to_zone = pytz.timezone("Asia/Urumqi")
    date = from_zone.localize(date)
    date = date.astimezone(to_zone)

    return date

def get_curr_time_stamp():
    date = get_date()
    stamp = time.mktime(date.timetuple())

    return stamp
 cur_time = get_curr_time_stamp()
 print "1", time.strftime("%Y %m %d %H:%M:%S", time.localtime(time.time()))
 print "2", time.strftime("%Y %m %d %H:%M:%S", time.localtime(cur_time))

When use this code to get time, the result of one computer(have 2 results) is:

1 2016 04 20 08:53:18
2 2016 04 20 06:53:18

and the other(have 5 results) is:

1 2016 04 20 08:53:18
2 2016 04 20 08:53:18

I don't know why?


Solution

  • You probably just have an outdated version of pytz on the system returning five time zones (or perhaps on both systems). You can find the latest releases here. It's important to stay on top of time zone updates, as the various governments of the world change their time zones often.

    Like most systems, pytz gets its data from the tz database. The five time zones for China were reduced to two in version 2014f (corresponding to pytz 2014.6). From the release notes:

    China's five zones have been simplified to two, since the post-1970 differences in the other three seem to have been imaginary. The zones Asia/Harbin, Asia/Chongqing, and Asia/Kashgar have been removed; backwards-compatibility links still work, albeit with different behaviors for time stamps before May 1980. Asia/Urumqi's 1980 transition to UTC+8 has been removed, so that it is now at UTC+6 and not UTC+8. (Thanks to Luther Ma and to Alois Treindl; Treindl sent helpful translations of two papers by Guo Qingsheng.)

    Also, you may wish to read Wikipedia's Time in China article, which explains that the Asia/Urumqui entry is for "Ürümqi Time", which is used unofficially in some parts of the Xinjiang region. This zone is not recognized by the Chinese government, and is considered a politically charged issue. As such, many systems choose to omit the Urumqi time zone, despite it being in listed in the tz database.