time 模块
Python 的时间有三种表示方法
- Unix时间戳
- 格式化的时间字符串
- 结构化时间类<class ‘time.struct_time’>(一个tuple的派生类)
时间的定义
- UTC - 国际标准时间 , 中国为UTC+8。DST Daylight Saving Time 夏令时
- struct_time - 是从tuole派生而来的,可以视为一个9元素的tuple
- Unixtimestamp时间戳 - 从1970年1月1日0点开始计时经过的秒数
Help on struct_time object:
class struct_time(builtins.tuple)
| The time value as returned by gmtime(), localtime(), and strptime(), and
| accepted by asctime(), mktime() and strftime(). May be considered as a
| sequence of 9 integers.
Method resolution order:
| struct_time
| builtins.tuple
| builtins.object
三种时间表示的转换关系
graph BT
结构化时间==time.mktime==>时间戳
格式化时间字符串==time.strptime==>结构化时间
时间戳==time.gmtime或者localtime==>结构化时间
结构化时间==time.strftime==>格式化时间字符串
结构化时间
gmtime(), localtime(), and strptime() 都会返回struct_time
,它有以下属性:
Data descriptors defined here:
| tm_hour
| hours, range [0, 23]
| tm_isdst
| 1 if summer time is in effect, 0 if not, and -1 if unknown
| tm_mday
| day of month, range [1, 31]
| tm_min
| minutes, range [0, 59]
| tm_mon
| month of year, range [1, 12]
| tm_sec
| seconds, range [0, 61])
| tm_wday
| day of week, range [0, 6], Monday is 0
| tm_yday
| day of year, range [1, 366]
| tm_year
| year, for example, 1993
time的方法
help(time)
- asctime(struct_time)
以固定格式显示时间,参数为struct_time,若无参数,则默认localtime()
2. localtime(n)
返回本地时间的格式化数组,默认为当前时间time.time()
3. clock()
返回进程开始计时或者上一次调用clock的时间差
4. ctime(n)
接受一个整数。等价于asctime(localtime(n)).无参数等价于asctime()
5. gmtime() 和 localtime()
区别在于一个是国际时一个是当地时。中国会早8小时
6. strptime和strftime
二者作用相反,前者是parser,后者时formator
- strftime(format[, tuple]) -> string
- strptime(string, format) -> struct_time
常用的格式有:
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
- sleep()
线程延迟几秒钟
datetime
datetime模块定义了下面这几个类:
- datetime.date:表示日期的类。常用的属性有year, month, day;
- datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;
- datetime.datetime:表示日期时间。
- datetime.timedelta:表示时间间隔,即两个时间点之间的长度。
- datetime.tzinfo:与时区有关的相关信息。(这里不详细充分讨论该类,感兴趣的童鞋可以参考python手册)
这些类的派生关系如下:
builtins.object
date
datetime # datetime是date的派生类,前者有的方法后者都有
time
timedelta
tzinfo
timezone
常用的方法
- 获取当前时间
datetime.datetime.now()
以当地时区返回一个datetime类型
d.timestamp(),d.today(),d.year,d.timetuple()等方法可以调用
- 从时间戳转入
datetime.date.fromtimestamp(float unixTimeStamp) 和 datetime.datetime.fromtimestamp(float unixTimeStamp)
用一个时间戳浮点数初始化一个date或者一个datetime类型
datetime.date.fromtimestamp(time.time())
>>> x=datetime.datetime.fromtimestamp(time.time())
>>> x
datetime.datetime(2018, 11, 23, 15, 37, 41, 669644)
>>> x=datetime.date.fromtimestamp(time.time())
>>> x
datetime.date(2018, 11, 23)
- 时间运算
要用到datetime.timedelta类
>>> datetime.timedelta(days =1,seconds=2,microseconds=3,milliseconds=1,minutes=10,hours=10,weeks=10)
datetime.timedelta(71, 36602, 1003)
# 然后这个timedelta可以和date或者datetime类相加
- 格式化输出/输入
date类的一个方法strftime
a = datetime.datetime.now()
delta = datetime.deltatime(days=-1)
c= a+delta
c.strftime('%Y%d%m %H:%M:%S')
datetime(仅限datetime)的另一个方法strptime
>>> datetime.datetime.strptime('20182311 08:12:09','%Y%d%m %H:%M:%S')
datetime.datetime(2018, 11, 23, 8, 12, 9)
- 时间替换
>>> d.replace(year=2999,month=11,day=30)
datetime.date(2999, 11, 30)