python time模块

---恢复内容开始---

  1. python 的内嵌time模板翻译及说明  
  2.     
  3. 一、简介  
  4.   
  5.   time模块提供各种操作时间的函数  
  6.   说明:一般有两种表示时间的方式:  
  7.        第一种是时间戳的方式(相对于1970.1.00:00:00以秒计算的偏移量),时间戳是惟一的  
  8.        第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同  
  9.     year (four digits, e.g. 1998)  
  10.     month (1-12)  
  11.     day (1-31)  
  12.     hours (0-23)  
  13.     minutes (0-59)  
  14.     seconds (0-59)  
  15.     weekday (0-6, Monday is 0)  
  16.     Julian day (day in the year, 1-366)  
  17.     DST (Daylight Savings Time) flag (-1, or 1) 是否是夏令时  
  18.     If the DST flag is 0, the time is given in the regular time zone;  
  19.     if it is 1, the time is given in the DST time zone;  
  20.     if it is -1, mktime() should guess based on the date and time.  
  21.     夏令时介绍:http://baike.baidu.com/view/100246.htm  
  22.     UTC介绍:http://wenda.tianya.cn/wenda/thread?tid=283921a9da7c5aef&clk=wttpcts  
  23.       
  24. 二、函数介绍  
  25. 1.asctime()  
  26.   asctime([tuple]) -> string  
  27.   将一个struct_time(默认为当时时间),转换成字符串  
  28.         Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.  
  29.         When the time tuple is not present, current time as returned by localtime()  
  30.         is used.  
  31.           
  32. 2.clock()  
  33.   clock() -> floating point number  
  34.   该函数有两个功能,  
  35.   在第一次调用的时候,返回的是程序运行的实际时间;  
  36.   以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔  
  37.     
  38.   示例:  
  39. import time    
  40. if __name__ == '__main__':    
  41.     time.sleep(1)    
  42.     print "clock1:%s" % time.clock()    
  43.     time.sleep(1)    
  44.     print "clock2:%s" % time.clock()    
  45.     time.sleep(1)    
  46.     print "clock3:%s" % time.clock()    
  47. import time  
  48. if __name__ == '__main__':  
  49.     time.sleep(1)  
  50.     print "clock1:%s" % time.clock()  
  51.     time.sleep(1)  
  52.     print "clock2:%s" % time.clock()  
  53.     time.sleep(1)  
  54.     print "clock3:%s" % time.clock()   
  55.   输出:  
  56.   clock1:3.35238137808e-006  
  57.   clock2:1.00004944763  
  58.   clock3:2.00012040636  
  59.   其中第一个clock输出的是程序运行时间  
  60.   第二、三个clock输出的都是与第一个clock的时间间隔  
  61.     
  62. 3.sleep(...)  
  63.   sleep(seconds)  
  64.   线程推迟指定的时间运行,经过测试,单位为秒,但是在帮助文档中有以下这样一句话,这关是看不懂  
  65.   “The argument may be a floating point number for subsecond precision.”  
  66.   
  67. 4.ctime(...)  
  68.   ctime(seconds) -> string  
  69.   将一个时间戳(默认为当前时间)转换成一个时间字符串  
  70.   例如:  
  71.   time.ctime()  
  72.   输出为:'Sat Mar 28 22:24:24 2009'  
  73.           
  74. 5.gmtime(...)  
  75.   gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)  
  76.   将一个时间戳转换成一个UTC时区(0时区)的struct_time,如果seconds参数未输入,则以当前时间为转换标准  
  77.     
  78.       
  79. 6.localtime(...)  
  80.   localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)  
  81.   将一个时间戳转换成一个当前时区的struct_time,如果seconds参数未输入,则以当前时间为转换标准  
  82.     
  83.           
  84. 7.mktime(...)  
  85.   mktime(tuple) -> floating point number  
  86.   将一个以struct_time转换为时间戳  
  87.           
  88. 8.strftime(...)  
  89.   strftime(format[, tuple]) -> string  
  90.   将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出  
  91.   python中时间日期格式化符号:  
  92.   %y 两位数的年份表示(00-99)  
  93.   %Y 四位数的年份表示(000-9999)  
  94.   %m 月份(01-12)  
  95.   %d 月内中的一天(0-31)  
  96.   %H 24小时制小时数(0-23)  
  97.   %I 12小时制小时数(01-12)   
  98.   %M 分钟数(00=59)  
  99.   %S 秒(00-59)  
  100.     
  101.   %a 本地简化星期名称  
  102.   %A 本地完整星期名称  
  103.   %b 本地简化的月份名称  
  104.   %B 本地完整的月份名称  
  105.   %c 本地相应的日期表示和时间表示  
  106.   %j 年内的一天(001-366)  
  107.   %p 本地A.M.或P.M.的等价符  
  108.   %U 一年中的星期数(00-53)星期天为星期的开始  
  109.   %w 星期(0-6),星期天为星期的开始  
  110.   %W 一年中的星期数(00-53)星期一为星期的开始  
  111.   %x 本地相应的日期表示  
  112.   %X 本地相应的时间表示  
  113.   %Z 当前时区的名称  
  114.   %% %号本身   
  115.       
  116. 9.strptime(...)  
  117.   strptime(string, format) -> struct_time  
  118.   将时间字符串根据指定的格式化符转换成数组形式的时间  
  119.   例如:  
  120.   2009-03-20 11:45:39  对应的格式化字符串为:%Y-%m-%d %H:%M:%S  
  121.   Sat Mar 28 22:24:24 2009 对应的格式化字符串为:%a %b %d %H:%M:%S %Y  
  122.       
  123. 10.time(...)  
  124.    time() -> floating point number  
  125.    返回当前时间的时间戳  
  126.   
  127. 三、疑点  
  128. 1.夏令时  
  129.   在struct_time中,夏令时好像没有用,例如  
  130.   a = (2009, 6, 28, 23, 8, 34, 5, 87, 1)  
  131.   b = (2009, 6, 28, 23, 8, 34, 5, 87, 0)  
  132.   a和b分别表示的是夏令时和标准时间,它们之间转换为时间戳应该相关3600,但是转换后输出都为646585714.0  
  133.     
  134. 四、小应用  
  135. 1.python获取当前时间  
  136.    time.time() 获取当前时间戳  
  137.    time.localtime() 当前时间的struct_time形式  
  138.    time.ctime() 当前时间的字符串形式  
  139.      
  140. 2.python格式化字符串    
  141.   格式化成2009-03-20 11:45:39形式  
  142.   time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())   
  143.     
  144.   格式化成Sat Mar 28 22:24:24 2009形式  
  145.   time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())   
  146.     
  147. 3.将格式字符串转换为时间戳  
  148.   a = "Sat Mar 28 22:24:24 2009"  
  149.   b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))  

