start_t = datetime.datetime.now() #开始计时
#运行程序代码
end_t = datetime.datetime.now() #运行结束记时
print ((end_t - start_t).seconds) #计算并输出运行时间
import timedef ztime(t,z):#t:时间戳,z:时区,参数格式为zls的key
#以下时区列表参考维基百科http://zh.wikipedia.org/wiki/%E6%97%B6%E5%8C%BA%E5%88%97%E8%A1%A8
zls={'0':0,
'-12':-12,
'-11':-11,
'-10':-10,
'-9:30':-9.5,
'-9':-9,
'-8':-8,
'-7':-7,
'-6':-6,
'-5':-5,
'-4:30':-4.5,
'-4':-4,
'-3:30':-3.5,
'-3':-3,
'-2':-2,
'-1':-1,
'1':1,
'2':2,
'3':3,
'3:30':3.5,
'4':4,
'4:30':4.5,
'5':5,
'5:30':5.5,
'5:45':5.75,
'6':6,
'6:30':6.5,
'7':7,
'8':8,
'9':9,
'9:30':9.5,
'10':10,
'10:30':10.5,
'11':11,
'11:30':11.5,
'12':12,
'12:45':12.75,
'13':13,
'14':14}
t2=time.localtime(t+3600*zls[z]) #在时间戳t上加减各时区对应时长(注:标准时间戳为GMT时间),当前时间的t值为time.mktime(time.gmtime())
return time.strftime('%Y-%m-%d %H:%M:%S',t2)
print ztime(time.mktime(time.gmtime()),'-12') #例:-12时区的当前时间
print ztime(time.mktime(time.gmtime()),'12:45') #例:+12:45时区的当前时间
print ztime(time.mktime(time.gmtime()),'8') #例:北京时间
print ztime(time.mktime(time.gmtime()),'0')#例:格林尼治时间
Python编程中,用Tkinter中的文本框获取系统当前的时间并且显示,代码如下:
import sysfrom tkinter import *
import time
def tick():
global time1
# 从运行程序的计算机上面获取当前的系统时间
time2 = time.strftime('%H:%M:%S')
# 如果时间发生变化,代码自动更新显示的系统时间
if time2 != time1:
time1 = time2
clock.config(text=time2)
# calls itself every 200 milliseconds
# to update the time display as needed
# could use >200 ms, but display gets jerky
clock.after(200, tick)
root = Tk()
time1 = ''
status = Label(root, text="v1.0", bd=1, relief=SUNKEN, anchor=W)
status.grid(row=0, column=0)
clock = Label(root, font=('times', 20, 'bold'), bg='green')
clock.grid(row=0, column=1)
tick()
root.mainloop()