python中的时间

来源:互联网 发布:c语言结构体赋初值 编辑:程序博客网 时间:2024/06/07 04:15

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

 

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

 

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

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

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

Python代码 复制代码
  1. __author__="wjason"  
  2. __date__ ="$2009-6-1 11:04:09$"  
  3.   
  4. filesList = [   
  5.         "xmldata/DataDict_en_GB.xml",   
  6.         "xmldata/DataDict_en_US.xml",   
  7.         "ReleaseNotes/ReleaseNotes_en.txt",   
  8.         "ReleaseNotes/ReleaseNotes_ja.txt",   
  9.         "Code/AAA.java",   
  10.         "Code/daemon/BBB.java"  
  11. ]   
  12.   
  13. if __name__ == "__main__":   
  14.     import os,time   
  15.        
  16.     filePath = "K:/release/history/2.24.1/Other/"  
  17.     # this result is csv format. seperated by ","   
  18.     result = [ fn +","+ time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(os.stat(filePath+fn).st_mtime))   
  19.                for fn in filesList]   
  20.     print '/n'.join(result)  

 

 

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

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

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

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

 

Python代码 复制代码
  1. #! /usr/bin/python   
  2. #coding:utf-8   
  3.   
  4. __author__="wjason"  
  5. __date__ ="$2009-6-10 11:32:31$"  
  6.   
  7. import os,time,datetime   
  8.   
  9. def generateFileName():   
  10.     ct = datetime.datetime.now()   
  11.     week  = ct.weekday()   
  12.   
  13.     startDay = ct + datetime.timedelta(days= (0 - week))   
  14.     endDay   = ct + datetime.timedelta(days= (4 - week))   
  15.   
  16.     startStr =  startDay.strftime("%y%m%d")   
  17.     endStr =  endDay.strftime("%y%m%d")   
  18.        
  19.     filename = startStr + "~" + endStr + "日報.xls"  
  20.     return filename   
  21.   
  22. if __name__ == "__main__":   
  23.     path = "////fileserver//作業報告path"  
  24.     source = path +"//日報のTemplate.xls"  
  25.     dest = path + "//" + generateFileName()   
  26.     if(os.path.exists(dest)):   
  27.         print "ERROR:  dest is existed."  
  28.     else:   
  29.         cmd = "cp -u " +"  "+source+"  "+dest   
  30.         print cmd   
  31.         os.popen(cmd)   
  32.         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代码 复制代码
  1. #! /usr/bin/python   
  2.   
  3. __author__="wjason"  
  4. __date__ ="$2009-6-11 13:52:20$"  
  5.   
  6. if __name__ == "__main__":   
  7.     import time,datetime   
  8.   
  9.     print "/n--- ===  #test01: ctime() return a String  === ---"  
  10.     t_ctime = time.ctime()   
  11.     print t_ctime   
  12.     print type(t_ctime)   
  13.   
  14.     print "/n--- ===  #test02: localtime() return time.struct_time  === ---"  
  15.     t_local = time.localtime()   
  16.     print type(t_local)   
  17.   
  18.     print "/n--- ===  #test03:  gmtime() return time.struct_time   === ---"  
  19.     t_gm = time.gmtime()   
  20.     print type(t_gm)   
  21.   
  22.     print "/n--- ===  #test04: datetime.now() return 'datetime' object  === ---"  
  23.     t_datetime = datetime.datetime.now()   
  24.     print type(t_datetime)   
  25.   
  26.     print "/n--- ===  #test05: convert time.struct_time to 'datatime' object=== ---"  
  27.     print t_local   
  28.     t_datetime = datetime.datetime(*t_local[:6])   
  29.     print t_local[:6]   
  30.     print t_datetime   
  31.   
  32.     print "/n--- ===  #test06: convert 'datatime' object to time.struct_time  === ---"  
  33.     t_temp =  t_datetime.timetuple()   
  34.     print t_temp   
  35.     print type(t_temp)   
  36.   
  37.     print "/n--- ===  #test07: mktime(struct_time ts) return float which represent the time === ---"  
  38.     print "--- ===  #test08: convert struts_time to float  === ---"  
  39.     t_mk = time.mktime(t_local)   
  40.     print type(t_mk)   
  41.   
  42.     print "/n--- ===  #test08: localtime(float f) return struct_time === ---"  
  43.     print "--- ===  #test08: localtime() and mktime() are inverse function of each other  === ---"  
  44.     print "--- ===  #test08: convert float to struts_time  === ---"  
  45.     t_local = time.localtime(t_mk)   
  46.     print type(t_local)  
 

资源:

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

Date and Time Representation in Python

 

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

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