---恢复内容结束---

  1. python 的内嵌time模板翻译及说明  
  2.     
  3. 一、简介  
  4.   
  5.   time模块提供各种操作时间的函数  
  6.   说明:一般有两种表示时间的方式:  
  7.        第一种是时间戳的方式(相对于1970.1.00:00:00以秒计算的偏移量),时间戳是惟一的  
  8.        第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同  
  9.     year (four digits, e.g. 1998)  
  10.     month (1-12)  
  11.     day (1-31)  
  12.     hours (0-23)  
  13.     minutes (0-59)  
  14.     seconds (0-59)  
  15.     weekday (0-6, Monday is 0)  
  16.     Julian day (day in the year, 1-366)  
  17.     DST (Daylight Savings Time) flag (-1, or 1) 是否是夏令时  
  18.     If the DST flag is 0, the time is given in the regular time zone;  
  19.     if it is 1, the time is given in the DST time zone;  
  20.     if it is -1, mktime() should guess based on the date and time.  
  21.     夏令时介绍:http://baike.baidu.com/view/100246.htm  
  22.     UTC介绍:http://wenda.tianya.cn/wenda/thread?tid=283921a9da7c5aef&clk=wttpcts  
  23.       
  24. 二、函数介绍  
  25. 1.asctime()  
  26.   asctime([tuple]) -> string  
  27.   将一个struct_time(默认为当时时间),转换成字符串  
  28.         Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.  
  29.         When the time tuple is not present, current time as returned by localtime()  
  30.         is used.  
  31.           
  32. 2.clock()  
  33.   clock() -> floating point number  
  34.   该函数有两个功能,  
  35.   在第一次调用的时候,返回的是程序运行的实际时间;  
  36.   以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔  
  37.     
  38.   示例:  
  39. import time    
  40. if __name__ == '__main__':    
  41.     time.sleep(1)    
  42.     print "clock1:%s" % time.clock()    
  43.     time.sleep(1)    
  44.     print "clock2:%s" % time.clock()    
  45.     time.sleep(1)    
  46.     print "clock3:%s" % time.clock()    
  47. import time  
  48. if __name__ == '__main__':  
  49.     time.sleep(1)  
  50.     print "clock1:%s" % time.clock()  
  51.     time.sleep(1)  
  52.     print "clock2:%s" % time.clock()  
  53.     time.sleep(1)  
  54.     print "clock3:%s" % time.clock()   
  55.   输出:  
  56.   clock1:3.35238137808e-006  
  57.   clock2:1.00004944763  
  58.   clock3:2.00012040636  
  59.   其中第一个clock输出的是程序运行时间  
  60.   第二、三个clock输出的都是与第一个clock的时间间隔  
  61.     
  62. 3.sleep(...)  
  63.   sleep(seconds)  
  64.   线程推迟指定的时间运行,经过测试,单位为秒,但是在帮助文档中有以下这样一句话,这关是看不懂  
  65.   “The argument may be a floating point number for subsecond precision.”  
  66.   
  67. 4.ctime(...)  
  68.   ctime(seconds) -> string  
  69.   将一个时间戳(默认为当前时间)转换成一个时间字符串  
  70.   例如:  
  71.   time.ctime()  
  72.   输出为:'Sat Mar 28 22:24:24 2009'  
  73.           
  74. 5.gmtime(...)  
  75.   gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)  
  76.   将一个时间戳转换成一个UTC时区(0时区)的struct_time,如果seconds参数未输入,则以当前时间为转换标准  
  77.     
  78.       
  79. 6.localtime(...)  
  80.   localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)  
  81.   将一个时间戳转换成一个当前时区的struct_time,如果seconds参数未输入,则以当前时间为转换标准  
  82.     
  83.           
  84. 7.mktime(...)  
  85.   mktime(tuple) -> floating point number  
  86.   将一个以struct_time转换为时间戳  
  87.           
  88. 8.strftime(...)  
  89.   strftime(format[, tuple]) -> string  
  90.   将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出  
  91.   python中时间日期格式化符号:  
  92.   %y 两位数的年份表示(00-99)  
  93.   %Y 四位数的年份表示(000-9999)  
  94.   %m 月份(01-12)  
  95.   %d 月内中的一天(0-31)  
  96.   %H 24小时制小时数(0-23)  
  97.   %I 12小时制小时数(01-12)   
  98.   %M 分钟数(00=59)  
  99.   %S 秒(00-59)  
  100.     
  101.   %a 本地简化星期名称  
  102.   %A 本地完整星期名称  
  103.   %b 本地简化的月份名称  
  104.   %B 本地完整的月份名称  
  105.   %c 本地相应的日期表示和时间表示  
  106.   %j 年内的一天(001-366)  
  107.   %p 本地A.M.或P.M.的等价符  
  108.   %U 一年中的星期数(00-53)星期天为星期的开始  
  109.   %w 星期(0-6),星期天为星期的开始  
  110.   %W 一年中的星期数(00-53)星期一为星期的开始  
  111.   %x 本地相应的日期表示  
  112.   %X 本地相应的时间表示  
  113.   %Z 当前时区的名称  
  114.   %% %号本身   
  115.       
  116. 9.strptime(...)  
  117.   strptime(string, format) -> struct_time  
  118.   将时间字符串根据指定的格式化符转换成数组形式的时间  
  119.   例如:  
  120.   2009-03-20 11:45:39  对应的格式化字符串为:%Y-%m-%d %H:%M:%S  
  121.   Sat Mar 28 22:24:24 2009 对应的格式化字符串为:%a %b %d %H:%M:%S %Y  
  122.       
  123. 10.time(...)  
  124.    time() -> floating point number  
  125.    返回当前时间的时间戳  
  126.   
  127. 三、疑点  
  128. 1.夏令时  
  129.   在struct_time中,夏令时好像没有用,例如  
  130.   a = (2009, 6, 28, 23, 8, 34, 5, 87, 1)  
  131.   b = (2009, 6, 28, 23, 8, 34, 5, 87, 0)  
  132.   a和b分别表示的是夏令时和标准时间,它们之间转换为时间戳应该相关3600,但是转换后输出都为646585714.0  
  133.     
  134. 四、小应用  
  135. 1.python获取当前时间  
  136.    time.time() 获取当前时间戳  
  137.    time.localtime() 当前时间的struct_time形式  
  138.    time.ctime() 当前时间的字符串形式  
  139.      
  140. 2.python格式化字符串    
  141.   格式化成2009-03-20 11:45:39形式  
  142.   time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())   
  143.     
  144.   格式化成Sat Mar 28 22:24:24 2009形式  
  145.   time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())   
  146.     
  147. 3.将格式字符串转换为时间戳  
  148.   a = "Sat Mar 28 22:24:24 2009"  
  149.   b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))  
原文地址:https://www.cnblogs.com/gongbo/p/5076226.html