python现在的时间是几点_python中的时间

最近写了两个python脚本, 他们都用到了python中的时间.

第一个脚本的背景是: 在一个维护的项目里面,每次Release都要更新一批文件, 有一个Excel文件专门管理这些变更文件的时间戳, 在Excel中客户要求填写变更的文件以及他们对应的最后编辑时间.

于是第一个脚本解决解决的需求便是: 批量获取一堆制定文件的时间戳.

维护的项目都差不多, 以前用java写过一个根据时间戳, 判断变更文件列表的东西, 代码确实没有python简洁,

不罗嗦了,这个脚本的代码如下:

Python代码

__author__="wjason"

__date__ ="$2009-6-1 11:04:09$"

filesList = [

"xmldata/DataDict_en_GB.xml",

"xmldata/DataDict_en_US.xml",

"ReleaseNotes/ReleaseNotes_en.txt",

"ReleaseNotes/ReleaseNotes_ja.txt",

"Code/AAA.java",

"Code/daemon/BBB.java"

]

if__name__ =="__main__":

importos,time

filePath ="K:/release/history/2.24.1/Other/"

# this result is csv format. seperated by ","

result = [ fn +","+ time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(filePath+fn).st_mtime))

forfninfilesList]

print'\n'.join(result)__author__="wjason"

__date__ ="$2009-6-1 11:04:09$"

filesList = [

"xmldata/DataDict_en_GB.xml",

"xmldata/DataDict_en_US.xml",

"ReleaseNotes/ReleaseNotes_en.txt",

"ReleaseNotes/ReleaseNotes_ja.txt",

"Code/AAA.java",

"Code/daemon/BBB.java"

]

if __name__ == "__main__":

import os,time

filePath = "K:/release/history/2.24.1/Other/"

# this result is csv format. seperated by ","

result = [ fn +","+ time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(filePath+fn).st_mtime))

for fn in filesList]

print '\n'.join(result)

第二个脚本的背景是: 项目的日报以周为单位, 我们有一个日报模板, 我的工作是每周一将我们的日报模板拷贝一份,

并重命名为本周的日报. 命名规则是:"这周周一的日期~这周周五的日期" + "日报"的形式.

于是这个脚本的任务是以适当的名字拷贝日报模板. 无论他在任何时间被运行, 如果本周的日报不存在, 他都将创建出来.

脚本被放在计划任务里面执行,代码如下:

Python代码

#! /usr/bin/python

#coding:utf-8

__author__="wjason"

__date__ ="$2009-6-10 11:32:31$"

importos,time,datetime

defgenerateFileName():

ct = datetime.datetime.now()

week  = ct.weekday()

startDay = ct + datetime.timedelta(days= (0- week))

endDay   = ct + datetime.timedelta(days= (4- week))

startStr =  startDay.strftime("%y%m%d")

endStr =  endDay.strftime("%y%m%d")

filename = startStr +"~"+ endStr +"日報.xls"

returnfilename

if__name__ =="__main__":

path ="\\\\fileserver\\作業報告path"

source = path +"\\日報のTemplate.xls"

dest = path +"\\"+ generateFileName()

if(os.path.exists(dest)):

print"ERROR:  dest is existed."

else:

cmd ="cp -u "+"  "+source+"  "+dest

printcmd

os.popen(cmd)

print"Finished."#! /usr/bin/python

#coding:utf-8

__author__="wjason"

__date__ ="$2009-6-10 11:32:31$"

import os,time,datetime

def generateFileName():

ct = datetime.datetime.now()

week = ct.weekday()

startDay = ct + datetime.timedelta(days= (0 - week))

endDay = ct + datetime.timedelta(days= (4 - week))

startStr = startDay.strftime("%y%m%d")

endStr = endDay.strftime("%y%m%d")

filename = startStr + "~" + endStr + "日報.xls"

return filename

if __name__ == "__main__":

path = "\\\\fileserver\\作業報告path"

source = path +"\\日報のTemplate.xls"

dest = path + "\\" + generateFileName()

if(os.path.exists(dest)):

print "ERROR: dest is existed."

else:

cmd = "cp -u " +" "+source+" "+dest

print cmd

os.popen(cmd)

print "Finished."

总结:

在这两个脚本里面都使用到了时间, 搞得我很迷糊, 于是总结整理如下:

