当前位置: 首页 > python > 正文

write a own tools of WorldTime with pytz

require : python2.6 pytz

#!/usr/bin/env python
# -*- coding: gbk -*-
from datetime import datetime, timedelta
from time import gmtime, strftime
from pytz import timezone
import pytz, time,os

#def convert_datetime(unix_timestamp=1143408000, tz=1, long_fmt=1):
def convert_datetime(dt=’2007-01-01 00:00:00′, tz=”, dest_fmt=”, time_stamp=0):
    fmt      = ‘%Y-%m-%d %H:%M:%S’
    if time_stamp == 0:
        dt_stamp = time.mktime(time.strptime(dt, fmt))
    else:
        dt_stamp = float(dt)
    utc      = pytz.utc
    utc_dt   = datetime.utcfromtimestamp(dt_stamp).replace(tzinfo=utc)  
    dest_tz  = timezone(tz)
    dest_dt  = dest_tz.normalize(utc_dt.astimezone(dest_tz))  
    return dest_dt.strftime(dest_fmt)

#define all citys here
citys = {‘Asia/Shanghai’:’Asia/Shanghai 上 海’,
        ‘America/Los_Angeles’:’America/Los_Angeles 旧金山’,
        ‘Etc/GMT’:’Etc/GMT 格林威治标准时间’,
        ‘US/Pacific’:’US/Pacific PT 太平洋时间’,
        ‘UTC’:’UTC 世界标准时间’,
        #’Etc/GMT+8′:’Etc/GMT+8′,
       }
 
if __name__ == ‘__main__’:
    while True:
        os.system(‘cls’)
        print ‘————–时间对照————–‘
        for k,v in citys.items():
            print convert_datetime(dt=strftime(“%Y-%m-%d %H:%M:%S”, time.localtime()), tz=k,dest_fmt=’%Y-%m-%d %H:%M:%S’),’\t[‘+v+’]’
        print ‘————————————‘
        time.sleep(1)

       
run.bat
set path=D:\python26\;%path%
D:
cd D:\Profiles\2hei.net\eclipse-project\py\myapp\src\time_format
python world_time.py

running like this:

world_time_zone.png

 

本文固定链接: https://www.2hei.net/2010/06/29/write_a_own_tools_of_worldtime/ | 2hei.net

该日志由 u2 于2010年06月29日发表在 python 分类下,
原创文章转载请注明: write a own tools of WorldTime with pytz | 2hei.net
关键字:

报歉!评论已关闭.