python time模块用法实例详解

yipeiwu_com6年前Python基础

本文详细讲述了python的内嵌time模块的用法。分享给大家供大家参考之用。具体分析如下:
 
一、简介

time模块提供各种操作时间的函数

说明:一般有两种表示时间的方式:

第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的

第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同

year (four digits, e.g. 1998)
month (1-12)
day (1-31)
hours (0-23)
minutes (0-59)
seconds (0-59)
weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1) 是否是夏令时
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is -1, mktime() should guess based on the date and time.
   
二、函数介绍

1.asctime()

asctime([tuple]) -> string
将一个struct_time(默认为当时时间),转换成字符串
Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
When the time tuple is not present, current time as returned by localtime()
is used.
       
2.clock()

clock() -> floating point number
该函数有两个功能,
在第一次调用的时候,返回的是程序运行的实际时间;
以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔

示例:

import time 
if __name__ == '__main__': 
  time.sleep(1) 
  print "clock1:%s" % time.clock() 
  time.sleep(1) 
  print "clock2:%s" % time.clock() 
  time.sleep(1) 
  print "clock3:%s" % time.clock() 

输出:

clock1:3.35238137808e-006
clock2:1.00004944763
clock3:2.00012040636

其中第一个clock输出的是程序运行时间
第二、三个clock输出的都是与第一个clock的时间间隔
 
3.sleep(...)

sleep(seconds)
线程推迟指定的时间运行,经过测试,单位为秒,但是在帮助文档中有以下这样一句话,这关是看不懂
“The argument may be a floating point number for subsecond precision.”

4.ctime(...)

ctime(seconds) -> string
将一个时间戳(默认为当前时间)转换成一个时间字符串
例如:
time.ctime()
输出为:'Sat Mar 28 22:24:24 2009'
       
5.gmtime(...)

gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)

将一个时间戳转换成一个UTC时区(0时区)的struct_time,如果seconds参数未输入,则以当前时间为转换标准

6.localtime(...)

localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)

将一个时间戳转换成一个当前时区的struct_time,如果seconds参数未输入,则以当前时间为转换标准

7.mktime(...)

mktime(tuple) -> floating point number
将一个以struct_time转换为时间戳
       
8.strftime(...)

strftime(format[, tuple]) -> string
将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出
python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)

%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身 
   
9.strptime(...)

strptime(string, format) -> struct_time

将时间字符串根据指定的格式化符转换成数组形式的时间
例如:
2009-03-20 11:45:39  对应的格式化字符串为:%Y-%m-%d %H:%M:%S
Sat Mar 28 22:24:24 2009 对应的格式化字符串为:%a %b %d %H:%M:%S %Y
   
10.time(...)

time() -> floating point number
返回当前时间的时间戳

三、疑点

1.夏令时

在struct_time中,夏令时好像没有用,例如
a = (2009, 6, 28, 23, 8, 34, 5, 87, 1)
b = (2009, 6, 28, 23, 8, 34, 5, 87, 0)
a和b分别表示的是夏令时和标准时间,它们之间转换为时间戳应该相关3600,但是转换后输出都为646585714.0
 
四、小应用

1.python获取当前时间

time.time() 获取当前时间戳
time.localtime() 当前时间的struct_time形式
time.ctime() 当前时间的字符串形式
  
2.python格式化字符串 

格式化成2009-03-20 11:45:39形式

time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 

格式化成Sat Mar 28 22:24:24 2009形式

time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) 

 
3.将格式字符串转换为时间戳

a = "Sat Mar 28 22:24:24 2009"
b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

相信本文所述对大家的Python程序设计有一定的借鉴价值。

相关文章

Python使用win32 COM实现Excel的写入与保存功能示例

Python使用win32 COM实现Excel的写入与保存功能示例

本文实例讲述了Python使用win32 COM实现Excel的写入与保存功能。分享给大家供大家参考,具体如下: 很久之前通过东拼西凑实现过使用Python通过win32 COM实现wo...

基于python实现KNN分类算法

kNN算法的核心思想是如果一个样本在特征空间中的k个最相邻的样本中的大多数属于某一个类别,则该样本也属于这个类别,并具有这个类别上样本的特性。该方法在确定分类决策上只依据最邻近的一个或者...

python类的方法属性与方法属性的动态绑定代码详解

动态语言与静态语言有很多不同,最大的特性之一就是可以实现动态的对类和实例进行修改,在Python中,我们创建了一个类后可以对实例和类绑定心的方法或者属性,实现动态绑定。 最近在学习pyt...

Python字符串特性及常用字符串方法的简单笔记

单引号和双引号都能表示字符串。区别在于转义的时候。 如果懒得加转义字符,可以通过在字符串前面加上r。例如: print r'C:\some\name' 通过在字符串里面添加反斜杠...

Python装饰器的函数式编程详解

Python的装饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西。虽然好像,...