1. python中跟时间有管理的module有两个: time,datetime。

其中包含了创建时间,转换时间,对时间进行运算的函数。

2. python中有三种表示, 分别是:

写道

a.   一个float

b.   time module中的struct_time

c.   datetime module中的datetime class

他们三个之间可以相互转换.

3. time,datetime这两个module都有一个叫ctime()的函数,他们都返回一个字符串。

当然对字符串进行格式化操作还可以用:strftime()

4. time,datetime这两个module都有一个叫strptime()的函数, 他们执行的是3中的反操作:将字符串变成时间,

注: time中的得到的是struts_time, datetime中的得到的是datetime object.

5. datetime中的datetime配合datetime.timedelta可以对时间进行运算。

至于关于其他的函数在什么时候都返回那种时间表示?上述的三种时间表示如何转化?

可以参考下面的测试代码,其中每个测试代表一个问题:

Python代码

#! /usr/bin/python

__author__="wjason"

__date__ ="$2009-6-11 13:52:20$"

if__name__ =="__main__":

importtime,datetime

print"\n--- ===  #test01: ctime() return a String  === ---"

t_ctime = time.ctime()

printt_ctime

printtype(t_ctime)

print"\n--- ===  #test02: localtime() return time.struct_time  === ---"

t_local = time.localtime()

printtype(t_local)

print"\n--- ===  #test03:  gmtime() return time.struct_time   === ---"

t_gm = time.gmtime()

printtype(t_gm)

print"\n--- ===  #test04: datetime.now() return 'datetime' object  === ---"

t_datetime = datetime.datetime.now()

printtype(t_datetime)

print"\n--- ===  #test05: convert time.struct_time to 'datatime' object=== ---"

printt_local

t_datetime = datetime.datetime(*t_local[:6])

printt_local[:6]

printt_datetime

print"\n--- ===  #test06: convert 'datatime' object to time.struct_time  === ---"

t_temp =  t_datetime.timetuple()

printt_temp

printtype(t_temp)

print"\n--- ===  #test07: mktime(struct_time ts) return float which represent the time === ---"

print"--- ===  #test08: convert struts_time to float  === ---"

t_mk = time.mktime(t_local)

printtype(t_mk)

print"\n--- ===  #test08: localtime(float f) return struct_time === ---"

print"--- ===  #test08: localtime() and mktime() are inverse function of each other  === ---"

print"--- ===  #test08: convert float to struts_time  === ---"

t_local = time.localtime(t_mk)

printtype(t_local)#! /usr/bin/python

__author__="wjason"

__date__ ="$2009-6-11 13:52:20$"

if __name__ == "__main__":

import time,datetime

print "\n--- === #test01: ctime() return a String === ---"

t_ctime = time.ctime()

print t_ctime

print type(t_ctime)

print "\n--- === #test02: localtime() return time.struct_time === ---"

t_local = time.localtime()

print type(t_local)

print "\n--- === #test03: gmtime() return time.struct_time === ---"

t_gm = time.gmtime()

print type(t_gm)

print "\n--- === #test04: datetime.now() return 'datetime' object === ---"

t_datetime = datetime.datetime.now()

print type(t_datetime)

print "\n--- === #test05: convert time.struct_time to 'datatime' object=== ---"

print t_local

t_datetime = datetime.datetime(*t_local[:6])

print t_local[:6]

print t_datetime

print "\n--- === #test06: convert 'datatime' object to time.struct_time === ---"

t_temp = t_datetime.timetuple()

print t_temp

print type(t_temp)

print "\n--- === #test07: mktime(struct_time ts) return float which represent the time === ---"

print "--- === #test08: convert struts_time to float === ---"

t_mk = time.mktime(t_local)

print type(t_mk)

print "\n--- === #test08: localtime(float f) return struct_time === ---"

print "--- === #test08: localtime() and mktime() are inverse function of each other === ---"

print "--- === #test08: convert float to struts_time === ---"

t_local = time.localtime(t_mk)

print type(t_local)

资源:

这部分资源网上以搜,很多很多。其中我觉得这篇最为不错:

里面涉及了: ISO Strings, Unix time, mxDateTime, matplotlib等等概念以及它们的常用操作。

可谓应有尽有,同时这里连接里面也有自己的一份References,指向详细的文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值